224 Commits

Author SHA1 Message Date
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
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
Brandon Freitag
97a8c1969a fix(grep, sed, sort, uniq): Split only on newline characters (#690)
* Split on newlines only

* Only split lines if need be

* Clarify code by making use of Array.prototype.reduce
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
5d05d27108 Safely exit by throwing an error (#649) 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
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
Brandon Freitag
e12da9d888 Refactor: Use process.platform across codebase (#689)
* Use process.platform across codebase

* Use process.platform in tests
2017-04-14 23:04:21 -07:00
Nate Fischer
54c1befe94 refactor(parseOptions): better handle errors (#674)
* refactor(parseOptions): better handle errors

For the case where `parseOptions()` is passed a string that does not
start with a hyphen, we should reject this string instead of returning
the same value. This has no change on any other test cases, and should
not affect any commands since we are careful about what input we pass to
`parseOptions()`

This also adjust how we were handling error cases in the function. We
were previously using two different calls to `common.error()`, one for
if `errorOptions` is passed, and one without. This hurts readability,
because it reads like "if we have errorOptions, then this is an error,
and if we don't then it's also an error," instead of the more
appropriate, "we reached an error case and should use errorOptions if
it's available, otherwise we should signal an error without using it."

We do not need to use `errorOptions` for the added call to `error()`,
since this is an error which indicates misuse, and should not be
recoverable.

This adds a test case as well, for a line which was not previously
covered in our tests.

Partial fix for #671

* Refactor parseOptions()

This validates input at the top of `parseOptions()` for type
correctness. This changes `typeof x === 'object'` to `x instanceOf
Object` to improve robustness. Now we can safely use `errorOptions`
knowing that if it has a truthy value, it will be an object, otherwise
we should use defaults instead.

The new tests ensure that we have 100% unit test coverage for this
function.

* Use exceptions, not common.error()s, for internal misuse

* Add common.isObject() helper function

The `isObject()` helper is used throughout common.js to reliably test if
variables are JavaScript objects.
2017-03-08 19:30:56 -08:00
Brandon Freitag
7949759463 Add support for removing fifos (#687)
* Add support for removing fifos in rm

* Add test for removing fifo

* Add mkfifo util for testing

* Use ES5 syntax in utils
2017-03-08 07:33:41 -08:00
Brandon Freitag
5b5a28d2de Fix cp overwriting identical files (#679)
* Implement fix for cp with identical files

* Implement tests for cp with identical files

* Change error message

* Use common.error immediately if the file is identical

* Add test for copying to exact same path
2017-03-07 22:18:43 -08:00
Nate Fischer
b6b2cd84ef fix: convert error output to be consistent cross-platform (#684)
* fix: convert error output to be consistent cross-platform

The error output produced by `shell.error()` or `result.stderr` should
not be inconsistent between platforms. This ensures that path separators
are always printed by ShellJS as `/` instead of as `\` on Windows. This
should allow scripts using ShellJS to be more consistent cross-platform.

We were not previously relying on error output to always be consistent--
only checking its truthiness. Since this was not part of our tested API,
it should be reasonable to change this and release in a patch.

Fixes #681

* Fix broken pushd test case

* Fix TODO in a test case
2017-03-06 00:54:26 -08:00
Nate Fischer
346fca4cb6 test: don't count hard-to-test lines for coverage (#672)
No change in logic.

Add `/* istanbul ignore next */` lines for hard-to-test lines so that
they don't count against us during code coverage.

I've also adjusted comments that I found confusing, and changed some
formatting.

Partial fix for #671
2017-03-04 17:54:43 -08:00
Nate Fischer
5318fdc43a docs: miscellaneous README changes (#661)
- Add @freitagbr to the bottom of the README
 - Remove docs about shjs (it's not well supported)
 - Remove docs about coffeescript (we don't do anything special for
   coffeescript anyway)
 - Modify the section about shelljs/global, since we strongly recommend
   avoiding this now
 - Rewrite the code example to use `require('shelljs')` instead of
   shelljs/global
 - Mention ESLint next to JSHint
 - Reformat sections with long lines to be 80 columns
2017-03-04 01:07:23 -08:00
Nate Fischer
3cd4470be3 refactor: reduce repeated code in which() (#659)
Use path.delimiter instead of explicit conditional. Try to reuse
repeated code blocks.

Fixes #656
2017-03-04 00:14:20 -08:00
Fran Dios
172d0f1543 Modified glob pattern. Fixes #666 (#676) 2017-02-28 17:32:02 -08:00
Fran Dios
df06ac4a8a ls -L (follow symlinks) (#665)
* feat: -L (follow symlinks) option for ls

* test: ls -L option

* docs: Add ls -L option to readme

* fix: ls -L ternary operator

* Revert "test: ls -L option"

This reverts commit dbb057ac087b23aa2c1358018d3c832dd546c5f2.

* test: ls -L option

* test: Remove duplicate test
2017-02-27 20:53:32 -08:00
George Kalpakas
434ed592de docs(chmod): document options argument (#663) 2017-02-20 15:45:46 -08:00
Stanislav Termosa
8dd2488ec5 feature: add -a option for which command (#655)
* feature: add -a option for which command

* chore(test): test which -a with unexisting name

* chore(test): check which -a has same result as regular one

* chord(test): fix errors mentioned by eslint
2017-02-10 10:32:22 -08:00
Brandon Freitag
055156f7f5 Fix find ENOENT (#654)
* Add find: nonexistant path test

* Catch statSync errors, return shell error
2017-02-02 15:21:58 -08:00
Nate Fischer
84ffb386eb chore(lint): Enforce a trailing comma for multi-line (#646) 2017-01-16 12:45:14 -08:00
Nate Fischer
4c48631d74 refactor: create common.execPath (#636)
Switch to using common.execPath instead of process.execPath directly and warn
electron users if we were unable to find the correct path to NodeJS.
2017-01-08 13:52:39 -08:00
Nate Fischer
ac0ff873f1 refactor: add config.reset() and .resetForTesting() (#641)
Add .reset() and .resetForTesting() to shell.config and use .resetForTesting()
as a standard set-up for unit tests.
2017-01-07 22:40:38 -08:00
Nate Fischer
2da9ab55be fix: allow non-normalized paths as input to mkdir (#635)
Adds tests to make sure that non-normalized paths (i.e. path/to/./dir) are
valid for a few commands, including mkdir() which previously failed when given
the -p flag.

Fixes #634
2017-01-07 18:06:15 -08:00
Brandon Freitag
ded1a3e4a3 Fix: rm behavior regarding symlinks (#598)
* Treat files and symlinks separately

* Remove -f flag from dir symlink test

* Simplify control flow
2016-12-08 23:45:35 -08:00
Brandon Freitag
5ded216dc2 Use Object.assign if possible (#593)
* Fix typo

* Set objectAssign to Object.assign if defined

* Use correct terminology
2016-12-07 12:11:40 -08:00
Brandon Freitag
e4b71f0a23 Add ability to configure error from parseOptions (#594) 2016-12-06 23:52:02 -08:00
Brandon Freitag
12c103fc43 Update sed documentation regarding capture groups (#558)
* Update sed documentation regarding capture groups

* Remove unix sed example for brevity

* Regen README
2016-11-17 21:37:42 -08:00
Nate Fischer
4a1bce8815 fix: handle code-less errors more carefully in exec (#554)
If an error exists, but has no error code, it defaults to 1 (a common code for
most Unix commands). Tests have been omitted since this is an edge case that is
difficult to reproduce.

Fixes #536
2016-11-16 21:51:08 -08:00
Nate Fischer
7a82946a8d fix: maxdepth doesn't limit total number of copies (#549)
* fix: maxdepth doesn't limit total number of copies

Prevent maxdepth from limiting the total number of copies, only allow it to
limit the maximum depth

Fixes #547

* refactor: make `depth` be its own argument
2016-11-10 00:18:04 -08:00
Brandon Freitag
9bc498e512 Fix lint warning (#543)
* Fix lint warning

* Add a TODO to refactor loop
2016-11-01 21:38:00 -07:00
Nate Fischer
4975b54a4f feat: plugin.error() takes an options parameter (#535)
This implements the following options: `continue`, `code`, `prefix`, & `silent`.

Fixes #522, #523
2016-10-23 23:17:42 -07:00
Gyandeep Singh
93ea025755 Revert "refactor: replace fs.existsSync" (fixes #531) (#532)
This reverts commit ab8cf5a8e027b28fc7b784587e4339d2b6d0f08f.
2016-10-17 20:14:43 -07:00
Gyandeep Singh
9bf98dece9 Fix: Remove default glob from shell.test (fixes #529) (#530) 2016-10-17 11:00:04 -07:00
Nate Fischer
d057e075a7 feat: cp -u option (#527)
Only update when the source file is older than the destination, or the
destination file is missing.

Fixes #526
2016-10-14 15:39:59 -07:00
Nate Fischer
45f719de75 fix: echo supports -e option properly (#511) 2016-08-23 19:36:05 -07:00
Nate Fischer
ab8cf5a8e0 refactor: replace fs.existsSync (#509)
* refactor: replace fs.existsSync

* fix: add support for v0.10
2016-08-20 18:28:49 -07:00
Nate Fischer
a0c9032072 refactor: readFromPipe() requires no arguments (#506) 2016-08-13 15:50:31 -07:00
Nate Fischer
902f92ab5b feat: add overWrite option for commands (#503)
* feat: add overWrite option for commands

* fix: throws an error if a command is overwritten
2016-08-08 11:44:16 -07:00