-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Change the current working directory of tests to be the same directory as package.json #1074
Change the current working directory of tests to be the same directory as package.json #1074
Conversation
You also need to update this paragraph in the readme:
|
@sindresorhus I've updated the readme to reflect the change, or rather how I'd expect it to work. However, now I'm slightly in doubt as to whether resolveTestsFrom should be implemented in cli.js as it is. I'm confused by why api.js:140 (https://github.com/alathon/ava/blob/use-resolveTestsFrom-in-fork/cli.js#L140) is implemented like that. Can someone elaborate on why |
@@ -247,7 +247,7 @@ If you're unable to use promises or observables, you may enable "callback mode" | |||
|
|||
You must define all tests synchronously. They can't be defined inside `setTimeout`, `setImmediate`, etc. | |||
|
|||
Test files are run from their current directory, so [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) is always the same as [`__dirname`](https://nodejs.org/api/globals.html#globals_dirname). You can just use relative paths instead of doing `path.join(__dirname, 'relative/path')`. | |||
The process that runs a test file has a [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) that is set to the directory of the package.json file AVA finds. In most projects this will be the root folder of the project. To override this behavior and run tests from a *different* folder, you can set the `resolveTestsFrom` option to a relative path, which will be used instead of the package file 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.
Drop the following: To override this behavior and run tests from a *different* folder, you can set the
resolveTestsFromoption to a relative path, which will be used instead of the package file directory.
It doesn't make sense here as it's for the API, not the consumer. I don't think we want it documented at all as the API is not stable.
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.
Good point. Removed that part again. Is it relevant to discuss my question above about resolveTestsFrom in a separate issue, or is Gitter more appropriate for that?
@novemberborn mentions that resolveTestsFrom is used to resolve command-line paths to files. That means I'm misusing it here, and ironically you'd end up with the same problematic behavior, if you ran ava from a sub-folder. I've gone ahead and added a package option called |
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.
Looking good, @alathon. Just a comment on the tests really, and I think we can simplify the README comment a bit more too.
@@ -307,12 +307,23 @@ function generateTests(prefix, apiCreator) { | |||
}); | |||
}); | |||
|
|||
test(prefix + 'change process.cwd() to a test\'s directory', function (t) { | |||
test(prefix + 'run from package.json folder by default', function (t) { |
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.
This test doesn't do what you think it does.
The apiCreator
executor doesn't actually set the pkgDir
option when it creates the api
. This means that the child process is forked with an undefined
cwd
option, so it ends up using process.cwd()
. Because we run tests from the project root that happens to be the expected directory, but that's just luck.
Instead let's find the correct pkgDir
value in the test itself, and provide it when creating the API on this line and this one.
The next test is good (since it tests a non-default value). Just make sure not to accidentally break it with the aforementioned changes.
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.
Ahh yeah, I see what you're saying. The fact that its even possible to create an Api() leading to an undefined pkgDir being given to fork is probably not a good idea. What about doing the following:
- Set pkgDir in the same way we do now, but in api.js instead, around https://github.com/alathon/ava/blob/2a65c1295588ebdf36dcc833484c34f3ed956add/api.js#L55
- Remove the option from cli.js (making it a flag you can set through the CLI is a decent idea, but probably a separate PR)
This would mean the test would work as expected, and test what it says - that if you just create an Api(), then it'll run from the package.json folder by default.
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 changed things to reflect what I suggest here. The commit history is a bit of a mess now though, due to the rebase from master happening before a pull/push. My bad ^^
@@ -247,7 +247,7 @@ If you're unable to use promises or observables, you may enable "callback mode" | |||
|
|||
You must define all tests synchronously. They can't be defined inside `setTimeout`, `setImmediate`, etc. | |||
|
|||
Test files are run from their current directory, so [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) is always the same as [`__dirname`](https://nodejs.org/api/globals.html#globals_dirname). You can just use relative paths instead of doing `path.join(__dirname, 'relative/path')`. | |||
The process that runs a test file has a [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) that is set to the directory of the package.json file AVA finds. In most projects this will be the root folder of the project. |
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.
How about simply:
AVA tries to run test files with their current working directory set to the directory that contains your
package.json
file.
(Please mark the package.json
bit as code.)
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.
Thats a lot more concise, nice :)
If you rebase against |
@@ -139,6 +139,7 @@ var api = new Api({ | |||
babelConfig: conf.babel, | |||
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(), | |||
timeout: cli.flags.timeout, | |||
pkgDir, |
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.
You can't use object literal shorthand. We still support older Node.js versions.
Yea that's kinda what #611 is about: making the interfaces more explicit.
I'd rather we keep passing it in, test issues aside. For instance #1041 relates to this PR as well, and we should just solve that in Could you go back to the previous implementation and fix the test instead? I'm actually OK with leaving the option implicit for the other places where tests create API instances.
No worries, we'll just squash and merge it. |
@novemberborn I've reverted the PR to the previous version where |
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)', | ||
' --timeout, -T Set global timeout', | ||
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)', | ||
' --init Add AVA to your project', |
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.
Eh, my editor seems to have done something odd with the tabs here.. Will fix
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.
Almost there, nice work @alathon.
@@ -18,6 +21,7 @@ test('must be called with new', function (t) { | |||
generateTests('Without Pool: ', function (options) { | |||
options = options || {}; | |||
options.powerAssert = true; | |||
options.pkgDir = options.pkgDir || path.dirname(pkgConf.filepath(conf)); |
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.
Could you recompute this on line 11? It's run every time a test needs to create an API which seems unnecessary.
You might be about to push the commit, in which case, apologies, but don't forget to fix the whitespace in the README 😉 |
Update description of where tests are run from.
Drop mention of overriding `resolveTestsFrom`
More concise description of where tests run from.
The Travis CI failures seem to be related to upgrading xo, am I right in this @sindresorhus ? |
' --watch, -w Re-run tests when tests and source files change', | ||
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)', | ||
' --timeout, -T Set global timeout', | ||
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)', |
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.
@alathon still seeing a diff here.
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.
Yes, there is a diff. But it lines up correctly in the terminal, when I execute it now. I'm not sure how to un-diff just that particular part of the file, although I know its possible through git.
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.
Well it renders the same so I'm not too bothered I suppose. @sindresorhus?
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.
They're different as you changed from tabs to spaces. They should remain spaces.
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'll fix it on merge, but try not to do unrelated changes when doing a PR ;)
You could have uncommitted that part btw. Would be easiest to use a GUI git app for that. I use GitUp.app.
0711e93
to
f1d21ba
Compare
Branch updated with changes from upstream master now (to fix |
Thanks @alathon :) |
@novemberborn This fixes #32, right? |
Description
Changes fork to spawn the process based on the (existing) value of
options.resolveTestsFrom
, which is the package directory by default. Fixes, amongst other things, problems with babel-root-import, which does not work with the current behavior (as it expects process.cwd() to be the package.json folder).Related to #32 and several other issues that stem from the same problem.
I'm not 100% sure that I'm meant to be piggybacking options.resolveTestsFrom for this purpose - but it seems like the appropriate option. There were no tests of resolveTestsFrom at all, so I'm not 100% sure what the option is meant to be doing, if not this.