Skip to content

Commit

Permalink
Refactor validate pattern and get pattern info function (#2985)
Browse files Browse the repository at this point in the history
* add validate pattern function and tests for that

* refactor get test path pattern info
  • Loading branch information
zouxuoz authored and cpojer committed Feb 27, 2017
1 parent 08b8f73 commit f4c3b78
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 73 deletions.
38 changes: 38 additions & 0 deletions packages/jest-cli/src/lib/__tests__/validatePattern-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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.
*
* @emails oncall+jsinfra
*/
'use strict';

const validatePattern = require('../validatePattern');

describe('validate pattern function', () => {
it('without passed args returns true', () => {
const isValid = validatePattern();

expect(isValid).toBeTruthy();
});

it('returns true for empty pattern', () => {
const isValid = validatePattern('');

expect(isValid).toBeTruthy();
});

it('returns true for valid pattern', () => {
const isValid = validatePattern('abc+');

expect(isValid).toBeTruthy();
});

it('returns false for invalid pattern', () => {
const isValid = validatePattern('\\');

expect(isValid).toBeFalsy();
});
});
68 changes: 0 additions & 68 deletions packages/jest-cli/src/lib/buildTestPathPatternInfo.js

This file was deleted.

70 changes: 70 additions & 0 deletions packages/jest-cli/src/lib/getTestPathPatternInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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';

import type {PatternInfo} from '../SearchSource';

const {clearLine} = require('jest-util');
const chalk = require('chalk');
const validatePattern = require('./validatePattern');

const DEFAULT_PATTERN_INFO = {
input: '',
shouldTreatInputAsPattern: false,
testPathPattern: '',
};

const showTestPathPatternError = (testPathPattern: string) => {
clearLine(process.stdout);

// eslint-disable-next-line max-len
console.log(chalk.red(` Invalid testPattern ${testPathPattern} supplied. Running all tests instead.`));
};

module.exports = (argv: Object): PatternInfo => {
if (argv.onlyChanged) {
return {
input: '',
lastCommit: argv.lastCommit,
onlyChanged: true,
watch: argv.watch,
};
}

if (argv.testPathPattern) {
if (validatePattern(argv.testPathPattern)) {
return {
input: argv.testPathPattern,
shouldTreatInputAsPattern: true,
testPathPattern: argv.testPathPattern,
};
} else {
showTestPathPatternError(argv.testPathPattern);
}
}

if (argv._ && argv._.length) {
const testPathPattern = argv._.join('|');

if (validatePattern(testPathPattern)) {
return {
findRelatedTests: argv.findRelatedTests,
input: argv._.join(' '),
paths: argv._,
shouldTreatInputAsPattern: false,
testPathPattern,
};
} else {
showTestPathPatternError(testPathPattern);
}
}

return DEFAULT_PATTERN_INFO;
};
4 changes: 2 additions & 2 deletions packages/jest-cli/src/lib/setState.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
'use strict';

const buildTestPathPatternInfo = require('./buildTestPathPatternInfo');
const getTestPathPatternInfo = require('./getTestPathPatternInfo');

module.exports = (
argv: Object,
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = (
}

argv.onlyChanged = false;
argv.onlyChanged = buildTestPathPatternInfo(argv).input === ''
argv.onlyChanged = getTestPathPatternInfo(argv).input === ''
&& !argv.watchAll
&& !argv.testNamePattern;

Expand Down
25 changes: 25 additions & 0 deletions packages/jest-cli/src/lib/validatePattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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 validatePattern = (pattern: string) => {
if (pattern) {
try {
// eslint-disable-next-line no-new
new RegExp(pattern, 'i');
} catch (e) {
return false;
}
}

return true;
};

module.exports = validatePattern;
6 changes: 3 additions & 3 deletions packages/jest-cli/src/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fs = require('graceful-fs');
const SearchSource = require('./SearchSource');
const TestRunner = require('./TestRunner');

const buildTestPathPatternInfo = require('./lib/buildTestPathPatternInfo');
const getTestPathPatternInfo = require('./lib/getTestPathPatternInfo');
const chalk = require('chalk');
const {Console, formatTestResults} = require('jest-util');
const getMaxWorkers = require('./lib/getMaxWorkers');
Expand Down Expand Up @@ -56,7 +56,7 @@ const runJest = (
) => {
const maxWorkers = getMaxWorkers(argv);
const localConsole = new Console(pipe, pipe);
let patternInfo = buildTestPathPatternInfo(argv);
let patternInfo = getTestPathPatternInfo(argv);
return Promise.resolve().then(() => {
const source = new SearchSource(hasteContext, config);
return source.getTestPaths(patternInfo)
Expand All @@ -68,7 +68,7 @@ const runJest = (
setState(argv, 'watchAll', {
noSCM: true,
});
patternInfo = buildTestPathPatternInfo(argv);
patternInfo = getTestPathPatternInfo(argv);
return source.getTestPaths(patternInfo);
} else {
localConsole.log(
Expand Down

0 comments on commit f4c3b78

Please sign in to comment.