File: integration.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (537 lines) | stat: -rw-r--r-- 17,623 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* @flow */
/* eslint max-len: 0 */

import http from 'http';
import {existsSync} from 'fs';

import invariant from 'invariant';
import execa from 'execa';
import {sh} from 'puka';
import makeTemp from './_temp.js';
import * as fs from '../src/util/fs.js';
import * as constants from '../src/constants.js';
import {explodeLockfile} from './commands/_helpers.js';
import en from '../src/reporters/lang/en.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;

const path = require('path');

if (!existsSync(path.resolve(__dirname, '../lib'))) {
  throw new Error('These tests require `yarn build` to have been run first.');
}

function addTest(pattern, {strictPeers} = {strictPeers: false}, yarnArgs: Array<string> = []) {
  test.concurrent(`yarn add ${pattern}`, async () => {
    const cwd = await makeTemp();
    const cacheFolder = path.join(cwd, 'cache');

    const command = path.resolve(__dirname, '../bin/yarn');
    const args = ['--cache-folder', cacheFolder, ...yarnArgs];

    const options = {cwd};

    await fs.writeFile(
      path.join(cwd, 'package.json'),
      JSON.stringify({
        name: 'test',
        license: 'MIT',
      }),
    );

    const result = await execa(command, ['add', pattern].concat(args), options);
    if (strictPeers) {
      expect(result.stderr).not.toMatch(/^warning .+ peer dependency/gm);
    }

    await fs.unlink(cwd);
  });
}

// TODO:
// addTest('gitlab:leanlabsio/kanban'); // gitlab
// addTest(
//   '@foo/bar@1.2.3',
//   async cacheFolder => {
//     const folder = path.join(cacheFolder, 'v1', 'npm-@foo', 'bar');
//     await fs.mkdirp(folder);
//     await fs.writeFile(
//       path.join(folder, constants.METADATA_FILENAME),
//       '{"remote": {"hash": "cafebabecafebabecafebabecafebabecafebabe"}}',
//     );
//     await fs.writeFile(path.join(foldresolve gitlab:leanlabsio/kanbaner, 'package.json'), '{"name": "@foo/bar", "version": "1.2.3"}');
//   },
//   true,
// ); // offline npm scoped package

addTest('scrollin'); // npm
addTest('https://git@github.com/stevemao/left-pad.git'); // git url, with username
addTest('https://github.com/bestander/chrome-app-livereload.git'); // no package.json
addTest('bestander/chrome-app-livereload'); // no package.json, github, tarball

if (process.platform !== 'win32') {
  addTest('https://github.com/yarnpkg/yarn/releases/download/v0.18.1/yarn-v0.18.1.tar.gz'); // tarball
  addTest('react-scripts@1.0.13', {strictPeers: true}, ['--no-node-version-check', '--ignore-engines']); // many peer dependencies, there shouldn't be any peerDep warnings
}

const MIN_PORT_NUM = 56000;
const MAX_PORT_NUM = 65535;
const PORT_RANGE = MAX_PORT_NUM - MIN_PORT_NUM;

const getRandomPort = () => Math.floor(Math.random() * PORT_RANGE) + MIN_PORT_NUM;

async function runYarn(args: Array<string> = [], options: Object = {}): Promise<Array<Buffer>> {
  if (!options['env']) {
    options['env'] = {...process.env};
    options['extendEnv'] = false;
  }
  options['env']['FORCE_COLOR'] = 0;
  const {stdout, stderr} = await execa.shell(sh`${path.resolve(__dirname, '../bin/yarn')} ${args}`, options);

  return [stdout, stderr];
}

describe('production', () => {
  test('it should be true when NODE_ENV=production', async () => {
    const cwd = await makeTemp();
    const options = {cwd, env: {YARN_SILENT: 1, NODE_ENV: 'production'}};

    const [stdoutOutput, _] = await runYarn(['config', 'current'], options);

    expect(JSON.parse(stdoutOutput.toString())).toHaveProperty('production', true);
  });

  test('it should default to false', async () => {
    const cwd = await makeTemp();
    const options = {cwd, env: {YARN_SILENT: 1, NODE_ENV: ''}};

    const [stdoutOutput, _] = await runYarn(['config', 'current'], options);

    expect(JSON.parse(stdoutOutput.toString())).toHaveProperty('production', false);
  });

  test('it should prefer CLI over NODE_ENV', async () => {
    const cwd = await makeTemp();
    const options = {cwd, env: {YARN_SILENT: 1, NODE_ENV: 'production'}};

    const [stdoutOutput, _] = await runYarn(['--prod', 'false', 'config', 'current'], options);

    expect(JSON.parse(stdoutOutput.toString())).toHaveProperty('production', false);
  });

  test('it should prefer YARN_PRODUCTION over NODE_ENV', async () => {
    const cwd = await makeTemp();
    const options = {cwd, env: {YARN_SILENT: 1, YARN_PRODUCTION: 'false', NODE_ENV: 'production'}};

    const [stdoutOutput, _] = await runYarn(['config', 'current'], options);

    expect(JSON.parse(stdoutOutput.toString())).toHaveProperty('production', false);
  });

  test('it should prefer CLI over YARN_PRODUCTION', async () => {
    const cwd = await makeTemp();
    const options = {cwd, env: {YARN_SILENT: 1, YARN_PRODUCTION: 'false', NODE_ENV: 'production'}};

    const [stdoutOutput, _] = await runYarn(['--prod', '1', 'config', 'current'], options);

    expect(JSON.parse(stdoutOutput.toString())).toHaveProperty('production', true);
  });
});

test('--mutex network', async () => {
  const cwd = await makeTemp();

  const port = getRandomPort();
  await fs.writeFile(path.join(cwd, '.yarnrc'), `--mutex "network:${port}"\n`);

  const promises = [];

  for (let t = 0; t < 40; ++t) {
    const subCwd = path.join(cwd, String(t));

    await fs.mkdirp(subCwd);
    await fs.writeFile(
      path.join(subCwd, 'package.json'),
      JSON.stringify({
        scripts: {test: 'node -e "setTimeout(function(){}, process.argv[1])"'},
      }),
    );

    promises.push(runYarn(['run', 'test', '100'], {cwd: subCwd}));
  }

  await Promise.all(promises);
});

test('--mutex network with busy port', async () => {
  const port = getRandomPort();

  const server = http.createServer((request, response) => {
    response.writeHead(200);
    response.end("I'm a broken JSON string to crash Yarn network mutex.");
  });
  server.listen({
    port,
    host: 'localhost',
  });

  const cwd = await makeTemp();
  await fs.writeFile(
    path.join(cwd, 'package.json'),
    JSON.stringify({
      scripts: {test: 'node -e "setTimeout(function(){}, process.argv[1])"'},
    }),
  );

  let mutexError;
  try {
    await runYarn(['--mutex', `network:${port}`, 'run', 'test', '100'], {cwd});
  } catch (error) {
    mutexError = error;
  } finally {
    server.close();
  }

  expect(mutexError).toBeDefined();
  invariant(mutexError != null, 'mutexError should be defined at this point otherwise Jest will throw above');
  expect(mutexError.message).toMatch(new RegExp(en.mutexPortBusy.replace(/\$\d/g, '\\d+')));
});

describe('--registry option', () => {
  test('--registry option with npm registry', async () => {
    const cwd = await makeTemp();

    const registry = 'https://registry.npmjs.org';
    const packageJsonPath = path.join(cwd, 'package.json');
    await fs.writeFile(packageJsonPath, JSON.stringify({}));

    await runYarn(['add', 'left-pad', '--registry', registry], {cwd});

    const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
    const lockfile = explodeLockfile(await fs.readFile(path.join(cwd, 'yarn.lock')));

    expect(packageJson.dependencies['left-pad']).toBeDefined();
    expect(lockfile).toHaveLength(4);
    expect(lockfile[2]).toContain(registry);
  });

  test('--registry option with yarn registry', async () => {
    const cwd = await makeTemp();

    const registry = 'https://registry.yarnpkg.com';
    const packageJsonPath = path.join(cwd, 'package.json');
    await fs.writeFile(packageJsonPath, JSON.stringify({}));

    await runYarn(['add', 'is-array', '--registry', registry], {cwd});

    const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
    const lockfile = explodeLockfile(await fs.readFile(path.join(cwd, 'yarn.lock')));

    expect(packageJson.dependencies['is-array']).toBeDefined();
    expect(lockfile).toHaveLength(4);
    expect(lockfile[2]).toContain(registry);
  });

  test('--registry option with non-exiting registry and show an error', async () => {
    const cwd = await makeTemp();
    const registry = 'https://example-registry-doesnt-exist.com';

    try {
      await runYarn(['add', 'is-array', '--registry', registry], {cwd});
    } catch (err) {
      const stdoutOutput = err.message;
      expect(stdoutOutput.toString()).toMatch(/getaddrinfo ENOTFOUND example-registry-doesnt-exist\.com/g);
    }
  });

  test('registry option from yarnrc', async () => {
    const cwd = await makeTemp();

    const registry = 'https://registry.npmjs.org';
    await fs.writeFile(`${cwd}/.yarnrc`, 'registry "' + registry + '"\n');

    const packageJsonPath = path.join(cwd, 'package.json');
    await fs.writeFile(packageJsonPath, JSON.stringify({}));

    await runYarn(['add', 'left-pad'], {cwd});

    const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
    const lockfile = explodeLockfile(await fs.readFile(path.join(cwd, 'yarn.lock')));

    expect(packageJson.dependencies['left-pad']).toBeDefined();
    expect(lockfile).toHaveLength(4);
    expect(lockfile[2]).toContain(registry);
  });
});

test('--cwd option', async () => {
  const cwd = await makeTemp();

  const subdir = path.join(cwd, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
  await fs.mkdirp(subdir);

  const packageJsonPath = path.join(cwd, 'package.json');
  await fs.writeFile(packageJsonPath, JSON.stringify({}));

  await runYarn(['add', 'left-pad'], {cwd: subdir});

  const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
  expect(packageJson.dependencies['left-pad']).toBeDefined();
});

const customCacheCwd = `${__dirname}/fixtures/cache/custom-location`;

test('default rc', async (): Promise<void> => {
  const [stdoutOutput] = await runYarn(['cache', 'dir'], {cwd: customCacheCwd});

  expect(stdoutOutput).toMatch(/uses-default-yarnrc/);
});

test('--no-default-rc', async (): Promise<void> => {
  const [stdoutOutput] = await runYarn(['cache', 'dir', '--no-default-rc'], {cwd: customCacheCwd});

  expect(stdoutOutput).not.toMatch(/uses-default-yarnrc/);
});

test('--use-yarnrc', async (): Promise<void> => {
  const [stdoutOutput] = await runYarn(['cache', 'dir', '--use-yarnrc', './custom-yarnrc'], {cwd: customCacheCwd});

  expect(stdoutOutput).toMatch(/uses-custom-yarnrc/);
});

test('yarnrc arguments', async () => {
  const cwd = await makeTemp();

  await fs.writeFile(
    `${cwd}/.yarnrc`,
    ['--emoji false', '--json true', '--add.exact true', '--no-progress true', '--cache-folder "./yarn-cache"'].join(
      '\n',
    ),
  );
  await fs.writeFile(`${cwd}/package.json`, JSON.stringify({name: 'test', license: 'ISC', version: '1.0.0'}));

  const [stdoutOutput] = await runYarn(['add', 'left-pad@1.1.3'], {cwd});
  expect(stdoutOutput).toMatchSnapshot('yarnrc-args');
  expect(JSON.parse(await fs.readFile(`${cwd}/package.json`)).dependencies['left-pad']).toMatch(/^\d+\./);
  expect((await fs.stat(`${cwd}/yarn-cache`)).isDirectory()).toBe(true);
});

describe('yarnrc path', () => {
  test('js file', async () => {
    const cwd = await makeTemp();

    await fs.writeFile(`${cwd}/.yarnrc`, 'yarn-path "./override.js"\n');
    await fs.writeFile(`${cwd}/override.js`, 'console.log("override called")\n');

    const [stdoutOutput] = await runYarn([], {cwd});
    expect(stdoutOutput.toString().trim()).toEqual('override called');
  });

  test('executable file', async () => {
    const cwd = await makeTemp();

    if (process.platform === 'win32') {
      await fs.writeFile(`${cwd}/.yarnrc`, 'yarn-path "./override.cmd"\n');
      await fs.writeFile(`${cwd}/override.cmd`, '@echo override called\n');
    } else {
      await fs.writeFile(`${cwd}/.yarnrc`, 'yarn-path "./override"\n');
      await fs.writeFile(`${cwd}/override`, '#!/usr/bin/env sh\necho override called\n');
      await fs.chmod(`${cwd}/override`, 0o755);
    }

    const [stdoutOutput] = await runYarn([], {cwd});
    expect(stdoutOutput.toString().trim()).toEqual('override called');
  });

  test('js file exit code', async () => {
    const cwd = await makeTemp();

    await fs.writeFile(`${cwd}/.yarnrc`, 'yarn-path "./override.js"\n');
    await fs.writeFile(`${cwd}/override.js`, 'process.exit(123);');

    let error = false;
    try {
      await runYarn([], {cwd});
    } catch (err) {
      error = err.code;
    }

    expect(error).toEqual(123);
  });

  test('sh file exit code', async () => {
    const cwd = await makeTemp();

    if (process.platform !== 'win32') {
      await fs.writeFile(`${cwd}/.yarnrc`, 'yarn-path "./override.sh"\n');
      await fs.writeFile(`${cwd}/override.sh`, '#!/usr/bin/env sh\n\nexit 123\n');

      await fs.chmod(`${cwd}/override.sh`, 0o755);
    } else {
      await fs.writeFile(`${cwd}/.yarnrc`, 'yarn-path "./override.cmd"\r\n');
      await fs.writeFile(`${cwd}/override.cmd`, 'exit /b 123\r\n');
    }

    let error = false;
    try {
      await runYarn([], {cwd});
    } catch (err) {
      error = err.code;
    }

    expect(error).toEqual(123);
  });
});

for (const withDoubleDash of [false, true]) {
  test(`yarn run <script> ${withDoubleDash ? '-- ' : ''}--opt`, async () => {
    const cwd = await makeTemp();

    await fs.writeFile(
      path.join(cwd, 'package.json'),
      JSON.stringify({
        scripts: {echo: `echo`},
      }),
    );

    const options = {cwd, env: {YARN_SILENT: 1}};

    const [stdoutOutput, stderrOutput] = await runYarn(
      ['run', 'echo', ...(withDoubleDash ? ['--'] : []), '--opt'],
      options,
    );

    expect(stdoutOutput.toString().trim()).toEqual('--opt');
    (exp => (withDoubleDash ? exp : exp.not))(expect(stderrOutput.toString())).toMatch(
      /From Yarn 1\.0 onwards, scripts don't require "--" for options to be forwarded/,
    );
  });
}

test('yarn run <script> <strings that need escaping>', async () => {
  const cwd = await makeTemp();

  await fs.writeFile(
    path.join(cwd, 'package.json'),
    JSON.stringify({
      scripts: {stringify: `node -p "JSON.stringify(process.argv.slice(1))"`},
    }),
  );

  const options = {cwd, env: {YARN_SILENT: 1}};

  const trickyStrings = ['$PWD', '%CD%', '^', '!', '\\', '>', '<', '|', '&', "'", '"', '`', '  ', '(', ')'];
  const [stdout] = await runYarn(['stringify', ...trickyStrings], options);

  expect(stdout.toString().trim()).toEqual(JSON.stringify(trickyStrings));
});

test('yarn run <failing script>', async () => {
  const cwd = await makeTemp();

  await fs.writeFile(
    path.join(cwd, 'package.json'),
    JSON.stringify({
      license: 'MIT',
      scripts: {false: 'exit 1'},
    }),
  );

  let stderr = null;
  let err = null;
  try {
    await runYarn(['run', 'false'], {cwd});
  } catch (e) {
    stderr = e.stderr.trim();
    err = e.code;
  }

  expect(err).toEqual(1);
  expect(stderr).toEqual('error Command failed with exit code 1.');
});

test('yarn run in path need escaping', async () => {
  const cwd = await makeTemp('special (chars)');

  await fs.writeFile(path.join(cwd, 'package.json'), '{}');
  const binDir = path.join(cwd, 'node_modules', '.bin');
  await fs.mkdirp(binDir);
  const executablePath = path.join(binDir, 'yolo');
  await fs.writeFile(executablePath, 'echo yolo');
  await fs.chmod(executablePath, 0o755);
  // For Windows
  await fs.writeFile(`${executablePath}.cmd`, '@ECHO off\necho yolo');

  const options = {cwd, env: {YARN_SILENT: 1}};

  const [stdout] = await runYarn(['yolo'], options);

  expect(stdout.toString().trim()).toEqual('yolo');
});

test('cache folder fallback', async () => {
  const cwd = await makeTemp();
  const cacheFolder = path.join(cwd, '.cache');

  const args = ['--preferred-cache-folder', cacheFolder];
  const options = {cwd};

  const runCacheDir = () => {
    return runYarn(['cache', 'dir'].concat(args), options);
  };

  const [stdoutOutput, stderrOutput] = await runCacheDir();

  expect(stdoutOutput.toString().trim()).toEqual(path.join(cacheFolder, `v${constants.CACHE_VERSION}`));
  expect(stderrOutput.toString()).not.toMatch(/Skipping preferred cache folder/);

  await fs.unlink(cacheFolder);
  await fs.writeFile(cacheFolder, `not a directory`);

  const [stdoutOutput2, stderrOutput2] = await runCacheDir();

  expect(stdoutOutput2.toString().trim()).toEqual(
    path.join(constants.PREFERRED_MODULE_CACHE_DIRECTORIES[0], `v${constants.CACHE_VERSION}`),
  );
  expect(stderrOutput2.toString()).toMatch(/Skipping preferred cache folder/);
});

test('relative cache folder', async () => {
  const base = await makeTemp();

  await fs.writeFile(`${base}/.yarnrc`, 'cache-folder "./foo"\n');

  await fs.mkdirp(`${base}/sub`);
  await fs.mkdirp(`${base}/foo`);

  const [stdoutOutput, _] = await runYarn(['cache', 'dir'], {cwd: `${base}/sub`});

  // The dirname is to remove the "v2" part
  expect(await fs.realpath(path.dirname(stdoutOutput.toString()))).toEqual(await fs.realpath(`${base}/foo`));
});

test('yarn create', async () => {
  const cwd = await makeTemp();
  const options = {cwd, env: {YARN_SILENT: 1}};

  const [stdoutOutput, _] = await runYarn(['create', 'html'], options);

  expect(stdoutOutput.toString()).toMatch(/<!doctype html>/);
});

test('yarn init -y', async () => {
  const cwd = await makeTemp();
  const innerDir = path.join(cwd, 'inner');
  const initialManifestFile = JSON.stringify({name: 'test', license: 'ISC', version: '1.0.0'});

  await fs.writeFile(`${cwd}/package.json`, initialManifestFile);
  await fs.mkdirp(innerDir);

  const options = {cwd: innerDir};
  await runYarn(['init', '-y'], options);

  expect(await fs.exists(path.join(innerDir, 'package.json'))).toEqual(true);

  const manifestFile = await fs.readFile(path.join(cwd, 'package.json'));
  expect(manifestFile).toEqual(initialManifestFile);
});