Skip to content

Commit

Permalink
lib: redirect tmpdir for modules being tested
Browse files Browse the repository at this point in the history
The test for some modules write into `os.tmpdir()` and do not always
clean up afterwards which can fill the temporary directory on our CI.
Redirect the temporary directory for modules being tested into CITGM's
`tmpDir` which gets removed at the end of the run.

Refs: nodejs/build#1757
  • Loading branch information
richardlau committed Apr 13, 2019
1 parent 75068e9 commit 4974854
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/create-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function createOptions(cwd, context) {
options.env = Object.create(process.env);
options.env['npm_loglevel'] = context.options.npmLevel;
options.env['npm_config_tmp'] = context.npmConfigTmp;
options.env['TEMP'] = context.npmConfigTmp;
options.env['TMP'] = context.npmConfigTmp;
options.env['TMPDIR'] = context.npmConfigTmp;

if (context.options.nodedir) {
const nodedir = path.resolve(process.cwd(), context.options.nodedir);
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/omg-i-write-to-tmpdir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "omg-i-write-to-tmpdir",
"version": "1.0.0",
"description": "Test suite writes to tmpdir",
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/citgm.git"
},
"main": "index.js",
"scripts": {
"test": "node test.js",
"install": "exit 0"
},
"keywords": [
"always",
"passes"
],
"license": "MIT"
}
7 changes: 7 additions & 0 deletions test/fixtures/omg-i-write-to-tmpdir/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const { join } = require('path');
const { tmpdir } = require('os');
const { writeFileSync } = require('fs');

writeFileSync(join(tmpdir(), 'omg-i-write-to-tmpdir-testfile'));
32 changes: 31 additions & 1 deletion test/npm/test-npm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ const badTemp = path.join(sandbox, 'omg-i-do-not-support-testing');
const scriptsFixtures = path.join(fixtures, 'omg-i-pass-with-scripts');
const scriptsTemp = path.join(sandbox, 'omg-i-pass-with-scripts');

const writeTmpdirFixtures = path.join(fixtures, 'omg-i-write-to-tmpdir');
const writeTmpdirTemp = path.join(sandbox, 'omg-i-write-to-tmpdir');

let packageManagers;

test('npm-test: setup', (t) => {
t.plan(13);
t.plan(16);
packageManager.getPackageManagers((e, res) => {
packageManagers = res;
t.error(e);
Expand All @@ -57,6 +60,11 @@ test('npm-test: setup', (t) => {
t.ok(fs.existsSync(path.join(scriptsTemp, 'package.json')));
t.ok(fs.existsSync(path.join(scriptsTemp, 'test.js')));
});
ncp(writeTmpdirFixtures, writeTmpdirTemp, (e) => {
t.error(e);
t.ok(fs.existsSync(path.join(writeTmpdirTemp, 'package.json')));
t.ok(fs.existsSync(path.join(writeTmpdirTemp, 'test.js')));
});
});
});

Expand Down Expand Up @@ -163,6 +171,28 @@ test('npm-test: module with scripts passing', (t) => {
});
});

test('npm-test: tmpdir is redirected', (t) => {
const context = makeContext.npmContext(
'omg-i-write-to-tmpdir',
packageManagers,
sandbox,
{
npmLevel: 'silly'
}
);
context.npmConfigTmp = writeTmpdirTemp;
packageManagerTest('npm', context, (err) => {
t.error(err);
t.ok(
fs.existsSync(
path.join(writeTmpdirTemp, 'omg-i-write-to-tmpdir-testfile')
),
'Temporary file is written into the redirected temporary directory'
);
t.end();
});
});

test('npm-test: teardown', (t) => {
rimraf(sandbox, (err) => {
t.error(err);
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 @@ -28,6 +28,9 @@ test('create-options:', (t) => {
env['npm_loglevel'] = 'warning';
env['npm_config_tmp'] = 'npm_config_tmp';
env['testenvVar'] = 'thisisatest';
env['TEMP'] = 'npm_config_tmp';
env['TMP'] = 'npm_config_tmp';
env['TMPDIR'] = 'npm_config_tmp';
// Set dynamically to support Windows.
env['npm_config_nodedir'] = path.resolve(process.cwd(), nodePath);

Expand Down
32 changes: 31 additions & 1 deletion test/yarn/test-yarn-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ const badTemp = path.join(sandbox, 'omg-i-do-not-support-testing');
const scriptsFixtures = path.join(fixtures, 'omg-i-pass-with-scripts');
const scriptsTemp = path.join(sandbox, 'omg-i-pass-with-scripts');

const writeTmpdirFixtures = path.join(fixtures, 'omg-i-write-to-tmpdir');
const writeTmpdirTemp = path.join(sandbox, 'omg-i-write-to-tmpdir');

let packageManagers;

test('yarn-test: setup', (t) => {
t.plan(13);
t.plan(16);
packageManager.getPackageManagers((e, res) => {
packageManagers = res;
t.error(e);
Expand All @@ -57,6 +60,11 @@ test('yarn-test: setup', (t) => {
t.ok(fs.existsSync(path.join(scriptsTemp, 'package.json')));
t.ok(fs.existsSync(path.join(scriptsTemp, 'test.js')));
});
ncp(writeTmpdirFixtures, writeTmpdirTemp, (e) => {
t.error(e);
t.ok(fs.existsSync(path.join(writeTmpdirTemp, 'package.json')));
t.ok(fs.existsSync(path.join(writeTmpdirTemp, 'test.js')));
});
});
});

Expand Down Expand Up @@ -158,6 +166,28 @@ test('yarn-test: module with scripts passing', (t) => {
});
});

test('yarn-test: tmpdir is redirected', (t) => {
const context = makeContext.npmContext(
'omg-i-write-to-tmpdir',
packageManagers,
sandbox,
{
npmLevel: 'silly'
}
);
context.npmConfigTmp = writeTmpdirTemp;
packageManagerTest('npm', context, (err) => {
t.error(err);
t.ok(
fs.existsSync(
path.join(writeTmpdirTemp, 'omg-i-write-to-tmpdir-testfile')
),
'Temporary file is written into the redirected temporary directory'
);
t.end();
});
});

test('yarn-test: teardown', (t) => {
rimraf(sandbox, (err) => {
t.error(err);
Expand Down

0 comments on commit 4974854

Please sign in to comment.