Skip to content

Commit

Permalink
add some tests for link/unlink command (#3293)
Browse files Browse the repository at this point in the history
* add tests for link command

* add tests for unlink command

* fix some lint issues
  • Loading branch information
voxsim authored and bestander committed May 1, 2017
1 parent 2bbdacc commit 85296b2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion __tests__/commands/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function run<T, R>(
cwd,
globalFolder: path.join(cwd, '.yarn-global'),
cacheFolder: flags.cacheFolder || path.join(cwd, '.yarn-cache'),
linkFolder: path.join(cwd, '.yarn-link'),
linkFolder: flags.linkFolder || path.join(cwd, '.yarn-link'),
production: flags.production,
}, reporter);

Expand Down
39 changes: 39 additions & 0 deletions __tests__/commands/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* @flow */

import {run as buildRun} from './_helpers.js';
import {run as link} from '../../src/cli/commands/link.js';
import {ConsoleReporter} from '../../src/reporters/index.js';
import type {CLIFunctionReturn} from '../../src/types.js';
import mkdir from './../_temp.js';
import * as fs from '../../src/util/fs.js';

const path = require('path');

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'link');
const runLink = buildRun.bind(
null,
ConsoleReporter,
fixturesLoc,
(args, flags, config, reporter): CLIFunctionReturn => {
return link(config, reporter, flags, args);
},
);

test.concurrent('creates folder in linkFolder', async (): Promise<void> => {
const linkFolder = await mkdir('link-folder');
await runLink([], {linkFolder}, 'package-with-name', async (config, reporter): Promise<void> => {
const existed = await fs.exists(path.join(linkFolder, 'a-package'));
expect(existed).toEqual(true);
});
});

test.concurrent('throws error if package.json does not have name', async (): Promise<void> => {
const linkFolder = await mkdir('link-folder');
const reporter = new ConsoleReporter({});

try {
await runLink([], {linkFolder}, 'package-no-name', () => {});
} catch (err) {
expect(err.message).toContain(reporter.lang('unknownPackageName'));
}
});
55 changes: 55 additions & 0 deletions __tests__/commands/unlink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* @flow */

import {run as buildRun} from './_helpers.js';
import {run as link} from '../../src/cli/commands/link.js';
import {run as unlink} from '../../src/cli/commands/unlink.js';
import {ConsoleReporter} from '../../src/reporters/index.js';
import type {CLIFunctionReturn} from '../../src/types.js';
import mkdir from './../_temp.js';
import * as fs from '../../src/util/fs.js';

const path = require('path');

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'link');
const runLink = buildRun.bind(
null,
ConsoleReporter,
fixturesLoc,
(args, flags, config, reporter): CLIFunctionReturn => {
return link(config, reporter, flags, args);
},
);

const runUnlink = buildRun.bind(
null,
ConsoleReporter,
fixturesLoc,
(args, flags, config, reporter): CLIFunctionReturn => {
return unlink(config, reporter, flags, args);
},
);

test.concurrent('creates folder in linkFolder', async (): Promise<void> => {
const linkFolder = await mkdir('link-folder');

await runLink([], {linkFolder}, 'package-with-name', async (config, reporter): Promise<void> => {
const existed = await fs.exists(path.join(linkFolder, 'a-package'));
expect(existed).toEqual(true);
});

await runUnlink([], {linkFolder}, 'package-with-name', async (config, reporter): Promise<void> => {
const existed = await fs.exists(path.join(linkFolder, 'a-package'));
expect(existed).toEqual(false);
});
});

test.concurrent('throws error if package.json does not have name', async (): Promise<void> => {
const linkFolder = await mkdir('link-folder');
const reporter = new ConsoleReporter({});

try {
await runUnlink([], {linkFolder}, 'package-no-name', () => {});
} catch (err) {
expect(err.message).toContain(reporter.lang('unknownPackageName'));
}
});
3 changes: 3 additions & 0 deletions __tests__/fixtures/link/package-no-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": {}
}
1 change: 1 addition & 0 deletions __tests__/fixtures/link/package-with-name/bar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
5 changes: 5 additions & 0 deletions __tests__/fixtures/link/package-with-name/bar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "bar",
"version": "0.0.0",
"main": "index.js"
}
6 changes: 6 additions & 0 deletions __tests__/fixtures/link/package-with-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "a-package",
"dependencies": {
"foo": "file:bar"
}
}
4 changes: 4 additions & 0 deletions __tests__/fixtures/link/package-with-name/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"foo@file:bar":
version "0.0.0"

0 comments on commit 85296b2

Please sign in to comment.