Skip to content

Commit

Permalink
Improve handling of temporary file changes in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Oct 31, 2021
1 parent 1a62f15 commit def2885
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/snapshot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ class Manager {
const {dir, relFile, snapFile, snapPath, reportPath} = this;

if (this.updating && this.newBlocksByTitle.size === 0) {
return [
...cleanFile(snapPath),
...cleanFile(reportPath),
];
return {
changedFiles: [cleanFile(snapPath), cleanFile(reportPath)].flat(),
temporaryFiles: [],
};
}

if (!this.hasChanges) {
Expand All @@ -376,11 +376,14 @@ class Manager {

fs.mkdirSync(dir, {recursive: true});

const paths = [snapPath, reportPath];
const tmpfileCreated = tmpfile => paths.push(tmpfile);
const temporaryFiles = [];
const tmpfileCreated = file => temporaryFiles.push(file);
writeFileAtomic.sync(snapPath, buffer, {tmpfileCreated});
writeFileAtomic.sync(reportPath, reportBuffer, {tmpfileCreated});
return paths;
return {
changedFiles: [snapPath, reportPath],
temporaryFiles,
};
}
}

Expand Down
15 changes: 14 additions & 1 deletion lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export default class Watcher {
this.testDependencies = [];
this.trackTestDependencies(api);

this.temporaryFiles = new Set();
this.touchedFiles = new Set();
this.trackTouchedFiles(api);

Expand Down Expand Up @@ -245,9 +246,13 @@ export default class Watcher {
return;
}

for (const file of evt.files) {
for (const file of evt.files.changedFiles) {
this.touchedFiles.add(file);
}

for (const file of evt.files.temporaryFiles) {
this.temporaryFiles.add(file);
}
});
});
}
Expand Down Expand Up @@ -393,6 +398,14 @@ export default class Watcher {
return false;
}

// Unlike touched files, temporary files are never cleared. We may see
// adds and unlinks detected separately, so we track the temporary files
// as long as AVA is running.
if (this.temporaryFiles.has(path)) {
debug('Ignoring known temporary file %s', path);
return false;
}

return true;
});

Expand Down

0 comments on commit def2885

Please sign in to comment.