309 Commits

Author SHA1 Message Date
Nate Fischer
a421b9e3a5
feat: add shell.cmd to replace exec (#866)
This adds an initial implementation of shell.cmd(), which is intended as
the eventual replacement for shell.exec(). This PR does not fully
implement the API, but demonstrates a simple and secure alternative, and
will allow further iteration to cover other use cases in follow-up PRs.

Design doc: https://shelljs.page.link/cmd-design

Issue #495
Test: automated test suite
2019-10-29 18:35:14 -07:00
Nate Fischer
37b42fef25
test: misc test changes (#970)
No change to production logic.

This renames test cases with duplicate names, deletes a redundant test,
and uses the public `t.title` API in favor of `t._test.title`.

This is a prerequisite for updating ava.

Issue #969
2019-10-08 22:41:16 -07:00
Wes Cossick
4e38240eda Add boolean fatal option to exec() function (#961)
This PR introduces a new boolean fatal option for the exec() function. Like the existing silent option, this new option allows you to override the global fatal configuration parameter on a per-command basis.
2019-10-01 23:43:54 -07:00
Nate Fischer
57df38c6ea
chore(lint): update lint dependencies (#948)
This updates `eslint-config-airbnb-base` and `eslint-plugin-import`.
This also addresses lint errors these updates raise, and excludes
several rules.

This also adds a minor simplification to the gendocs script.

Test: npm run lint
Test: npm run gendocs
2019-07-12 12:36:55 -07:00
Nate Fischer
a8e543a8d0
chore(lint): upgrade eslint (#947)
This upgrades eslint to a new version to resolve a security warning.
This fixes a couple new lint errors, and disables some lint checks.

The current version of the airbnb lint config triggers
DeprecationWarnings in the new version of eslint. These warnings will be
resolved in a follow-up.

Test: npm run lint
2019-06-20 23:34:41 -07:00
Nate Fischer
f7dbc61d41
chore(lint): alphabetize lint rules (#946)
No change to the rules, this just reorders the eslintrc file so there's
a sane way to add new rules.

Test: npm run lint
2019-06-20 22:52:47 -07:00
Nate Fischer
1560ffdc5f
feat(options): initial support for long options (#926)
* feat(options): initial support for long options

This adds initial support for long options. This integration tests them
with `shell.touch()` and unit tests them in `test/common.js`. This
documents the new syntax.

This also refactors some of the common internals:

 * Creates a new CommandError type to replace the `'earlyExit'` hack
 * Clearer Error types for `parseOptions()` exceptions
 * Removes side effects from a test which modifies `common.config`
 * Fixes appveyor and travis config to run lint (regressed in #920)

Issue #924
Test: touch.js, common.js

* small refactor for errorOptions
2019-01-05 00:54:30 -08:00
Nate Fischer
5371e1629f
test(touch): add coverage for -d option (#925)
No change to production logic.

We never had coverage for `touch({'-d': ...})`, so this adds test
coverage. This also updates documentation to clarify the parameter
should be an instance of the `Date` type.

Test: `handles date argument` case
2019-01-04 02:02:04 -08:00
Nate Fischer
e606706855
fix(exec): consistent error message for maxBuffer (#919)
* fix(exec): consistent error message for maxBuffer

This explicitly checks for maxBuffer errors in exec-child.js and
provides a consistent error message. This modifies the test to verify
this.

This also supports the change in Node v10, which emits this as a
RangeError instead of a regular Error.

Although the error message is now explicit, this is not part of our API,
since it's just tacked onto the end of exec's stderr.

Fixes #915

* Remove debugging log, ignore uncovered lines
2018-12-02 21:52:54 -08:00
Shubham Joshi
18d8bbf290 feat(cp): support update flag when recursing (#889)
Fixed `cp -Ru` ignoring `-u`, and added a test.

Fixes #808
2018-11-12 21:35:06 -08:00
Will Yardley
5da1dda414 fix: Exit 1 with no output if no match (#900) (#901)
When we can read all files, but none match, exit 1 and return no output. This
matches the behavior of grep more closely.

Fixes #900
2018-11-12 21:34:13 -08:00
Will Yardley
db317bf092 Add test case for sed on empty file (#904)
As discussed as an aside in #900, add test case with an empty file.
2018-11-12 20:46:10 -08:00
Nate Fischer
6b3c7b1e44
refactor: don't expose tempdir in common.state (#903)
Previously, the cached `tempdir` value was stored in `common.state`.
Unlike the other `common.state` values, this isn't immediately useful to
other commands (they can just call the tempdir API). So, this moves the
cached value into `tempdir.js`.

This also adds a unit test for the caching behavior, and exposes
test-only helpers to verify this behavior.

Finally, this adds a note to `common.state` that values should generally
be considered read-only, since this can be important for customized
behavior. Although, I recognize our code base has one exception to this
rule (`echo()`), we should strive to maintain this.

Fixes #902
Test: Added a unit test.
2018-11-08 23:00:22 -08:00
Nate Fischer
2b3b781bbc
fix: silent exec (#892)
Unconditionally apply `silent: true` when calling `common.error()` from
`exec()`. This is because errors are already printed to stderr, or are
intentionally silenced by `shell.config.silent`.

Based on #861 

Fixes #851
2018-10-27 13:51:36 -07:00
Peng Zhao
4113a72c16 grep includes the i flag (#876)
grep includes the i flag to ignored upper/lower case differences
2018-07-22 20:38:13 -07:00
Stanislav Termosa
8dae55ff4a Fix(which): match only executable files (#874)
On Unix, this only matches files with the exec bit set. On Windows, this only
matches files which are readable (since Windows has different rules for
execution).

Fixes #657.
2018-07-18 15:33:08 -07:00
Nate Fischer
6d66a1af98
chore: rename some tests (#871)
No change to logic.

This renames some tests to be a bit more readable.
2018-07-11 15:06:20 -07:00
Nate Fischer
131b88f992
Fix cp from readonly source (#870)
This is a redo of PR #555.

This rebases, cleans up a test, and fixes a bug (the original PR uses `fs.chown()` instead of `fs.chownSync()`).

Fixes #98
2018-07-09 22:53:58 -07:00
Nate Fischer
1dd437eb6b
fix(mocks): fix conflict between mocks and skip (#863)
This fixes a conflict between mocks.init() and
utils.skipOnWin/skipOnUnix. mocks.init() mocks out process.stderr.write,
which utils.js implicitly depends on.

Instead, preserve stderr.write in a local variable to avoid polluting
mocked stdio and to correctly output warning messages.

Fixes #862
Test: locally apply mocks.init() inside test/which.js
2018-06-28 14:15:54 -07:00
Brandon Freitag
93bbf684c6
Prevent require-ing bin/shjs (#848)
* Prevent require-ing bin/shjs

* Move require guard up, and improve function name

* Move require up and clarify comment
2018-06-27 00:02:32 -07:00
Brandon Freitag
9077f4171e
Remove separate "internal error" from exec (#802)
* Remove separate "internal error" from exec

* Fix unknown command error regex

* Add message about command not found regex

* Silence errors while reading files in exec

The stdout and stderr files may never be opened or written to in certain
circumstances. In particular, if the timeout is short enough, the child
node process does not have enough time to start, and the child script
does not execute, so the files are not written to. So, catch errors form
trying to read the files, and ignore them.

* Do not silence errors due to short timeouts

* Simplify test regex for missing command

* Default error code to 1 if not set
2018-01-29 12:28:02 -08:00
Nate Fischer
a2343d0eab
refactor: harden plugins against unknown options (#804)
This reworks the plugin API such that:

 - Unable to register a command with unknown wrap-options
 - `TypeError` raised for wrap-option type mistakes
 - Remove the `overWrite` option (it's unused, probably safest to not
   expose for now)
 - `cmdOptions` defaults to `null` instead of `false` for type
   consistency (no change to default behavior)
 - Move `pipeMethods` logic into `_register`, since it makes more sense
   there

This is not expected to have any effect on existing plugins.
2017-11-16 12:44:51 -08:00
Nate Fischer
8451fceb81
chore(eslint): use words instead of numbers (#797)
Eslint rules can be configured either using words or number values:

 * "off" or 0
 * "warn" or 1
 * "error" or 2

This switches our config to use the string values instead of the number
values, since the number values are too cryptic.

No change to our actual settings.
2017-10-30 18:04:48 -07:00
Nate Fischer
a187bd1b36 Provide an API to pass parameters which resemble options (#792)
This adds the special option string `--`, which means "no options". This can be
passed if the first parameter looks like an option (starts with a `-` followed
by 1+ letters).

Fixes #778
2017-10-26 21:29:26 -07:00
Nate Fischer
9e3f9abd2a refactor(exec): move child process to source file (#786)
This PR refactors `shell.exec()` by putting its child process in a separate code
file. This also slightly cleans up dead code.

There's more potential to clean this up (e.g. exit status), but this is a good
enough start.

Issue #782
2017-10-19 13:19:52 -07:00
Brandon Freitag
a7d6df5f6d Wrap fs.statSync and fs.lstatSync (#783)
Adds two new methods to src/common.js, common.statFollowLinks and common.statNoFollowLinks, which wrap fs.statSync and fs.lstatSync, respectively. This change is meant to improve readability and clarify intent.

* Add common.statFollowLinks and common.statNoFollowLinks

* Replace fs.statSync and fs.lstatSync in source files
2017-10-17 19:25:09 -07:00
Brandon Freitag
7cbce88915 Add a test for ls for a single file (#784) 2017-10-17 19:22:37 -07:00
Brandon Freitag
32972e0cdd Remove unnecessary shell.error checks from common tests (#785) 2017-10-14 02:38:12 -07:00
Nate Fischer
df1460ff53 chore: clean up refs to unsupported node versions (#779)
Also removes unnecessary guard code leftover from old versions.
2017-10-12 23:23:23 -07:00
Alexander Regueiro
cea0e58250 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.
2017-10-09 20:53:17 -07:00
cristHian Gz
c889075f78 feat(cat): number output lines (#750) (#775) 2017-09-22 21:27:42 -07:00
Brandon Freitag
dcead1be86 Add "encoding" option to exec (#763)
* Add 'encoding' option to shell.exec

* Add 'encoding' option to docs

* Add exec encoding option tests

* Clarify use of encoding with fs.readFileSync

* Add check for stderr buffer
2017-09-13 16:18:28 -07:00
Brandon Freitag
c7d65ac590 fix(ls): ls not following links to directories by default (#764)
* Fix ls not following links to directories by default
2017-08-23 19:06:25 -07:00
Nate Fischer
2ee83ebf74 refactor(test): update AVA and refactor tests (#760)
This updates tests for `AVA` 19.0.0+.

`AVA` 0.18.0 has a breaking change which changes the current working directory
for tests. As a result, we need to change 'resources' -> 'test/resources' (and
 similar path changes).

`AVA` 0.19.0 has a breaking change that all tests must run at least one assert.
This breaking change was already resolved by #746, so no change was necessary in
this PR.

This updates to `AVA` 0.21.0, since there are no other breaking changes.
2017-08-11 11:03:13 -07:00
Nate Fischer
38b57c8942 chore: add skipOnWin and skipOnUnix test helpers (#746)
This adds `skipOnWin` and `skipOnUnix` to help us manage our platform-dependent
tests. These methods give a nice warning message when we skip tests. We may also
consider adding warnings when running platform-dependent tests.

Part of the motivation for this is if we ever update to AVA v0.19. This version
requires at least one assertion per test case. While this could be disabled with
an AVA setting, we instead benefit from warnings for any case when we
unintentionally skip assertions.

This adds chalk as a dev dependency to enable colored messages.
2017-06-18 11:51:01 -07:00
Nate Fischer
5823ab18bb test(exec): add tests for coverage (#744)
* test(exec): add tests for coverage

No logic change.

This adds one test to cover some missing lines, and adds some `istanbul ignore`
directives.

I see 100% line coverage for `src/exec.js` when running:

```sh
$ nyc --reporter=text --reporter=lcov ava --serial test/exec.js`
```

Fixes #742

* Fix lint
2017-06-17 19:09:01 -07:00
Nate Fischer
291241811f test(head): improve coverage (#743)
This adds a test for `head()` on the right-hand side of a pipe. This also
removes the try-catch surrounding `fs.openSync()`, because it was unreachable
code. `fs.existsSync()` guarantees that the file exists, and `fs.openSync()`
only throws if the file does not exist, according to official documentation.

Fixes #671
2017-06-17 18:31:24 -07:00
Brandon Freitag
522a46dcf4 Deprecate common.getUserHome, advise using os.homedir instead (#725)
* Deprecate common.getUserHome, advise using os.homedir instead

* Remove common.getUserHome
2017-06-06 21:10:27 -07:00
Brandon Freitag
e8ec60bc2f Echo test mocks (#708)
* Add stdout/stderr test mocks

* Mock stdout/stderr during echo tests

* Fix lint issues

* Use 'use strict'

* Re-implement mocks as a prototype

* Implement mocks as a single-instance

* Remove redundant test

* Create mocked stdout/stderr.write methods once
2017-06-06 21:10:27 -07:00
Nate Fischer
c1d8fecc56 Fix broken test 2017-06-06 21:10:27 -07:00
Nate Fischer
18034486a5 Echo stdout (#677)
* Add newline to output of echo (#557)

* Add newline to output of echo

* Add test

* Throw an error if the options string does not start with '-' (#615)

* Throw an error if the options string does not start with '-'

* Add test

* Change message grammar

* Add -n option to echo

* Fix null argument issue

* Add -n tests

* Add documentation

* Add -en escaped character test

* Add function to parse options for echo

* Use parseOptions to parse echo options

* Simplify control flow

* parseOptions throws now

* Allow null to be echoed

* Prevent echo stderr on unrecognized option

* Add test to check stderr of returned value

* Use consistent variable name

* Change test message, leave TODO about console output
2017-06-06 21:10:27 -07:00
Nate Fischer
f74e783890 chore: remove v0.12 and iojs support (#648)
Fixes #647
2017-06-06 21:10:27 -07:00
Brandon Freitag
5acb353bc5 Add newline to output of echo (#557)
* Add newline to output of echo

* Add test
2017-06-06 21:10:27 -07:00
Nate Fischer
15558cf67a fix(mkdir): improve error handling around files (#721)
* fix(mkdir): improve error handling around files

In particular, this fixes:

 - if we try to overwrite a file with a mkdir
 - if we try to create a subdirectory of a file
 - adds `continue: true` in spots where we missed it

Fixes #720

* Fixing tests on Windows
2017-05-16 00:30:11 -07:00
Nate Fischer
a2e13b62dc Properly handle directories as arguments (#713)
* fix(cat): do not cat directories

Fixes #707

* fix(head): do not let head() read directories

Also fixes a typo

* fix(sort): do not sort directories

Also fixes a typo

* fix(tail): do not let tail() read directories

Also fixes a typo

* fix(uniq): do not let uniq() read directories

We also had a test which called sort() instead of uniq(), so we never
actually tested the missing-file case. This fixes that as well.

This also throws an error for using a directory as output.

* fix(pipe): fix breakages with piped commands
2017-05-08 22:57:58 -07:00
Brandon Freitag
951ff22671 Add common.buffer (#710)
* Add common.buffer

* Fix comment
2017-05-02 23:04:56 -07:00
Brandon Freitag
92d3f3982e Fix common.expand error (#709)
* When glob.sync errors, return original array element

* Add test for cp with empty string
2017-05-02 19:54:56 -07:00
Kumar Uttpal
bbe521b57d Fix #631 throw error when overwriting recently created file (#702)
* cp: add error to not overwrite recently created files #631

* cp: add tests for errors not overwrite recently created files #631

* mv: show error when overwriting recently created file #631

* mv: add tests for error on recently created files #631

* mv: test remove unnecessary steps #631
2017-04-15 02:01:58 -07:00
Nate Fischer
d8ac4d3f02 refactor: remove unnecessary common.js imports (#703)
No change in logic.

This removes unnecessary imports of `common.js` in favor of the `shell`
package.
2017-04-15 00:51:29 -07:00
Brandon Freitag
7feabda0d6 Remove contents of symlink to dir with rm -rf (#688)
* rm -rf on a symlink to a dir deletes contents

* Fix comment typo

* Clarify comments

* Pass symlink directly to rmdirSync

* Skip rm -rf symlink test on windows
2017-04-15 00:49:10 -07:00