Skip to content

Commit

Permalink
lib: redirect homedir for modules being tested (#755)
Browse files Browse the repository at this point in the history
The tests for some modules write into `os.homedir()` and are not
cleaning up afterwards. This is accumulating over time on our CI
and require periodic manual cleaning.

Redirect the environment variables used by `os.homedir()` into
CITGM's temporary directory for the module being tested, which gets
removed at the end of the test run.

Refs: nodejs/build#1908 (comment)
  • Loading branch information
richardlau authored Oct 22, 2019
1 parent 63e2dae commit 5421cf6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/create-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function createOptions(cwd, context) {
if (context.options.gid) options.gid = context.options.gid;
}
options.env = Object.create(process.env);
options.env['HOME'] = context.homeDir;
options.env['USERPROFILE'] = context.homeDir;
options.env['npm_config_loglevel'] = context.options.npmLevel;
options.env['npm_config_tmp'] = context.npmConfigTmp;
options.env['TEMP'] = context.npmConfigTmp;
Expand Down
2 changes: 2 additions & 0 deletions lib/temp-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ async function create(context) {
context.path
);

context.homeDir = path.join(context.path, 'home');
context.npmConfigTmp = path.join(context.path, 'npm_config_tmp');

await mkdirp(context.homeDir);
await mkdirp(context.npmConfigTmp);
}

Expand Down
3 changes: 3 additions & 0 deletions test/test-create-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test('create-options:', (t) => {
npmLevel: 'warning'
},
emit: function() {},
homeDir: 'homedir',
npmConfigTmp: 'npm_config_tmp',
module: { envVar: { testenvVar: 'thisisatest' } }
};
Expand All @@ -26,6 +27,8 @@ test('create-options:', (t) => {
// Create a copy of process.env to set the properties added by createOptions
// for the deepequal test.
const env = Object.create(process.env);
env['HOME'] = 'homedir';
env['USERPROFILE'] = 'homedir';
env['npm_config_loglevel'] = 'warning';
env['npm_config_tmp'] = 'npm_config_tmp';
env['testenvVar'] = 'thisisatest';
Expand Down

0 comments on commit 5421cf6

Please sign in to comment.