Skip to content

Commit cea0e58

Browse files
alexregnfischer
authored andcommitted
Added -q (quiet) option to push, popd, dirs functions. (shelljs#777)
* Added `-q` (quiet) option to `push`, `popd`, `dirs` functions. * Added unit tests for pushd/popd quiet mode. * Added tests for `pushd` and `popd` with quiet mode off. * Updated docs for `pushd` and `popd` functions. * Moved preliminary `pushd` commands for `popd` tests before disabling of silent flag.
1 parent c889075 commit cea0e58

4 files changed

Lines changed: 99 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Copies files.
201201
Available options:
202202

203203
+ `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
204+
+ `-q`: Supresses output to the console.
204205

205206
Arguments:
206207

@@ -223,6 +224,7 @@ Save the current directory on the top of the directory stack and then cd to `dir
223224
Available options:
224225

225226
+ `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
227+
+ `-q`: Supresses output to the console.
226228

227229
Arguments:
228230

@@ -246,6 +248,7 @@ When no arguments are given, popd removes the top directory from the stack and p
246248
Available options:
247249

248250
+ `-c`: Clears the directory stack by deleting all of the elements.
251+
+ `-q`: Supresses output to the console.
249252

250253
Arguments:
251254

src/dirs.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function _actualDirStack() {
4040
//@ Available options:
4141
//@
4242
//@ + `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
43+
//@ + `-q`: Supresses output to the console.
4344
//@
4445
//@ Arguments:
4546
//@
@@ -64,6 +65,7 @@ function _pushd(options, dir) {
6465

6566
options = common.parseOptions(options, {
6667
'n': 'no-cd',
68+
'q': 'quiet',
6769
});
6870

6971
var dirs = _actualDirStack();
@@ -95,7 +97,7 @@ function _pushd(options, dir) {
9597
}
9698

9799
_dirStack = dirs;
98-
return _dirs('');
100+
return _dirs(options.quiet ? '-q' : '');
99101
}
100102
exports.pushd = _pushd;
101103

@@ -105,6 +107,7 @@ exports.pushd = _pushd;
105107
//@ Available options:
106108
//@
107109
//@ + `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
110+
//@ + `-q`: Supresses output to the console.
108111
//@
109112
//@ Arguments:
110113
//@
@@ -130,6 +133,7 @@ function _popd(options, index) {
130133

131134
options = common.parseOptions(options, {
132135
'n': 'no-cd',
136+
'q': 'quiet',
133137
});
134138

135139
if (!_dirStack.length) {
@@ -146,7 +150,7 @@ function _popd(options, index) {
146150
_cd('', dir);
147151
}
148152

149-
return _dirs('');
153+
return _dirs(options.quiet ? '-q' : '');
150154
}
151155
exports.popd = _popd;
152156

@@ -156,6 +160,7 @@ exports.popd = _popd;
156160
//@ Available options:
157161
//@
158162
//@ + `-c`: Clears the directory stack by deleting all of the elements.
163+
//@ + `-q`: Supresses output to the console.
159164
//@
160165
//@ Arguments:
161166
//@
@@ -173,6 +178,7 @@ function _dirs(options, index) {
173178

174179
options = common.parseOptions(options, {
175180
'c': 'clear',
181+
'q': 'quiet',
176182
});
177183

178184
if (options.clear) {
@@ -189,11 +195,15 @@ function _dirs(options, index) {
189195
index = stack.length + index;
190196
}
191197

192-
common.log(stack[index]);
198+
if (!options.quiet) {
199+
common.log(stack[index]);
200+
}
193201
return stack[index];
194202
}
195203

196-
common.log(stack.join(' '));
204+
if (!options.quiet) {
205+
common.log(stack.join(' '));
206+
}
197207

198208
return stack;
199209
}

test/popd.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path';
33
import test from 'ava';
44

55
import shell from '..';
6+
import mocks from './utils/mocks';
67

78
const rootDir = path.resolve();
89

@@ -114,3 +115,41 @@ test('Test that rootDir is not stored', t => {
114115
shell.popd(); // no more in the stack
115116
t.truthy(shell.error());
116117
});
118+
119+
test('quiet mode off', t => {
120+
try {
121+
shell.pushd('test/resources/pushd');
122+
shell.config.silent = false;
123+
mocks.init();
124+
const trail = shell.popd();
125+
const stdout = mocks.stdout();
126+
const stderr = mocks.stderr();
127+
t.falsy(shell.error());
128+
t.is(stdout, '');
129+
t.is(stderr, `${rootDir}\n`);
130+
t.is(process.cwd(), trail[0]);
131+
t.deepEqual(trail, [rootDir]);
132+
} finally {
133+
shell.config.silent = true;
134+
mocks.restore();
135+
}
136+
});
137+
138+
test('quiet mode on', t => {
139+
try {
140+
shell.pushd('test/resources/pushd');
141+
shell.config.silent = false;
142+
mocks.init();
143+
const trail = shell.popd('-q');
144+
const stdout = mocks.stdout();
145+
const stderr = mocks.stderr();
146+
t.falsy(shell.error());
147+
t.is(stdout, '');
148+
t.is(stderr, '');
149+
t.is(process.cwd(), trail[0]);
150+
t.deepEqual(trail, [rootDir]);
151+
} finally {
152+
shell.config.silent = true;
153+
mocks.restore();
154+
}
155+
});

test/pushd.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path';
33
import test from 'ava';
44

55
import shell from '..';
6+
import mocks from './utils/mocks';
67

78
const rootDir = path.resolve();
89

@@ -328,3 +329,45 @@ test('Push without arguments invalid when stack is empty', t => {
328329
shell.pushd();
329330
t.is(shell.error(), 'pushd: no other directory');
330331
});
332+
333+
test('quiet mode off', t => {
334+
try {
335+
shell.config.silent = false;
336+
mocks.init();
337+
const trail = shell.pushd('test/resources/pushd');
338+
const stdout = mocks.stdout();
339+
const stderr = mocks.stderr();
340+
t.falsy(shell.error());
341+
t.is(stdout, '');
342+
t.is(stderr, `${path.resolve(rootDir, 'test/resources/pushd')} ${rootDir}\n`);
343+
t.is(process.cwd(), trail[0]);
344+
t.deepEqual(trail, [
345+
path.resolve(rootDir, 'test/resources/pushd'),
346+
rootDir,
347+
]);
348+
} finally {
349+
shell.config.silent = true;
350+
mocks.restore();
351+
}
352+
});
353+
354+
test('quiet mode on', t => {
355+
try {
356+
shell.config.silent = false;
357+
mocks.init();
358+
const trail = shell.pushd('-q', 'test/resources/pushd');
359+
const stdout = mocks.stdout();
360+
const stderr = mocks.stderr();
361+
t.falsy(shell.error());
362+
t.is(stdout, '');
363+
t.is(stderr, '');
364+
t.is(process.cwd(), trail[0]);
365+
t.deepEqual(trail, [
366+
path.resolve(rootDir, 'test/resources/pushd'),
367+
rootDir,
368+
]);
369+
} finally {
370+
shell.config.silent = true;
371+
mocks.restore();
372+
}
373+
});

0 commit comments

Comments
 (0)