-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lockfile direct dep affected detection
- Loading branch information
1 parent
161c86c
commit 7bd8bf0
Showing
12 changed files
with
656 additions
and
181 deletions.
There are no files selected for viewing
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,95 @@ | ||
import { fastFindInFiles } from 'fast-find-in-files'; | ||
import { existsSync } from 'fs'; | ||
import * as path from 'path'; | ||
import { findNonSourceAffectedFiles } from './assets'; | ||
|
||
jest.mock('fast-find-in-files'); | ||
jest.mock('fs'); | ||
|
||
describe('findNonSourceAffectedFiles', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should return relevant files', () => { | ||
const cwd = '/project'; | ||
const changedFilePath = '/project/src/file.ts'; | ||
const excludeFolderPaths = ['node_modules', 'dist', '.git']; | ||
|
||
(fastFindInFiles as jest.Mock).mockReturnValue([ | ||
{ | ||
filePath: '/project/src/file.ts', | ||
queryHits: [{ lineNumber: 1, line: `"file.ts"` }], | ||
}, | ||
]); | ||
(existsSync as jest.Mock).mockReturnValue(true); | ||
|
||
const result = findNonSourceAffectedFiles( | ||
cwd, | ||
changedFilePath, | ||
excludeFolderPaths | ||
); | ||
|
||
expect(result).toEqual([{ filePath: 'src/file.ts', changedLines: [1] }]); | ||
expect(fastFindInFiles).toHaveBeenCalledWith({ | ||
directory: cwd, | ||
needle: path.basename(changedFilePath), | ||
excludeFolderPaths: excludeFolderPaths.map((folder) => | ||
path.join(cwd, folder) | ||
), | ||
}); | ||
}); | ||
|
||
it('should return empty array if no relevant files found', () => { | ||
const cwd = '/project'; | ||
const changedFilePath = '/project/src/file.ts'; | ||
const excludeFolderPaths = ['node_modules', 'dist', '.git']; | ||
|
||
(fastFindInFiles as jest.Mock).mockReturnValue([]); | ||
(existsSync as jest.Mock).mockReturnValue(true); | ||
|
||
const result = findNonSourceAffectedFiles( | ||
cwd, | ||
changedFilePath, | ||
excludeFolderPaths | ||
); | ||
|
||
expect(result).toEqual([]); | ||
expect(fastFindInFiles).toHaveBeenCalledWith({ | ||
directory: cwd, | ||
needle: path.basename(changedFilePath), | ||
excludeFolderPaths: excludeFolderPaths.map((folder) => | ||
path.join(cwd, folder) | ||
), | ||
}); | ||
}); | ||
|
||
it("should still work even if found file didn't have a match", () => { | ||
const cwd = '/project'; | ||
const changedFilePath = '/project/src/file.ts'; | ||
const excludeFolderPaths = ['node_modules', 'dist', '.git']; | ||
|
||
(fastFindInFiles as jest.Mock).mockReturnValue([ | ||
{ | ||
filePath: '/project/src/file.ts', | ||
queryHits: [{ lineNumber: 1, line: `console.log('hi')` }], | ||
}, | ||
]); | ||
(existsSync as jest.Mock).mockReturnValue(true); | ||
|
||
const result = findNonSourceAffectedFiles( | ||
cwd, | ||
changedFilePath, | ||
excludeFolderPaths | ||
); | ||
|
||
expect(result).toEqual([]); | ||
expect(fastFindInFiles).toHaveBeenCalledWith({ | ||
directory: cwd, | ||
needle: path.basename(changedFilePath), | ||
excludeFolderPaths: excludeFolderPaths.map((folder) => | ||
path.join(cwd, folder) | ||
), | ||
}); | ||
}); | ||
}); |
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,63 @@ | ||
import { basename, dirname, join, relative, resolve } from 'path'; | ||
import { ChangedFiles } from './git'; | ||
import { FastFindInFiles, fastFindInFiles } from 'fast-find-in-files'; | ||
import { existsSync } from 'fs'; | ||
|
||
export function findNonSourceAffectedFiles( | ||
cwd: string, | ||
changedFilePath: string, | ||
excludeFolderPaths: string[] | ||
): ChangedFiles[] { | ||
const fileName = basename(changedFilePath); | ||
|
||
const files = fastFindInFiles({ | ||
directory: cwd, | ||
needle: fileName, | ||
excludeFolderPaths: excludeFolderPaths.map((path) => join(cwd, path)), | ||
}); | ||
|
||
const relevantFiles = filterRelevantFiles(cwd, files, changedFilePath); | ||
|
||
return relevantFiles; | ||
} | ||
|
||
function filterRelevantFiles( | ||
cwd: string, | ||
files: FastFindInFiles[], | ||
changedFilePath: string | ||
): ChangedFiles[] { | ||
const fileName = basename(changedFilePath); | ||
const regExp = new RegExp(`['"\`](?<relFilePath>.*${fileName})['"\`]`); | ||
|
||
return files | ||
.map(({ filePath: foundFilePath, queryHits }) => ({ | ||
filePath: relative(cwd, foundFilePath), | ||
changedLines: queryHits | ||
.filter(({ line }) => | ||
isRelevantLine(line, regExp, cwd, foundFilePath, changedFilePath) | ||
) | ||
.map(({ lineNumber }) => lineNumber), | ||
})) | ||
.filter(({ changedLines }) => changedLines.length > 0); | ||
} | ||
|
||
function isRelevantLine( | ||
line: string, | ||
regExp: RegExp, | ||
cwd: string, | ||
foundFilePath: string, | ||
changedFilePath: string | ||
): boolean { | ||
const match = regExp.exec(line); | ||
const { relFilePath } = match?.groups ?? {}; | ||
|
||
if (relFilePath == null) return false; | ||
|
||
const changedFile = resolve(cwd, changedFilePath); | ||
const relatedFilePath = resolve( | ||
cwd, | ||
relative(cwd, join(dirname(foundFilePath), relFilePath)) | ||
); | ||
|
||
return relatedFilePath === changedFile && existsSync(relatedFilePath); | ||
} |
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
Oops, something went wrong.