-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(testlab): decache files in TestSandbox when resetting it #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
bajtos
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lovely, we should have fixed this issue long time ago 👏
I personally prefer a slightly different implementation that does not require glob and decache dependencies, see https://github.com/strongloop/loopback-workspace/blob/f3fc7f12db9501a5cd8d6d24d0979f3ce8cea90c/test/support.js#L61-L65:
// Remove any cached modules from SANDBOX
for (var key in require.cache) {
if (key.slice(0, SANDBOX.length) == SANDBOX)
delete require.cache[key];
}The condition can be further simplified using String.prototype.startsWith, now that we can use ES6/ES2017 in our code.
|
I'm not very confident in the robustness of cleaning up |
|
@raymondfeng What are your concerns with cleaning up the
|
In the past, I had trouble making
We have two choices:
The |
|
I am personally not a big fan of using a temp dir:
The "random child folder" approach is something that has been working well for us in loopback-boot, see here: https://github.com/ebbnormal/loopback-boot/blob/f7108a72558b24ec27be7c320b89329a17402d62/test/helpers/appdir.js#L15-L25 appdir.init = function(cb) {
// Node's module loader has a very aggressive caching, therefore
// we can't reuse the same path for multiple tests
// The code here is used to generate a random string
require('crypto').randomBytes(5, function(err, buf) {
if (err) return cb(err);
var randomStr = buf.toString('hex');
PATH = appdir.PATH = sandbox.resolve(randomStr);
cb(null, appdir.PATH);
});
};Another benefit of using a random subfolder: I think it can fix some (if not all) issues you have discovered while trying out parallel-mocha, @virkt25. Having said that, I am ok with cleaning |
| // a file is recreated in sandbox with the same file name but different | ||
| // contents after resetting the sandbox. | ||
| for (const key in require.cache) { | ||
| if (key.startsWith(this.path)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check that the key starts with this.path + '/' to ensure we don't accidentally decache file names like sandbox-123/file.js or sandbox.bak/file.js when this.path = 'sandbox'? (Plus handle \ on Windows.)
Not a big deal, maybe fixing this edge case is not worth the implementation complexity.
Signed-off-by: Taranveer Virk <taranveer@virk.cc>
Signed-off-by: Taranveer Virk taranveer@virk.cc
Ran into an issue when working on DataSource Declarative support -- in writing the tests as follows:
requirecaches the file.This PR fixes that by decaching
*.jsonand*.jsfiles in the sandbox when the sandbox is reset.cc: @bajtos you warned me this would happen and it came to bite me in the behind. Took me 2 hours to realize what was happening when the test kept failing.
Checklist
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated