Skip to content

Commit

Permalink
fix(betterer 🐛): more cache fixes (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
phenomnomnominal authored Jun 11, 2021
1 parent 32adb67 commit 23237e1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
35 changes: 24 additions & 11 deletions packages/betterer/src/fs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export class BettererGit implements BettererVersionControl {
}

private async _sync(): Promise<void> {
this._fileMap = {};
this._filePaths = [];

const tree = await this._git.raw(['ls-tree', '--full-tree', '-r', 'HEAD']);
const fileInfo = this._toFilePaths(tree).map((info) => info.split(/\s/));
const gitHashes: Record<string, string> = {};
Expand All @@ -89,27 +92,37 @@ export class BettererGit implements BettererVersionControl {
gitHashes[absolutePath] = hash;
});

this._fileMap = {};
this._filePaths = [];
const modified = await this._git.raw(['ls-files', '--cached', '--modified', '--others', '--exclude-standard']);
const untracked = await this._git.raw(['ls-files', '--cached', '--others', '--exclude-standard']);
const untrackedFilePaths = this._toFilePaths(untracked);
await Promise.all(
untrackedFilePaths.map(async (relativePath) => {
const absolutePath = normalisedPath(path.join(this._rootDir, relativePath));
const hash = gitHashes[absolutePath] || (await this._getUntrackedHash(absolutePath));
if (hash == null) {
return;
}
this._fileMap[absolutePath] = hash;
this._filePaths.push(absolutePath);
})
);

const modified = await this._git.raw(['ls-files', '--modified']);
const modifiedFilePaths = this._toFilePaths(modified);
await Promise.all(
modifiedFilePaths.map(async (relativePath) => {
const absolutePath = normalisedPath(path.join(this._rootDir, relativePath));
const hash = gitHashes[absolutePath] || (await this._getUntrackedHash(absolutePath));
const hash = await this._getUntrackedHash(absolutePath);

// If hash is null then the modification was a deletion:
if (hash == null) {
delete this._fileMap[absolutePath];
this._filePaths.splice(this._filePaths.indexOf(absolutePath), 1);
return;
}

this._fileMap[absolutePath] = hash;
this._filePaths.push(absolutePath);
})
);
const deleted = await this._git.raw(['ls-files', '--deleted']);
const deletedFilePaths = this._toFilePaths(deleted);
deletedFilePaths.forEach((relativePath) => {
const absolutePath = normalisedPath(path.join(this._rootDir, relativePath));
delete this._fileMap[absolutePath];
this._filePaths.splice(this._filePaths.indexOf(absolutePath), 1);
});
}
}
6 changes: 5 additions & 1 deletion test/__snapshots__/betterer-cache.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ exports[\`regexp no hack comments\`] = {
"
`;
exports[`betterer should write a cache file 4`] = `"{}"`;
exports[`betterer should write a cache file 4`] = `
"{
\\"src/index.ts\\": \\"5381\\"
}"
`;
exports[`betterer should write a cache file 5`] = `"// BETTERER RESULTS V2."`;
Expand Down
4 changes: 2 additions & 2 deletions test/betterer-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createFixture } from './fixture';

describe('betterer', () => {
it('should write a cache file', async () => {
const { logs, paths, readFile, cleanup, resolve, writeFile, deleteFile, runNames } = await createFixture(
const { logs, paths, readFile, cleanup, resolve, writeFile, runNames } = await createFixture(
'test-betterer-cache',
{
'.betterer.js': `
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = {

expect(worseResult).toMatchSnapshot();

await deleteFile(indexPath);
await writeFile(indexPath, ``);

const betterTestRun = await betterer({ configPaths, resultsPath, cache: true, cachePath });

Expand Down

0 comments on commit 23237e1

Please sign in to comment.