-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Conversation
Commit message has a single quote instead of a backtick FWIW |
There was a problem hiding this 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
test/common/README.md
Outdated
|
||
* [<String>] | ||
|
||
The absolute path to the `test/fixtures/ directory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing `
test/common/README.md
Outdated
|
||
* `...args` [<String>] | ||
|
||
Returns the result of `path.join(fixturesDir, ...args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing `
test/common/README.md
Outdated
|
||
* `args` [<String|Array>] | ||
|
||
Returns the result of `fs.readFileSync(path.join(fixturesDir, ...args), 'enc'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing `
test/common/README.md
Outdated
|
||
* `arg` [<String>] | ||
|
||
Returns the result of `fs.readFileSync(path.join(fixturesDir, 'keys', arg), 'enc'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing `
test/common/README.md
Outdated
|
||
### fixtures.readFixtureSync(args[, enc]) | ||
|
||
* `args` [<String|Array>] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[<String>] | [<Array>]
?
I'm way too tired right now to review this, but not too tired to express enthusiasm for more cohesive |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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');
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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:
- What this PR currently does, import the functions you want to use directly from
fixtures
, so you use withfixturesPath()
. - Don't use the
{ fixturesPath }
require syntax, so you doconst fixtures = require('../common/fixtures)
, and use withfixtures.fixturesPath()
(or maybe justfixtures.path()
). - Import and re-export each function/object from the common module, so you do
common.fixturesPath()
. - Import and re-export
fixtures
from common, so you docommon.fixtures.fixturesPath()
(orcommon.fixtures.path()
).
I think I'd prefer 4.
, interested to see what other people think.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Option 2 is fine.
There was a problem hiding this comment.
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()
😁
test/common/README.md
Outdated
|
||
* `...args` [<String>] | ||
|
||
Returns the result of `path.join(fixturesDir, ...args)`. |
There was a problem hiding this comment.
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)`.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
test/common/README.md
Outdated
|
||
* `args` [<String>] | [<Array>] | ||
|
||
Returns the result of `fs.readFileSync(path.join(fixturesDir, ...args), 'enc')`. |
There was a problem hiding this comment.
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')`.
7883d07
to
fc179c7
Compare
Rebased... |
Updated to use |
d3ff72f
to
5d870ac
Compare
} | ||
|
||
function readFixtureKey(name, enc) { | ||
return fs.readFileSync(fixturesPath('keys', name), enc); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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');
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
530950e
to
1205a5d
Compare
@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! |
1205a5d
to
fbb654e
Compare
There was a problem hiding this 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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
8ca4083
to
78e5757
Compare
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.
78e5757
to
81e35b4
Compare
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]>
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]>
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]>
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]>
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]>
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]>
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]>
This should likely be backported to @jasnell would you be able to backport? |
Not anytime within the next couple of weeks. |
I'm give it a go. |
ping @refack this blocking landing a whole bunch of the code and learn patches to v6.x |
Oh it now. |
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]>
Backport PR: #16265 |
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]>
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]>
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]>
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 uses
path.join()`, some code uses string concats, some othercode 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), orvcbuild test
(Windows) passesAffected core subsystem(s)
test