/
config.test.js
130 lines (107 loc) Β· 5.16 KB
/
config.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const {
fs: {mkdirp, writeFile, createTemporaryFolder},
} = require('pkg-tests-core');
const RC_FILENAME = `.spec-yarnrc`;
const SUBFOLDER = `subfolder`;
const FAKE_REGISTRY_URL = `http://yarn.test.registry`;
const FAKE_LOCAL_APP_DATA = `LOCAL_APP_DATA`;
const FAKE_WORKSPACE_ROOT = `WORKSPACE_ROOT`;
const FAKE_HOME = `HOME`;
const environments = {
[`folder without rcfile in ancestry`]: async () => {
// Nothing to do
},
[`folder with rcfile`]: async ({path}) => {
await writeFile(`${path}/${SUBFOLDER}/${SUBFOLDER}/${RC_FILENAME}`, `init-scope: berry-test\n`);
},
[`folder with rcfile without trailing newline`]: async ({path}) => {
await writeFile(`${path}/${SUBFOLDER}/${SUBFOLDER}/${RC_FILENAME}`, `init-scope: berry-test`);
},
[`folder with rcfile and rc in parent`]: async ({path}) => {
await writeFile(`${path}/${SUBFOLDER}/${SUBFOLDER}/${RC_FILENAME}`, `init-scope: berry-test\n`);
await writeFile(`${path}/${SUBFOLDER}/${RC_FILENAME}`, `init-scope: value-to-override\nlast-update-check: 1555784893958\n`);
},
[`folder with rcfile and rc in ancestor parent`]: async ({path}) => {
await writeFile(`${path}/${SUBFOLDER}/${SUBFOLDER}/${RC_FILENAME}`, `init-scope: berry-test\n`);
await writeFile(`${path}/${RC_FILENAME}`, `init-scope: value-to-override\nlast-update-check: 1555784893958\n`);
},
[`folder with rcfile and rc in home folder`]: async ({path, homePath}) => {
await writeFile(`${homePath}/${RC_FILENAME}`, `init-scope: value-to-override\ndefault-language-name: python\n`);
await writeFile(`${path}/${RC_FILENAME}`, `init-scope: berry-test\nlast-update-check: 1555784893958\n`);
},
};
function cleanupPlainOutput(output, path, homePath) {
// Replace multiple consecutive spaces with one space.
// The output of the config command is aligned according to the longest value, which probably
// contains `path`. In other words, the formatting depends on the length of `path`.
output = output.replace(/ +/g, ` - `);
// replace the generated workspace root with a constant
output = output.replace(new RegExp(path, `g`), FAKE_WORKSPACE_ROOT);
// replace the generated home folder with a constant
output = output.replace(new RegExp(homePath, `g`), FAKE_HOME);
// replace the generated registry server URL with a constant
output = output.replace(/http:\/\/localhost:\d+/g, FAKE_REGISTRY_URL);
// replace the default global folder with a constant
output = output.replace(/[^"]+\/\.?yarn\/berry/ig, FAKE_LOCAL_APP_DATA);
return output;
}
function cleanupJsonOutput(output, path, homePath) {
let outputObject;
try {
outputObject = JSON.parse(output);
} catch (e) {
return cleanupPlainOutput(output, path, homePath);
}
// the default globalFolder contains the user's home folder, override that value
outputObject[`globalFolder`].default = `DEFAULT_GLOBAL_FOLDER`;
// replace the generated registry server URL with a constant
outputObject[`npmRegistryServer`].effective = FAKE_REGISTRY_URL;
const pathRegExp = new RegExp(path, `g`);
const homePathRegExp = new RegExp(homePath, `g`);
for (const setting of Object.values(outputObject)) {
if (typeof setting.source === 'string') {
setting.source = setting.source.replace(pathRegExp, FAKE_WORKSPACE_ROOT);
setting.source = setting.source.replace(homePathRegExp, FAKE_HOME);
}
if (typeof setting.default === 'string') {
setting.default = setting.default.replace(pathRegExp, FAKE_WORKSPACE_ROOT);
setting.default = setting.default.replace(homePathRegExp, FAKE_HOME);
}
if (typeof setting.effective === 'string') {
setting.effective = setting.effective.replace(pathRegExp, FAKE_WORKSPACE_ROOT);
setting.effective = setting.effective.replace(homePathRegExp, FAKE_HOME);
}
}
return JSON.stringify(outputObject);
}
const options = {
[`without flags`]: {cleanupStdout: cleanupPlainOutput, flags: []},
[`showing the source`]: {cleanupStdout: cleanupPlainOutput, flags: [`--why`]},
[`showing explanation`]: {cleanupStdout: cleanupPlainOutput, flags: [`--verbose`]},
[`as json`]: {cleanupStdout: cleanupJsonOutput, flags: [`--json`]},
};
describe(`Commands`, () => {
describe(`config`, () => {
for (const [environmentDescription, environment] of Object.entries(environments)) {
for (const [optionDescription, {flags, cleanupStdout}] of Object.entries(options)) {
test(`test (${environmentDescription} / ${optionDescription})`, makeTemporaryEnv({}, async ({path, run, source}) => {
const cwd = `${path}/${SUBFOLDER}/${SUBFOLDER}`;
const homePath = await createTemporaryFolder();
await mkdirp(cwd);
await environment({path, homePath});
let code;
let stdout;
let stderr;
try {
({code, stdout, stderr} = await run(`config`, ...flags, {cwd, env: {YARN_RC_FILENAME: RC_FILENAME, HOME: homePath, USERPROFILE: homePath}}));
} catch (error) {
({code, stdout, stderr} = error);
}
stdout = cleanupStdout(stdout, path, homePath);
stderr = cleanupPlainOutput(stderr, path, homePath);
expect({code, stdout, stderr}).toMatchSnapshot();
}));
}
}
});
});