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

Echo test mocks #708

Merged
merged 8 commits into from May 6, 2017
Merged

Echo test mocks #708

merged 8 commits into from May 6, 2017

Conversation

freitagbr
Copy link
Contributor

@freitagbr freitagbr commented Apr 27, 2017

Fixes #622

Adds mocks for process.stdout.write and process.stderr.write that allows the written values to be extracted. This way, we can inspect the output of echo without actually outputting to stdout.

@freitagbr freitagbr added fix Bug/defect, or a fix for such a problem high priority test labels Apr 27, 2017
@freitagbr freitagbr added this to the v0.8.0 milestone Apr 27, 2017
@freitagbr freitagbr requested a review from nfischer April 27, 2017 05:35
@freitagbr freitagbr changed the base branch from master to dev April 27, 2017 05:35
@codecov-io
Copy link

codecov-io commented Apr 28, 2017

Codecov Report

❗ No coverage uploaded for pull request base (dev@7527b36). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##             dev     #708   +/-   ##
======================================
  Coverage       ?   96.63%           
======================================
  Files          ?       33           
  Lines          ?     1160           
  Branches       ?        0           
======================================
  Hits           ?     1121           
  Misses         ?       39           
  Partials       ?        0
Impacted Files Coverage Δ
src/echo.js 100% <ø> (ø)

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 7527b36...ded614b. 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.

Seems mostly good, just had a few questions inline.

Any reason this needs to be merged onto dev instead of master?

Also, can you add some mention of the mocks in the PR description? I imagine we may use these mocks in other tests, so it's worth mentioning.

test/echo.js Outdated
t.is(stdout, '555\n');
t.end();
});
test.skip('simple test with silent(true)', t => {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why we're skipping this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think because this is testing with config.silent = true;, but we already have set shell.config.silent = true above on line 7, so this test is redundant.

Copy link
Member

Choose a reason for hiding this comment

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

In that case, why not delete it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will delete this test.

@@ -0,0 +1,73 @@
/* eslint-disable prefer-rest-params, strict */
Copy link
Member

Choose a reason for hiding this comment

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

Why are we disabling linting?


function init() {
resetValues();
console.log = consoleLog;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to mock console.log and console.error? What happens if we just mock process.stdout & .stderr objects? That might give us more reliable results overall.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that's probably a better thing to do in this scenario. I actually just copied this over from the shx mocks. I'll reduce this down to just the process.stdout and process.stderr.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, SGTM

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.

@freitagbr can you manually confirm that ava still outputs the result of the test (pass/fail)? I want to make sure the mocks don't interfere with test reporting.

process.exit = oldProcessExit;
}
exports.restore = restore;
function Mocks() {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason we're using mocks as a prototype? I like the idea in general, but we're mocking global values. Using prototypes gives the illusion that multiple mocks can be created, which wouldn't work very well (the second mock would be mocking the first one).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point. I implemented it as a prototype because I though it was a good way to implement this. But, if you have several mocks going at once, the true process.stdout.write will get swapped around in them, and it may be hard to figure out where it went, unless you restore in the right order. This seems like it could be problematic, so I will revert this to the global version.

Copy link
Member

Choose a reason for hiding this comment

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

Might be good to enforce that init() can only be called once though (since calling it twice will lose the real process.stdout). Maybe we can just implement this as 2 streams for process.stdout and process.stdin, then we don't have to reinvent the stream logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've re-implemented the mocks using globals, and calling init() or restore() any number of times will not lose the real process.stdout.write, nor the mock functions

@freitagbr
Copy link
Contributor Author

Yes, the tests still output during these tests, even with the mocks. In fact, that spinny thing is still shown, so I don't think the mocks interfere with anything.

@freitagbr
Copy link
Contributor Author

Also, the reason I merged this into dev instead of master is because of the extra unit tests added in the newline echo branch. I figured it would just be easier to refactor all the tests at once, instead of fixing merge conflicts later on.

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.

LGTM

@freitagbr freitagbr merged commit e7bebba into dev May 6, 2017
@freitagbr freitagbr deleted the echo-test-mocks branch May 6, 2017 02:12
freitagbr added a commit that referenced this pull request Jun 7, 2017
* 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
@nfischer nfischer mentioned this pull request Jun 7, 2017
nfischer pushed a commit that referenced this pull request Jun 7, 2017
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug/defect, or a fix for such a problem high priority test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants