From 85296b2b4b39c5e156656950914a994107b91b19 Mon Sep 17 00:00:00 2001 From: Simon Vocella Date: Mon, 1 May 2017 18:18:28 +0200 Subject: [PATCH] add some tests for link/unlink command (#3293) * add tests for link command * add tests for unlink command * fix some lint issues --- __tests__/commands/_helpers.js | 2 +- __tests__/commands/link.js | 39 +++++++++++++ __tests__/commands/unlink.js | 55 +++++++++++++++++++ .../link/package-no-name/package.json | 3 + .../link/package-with-name/bar/index.js | 1 + .../link/package-with-name/bar/package.json | 5 ++ .../link/package-with-name/package.json | 6 ++ .../fixtures/link/package-with-name/yarn.lock | 4 ++ 8 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 __tests__/commands/link.js create mode 100644 __tests__/commands/unlink.js create mode 100644 __tests__/fixtures/link/package-no-name/package.json create mode 100644 __tests__/fixtures/link/package-with-name/bar/index.js create mode 100644 __tests__/fixtures/link/package-with-name/bar/package.json create mode 100644 __tests__/fixtures/link/package-with-name/package.json create mode 100644 __tests__/fixtures/link/package-with-name/yarn.lock diff --git a/__tests__/commands/_helpers.js b/__tests__/commands/_helpers.js index 68987908e1..1a1caacb50 100644 --- a/__tests__/commands/_helpers.js +++ b/__tests__/commands/_helpers.js @@ -124,7 +124,7 @@ export async function run( 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); diff --git a/__tests__/commands/link.js b/__tests__/commands/link.js new file mode 100644 index 0000000000..59fd7db353 --- /dev/null +++ b/__tests__/commands/link.js @@ -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 => { + const linkFolder = await mkdir('link-folder'); + await runLink([], {linkFolder}, 'package-with-name', async (config, reporter): Promise => { + 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 => { + 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')); + } +}); diff --git a/__tests__/commands/unlink.js b/__tests__/commands/unlink.js new file mode 100644 index 0000000000..e54913f0c8 --- /dev/null +++ b/__tests__/commands/unlink.js @@ -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 => { + const linkFolder = await mkdir('link-folder'); + + await runLink([], {linkFolder}, 'package-with-name', async (config, reporter): Promise => { + const existed = await fs.exists(path.join(linkFolder, 'a-package')); + expect(existed).toEqual(true); + }); + + await runUnlink([], {linkFolder}, 'package-with-name', async (config, reporter): Promise => { + 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 => { + 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')); + } +}); diff --git a/__tests__/fixtures/link/package-no-name/package.json b/__tests__/fixtures/link/package-no-name/package.json new file mode 100644 index 0000000000..18a1e415e5 --- /dev/null +++ b/__tests__/fixtures/link/package-no-name/package.json @@ -0,0 +1,3 @@ +{ + "dependencies": {} +} diff --git a/__tests__/fixtures/link/package-with-name/bar/index.js b/__tests__/fixtures/link/package-with-name/bar/index.js new file mode 100644 index 0000000000..323fae03f4 --- /dev/null +++ b/__tests__/fixtures/link/package-with-name/bar/index.js @@ -0,0 +1 @@ +foobar diff --git a/__tests__/fixtures/link/package-with-name/bar/package.json b/__tests__/fixtures/link/package-with-name/bar/package.json new file mode 100644 index 0000000000..f92edb96b8 --- /dev/null +++ b/__tests__/fixtures/link/package-with-name/bar/package.json @@ -0,0 +1,5 @@ +{ + "name": "bar", + "version": "0.0.0", + "main": "index.js" +} diff --git a/__tests__/fixtures/link/package-with-name/package.json b/__tests__/fixtures/link/package-with-name/package.json new file mode 100644 index 0000000000..4b79f3ffd9 --- /dev/null +++ b/__tests__/fixtures/link/package-with-name/package.json @@ -0,0 +1,6 @@ +{ + "name": "a-package", + "dependencies": { + "foo": "file:bar" + } +} diff --git a/__tests__/fixtures/link/package-with-name/yarn.lock b/__tests__/fixtures/link/package-with-name/yarn.lock new file mode 100644 index 0000000000..83dbd093dd --- /dev/null +++ b/__tests__/fixtures/link/package-with-name/yarn.lock @@ -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"