From 39a6caa5d5dbe77c594836f4b2333df3a33ba937 Mon Sep 17 00:00:00 2001 From: Elad Bezalel Date: Tue, 2 Jan 2024 11:22:41 +0200 Subject: [PATCH] fix: block lockfile behind flag until it's ready (#28) --- libs/core/src/true-affected.spec.ts | 72 ++++++++++++++++++++--------- libs/core/src/true-affected.ts | 3 +- libs/core/src/types.ts | 3 ++ libs/nx/src/cli.spec.ts | 2 + libs/nx/src/cli.ts | 10 ++++ 5 files changed, 66 insertions(+), 24 deletions(-) diff --git a/libs/core/src/true-affected.spec.ts b/libs/core/src/true-affected.spec.ts index 64cddca..73ed654 100644 --- a/libs/core/src/true-affected.spec.ts +++ b/libs/core/src/true-affected.spec.ts @@ -241,33 +241,59 @@ describe('trueAffected', () => { expect(affected).toEqual(['angular-component']); }); - it('should find fils that are related to changed modules from lockfile', async () => { - jest.spyOn(git, 'getChangedFiles').mockReturnValue([ - { - filePath: 'package-lock.json', - changedLines: [2], - }, - ]); - jest.spyOn(lockFiles, 'hasLockfileChanged').mockReturnValue(true); - jest.spyOn(lockFiles, 'findAffectedFilesByLockfile').mockReturnValue([ - { - filePath: 'proj1/index.ts', - changedLines: [2], - }, - ]); - - const affected = await trueAffected({ - cwd, - base: 'main', - projects: [ + describe('__experimentalLockfileCheck', () => { + it('should find files that are related to changed modules from lockfile if flag is on', async () => { + jest.spyOn(git, 'getChangedFiles').mockReturnValue([ { - name: 'proj1', - sourceRoot: 'proj1/', + filePath: 'package-lock.json', + changedLines: [2], }, - ], + ]); + jest.spyOn(lockFiles, 'hasLockfileChanged').mockReturnValue(true); + jest.spyOn(lockFiles, 'findAffectedFilesByLockfile').mockReturnValue([ + { + filePath: 'proj1/index.ts', + changedLines: [2], + }, + ]); + + const affected = await trueAffected({ + cwd, + base: 'main', + __experimentalLockfileCheck: true, + projects: [ + { + name: 'proj1', + sourceRoot: 'proj1/', + }, + ], + }); + + expect(affected).toEqual(['proj1']); }); - expect(affected).toEqual(['proj1']); + it('should not find files that are related to changed modules from lockfile if flag is off', async () => { + jest.spyOn(git, 'getChangedFiles').mockReturnValue([ + { + filePath: 'package-lock.json', + changedLines: [2], + }, + ]); + + const affected = await trueAffected({ + cwd, + base: 'main', + __experimentalLockfileCheck: false, + projects: [ + { + name: 'proj1', + sourceRoot: 'proj1/', + }, + ], + }); + + expect(affected).toEqual([]); + }); }); it("should ignore files when can't find the changed line", async () => { diff --git a/libs/core/src/true-affected.ts b/libs/core/src/true-affected.ts index 08cc111..0ca9d10 100644 --- a/libs/core/src/true-affected.ts +++ b/libs/core/src/true-affected.ts @@ -27,6 +27,7 @@ export const trueAffected = async ({ base = 'origin/main', projects, include = [DEFAULT_INCLUDE_TEST_FILES], + __experimentalLockfileCheck = false, }: TrueAffected) => { const project = new Project({ compilerOptions: { @@ -87,7 +88,7 @@ export const trueAffected = async ({ ); let changedFilesByLockfile: ChangedFiles[] = []; - if (hasLockfileChanged(changedFiles)) { + if (__experimentalLockfileCheck && hasLockfileChanged(changedFiles)) { changedFilesByLockfile = findAffectedFilesByLockfile( cwd, base, diff --git a/libs/core/src/types.ts b/libs/core/src/types.ts index 704133b..dee5f83 100644 --- a/libs/core/src/types.ts +++ b/libs/core/src/types.ts @@ -12,4 +12,7 @@ export interface TrueAffected { base?: string; projects: TrueAffectedProject[]; include?: (string | RegExp)[]; + + // **experimental** - this is an experimental feature and may be removed or changed at any time + __experimentalLockfileCheck?: boolean; } diff --git a/libs/nx/src/cli.spec.ts b/libs/nx/src/cli.spec.ts index 88dbd2d..0c1fe85 100644 --- a/libs/nx/src/cli.spec.ts +++ b/libs/nx/src/cli.spec.ts @@ -72,6 +72,7 @@ describe('cli', () => { restArgs: [], tsConfigFilePath: 'tsconfig.base.json', target: [], + experimentalLockfileCheck: false, }); }); @@ -95,6 +96,7 @@ describe('cli', () => { restArgs: [], tsConfigFilePath: 'tsconfig.json', target: [], + experimentalLockfileCheck: false, }); }); }); diff --git a/libs/nx/src/cli.ts b/libs/nx/src/cli.ts index 5a43820..ec48c12 100644 --- a/libs/nx/src/cli.ts +++ b/libs/nx/src/cli.ts @@ -24,6 +24,7 @@ export const affectedAction = async ({ tsConfigFilePath, includeFiles, target, + experimentalLockfileCheck, }: AffectedOptions) => { let projects = await getNxTrueAffectedProjects(cwd); @@ -44,6 +45,7 @@ export const affectedAction = async ({ base, projects, include: [...includeFiles, DEFAULT_INCLUDE_TEST_FILES], + __experimentalLockfileCheck: experimentalLockfileCheck, }); if (json) { @@ -91,6 +93,7 @@ interface AffectedOptions { includeFiles: string[]; restArgs: string[]; target: string[]; + experimentalLockfileCheck?: boolean; } const affectedCommand: CommandModule = { @@ -138,6 +141,11 @@ const affectedCommand: CommandModule = { return array.flatMap((v) => v.split(',')).map((v) => v.trim()); }, }, + experimentalLockfileCheck: { + desc: 'Experimental lockfile check', + type: 'boolean', + default: false, + }, }, handler: async ({ cwd, @@ -148,6 +156,7 @@ const affectedCommand: CommandModule = { json, includeFiles, target, + experimentalLockfileCheck, // eslint-disable-next-line @typescript-eslint/no-unused-vars $0, // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -163,6 +172,7 @@ const affectedCommand: CommandModule = { json, includeFiles, target, + experimentalLockfileCheck, restArgs: Object.entries(rest).map( /* istanbul ignore next */ ([key, value]) => `--${key}=${value}`