From 1f801273d731af1a73324e7b19723a1eaac52205 Mon Sep 17 00:00:00 2001 From: Simon Vocella Date: Wed, 8 Mar 2017 17:18:12 +0100 Subject: [PATCH] add tests for cache commands --- __tests__/commands/cache.js | 65 +++++++++++++++++++++++++++++++ __tests__/fixtures/cache/.gitkeep | 0 2 files changed, 65 insertions(+) create mode 100644 __tests__/commands/cache.js create mode 100644 __tests__/fixtures/cache/.gitkeep diff --git a/__tests__/commands/cache.js b/__tests__/commands/cache.js new file mode 100644 index 0000000000..433ecf9052 --- /dev/null +++ b/__tests__/commands/cache.js @@ -0,0 +1,65 @@ +/* @flow */ + +import * as reporters from '../../src/reporters/index.js'; +import * as fs from '../../src/util/fs.js'; +import {run} from '../../src/cli/commands/cache.js'; +import {run as buildRun, runInstall} from './_helpers.js'; + +const path = require('path'); +const stream = require('stream'); + +const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'cache'); + +const runCache = buildRun.bind( + null, + reporters.JSONReporter, + fixturesLoc, + async (args, flags, config, reporter, lockfile, getStdout): Promise => { + await run(config, reporter, flags, args); + return getStdout(); + }, +); + +test('ls', async (): Promise => { + await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise => { + const out = new stream.PassThrough(); + const reporter = new reporters.JSONReporter({stdout: out}); + await run(config, reporter, {}, ['ls']); + const stdout = String(out.read()); + expect(stdout).toContain('dummy'); + expect(stdout).toContain('0.0.0'); + }); +}); + +test('ls with scoped package', async (): Promise => { + await runInstall({}, 'install-from-authed-private-registry', async (config): Promise => { + const out = new stream.PassThrough(); + const reporter = new reporters.JSONReporter({stdout: out}); + await run(config, reporter, {}, ['ls']); + const stdout = String(out.read()); + expect(stdout).toContain('@types/lodash'); + expect(stdout).toContain('4.14.37'); + }); +}); + +test('dir', async (): Promise => { + await runCache(['dir'], {}, '', (config, reporter, stdout) => { + expect(stdout).toContain(JSON.stringify(config.cacheFolder)); + }); +}); + +test('clean', async (): Promise => { + await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise => { + let files = await fs.readdir(config.cacheFolder); + expect(files.length).toEqual(2); // we need to add one .tmp folder + + const out = new stream.PassThrough(); + const reporter = new reporters.JSONReporter({stdout: out}); + await run(config, reporter, {}, ['clean']); + + expect(await fs.exists(config.cacheFolder)).toBeTruthy(); + + files = await fs.readdir(config.cacheFolder); + expect(files.length).toEqual(0); + }); +}); diff --git a/__tests__/fixtures/cache/.gitkeep b/__tests__/fixtures/cache/.gitkeep new file mode 100644 index 0000000000..e69de29bb2