Skip to content

Commit 5421cf6

Browse files
authored
lib: redirect homedir for modules being tested (#755)
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)
1 parent 63e2dae commit 5421cf6

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

lib/create-options.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function createOptions(cwd, context) {
1212
if (context.options.gid) options.gid = context.options.gid;
1313
}
1414
options.env = Object.create(process.env);
15+
options.env['HOME'] = context.homeDir;
16+
options.env['USERPROFILE'] = context.homeDir;
1517
options.env['npm_config_loglevel'] = context.options.npmLevel;
1618
options.env['npm_config_tmp'] = context.npmConfigTmp;
1719
options.env['TEMP'] = context.npmConfigTmp;

lib/temp-directory.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ async function create(context) {
2121
context.path
2222
);
2323

24+
context.homeDir = path.join(context.path, 'home');
2425
context.npmConfigTmp = path.join(context.path, 'npm_config_tmp');
2526

27+
await mkdirp(context.homeDir);
2628
await mkdirp(context.npmConfigTmp);
2729
}
2830

test/test-create-options.js

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ test('create-options:', (t) => {
1717
npmLevel: 'warning'
1818
},
1919
emit: function() {},
20+
homeDir: 'homedir',
2021
npmConfigTmp: 'npm_config_tmp',
2122
module: { envVar: { testenvVar: 'thisisatest' } }
2223
};
@@ -26,6 +27,8 @@ test('create-options:', (t) => {
2627
// Create a copy of process.env to set the properties added by createOptions
2728
// for the deepequal test.
2829
const env = Object.create(process.env);
30+
env['HOME'] = 'homedir';
31+
env['USERPROFILE'] = 'homedir';
2932
env['npm_config_loglevel'] = 'warning';
3033
env['npm_config_tmp'] = 'npm_config_tmp';
3134
env['testenvVar'] = 'thisisatest';

0 commit comments

Comments
 (0)