Skip to content
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

Add tests for global command #3238

Merged
merged 7 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '[email protected]'], 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('[email protected]');
});
});
});
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