-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest.js
72 lines (66 loc) · 2.32 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var fs = require('fs');
var exec = require('child_process').exec;
var path = require('path');
var testDir = path.resolve(__dirname + '/test-dir');
if (fs.existsSync(testDir)) {
deleteFolderRecursive(testDir);
}
fs.mkdirSync(testDir);
fs.writeFileSync(testDir + '/package.json', '{}');
fs.writeFileSync(testDir + '/index.js', 'console.log(require("~/foo"))');
fs.writeFileSync(testDir + '/foo.js', 'module.exports = "foo";');
console.log('wavy');
exec('node --version', function(err, out) {
var isgnoreNpmInstallDotDot = Number(out.match(/v(\d+)/)[1]) >= 5;
exec('npm install -S ..', { cwd: testDir }, function(err) {
handleError(err);
console.log('√ installs without errors');
exec('node index', { cwd: testDir }, function(err, stdout) {
handleError(err);
if (stdout.trim() !== 'foo') {
handleError(new Error('expected output to be "foo" got ' + stdout.trim()));
}
console.log('√ works after install');
exec('npm install ..', { cwd: testDir }, function(err) {
if (!isgnoreNpmInstallDotDot) handleError(err);
console.log('√ can handle multiple installs');
deleteFolderRecursive(testDir + '/node_modules');
fs.mkdirSync(testDir + '/node_modules/');
fs.writeFileSync(testDir + '/node_modules/~', '123');
exec('npm install ..', { cwd: testDir }, function(err) {
var regex = /^Error: .*[\/\\]node_modules[\/\\]~ is already being used$/m;
if (!regex.test(err.message)) {
handleError(new Error('did not handle improper existing ~'))
}
console.log('√ handles `~` already being used by something else');
console.log('');
console.log('all tests passed!');
deleteFolderRecursive(testDir);
});
});
});
});
});
function handleError(err) {
if (err) {
deleteFolderRecursive(testDir);
console.error(err.stack);
process.exit(1);
}
}
// http://stackoverflow.com/a/12761924
function deleteFolderRecursive(path) {
var files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};