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

test: begin normalizing fixtures use #14332

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jul 17, 2017

Adds a new ../common/fixtures' module to begin normalizing test/fixturesuse. Our test code is a bit inconsistent with regards to use of the fixtures directory. Some code usespath.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

test

@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label Jul 17, 2017
@mscdex
Copy link
Contributor

mscdex commented Jul 17, 2017

Commit message has a single quote instead of a backtick FWIW

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

LGTM fort the test/common changes, somewhat rubber-stamp-y LGTM for the rest


* [<String>]

The absolute path to the `test/fixtures/ directory.
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing `


* `...args` [<String>]

Returns the result of `path.join(fixturesDir, ...args);
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing `


* `args` [<String|Array>]

Returns the result of `fs.readFileSync(path.join(fixturesDir, ...args), 'enc');
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing `


* `arg` [<String>]

Returns the result of `fs.readFileSync(path.join(fixturesDir, 'keys', arg), 'enc');
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing `


### fixtures.readFixtureSync(args[, enc])

* `args` [<String|Array>]
Copy link
Contributor

Choose a reason for hiding this comment

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

[<String>] | [<Array>]?

@Trott
Copy link
Member

Trott commented Jul 18, 2017

I'm way too tired right now to review this, but not too tired to express enthusiasm for more cohesive common modules like this for tests! 🎉

Copy link
Member

@gibfahn gibfahn left a comment

Choose a reason for hiding this comment

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

Some minor suggestions, and one question I think we should discuss now.

+1 on this otherwise, seems like a really set of helpers for common.

}

function readFixtureKey(name, enc) {
return fs.readFileSync(fixturesPath('keys', name), enc);
Copy link
Member

Choose a reason for hiding this comment

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

Unless I'm misreading this (quite possible), readFixtureKey('agent1-key.pem') == readFixtureSync('keys', 'agent1-key.pem'),. If so I'd rather we just have the latter, having an extra method adds unnecessary complexity for no particular gain IMO.

Copy link
Member Author

Choose a reason for hiding this comment

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

Take another look at the signatures. readFixtureSync(...args) takes an arbitrary number of args that are concatenated using path.join(...args), whereas readFixtureKey(name, enc) takes a single path segment and an optional encoding. Yes, readFixtureKey() uses readFixtureSync() but they are not equivalent to one.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not saying they're identical, I'm saying that fixtures.readKey() is a specialised version of fixtures.readSync() (the one where the first arg is keys and you can omit the encoding).

You're right, I should have said:

fixtures.readKey('agent1-key.pem') == fixtures.readSync(['keys', 'agent1-key.pem'], 'utf8')

For me the latter is clearer (which is why I don't understand the need for readFixtureKey())

Copy link
Member Author

Choose a reason for hiding this comment

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

Generally because I find the necessity of using the [...] syntax a bit ugly and unfriendly.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, I have a mild preference the other way, but not strongly enough to block this.

const key = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
const ca = fs.readFileSync(fixturesPath('test_ca.pem'), 'ascii');
const cert = fs.readFileSync(fixturesPath('test_cert.pem'), 'ascii');
const key = fs.readFileSync(fixturesPath('test_key.pem'), 'ascii');
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't these be const key = readFixtureSync('test_key.pem', 'ascii');?

Copy link
Member Author

Choose a reason for hiding this comment

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

no... see above. I left these because the keys being read require the additional encoding arg and but the paths do not include the keys directory.

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT:

(function readFixtureSync(args, enc) {
  if (Array.isArray(args))
    return fs.readFileSync(fixturesPath(...args), enc);
  return fs.readFileSync(fixturesPath(args), enc);
})('test_key.pem', 'ascii')
// =>
if (Array.isArray('test_key.pem'))
  return fs.readFileSync(fixturesPath(...'test_key.pem'), 'ascii');
return fs.readFileSync(fixturesPath('test_key.pem'), 'ascii');
// =>
return fs.readFileSync(fixturesPath('test_key.pem'), 'ascii');

Q.E.D.


const fixture = path.join(common.fixturesDir, 'exit.js');
const fixture = fixturesPath('exit.js');
Copy link
Member

Choose a reason for hiding this comment

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

I don't like losing the common. prefix, I think the const { fixturesPath } is a really nice syntax, but when you're going through tests it's really helpful to know which variables are common. Assuming the movement towards breaking up common continues, we'll have more and more variables that aren't obviously from common.

I think the options are:

  1. What this PR currently does, import the functions you want to use directly from fixtures, so you use with fixturesPath().
  2. Don't use the { fixturesPath } require syntax, so you do const fixtures = require('../common/fixtures), and use with fixtures.fixturesPath() (or maybe just fixtures.path()).
  3. Import and re-export each function/object from the common module, so you do common.fixturesPath().
  4. Import and re-export fixtures from common, so you do common.fixtures.fixturesPath() (or common.fixtures.path()).

I think I'd prefer 4., interested to see what other people think.

Copy link
Member Author

Choose a reason for hiding this comment

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

-1 ... I personally prefer the more compact syntax.

Copy link
Member

Choose a reason for hiding this comment

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

I like option 2. It makes it obvious where the function came from without having to find the declaration and it allows us the option to continue to make common less monolithic.

That said, I'm actually OK with anything. Just, I like option 2 the best.

Copy link
Member

Choose a reason for hiding this comment

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

cc/ @nodejs/collaborators, I'd like to know what others think.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to use option 2 too

Copy link
Member Author

Choose a reason for hiding this comment

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

Option 2 is fine.

Copy link
Member

@gibfahn gibfahn Jul 25, 2017

Choose a reason for hiding this comment

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

@jasnell taking this test as an example, going forward when looking at this test one has to remember that fixturesPath is from common, and fixture is a local variable defined in this test. Multiply that by 1500 tests and five common modules and things get complex.

More compact syntax seems to me to be favouring writability over readability, I'd rather do the opposite.

I'm fine with 2., it's slightly more mental load, but not much, and fixtures.path() is no less compact than fixturesPath() 😁


* `...args` [<String>]

Returns the result of `path.join(fixturesDir, ...args)`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a sentence for each of these, e.g.:

Returns the absolute path to the specified fixture, i.e. the result of 
`path.join(fixturesDir, ...args)`.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if that's much better.

Copy link
Member

@gibfahn gibfahn Jul 25, 2017

Choose a reason for hiding this comment

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

@jasnell do you think it's worse? Open to alternatives.

I think the docs should add something to the code, if the docs just repeat the code then what's the point?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just don't see the suggested bits as adding any significant value. The short bit that is there highlights the code that is being replaced by the common utility, the fact that it's an absolute path is largely irrelevant.


* `args` [<String>] | [<Array>]

Returns the result of `fs.readFileSync(path.join(fixturesDir, ...args), 'enc')`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe

Takes the relative path to the fixture, and returns the contents of the file, ie. the result of
`fs.readFileSync(path.join(fixturesDir, ...args), 'enc')`.

@jasnell
Copy link
Member Author

jasnell commented Jul 24, 2017

Rebased...

@jasnell
Copy link
Member Author

jasnell commented Jul 24, 2017

@jasnell
Copy link
Member Author

jasnell commented Jul 25, 2017

Updated to use fixtures.path, fixtures.readSync and fixtures.readKey

}

function readFixtureKey(name, enc) {
return fs.readFileSync(fixturesPath('keys', name), enc);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not saying they're identical, I'm saying that fixtures.readKey() is a specialised version of fixtures.readSync() (the one where the first arg is keys and you can omit the encoding).

You're right, I should have said:

fixtures.readKey('agent1-key.pem') == fixtures.readSync(['keys', 'agent1-key.pem'], 'utf8')

For me the latter is clearer (which is why I don't understand the need for readFixtureKey())

const key = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
const ca = fs.readFileSync(fixtures.path('test_ca.pem'), 'ascii');
const cert = fs.readFileSync(fixtures.path('test_cert.pem'), 'ascii');
const key = fs.readFileSync(fixtures.path('test_key.pem'), 'ascii');
Copy link
Member

Choose a reason for hiding this comment

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

@jasnell re/ #14332 (comment)

I don't understand, the test passes for me with this change:

-  const ca = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
-  const cert = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
-  const key = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
+  const ca = fixtures.readSync('test_ca.pem', 'ascii');
+  const cert = fixtures.readSync('test_cert.pem', 'ascii');
+  const key = fixtures.readSync('test_key.pem', 'ascii');

Copy link
Member Author

Choose a reason for hiding this comment

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

heh... I misread your original comment on this :-). You're right :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

@jasnell jasnell force-pushed the test-common-fixture branch 2 times, most recently from 530950e to 1205a5d Compare August 3, 2017 18:51
@jasnell
Copy link
Member Author

jasnell commented Aug 3, 2017

@gibfahn ... fixed the one issue and provided an explanation for the other. Can you please take another look when you get a moment. Thank you!

@jasnell
Copy link
Member Author

jasnell commented Aug 4, 2017

Copy link
Member

@gibfahn gibfahn left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @jasnell

const fixture = path.resolve(common.fixturesDir, 'person.jpg.gz');
const unzippedFixture = path.resolve(common.fixturesDir, 'person.jpg');
const fixture = fixtures.path('person.jpg.gz');
const unzippedFixture = fixtures.path('person.jpg');
const outputFile = path.resolve(common.tmpDir, 'person.jpg');
const expect = fs.readFileSync(unzippedFixture);
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't really need to be part of this PR, but this could now be:

const expect = fixtures.readFile('person.jpg');

right?

}

function readFixtureKey(name, enc) {
return fs.readFileSync(fixturesPath('keys', name), enc);
Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, I have a mild preference the other way, but not strongly enough to block this.

@jasnell jasnell force-pushed the test-common-fixture branch 2 times, most recently from 8ca4083 to 78e5757 Compare August 8, 2017 00:15
@jasnell
Copy link
Member Author

jasnell commented Aug 8, 2017

Final CI before landing: https://ci.nodejs.org/job/node-test-pull-request/9530/

Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.
maasencioh added a commit to maasencioh/node that referenced this pull request Aug 24, 2017
BridgeAR pushed a commit that referenced this pull request Aug 28, 2017
PR-URL: #14716
Refs: #14332
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
ghost pushed a commit to ayojs/ayo that referenced this pull request Aug 30, 2017
PR-URL: nodejs/node#14716
Refs: nodejs/node#14332
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
ghost pushed a commit to ayojs/ayo that referenced this pull request Aug 30, 2017
PR-URL: nodejs/node#14716
Refs: nodejs/node#14332
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Sep 9, 2017
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

PR-URL: #14332
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
MylesBorins pushed a commit that referenced this pull request Sep 10, 2017
PR-URL: #14716
Refs: #14332
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Sep 10, 2017
MylesBorins pushed a commit that referenced this pull request Sep 11, 2017
PR-URL: #14716
Refs: #14332
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
MylesBorins pushed a commit that referenced this pull request Sep 12, 2017
PR-URL: #14716
Refs: #14332
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
@MylesBorins
Copy link
Contributor

This should likely be backported to v6.x-staging as it introduces a decent sized delta.

@jasnell would you be able to backport?

@jasnell
Copy link
Member Author

jasnell commented Sep 20, 2017

Not anytime within the next couple of weeks.

@refack
Copy link
Contributor

refack commented Sep 20, 2017

I'm give it a go.

@refack refack self-assigned this Sep 20, 2017
@MylesBorins
Copy link
Contributor

ping @refack this blocking landing a whole bunch of the code and learn patches to v6.x

@refack
Copy link
Contributor

refack commented Oct 17, 2017

Oh it now.

refack pushed a commit to refack/node that referenced this pull request Oct 17, 2017
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

PR-URL: nodejs#14332
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@refack
Copy link
Contributor

refack commented Oct 17, 2017

Backport PR: #16265

MylesBorins pushed a commit that referenced this pull request Oct 19, 2017
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

Backport-PR-URL: #16265
PR-URL: #14332
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Oct 19, 2017
MylesBorins pushed a commit that referenced this pull request Oct 25, 2017
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

Backport-PR-URL: #16265
PR-URL: #14332
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
MylesBorins pushed a commit that referenced this pull request Oct 26, 2017
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

Backport-PR-URL: #16265
PR-URL: #14332
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Nov 3, 2017
@refack refack removed their assignment Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.