-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix user home config conflicting with the test suite #2725
Conversation
src/registries/npm-registry.js
Outdated
@@ -98,7 +98,7 @@ export default class NpmRegistry extends Registry { | |||
async getPossibleConfigLocations(filename: string): Promise<Array<[boolean, string, string]>> { | |||
const possibles = [ | |||
[false, path.join(this.cwd, filename)], | |||
[true, this.config.userconfig || path.join(userHome, filename)], | |||
[true, this.config.userconfig || path.join(process.env.YARN_HOME_FOLDER || userHome, filename)], |
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.
Before coming up with this fix, I tried several others. Most notably I tried to pass on the config to override this.config.userconfig
used on the same line. However, this.config
is initialized as empty object in the constructor
of the Registries. Passing on an initialConfig
would work, but in cli/commands/global.js
this config is discarded and rewritten. Therefore that solution would require passing on config objects all over the place in order to properly inject the location.
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 is a bit confusing because we have HOME defined in constants.js, npm-registry and yarn-registry.
Could you bring all of them into one place?
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 have modified the util-home-dir
to export a function instead. This allows for later extension if developers want to modify their home dir too. I hope that this was what you were looking for, else please let me know!
3ab1b0b
to
ce70e4a
Compare
Rebased this PR to fix the import test failures, but now AppVeyor is still failing per #2613 (comment) |
ce70e4a
to
3ca3c32
Compare
I rebased this PR on master and all tests are passing now. |
3ca3c32
to
550a74f
Compare
__tests__/commands/_helpers.js
Outdated
@@ -9,6 +9,7 @@ import {run as check} from '../../src/cli/commands/check.js'; | |||
import * as fs from '../../src/util/fs.js'; | |||
import {Install} from '../../src/cli/commands/install.js'; | |||
import Config from '../../src/config.js'; | |||
import {userHomeDir, setUserHomeDir} from '../../src/util/user-home-dir'; |
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 think for tests this can be achieved with a simple jest mock,
jest.setMock('../../../src/constants', Object.assign(automock, mocks)); |
Then you won't need to do setter/getter and change the 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.
Better mock user-home with Jest
550a74f
to
1f52ca8
Compare
@bestander thank you for your suggestion of using Could you maybe point out what the issue is? Thank you in advance! |
@TimvdLippe, you can have a look at https://github.com/yarnpkg/yarn/blob/master/__tests__/commands/install/integration.js#L38 how an internal js module can be mocked. |
@bestander I have copied that to https://github.com/yarnpkg/yarn/pull/2725/files#diff-ae8f7de5bfa66fccd0d4a651ae01fc77R22 The problem right now is that when I run the test, the home directory does not seem to be mocked. I suspect that the problem is that |
Yeah, you are right, the value from user-home-dir is imported into other projects. |
The test suite did not properly isolate the home config from influencing the behavior of the tests. Therefore if a user used the global configuration and also cloned Yarn, the tests could fail. A primary example of that was when a user set the prefix value. If the user would set the prefix, the test suite would fail.
1f52ca8
to
d2ce0ad
Compare
The latest commit tries to do so, but sadly still fails locally 😭 Maybe a Jest engineer could chime in to explain why it is not working? |
Summary
The test suite did not properly isolate the home config from
influencing the behavior of the tests. Therefore if a user
used the global configuration and also cloned Yarn, the tests could fail.
A primary example of that was when a user set the prefix value.
If the user would set the prefix, the test suite would fail.
Test plan
yarn
on your machine following https://yarnpkg.com/en/docs/installyarn config set prefix /usr/local
(or any other location)yarn
yarn
Without this fix,
react-native
would be installed in/usr/local/bin/
and the test suite fails in__tests__/commands/global.js
. Now the prefix is correctly set andreact-native
is installed in the proper location.