Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "encoding" option to exec #763

Merged
merged 5 commits into from Sep 13, 2017
Merged

Add "encoding" option to exec #763

merged 5 commits into from Sep 13, 2017

Conversation

freitagbr
Copy link
Contributor

Fixes #675 by adding support for the 'encoding' option to exec. It technically supported the 'encoding' option before, but the return values of stdout and stderr did not honor the encoding. Now, they will.

@freitagbr freitagbr added exec Issues specific to the shell.exec() API feature labels Aug 23, 2017
@freitagbr freitagbr added this to the v0.8.0 milestone Aug 23, 2017
src/exec.js Outdated
var stderr = fs.readFileSync(stderrFile, 'utf8');
// fs.readFileSync uses buffer encoding by default, so set
// the encoding only if opts.encoding wasn't set to 'buffer'
var encoding = opts.encoding !== 'buffer' ? opts.encoding : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we get rid of the ternary and replace with encoding = opts.encoding? It seems weird to specify 'buffer' if what it actually turns into is null

Copy link
Contributor Author

@freitagbr freitagbr Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this would be clearer:

var stdout;
var stderr;
if (opts.encoding === 'buffer') {
    stdout = fs.readFileSync(stdoutFile);
    stderr = fs.readFileSync(stderrFile);
} else {
    stdout = fs.readFileSync(stdoutFile, opts.encoding);
    stderr = fs.readFileSync(stderrFile, opts.encoding);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the way to go:

var stdout = fs.readFileSync(stdoutFile, opts.encoding);
var stderr = fs.readFileSync(stderrFile, opts.encoding);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work, because the 'buffer' encoding is only valid for the child_process methods:

> fs.readFileSync('./package.json', 'buffer')
Error: Unknown encoding: buffer
    at assertEncoding (fs.js:88:11)
    at Object.fs.readFileSync (fs.js:394:3)
    at repl:1:4
    at REPLServer.defaultEval (repl.js:272:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:441:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:203:10)

If you need to get a buffer out of the fs methods, encoding needs to be null or undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok. Nevermind, your proposal SGTM

test/exec.js Outdated
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(Buffer.isBuffer(result.stdout));
t.is(result.stdout.toString(), '1234\n');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check stderr too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will add a stderr check here.

@codecov-io
Copy link

codecov-io commented Aug 24, 2017

Codecov Report

Merging #763 into master will decrease coverage by 0.12%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #763      +/-   ##
==========================================
- Coverage   97.38%   97.25%   -0.13%     
==========================================
  Files          33       33              
  Lines        1222     1240      +18     
==========================================
+ Hits         1190     1206      +16     
- Misses         32       34       +2
Impacted Files Coverage Δ
src/exec.js 97.4% <77.77%> (-2.6%) ⬇️
src/ls.js 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ee83eb...0e0994b. Read the comment docs.

Copy link
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this. Have we verified it works for the reported use case from the bug?

@freitagbr
Copy link
Contributor Author

I have tested this with something simple, like cat foo.tar | tar -xO, and the output is correct. Maybe we can get @vongohren to chime in here to see if this branch fixes the problem.

@vongohren
Copy link

Hey we are not using shelljs anymore for this task, so I dont have anything laying around to verify.

@freitagbr
Copy link
Contributor Author

I'm going to go ahead and merge this, since it seems like a useful feature to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exec Issues specific to the shell.exec() API feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants