From cea0e58250b357e916ecef7c4e75294632af4597 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Tue, 10 Oct 2017 04:53:17 +0100 Subject: [PATCH] Added `-q` (quiet) option to `push`, `popd`, `dirs` functions. (#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. --- README.md | 3 +++ src/dirs.js | 18 ++++++++++++++---- test/popd.js | 39 +++++++++++++++++++++++++++++++++++++++ test/pushd.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c52c12a..3608792 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,7 @@ Copies files. Available options: + `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated. ++ `-q`: Supresses output to the console. Arguments: @@ -223,6 +224,7 @@ Save the current directory on the top of the directory stack and then cd to `dir Available options: + `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated. ++ `-q`: Supresses output to the console. Arguments: @@ -246,6 +248,7 @@ When no arguments are given, popd removes the top directory from the stack and p Available options: + `-c`: Clears the directory stack by deleting all of the elements. ++ `-q`: Supresses output to the console. Arguments: diff --git a/src/dirs.js b/src/dirs.js index 3806c14..3e27ac9 100644 --- a/src/dirs.js +++ b/src/dirs.js @@ -40,6 +40,7 @@ function _actualDirStack() { //@ Available options: //@ //@ + `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated. +//@ + `-q`: Supresses output to the console. //@ //@ Arguments: //@ @@ -64,6 +65,7 @@ function _pushd(options, dir) { options = common.parseOptions(options, { 'n': 'no-cd', + 'q': 'quiet', }); var dirs = _actualDirStack(); @@ -95,7 +97,7 @@ function _pushd(options, dir) { } _dirStack = dirs; - return _dirs(''); + return _dirs(options.quiet ? '-q' : ''); } exports.pushd = _pushd; @@ -105,6 +107,7 @@ exports.pushd = _pushd; //@ Available options: //@ //@ + `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated. +//@ + `-q`: Supresses output to the console. //@ //@ Arguments: //@ @@ -130,6 +133,7 @@ function _popd(options, index) { options = common.parseOptions(options, { 'n': 'no-cd', + 'q': 'quiet', }); if (!_dirStack.length) { @@ -146,7 +150,7 @@ function _popd(options, index) { _cd('', dir); } - return _dirs(''); + return _dirs(options.quiet ? '-q' : ''); } exports.popd = _popd; @@ -156,6 +160,7 @@ exports.popd = _popd; //@ Available options: //@ //@ + `-c`: Clears the directory stack by deleting all of the elements. +//@ + `-q`: Supresses output to the console. //@ //@ Arguments: //@ @@ -173,6 +178,7 @@ function _dirs(options, index) { options = common.parseOptions(options, { 'c': 'clear', + 'q': 'quiet', }); if (options.clear) { @@ -189,11 +195,15 @@ function _dirs(options, index) { index = stack.length + index; } - common.log(stack[index]); + if (!options.quiet) { + common.log(stack[index]); + } return stack[index]; } - common.log(stack.join(' ')); + if (!options.quiet) { + common.log(stack.join(' ')); + } return stack; } diff --git a/test/popd.js b/test/popd.js index 0e83a7c..0626a0b 100644 --- a/test/popd.js +++ b/test/popd.js @@ -3,6 +3,7 @@ import path from 'path'; import test from 'ava'; import shell from '..'; +import mocks from './utils/mocks'; const rootDir = path.resolve(); @@ -114,3 +115,41 @@ test('Test that rootDir is not stored', t => { shell.popd(); // no more in the stack t.truthy(shell.error()); }); + +test('quiet mode off', t => { + try { + shell.pushd('test/resources/pushd'); + shell.config.silent = false; + mocks.init(); + const trail = shell.popd(); + const stdout = mocks.stdout(); + const stderr = mocks.stderr(); + t.falsy(shell.error()); + t.is(stdout, ''); + t.is(stderr, `${rootDir}\n`); + t.is(process.cwd(), trail[0]); + t.deepEqual(trail, [rootDir]); + } finally { + shell.config.silent = true; + mocks.restore(); + } +}); + +test('quiet mode on', t => { + try { + shell.pushd('test/resources/pushd'); + shell.config.silent = false; + mocks.init(); + const trail = shell.popd('-q'); + const stdout = mocks.stdout(); + const stderr = mocks.stderr(); + t.falsy(shell.error()); + t.is(stdout, ''); + t.is(stderr, ''); + t.is(process.cwd(), trail[0]); + t.deepEqual(trail, [rootDir]); + } finally { + shell.config.silent = true; + mocks.restore(); + } +}); diff --git a/test/pushd.js b/test/pushd.js index 9ca0f76..d24b321 100644 --- a/test/pushd.js +++ b/test/pushd.js @@ -3,6 +3,7 @@ import path from 'path'; import test from 'ava'; import shell from '..'; +import mocks from './utils/mocks'; const rootDir = path.resolve(); @@ -328,3 +329,45 @@ test('Push without arguments invalid when stack is empty', t => { shell.pushd(); t.is(shell.error(), 'pushd: no other directory'); }); + +test('quiet mode off', t => { + try { + shell.config.silent = false; + mocks.init(); + const trail = shell.pushd('test/resources/pushd'); + const stdout = mocks.stdout(); + const stderr = mocks.stderr(); + t.falsy(shell.error()); + t.is(stdout, ''); + t.is(stderr, `${path.resolve(rootDir, 'test/resources/pushd')} ${rootDir}\n`); + t.is(process.cwd(), trail[0]); + t.deepEqual(trail, [ + path.resolve(rootDir, 'test/resources/pushd'), + rootDir, + ]); + } finally { + shell.config.silent = true; + mocks.restore(); + } +}); + +test('quiet mode on', t => { + try { + shell.config.silent = false; + mocks.init(); + const trail = shell.pushd('-q', 'test/resources/pushd'); + const stdout = mocks.stdout(); + const stderr = mocks.stderr(); + t.falsy(shell.error()); + t.is(stdout, ''); + t.is(stderr, ''); + t.is(process.cwd(), trail[0]); + t.deepEqual(trail, [ + path.resolve(rootDir, 'test/resources/pushd'), + rootDir, + ]); + } finally { + shell.config.silent = true; + mocks.restore(); + } +});