Skip to content

Commit fbf7c38

Browse files
author
Tom Sparrow
committed
Add tests for --no-default and --no-default --json
Squashed from several commits while figuring out what was going on: Remove unnecessary replacements Back in 7835a7d there was no `FILTER` at the top but now that there is, these settings are not in the output. Remove confusing cleanupJsonOutput method This does not support ndjson so in all tests (prior to this PR) it was failing to parse the JSON and not actually executing most of the method body. Now that one of the new tests returns only a single line of JSON this 'resurrected' this dead broken method causing tests to fail. Instead use the same cleanup method for all cases, which is what has been happening anyway (due to the fallback in the `catch`) Add the remaining to snapshots now the tests are fixed
1 parent c020664 commit fbf7c38

File tree

2 files changed

+147
-43
lines changed

2 files changed

+147
-43
lines changed

packages/acceptance-tests/pkg-tests-specs/sources/commands/__snapshots__/config.test.ts.snap

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Commands config test (folder with rcfile / as json no defaults) 1`] = `
4+
{
5+
"code": 0,
6+
"stderr": "",
7+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
8+
",
9+
}
10+
`;
11+
312
exports[`Commands config test (folder with rcfile / as json) 1`] = `
413
{
514
"code": 0,
@@ -11,6 +20,18 @@ exports[`Commands config test (folder with rcfile / as json) 1`] = `
1120
}
1221
`;
1322
23+
exports[`Commands config test (folder with rcfile / no defaults) 1`] = `
24+
{
25+
"code": 0,
26+
"stderr": "",
27+
"stdout": "└─ initScope
28+
- ├─ Description: Scope used when creating packages via the init command
29+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
30+
- └─ Value: 'my-test'
31+
",
32+
}
33+
`;
34+
1435
exports[`Commands config test (folder with rcfile / without flags) 1`] = `
1536
{
1637
"code": 0,
@@ -33,6 +54,16 @@ exports[`Commands config test (folder with rcfile / without flags) 1`] = `
3354
}
3455
`;
3556
57+
exports[`Commands config test (folder with rcfile and rc in ancestor parent / as json no defaults) 1`] = `
58+
{
59+
"code": 0,
60+
"stderr": "",
61+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
62+
{"key":"lastUpdateCheck","effective":"1555784893958","source":"WORKSPACE_ROOT/.yarnrc.yml","description":"Last timestamp we checked whether new Yarn versions were available","type":"STRING","default":null}
63+
",
64+
}
65+
`;
66+
3667
exports[`Commands config test (folder with rcfile and rc in ancestor parent / as json) 1`] = `
3768
{
3869
"code": 0,
@@ -44,6 +75,23 @@ exports[`Commands config test (folder with rcfile and rc in ancestor parent / as
4475
}
4576
`;
4677
78+
exports[`Commands config test (folder with rcfile and rc in ancestor parent / no defaults) 1`] = `
79+
{
80+
"code": 0,
81+
"stderr": "",
82+
"stdout": "├─ initScope
83+
- ├─ Description: Scope used when creating packages via the init command
84+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
85+
- └─ Value: 'my-test'
86+
87+
└─ lastUpdateCheck
88+
- ├─ Description: Last timestamp we checked whether new Yarn versions were available
89+
- ├─ Source: WORKSPACE_ROOT/.yarnrc.yml
90+
- └─ Value: '1555784893958'
91+
",
92+
}
93+
`;
94+
4795
exports[`Commands config test (folder with rcfile and rc in ancestor parent / without flags) 1`] = `
4896
{
4997
"code": 0,
@@ -66,6 +114,16 @@ exports[`Commands config test (folder with rcfile and rc in ancestor parent / wi
66114
}
67115
`;
68116
117+
exports[`Commands config test (folder with rcfile and rc in home folder / as json no defaults) 1`] = `
118+
{
119+
"code": 0,
120+
"stderr": "",
121+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
122+
{"key":"lastUpdateCheck","effective":"1555784893958","source":"WORKSPACE_ROOT/.yarnrc.yml","description":"Last timestamp we checked whether new Yarn versions were available","type":"STRING","default":null}
123+
",
124+
}
125+
`;
126+
69127
exports[`Commands config test (folder with rcfile and rc in home folder / as json) 1`] = `
70128
{
71129
"code": 0,
@@ -77,6 +135,23 @@ exports[`Commands config test (folder with rcfile and rc in home folder / as jso
77135
}
78136
`;
79137
138+
exports[`Commands config test (folder with rcfile and rc in home folder / no defaults) 1`] = `
139+
{
140+
"code": 0,
141+
"stderr": "",
142+
"stdout": "├─ initScope
143+
- ├─ Description: Scope used when creating packages via the init command
144+
- ├─ Source: WORKSPACE_ROOT/.yarnrc.yml
145+
- └─ Value: 'my-test'
146+
147+
└─ lastUpdateCheck
148+
- ├─ Description: Last timestamp we checked whether new Yarn versions were available
149+
- ├─ Source: WORKSPACE_ROOT/.yarnrc.yml
150+
- └─ Value: '1555784893958'
151+
",
152+
}
153+
`;
154+
80155
exports[`Commands config test (folder with rcfile and rc in home folder / without flags) 1`] = `
81156
{
82157
"code": 0,
@@ -99,6 +174,16 @@ exports[`Commands config test (folder with rcfile and rc in home folder / withou
99174
}
100175
`;
101176
177+
exports[`Commands config test (folder with rcfile and rc in parent / as json no defaults) 1`] = `
178+
{
179+
"code": 0,
180+
"stderr": "",
181+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
182+
{"key":"lastUpdateCheck","effective":"1555784893958","source":"WORKSPACE_ROOT/subfolder/.yarnrc.yml","description":"Last timestamp we checked whether new Yarn versions were available","type":"STRING","default":null}
183+
",
184+
}
185+
`;
186+
102187
exports[`Commands config test (folder with rcfile and rc in parent / as json) 1`] = `
103188
{
104189
"code": 0,
@@ -110,6 +195,23 @@ exports[`Commands config test (folder with rcfile and rc in parent / as json) 1`
110195
}
111196
`;
112197
198+
exports[`Commands config test (folder with rcfile and rc in parent / no defaults) 1`] = `
199+
{
200+
"code": 0,
201+
"stderr": "",
202+
"stdout": "├─ initScope
203+
- ├─ Description: Scope used when creating packages via the init command
204+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
205+
- └─ Value: 'my-test'
206+
207+
└─ lastUpdateCheck
208+
- ├─ Description: Last timestamp we checked whether new Yarn versions were available
209+
- ├─ Source: WORKSPACE_ROOT/subfolder/.yarnrc.yml
210+
- └─ Value: '1555784893958'
211+
",
212+
}
213+
`;
214+
113215
exports[`Commands config test (folder with rcfile and rc in parent / without flags) 1`] = `
114216
{
115217
"code": 0,
@@ -132,6 +234,15 @@ exports[`Commands config test (folder with rcfile and rc in parent / without fla
132234
}
133235
`;
134236
237+
exports[`Commands config test (folder with rcfile without trailing newline / as json no defaults) 1`] = `
238+
{
239+
"code": 0,
240+
"stderr": "",
241+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
242+
",
243+
}
244+
`;
245+
135246
exports[`Commands config test (folder with rcfile without trailing newline / as json) 1`] = `
136247
{
137248
"code": 0,
@@ -143,6 +254,18 @@ exports[`Commands config test (folder with rcfile without trailing newline / as
143254
}
144255
`;
145256
257+
exports[`Commands config test (folder with rcfile without trailing newline / no defaults) 1`] = `
258+
{
259+
"code": 0,
260+
"stderr": "",
261+
"stdout": "└─ initScope
262+
- ├─ Description: Scope used when creating packages via the init command
263+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
264+
- └─ Value: 'my-test'
265+
",
266+
}
267+
`;
268+
146269
exports[`Commands config test (folder with rcfile without trailing newline / without flags) 1`] = `
147270
{
148271
"code": 0,
@@ -165,6 +288,14 @@ exports[`Commands config test (folder with rcfile without trailing newline / wit
165288
}
166289
`;
167290
291+
exports[`Commands config test (folder without rcfile in ancestry / as json no defaults) 1`] = `
292+
{
293+
"code": 0,
294+
"stderr": "",
295+
"stdout": "",
296+
}
297+
`;
298+
168299
exports[`Commands config test (folder without rcfile in ancestry / as json) 1`] = `
169300
{
170301
"code": 0,
@@ -176,6 +307,14 @@ exports[`Commands config test (folder without rcfile in ancestry / as json) 1`]
176307
}
177308
`;
178309
310+
exports[`Commands config test (folder without rcfile in ancestry / no defaults) 1`] = `
311+
{
312+
"code": 0,
313+
"stderr": "",
314+
"stdout": "",
315+
}
316+
`;
317+
179318
exports[`Commands config test (folder without rcfile in ancestry / without flags) 1`] = `
180319
{
181320
"code": 0,

packages/acceptance-tests/pkg-tests-specs/sources/commands/config.test.ts

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
22

33
const RC_FILENAME = `.yarnrc.yml`;
44
const SUBFOLDER = `subfolder`;
5-
const FAKE_REGISTRY_URL = `http://yarn.test.registry`;
65
const FAKE_LOCAL_APP_DATA = `LOCAL_APP_DATA`;
76
const FAKE_WORKSPACE_ROOT = `WORKSPACE_ROOT`;
87
const FAKE_HOME = `HOME`;
@@ -40,7 +39,7 @@ const environments: Record<string, (opts: {
4039
},
4140
};
4241

43-
function cleanupPlainOutput(output: string, path: PortablePath, homePath: PortablePath) {
42+
function cleanupOutput(output: string, path: PortablePath, homePath: PortablePath) {
4443
// Replace multiple consecutive spaces with one space.
4544
// The output of the config command is aligned according to the longest value, which probably
4645
// contains `path`. In other words, the formatting depends on the length of `path`.
@@ -65,51 +64,17 @@ function cleanupPlainOutput(output: string, path: PortablePath, homePath: Portab
6564
return output;
6665
}
6766

68-
function cleanupJsonOutput(output: string, path: PortablePath, homePath: PortablePath) {
69-
let outputObject;
70-
try {
71-
outputObject = JSON.parse(output);
72-
} catch (e) {
73-
return cleanupPlainOutput(output, path, homePath);
74-
}
75-
76-
// the default globalFolder contains the user's home folder, override that value
77-
outputObject.globalFolder.default = `DEFAULT_GLOBAL_FOLDER`;
78-
79-
// replace the generated registry server URL with a constant
80-
outputObject.npmRegistryServer.effective = FAKE_REGISTRY_URL;
81-
82-
const pathN = npath.fromPortablePath(path);
83-
const homePathN = npath.fromPortablePath(homePath);
84-
85-
const cleanPath = (input: string) => input
86-
.replaceAll(pathN, FAKE_WORKSPACE_ROOT)
87-
.replaceAll(homePathN, FAKE_HOME);
88-
89-
for (const setting of Object.values<any>(outputObject)) {
90-
if (typeof setting.source === `string`)
91-
setting.source = cleanPath(setting.source);
92-
93-
if (typeof setting.default === `string`)
94-
setting.default = cleanPath(setting.default);
95-
96-
if (typeof setting.effective === `string`) {
97-
setting.effective = cleanPath(setting.effective);
98-
}
99-
}
100-
101-
return JSON.stringify(outputObject);
102-
}
103-
10467
const options = {
105-
[`without flags`]: {cleanupStdout: cleanupPlainOutput, flags: []},
106-
[`as json`]: {cleanupStdout: cleanupJsonOutput, flags: [`--json`]},
68+
[`without flags`]: {flags: []},
69+
[`as json`]: {flags: [`--json`]},
70+
[`no defaults`]: {flags: [`--no-defaults`]},
71+
[`as json no defaults`]: {flags: [`--json`, `--no-defaults`]},
10772
};
10873

10974
describe(`Commands`, () => {
11075
describe(`config`, () => {
11176
for (const [environmentDescription, environment] of Object.entries(environments)) {
112-
for (const [optionDescription, {flags, cleanupStdout}] of Object.entries(options)) {
77+
for (const [optionDescription, {flags}] of Object.entries(options)) {
11378
test(`test (${environmentDescription} / ${optionDescription})`, makeTemporaryEnv({}, async ({path, run, source}) => {
11479
const cwd = ppath.join(path, `${SUBFOLDER}/${SUBFOLDER}`);
11580
const homePath = await xfs.mktempPromise();
@@ -127,8 +92,8 @@ describe(`Commands`, () => {
12792
({code, stdout, stderr} = error);
12893
}
12994

130-
stdout = cleanupStdout(stdout, path, homePath);
131-
stderr = cleanupPlainOutput(stderr, path, homePath);
95+
stdout = cleanupOutput(stdout, path, homePath);
96+
stderr = cleanupOutput(stderr, path, homePath);
13297

13398
expect({code, stdout, stderr}).toMatchSnapshot();
13499
}));

0 commit comments

Comments
 (0)