Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tracking of file removal in watch-run -Closes #5072.
- Loading branch information
Showing
5 changed files
with
145 additions
and
8 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
"use strict"; | ||
|
||
/* globals describe it */ | ||
const path = require("path"); | ||
const MemoryFs = require("memory-fs"); | ||
const webpack = require("../"); | ||
const fs = require("fs"); | ||
const rimraf = require("rimraf"); | ||
|
||
const createCompiler = config => { | ||
const compiler = webpack(config); | ||
compiler.outputFileSystem = new MemoryFs(); | ||
return compiler; | ||
}; | ||
|
||
const tempFolderPath = path.join(__dirname, "temp"); | ||
const tempFilePath = path.join(tempFolderPath, "temp-file.js"); | ||
const tempFile2Path = path.join(tempFolderPath, "temp-file2.js"); | ||
|
||
const createSingleCompiler = () => { | ||
return createCompiler({ | ||
entry: tempFilePath, | ||
watch: true, | ||
output: { | ||
path: tempFolderPath, | ||
filename: "bundle.js" | ||
} | ||
}); | ||
}; | ||
|
||
describe("RemovedFiles", () => { | ||
jest.setTimeout(20000); | ||
|
||
function cleanup() { | ||
rimraf.sync(tempFolderPath); | ||
} | ||
|
||
beforeAll(() => { | ||
cleanup(); | ||
fs.mkdirSync(tempFolderPath); | ||
fs.writeFileSync( | ||
tempFilePath, | ||
"module.exports = function temp() {return 'temp file';};\n require('./temp-file2')", | ||
"utf-8" | ||
); | ||
fs.writeFileSync( | ||
tempFile2Path, | ||
"module.exports = function temp2() {return 'temp file 2';};", | ||
"utf-8" | ||
); | ||
}); | ||
afterAll(done => { | ||
cleanup(); | ||
done(); | ||
}); | ||
|
||
it("should track removed files when they've been deleted in watchRun", done => { | ||
const compiler = createSingleCompiler(); | ||
let watcher; | ||
function handleError(err) { | ||
if (err) done(err); | ||
} | ||
setTimeout(() => { | ||
fs.unlinkSync(tempFilePath, handleError); | ||
}, 2000); | ||
compiler.hooks.watchRun.tap("RemovedFilesTest", (compiler, err) => { | ||
if (err) { | ||
done(err); | ||
} | ||
if (compiler.removedFiles.length > 0) { | ||
setTimeout(() => { | ||
expect(Array.from(compiler.removedFiles)).toContain(tempFilePath); | ||
watcher.close(); | ||
done(); | ||
}, 500); | ||
} | ||
}); | ||
|
||
watcher = compiler.watch( | ||
{ | ||
aggregateTimeout: 50 | ||
}, | ||
(err, stats) => {} | ||
); | ||
}); | ||
|
||
it("should not track removed files when they have not been deleted in watchRun", done => { | ||
const compiler = createSingleCompiler(); | ||
let watcher; | ||
compiler.hooks.watchRun.tap("RemovedFilesTest", (compiler, err) => { | ||
if (err) { | ||
done(err); | ||
} | ||
expect(Array.from(compiler.removedFiles)).toHaveLength(0); | ||
done(); | ||
watcher.close(); | ||
}); | ||
|
||
watcher = compiler.watch( | ||
{ | ||
aggregateTimeout: 50 | ||
}, | ||
(err, stats) => {} | ||
); | ||
}); | ||
|
||
it("should not track removed files when files have been modified", done => { | ||
const compiler = createSingleCompiler(); | ||
let watcher; | ||
function handleError(err) { | ||
if (err) done(err); | ||
} | ||
let updateFile = () => { | ||
fs.writeFile(tempFile2Path, "hello world", "utf-8", handleError); | ||
}; | ||
updateFile(); | ||
compiler.hooks.watchRun.tap("RemovedFilesTest", (compiler, err) => { | ||
handleError(err); | ||
setTimeout(() => { | ||
expect(Array.from(compiler.removedFiles)).toHaveLength(0); | ||
watcher.close(); | ||
done(); | ||
}, 500); | ||
watcher.close(); | ||
}); | ||
|
||
watcher = compiler.watch( | ||
{ | ||
aggregateTimeout: 50 | ||
}, | ||
(err, stats) => {} | ||
); | ||
}); | ||
}); |