Skip to content

Commit

Permalink
use random uuid for assigning project name
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Jan 20, 2019
1 parent 44aa91d commit a370c21
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 98 deletions.
3 changes: 2 additions & 1 deletion packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"jest-validate": "^23.6.0",
"micromatch": "^3.1.10",
"pretty-format": "^23.6.0",
"realpath-native": "^1.0.2"
"realpath-native": "^1.0.2",
"uuid": "^3.3.2"
},
"engines": {
"node": ">= 6"
Expand Down
88 changes: 11 additions & 77 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,95 +47,29 @@ beforeEach(() => {
require('jest-resolve').findNodeModule = findNodeModule;
});

it('picks a name based on the rootDir', () => {
it('assigns a random 32-byte hash as a name to avoid clashes', () => {
const rootDir = '/root/path/foo';
const expected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
expect(
normalize(
{
rootDir,
},
{},
).options.name,
).toBe(expected);
});

it('picks a name for projects based on the main rootDir', () => {
const rootDir = '/root/path/foo';
const firstExpected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
const secondExpected = crypto
.createHash('md5')
.update('/root/path/foo:1')
.digest('hex');

const options = normalize(
{
projects: [{}, {}],
rootDir,
},
{},
);

expect(options.options.projects[0].name).toBe(firstExpected);
expect(options.options.projects[1].name).toBe(secondExpected);
});

it('picks a name for projects based on the projects rootDir', () => {
const firstRootDir = '/root/path/foo';
const secondRootDir = '/root/path/bar';
const firstExpected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
const secondExpected = crypto
.createHash('md5')
.update('/root/path/foo:1')
.digest('hex');
const thirdExpected = crypto
.createHash('md5')
.update('/root/path/bar')
.digest('hex');
const fourthExpected = crypto
.createHash('md5')
.update('/root/path/baz')
.digest('hex');

const options = normalize(
{
projects: [
{rootDir: firstRootDir},
{rootDir: firstRootDir},
{rootDir: secondRootDir},
{},
],
rootDir: '/root/path/baz',
},
{},
);

expect(options.options.projects[0].name).toBe(firstExpected);
expect(options.options.projects[1].name).toBe(secondExpected);
expect(options.options.projects[2].name).toBe(thirdExpected);
expect(options.options.projects[3].name).toBe(fourthExpected);
const {name: name1} = normalize({rootDir}, {}).options;
const {name: name2} = normalize({rootDir}, {}).options;

expect(name1).toEqual(expect.any(String));
expect(name1).toHaveLength(32);
expect(name2).toEqual(expect.any(String));
expect(name2).toHaveLength(32);
expect(name1).not.toBe(name2);
});

it('keeps custom project name based on the projects rootDir', () => {
const name = 'test';
const options = normalize(
const {options} = normalize(
{
projects: [{name, rootDir: '/path/to/foo'}],
rootDir: '/root/path/baz',
},
{},
);

expect(options.options.projects[0].name).toBe(name);
expect(options.projects[0].name).toBe(name);
});

it('keeps custom names based on the rootDir', () => {
Expand Down
22 changes: 2 additions & 20 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
} from 'types/Config';

import crypto from 'crypto';
import uuid from 'uuid/v4';
import glob from 'glob';
import path from 'path';
import {ValidationError, validate} from 'jest-validate';
Expand Down Expand Up @@ -264,33 +265,14 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => {
};

const normalizeMissingOptions = (options: InitialOptions): InitialOptions => {
const knownRootDirs = new Set();
if (!options.name) {
options.name = crypto
.createHash('md5')
.update(options.rootDir)
.update(uuid())
.digest('hex');
}

if (Array.isArray(options.projects)) {
options.projects = options.projects.map((project, index) => {
if (typeof project !== 'string' && !project.name) {
let rootDir = project.rootDir || options.rootDir;
if (knownRootDirs.has(rootDir)) {
rootDir = `${rootDir}:${index}`;
}

knownRootDirs.add(rootDir);
project.name = crypto
.createHash('md5')
.update(rootDir)
.digest('hex');
}

return project;
});
}

if (!options.setupFiles) {
options.setupFiles = [];
}
Expand Down

0 comments on commit a370c21

Please sign in to comment.