From a370c21539aad54f79d577b3b95203351b1fc169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 20 Jan 2019 19:13:20 +0100 Subject: [PATCH] use random uuid for assigning project name --- packages/jest-config/package.json | 3 +- .../src/__tests__/normalize.test.js | 88 +++---------------- packages/jest-config/src/normalize.js | 22 +---- 3 files changed, 15 insertions(+), 98 deletions(-) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index f41992144802..63d03d525a13 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -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" diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 271b7153b0fb..a4df3af60e81 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -47,87 +47,21 @@ 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', @@ -135,7 +69,7 @@ it('keeps custom project name based on the projects rootDir', () => { {}, ); - expect(options.options.projects[0].name).toBe(name); + expect(options.projects[0].name).toBe(name); }); it('keeps custom names based on the rootDir', () => { diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 76e7833aece2..f63714494aca 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -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'; @@ -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 = []; }