From 6f51489969abb7a88e29828c900db94d5562981f Mon Sep 17 00:00:00 2001 From: Trevor Brindle Date: Wed, 11 Oct 2017 09:45:29 -0400 Subject: [PATCH] rebase and squash - add clearCache cli command --- .../__tests__/clear_cache.test.js | 37 +++++++++++++++++++ .../clear_cache/__tests__/clear_cache.test.js | 10 +++++ integration_tests/clear_cache/package.json | 5 +++ packages/jest-cli/package.json | 1 + packages/jest-cli/src/cli/args.js | 7 ++++ packages/jest-cli/src/cli/index.js | 10 +++++ 6 files changed, 70 insertions(+) create mode 100644 integration_tests/__tests__/clear_cache.test.js create mode 100644 integration_tests/clear_cache/__tests__/clear_cache.test.js create mode 100644 integration_tests/clear_cache/package.json diff --git a/integration_tests/__tests__/clear_cache.test.js b/integration_tests/__tests__/clear_cache.test.js new file mode 100644 index 000000000000..a4190810e324 --- /dev/null +++ b/integration_tests/__tests__/clear_cache.test.js @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ +'use strict'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const runJest = require('../runJest'); + +const CACHE = path.resolve(os.tmpdir(), 'clear_cache_directory'); + +describe('jest --clearCache', () => { + test('normal run results in cache directory being written', () => { + const {status} = runJest('clear_cache', [`--cacheDirectory=${CACHE}`]); + + expect(fs.existsSync(CACHE)).toBe(true); + expect(status).toBe(0); + }); + test('clearCache results in deleted directory and exit status 0', () => { + const {status, stdout, stderr} = runJest('clear_cache', [ + '--clearCache', + `--cacheDirectory=${CACHE}`, + ]); + + expect(fs.existsSync(CACHE)).toBe(false); + expect(stdout).toBe(`Cleared ${CACHE}\n`); + expect(stderr.trim()).toBe(''); + expect(status).toBe(0); + }); +}); diff --git a/integration_tests/clear_cache/__tests__/clear_cache.test.js b/integration_tests/clear_cache/__tests__/clear_cache.test.js new file mode 100644 index 000000000000..f09a606fe9eb --- /dev/null +++ b/integration_tests/clear_cache/__tests__/clear_cache.test.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +'use strict'; + +test('stub', () => expect(1).toBe(1)); diff --git a/integration_tests/clear_cache/package.json b/integration_tests/clear_cache/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/clear_cache/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 6ba1066c5fab..01af8aeb9802 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -27,6 +27,7 @@ "micromatch": "^2.3.11", "node-notifier": "^5.1.2", "pify": "^3.0.0", + "rimraf": "^2.5.4", "slash": "^1.0.0", "string-length": "^2.0.0", "strip-ansi": "^4.0.0", diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index e60f377ce002..018b7cbd619d 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -117,6 +117,13 @@ export const options = { ' prevent snapshots from being written unless explicitly requested.', type: 'boolean', }, + clearCache: { + default: undefined, + description: + 'Clears the configured Jest cache directory and then exits. ' + + 'Default directory can be found by calling jest --showConfig', + type: 'boolean', + }, clearMocks: { default: undefined, description: diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 30bddce8ef9a..752fbeb8cf04 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -32,6 +32,7 @@ import Runtime from 'jest-runtime'; import TestWatcher from '../test_watcher'; import watch from '../watch'; import yargs from 'yargs'; +import rimraf from 'rimraf'; export async function run(maybeArgv?: Argv, project?: Path) { const argv: Argv = buildArgv(maybeArgv, project); @@ -68,6 +69,15 @@ export const runCLI = async ( outputStream, ); + if (argv.clearCache) { + configs.forEach(config => { + rimraf.sync(config.cacheDirectory); + process.stdout.write(`Cleared ${config.cacheDirectory}\n`); + }); + + process.exit(0); + } + await _run( globalConfig, configs,