-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor validate pattern and get pattern info function (#2985)
* add validate pattern function and tests for that * refactor get test path pattern info
- Loading branch information
Showing
6 changed files
with
138 additions
and
73 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/jest-cli/src/lib/__tests__/validatePattern-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters