Skip to content

Commit

Permalink
Add tests for global command (#3238)
Browse files Browse the repository at this point in the history
* add test for global bin command

* add test for global add command

* add test for global remove

* add test for global ls command and fix issue #3142

* add test for global upgrade

* move tests with side-effects under isCI branch; the other tests now are side-effects free

* the global folder in win32 platform is prefix env/parameter without bin folder inside, do assert only on global folders for yarn global v0.23.3
Done in 0.26s.
  • Loading branch information
voxsim authored and arcanis committed May 2, 2017
1 parent 5f28a90 commit 4731641
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 19 deletions.
3 changes: 2 additions & 1 deletion __tests__/commands/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ export async function run<T, R>(
const config = await Config.create({
binLinks: !!flags.binLinks,
cwd,
globalFolder: path.join(cwd, '.yarn-global'),
globalFolder: flags.globalFolder || path.join(cwd, '.yarn-global'),
cacheFolder: flags.cacheFolder || path.join(cwd, '.yarn-cache'),
linkFolder: flags.linkFolder || path.join(cwd, '.yarn-link'),
prefix: flags.prefix,
production: flags.production,
}, reporter);

Expand Down
92 changes: 79 additions & 13 deletions __tests__/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ConsoleReporter} from '../../src/reporters/index.js';
import {run as buildRun} from './_helpers.js';
import {run as global} from '../../src/cli/commands/global.js';
import * as fs from '../../src/util/fs.js';
import mkdir from '../_temp.js';
const isCI = require('is-ci');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;
Expand Down Expand Up @@ -34,31 +35,96 @@ function getTempGlobalFolder(): string {
return path.join(os.tmpdir(), `yarn-global-${Math.random()}`);
}

// this test has global folder side effects, run it only in CI
async function createTempGlobalFolder(): Promise<string> {
return await mkdir('yarn-global');
}

async function createTempPrefixFolder(): Promise<string> {
const prefixFolder = await mkdir('yarn-prefix');
return path.join(prefixFolder, 'bin');
}

// these tests have global folder side or prefix folder effects, run it only in CI
if (isCI) {
test.concurrent('add without flag', (): Promise<void> => {
return runGlobal(['add', 'react-native-cli'], {}, 'add-without-flag', async (config) => {
expect(await fs.exists(path.join(config.globalFolder, 'node_modules', 'react-native-cli'))).toEqual(true);
expect(await fs.exists(path.join(config.globalFolder, 'node_modules', '.bin', 'react-native'))).toEqual(true);
});
});

test.concurrent('add with prefix flag', (): Promise<void> => {
const tmpGlobalFolder = getTempGlobalFolder();
return runGlobal(['add', 'react-native-cli'], {prefix: tmpGlobalFolder}, 'add-with-prefix-flag', async (config) => {
expect(await fs.exists(getGlobalPath(tmpGlobalFolder, 'react-native'))).toEqual(true);
});
});

// don't run this test in `concurrent`, it will affect other tests
test('add with PREFIX enviroment variable', (): Promise<void> => {
const tmpGlobalFolder = getTempGlobalFolder();
const envPrefix = process.env.PREFIX;
process.env.PREFIX = tmpGlobalFolder;
return runGlobal(['add', 'react-native-cli'], {}, 'add-with-prefix-env', async (config) => {
expect(await fs.exists(getGlobalPath(tmpGlobalFolder, 'react-native'))).toEqual(true);
// restore env
process.env.PREFIX = envPrefix;
});
});
}

test.concurrent('add with prefix flag', (): Promise<void> => {
test.concurrent('bin', (): Promise<void> => {
const tmpGlobalFolder = getTempGlobalFolder();
return runGlobal(['add', 'react-native-cli'], {prefix: tmpGlobalFolder}, 'add-with-prefix-flag', async (config) => {
expect(await fs.exists(getGlobalPath(tmpGlobalFolder, 'react-native'))).toEqual(true);
return runGlobal(['bin'], {prefix: tmpGlobalFolder}, 'add-with-prefix-flag',
(config, reporter, install, getStdout) => {
expect(getStdout()).toContain(tmpGlobalFolder);
});
});

// don't run this test in `concurrent`, it will affect other tests
test('add with PREFIX enviroment variable', (): Promise<void> => {
const tmpGlobalFolder = getTempGlobalFolder();
const envPrefix = process.env.PREFIX;
process.env.PREFIX = tmpGlobalFolder;
return runGlobal(['add', 'react-native-cli'], {}, 'add-with-prefix-env', async (config) => {
expect(await fs.exists(getGlobalPath(tmpGlobalFolder, 'react-native'))).toEqual(true);
// restore env
process.env.PREFIX = envPrefix;
test.concurrent('add', async (): Promise<void> => {
const tmpGlobalFolder = await createTempGlobalFolder();
const tmpPrefixFolder = await createTempPrefixFolder();
const flags = {globalFolder: tmpGlobalFolder, prefix: tmpPrefixFolder};
return runGlobal(['add', 'react-native-cli'], flags, 'add-with-prefix-flag',
async (config) => {
expect(await fs.exists(path.join(tmpGlobalFolder, 'node_modules', 'react-native-cli'))).toEqual(true);
});
});

test.concurrent('remove', async (): Promise<void> => {
const tmpGlobalFolder = await createTempGlobalFolder();
const tmpPrefixFolder = await createTempPrefixFolder();
const flags = {globalFolder: tmpGlobalFolder, prefix: tmpPrefixFolder};
return runGlobal(['add', 'react-native-cli'], flags, 'add-with-prefix-flag', () => {})
.then(() => {
return runGlobal(['remove', 'react-native-cli'], flags, 'add-with-prefix-flag', async (config) => {
expect(await fs.exists(path.join(tmpGlobalFolder, 'node_modules', 'react-native-cli'))).toEqual(false);
});
});
});

test.concurrent('ls', async (): Promise<void> => {
const tmpGlobalFolder = await createTempGlobalFolder();
const tmpPrefixFolder = await createTempPrefixFolder();
const flags = {globalFolder: tmpGlobalFolder, prefix: tmpPrefixFolder};
return runGlobal(['add', 'react-native-cli'], flags, 'add-with-prefix-flag', () => {})
.then(() => {
return runGlobal(['ls'], flags, 'add-with-prefix-flag', (config, reporter, install, getStdout) => {
expect(getStdout()).toContain('react-native-cli');
});
});
});

test.concurrent('upgrade', async (): Promise<void> => {
const tmpGlobalFolder = await createTempGlobalFolder();
const tmpPrefixFolder = await createTempPrefixFolder();
const flags = {globalFolder: tmpGlobalFolder, prefix: tmpPrefixFolder};
return runGlobal(['add', 'react-native-cli@2.0.0'], flags, 'add-with-prefix-flag', () => {})
.then(() => {
return runGlobal(['upgrade', 'react-native-cli'], flags, 'add-with-prefix-flag',
(config, reporter, install, getStdout) => {
expect(getStdout()).toContain('react-native-cli');
expect(getStdout()).not.toContain('react-native-cli@2.0.0');
});
});
});
Binary file not shown.
4 changes: 2 additions & 2 deletions src/cli/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const {run, setFlags: _setFlags} = buildSubCommands('global', {
flags: Object,
args: Array<string>,
) {
console.log(getBinFolder(config, flags));
reporter.log(getBinFolder(config, flags));
},

async ls(
Expand All @@ -214,7 +214,7 @@ const {run, setFlags: _setFlags} = buildSubCommands('global', {

// install so we get hard file paths
const lockfile = await Lockfile.fromDirectory(config.cwd);
const install = new Install({skipIntegrity: true}, config, new NoopReporter(), lockfile);
const install = new Install({skipIntegrityCheck: true}, config, new NoopReporter(), lockfile);
const patterns = await install.init();

// dump global modules
Expand Down
4 changes: 1 addition & 3 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ commander.option('--no-lockfile', "don't read or generate a lockfile");
commander.option('--pure-lockfile', "don't generate a lockfile");
commander.option('--frozen-lockfile', "don't generate a lockfile and fail if an update is needed");
commander.option('--link-duplicates', 'create hardlinks to the repeated modules in node_modules');
commander.option('--global-folder <path>', '');
commander.option('--global-folder <path>', 'specify a custom folder to store global packages');
commander.option(
'--modules-folder <path>',
'rather than installing modules into the node_modules folder relative to the cwd, output them here',
Expand Down Expand Up @@ -299,7 +299,6 @@ function writeErrorReport(log) : ?string {
return errorReportLoc;
}

//
config.init({
binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
Expand All @@ -319,7 +318,6 @@ config.init({
nonInteractive: commander.nonInteractive,
commandName: commandName === 'run' ? commander.args[0] : commandName,
}).then(() => {

// option "no-progress" stored in yarn config
const noProgressConfig = config.registries.yarn.getOption('no-progress');

Expand Down

0 comments on commit 4731641

Please sign in to comment.