Skip to content

Commit

Permalink
Create jest-file-exists and jest-resolve-dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Jul 14, 2016
1 parent df2b6f1 commit 39ae874
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 120 deletions.
4 changes: 3 additions & 1 deletion packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
"jest-changed-files": "^13.2.2",
"jest-config": "^13.2.3",
"jest-environment-jsdom": "^13.2.2",
"jest-file-exists": "^13.2.2",
"jest-haste-map": "^13.2.2",
"jest-jasmine1": "^13.2.2",
"jest-jasmine2": "^13.2.3",
"jest-mock": "^13.2.2",
"jest-resolve-dependencies": "^13.2.2",
"jest-resolve": "^13.2.2",
"jest-runtime": "^13.2.3",
"jest-util": "^13.2.2",
"jest-snapshot": "^13.2.3",
"jest-util": "^13.2.2",
"json-stable-stringify": "^1.0.0",
"sane": "^1.2.0",
"which": "^1.1.1",
Expand Down
11 changes: 8 additions & 3 deletions packages/jest-cli/src/SearchSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import type {HasteContext} from 'types/HasteMap';
import type {Path} from 'types/Config';
import type {ResolveModuleConfig} from '../../jest-resolve/src';

const Resolver = require('jest-resolve');
const DependencyResolver = require('jest-resolve-dependencies');

const chalk = require('chalk');
const changedFiles = require('jest-changed-files');
const fileExists = require('jest-file-exists');
const path = require('path');
const utils = require('jest-util');

Expand Down Expand Up @@ -147,7 +148,7 @@ class SearchSource {
): SearchResult {
if (testPathPattern && !(testPathPattern instanceof RegExp)) {
const maybeFile = path.resolve(process.cwd(), testPathPattern);
if (Resolver.fileExists(maybeFile)) {
if (fileExists(maybeFile, this._hasteContext.moduleMap.files)) {
return this._filterTestPathsWithStats([maybeFile]);
}
}
Expand All @@ -156,8 +157,12 @@ class SearchSource {
}

findRelatedTests(allPaths: Set<Path>): SearchResult {
const dependencyResolver = new DependencyResolver(
this._hasteContext.resolver,
this._hasteContext.moduleMap,
);
return {
paths: this._hasteContext.resolver.resolveInverseDependencies(
paths: dependencyResolver.resolveInverse(
allPaths,
this.isTestFilePath.bind(this),
{
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-cli/src/__tests__/SearchSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

jest.disableAutomock();

jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;

const path = require('path');

const rootDir = path.resolve(__dirname, 'test_root');
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-file-exists/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
19 changes: 19 additions & 0 deletions packages/jest-file-exists/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "jest-file-exists",
"version": "13.2.2",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"license": "BSD-3-Clause",
"main": "build/index.js",
"jest": {
"automock": false,
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js"
}
}
32 changes: 32 additions & 0 deletions packages/jest-file-exists/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 fileExists = require('../');
const path = require('path');

test('file exists', () => {
expect(fileExists(__filename)).toBe(true);
});

test('file exists if module map is provided', () => {
expect(fileExists('/random-string.js', {
'/random-string.js': true,
})).toBe(true);
});

test('file does not exist', () => {
expect(fileExists(
path.join(path.basename(__filename), 'does-probably-not-exist.js'),
{
'/random-string.js': true,
},
)).toBe(false);
});
30 changes: 30 additions & 0 deletions packages/jest-file-exists/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2014, 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 {Path} from 'types/Config';
import type {FileMetaData} from 'types/HasteMap';

const fs = require('fs');

module.exports = (
filePath: Path,
files: ?{[filepath: string]: FileMetaData},
): boolean => {
if (files && files[filePath]) {
return true;
}

try {
return fs.statSync(filePath).isFile();
} catch (e) {}
return false;
};
3 changes: 3 additions & 0 deletions packages/jest-resolve-dependencies/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
22 changes: 22 additions & 0 deletions packages/jest-resolve-dependencies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "jest-resolve-dependencies",
"version": "13.2.2",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"license": "BSD-3-Clause",
"main": "build/index.js",
"dependencies": {
"jest-file-exists": "13.2.2",
"jest-resolve": "13.2.2"
},
"jest": {
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js"
}
}
118 changes: 118 additions & 0 deletions packages/jest-resolve-dependencies/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright (c) 2014, 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 {HasteMap} from 'types/HasteMap';
import type {Path} from 'types/Config';
import type Resolver from '../../jest-resolve/src';

const fileExists = require('jest-file-exists');

export type ResolveModuleConfig = {skipNodeResolution?: boolean};

function compact(array: Array<?Path>): Array<Path> {
const result = [];
for (let i = 0; i < array.length; ++i) {
const element = array[i];
if (element != null) {
result.push(element);
}
}
return result;
}

/**
* DependencyResolver is used to resolve the direct dependencies of a module or
* to retrieve a list of all transitive inverse dependencies.
*/
class DependencyResolver {
_moduleMap: HasteMap;
_resolver: Resolver;

constructor(resolver: Resolver, moduleMap: HasteMap) {
this._resolver = resolver;
this._moduleMap = moduleMap;
}

resolve(
file: Path,
options?: ResolveModuleConfig,
): Array<Path> {
if (!this._moduleMap.files[file]) {
return [];
}
return compact(
this._moduleMap.files[file][3].map(dependency => {
if (this._resolver.isCoreModule(dependency)) {
return null;
}
try {
return this._resolver.resolveModule(file, dependency, options);
} catch (e) {}
return this._resolver.getMockModule(dependency) || null;
}),
);
}

resolveInverse(
paths: Set<Path>,
filter: (file: Path) => boolean,
options?: ResolveModuleConfig,
): Array<Path> {
const collectModules = (relatedPaths, moduleMap, changed) => {
const visitedModules = new Set();
while (changed.size) {
changed = new Set(moduleMap.filter(module => (
!visitedModules.has(module.file) &&
module.dependencies.some(dep => dep && changed.has(dep))
)).map(module => {
const file = module.file;
if (filter(file)) {
relatedPaths.add(file);
}
visitedModules.add(file);
return module.file;
}));
}
return relatedPaths;
};

if (!paths.size) {
return [];
}

const relatedPaths = new Set();
const changed = new Set();
for (const path of paths) {
if (fileExists(path, this._moduleMap.files)) {
const module = this._moduleMap.files[path];
if (module) {
changed.add(path);
if (filter(path)) {
relatedPaths.add(path);
}
}
}
}

const modules = [];
for (const file in this._moduleMap.files) {
modules.push({
file,
dependencies: this.resolve(file, options),
});
}
return Array.from(collectModules(relatedPaths, modules, changed));
}

}

module.exports = DependencyResolver;
1 change: 1 addition & 0 deletions packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "build/index.js",
"dependencies": {
"browser-resolve": "^1.11.2",
"jest-file-exists": "^13.2.2",
"jest-haste-map": "^13.2.2",
"resolve": "^1.1.6"
},
Expand Down
Loading

0 comments on commit 39ae874

Please sign in to comment.