forked from claudiajs/claudia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack-spec.js
138 lines (137 loc) · 6.28 KB
/
pack-spec.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const underTest = require('../src/commands/pack'),
os = require('os'),
path = require('path'),
fs = require('fs'),
ArrayLogger = require('../src/util/array-logger'),
fsPromise = require('../src/util/fs-promise'),
decompress = require('decompress'),
fsUtil = require('../src/util/fs-util');
describe('pack', () => {
'use strict';
let pwd, workingdir, logger, unpackPath;
beforeEach(done => {
pwd = process.cwd();
logger = new ArrayLogger();
fsPromise.mkdtempAsync(os.tmpdir() + path.sep)
.then(dir => {
workingdir = path.resolve(dir);
fsUtil.copy(path.join(__dirname, 'test-projects', 'optional-dependencies'), workingdir);
unpackPath = path.join(workingdir, 'unpack');
fs.mkdirSync(unpackPath);
process.chdir(workingdir);
})
.then(done, done.fail);
});
afterEach(() => {
process.chdir(pwd);
fsUtil.rmDir(workingdir);
});
it('fails if the source folder is same as os tmp folder', done => {
underTest({source: os.tmpdir()}, logger)
.then(done.fail, message => expect(message).toEqual('Source directory is the Node temp directory. Cowardly refusing to fill up disk with recursive copy.'))
.then(done);
});
it('fails if local dependencies and optional dependencies are mixed', done => {
underTest({'use-local-dependencies': true, 'optional-dependencies': false}, logger)
.then(done.fail, message => expect(message).toEqual('incompatible arguments --use-local-dependencies and --no-optional-dependencies'))
.then(done);
});
it('fails if the source directory is not a node project', done => {
underTest({source: workingdir}, logger)
.then(done.fail, message => expect(message).toEqual('package.json does not exist in the source folder'))
.then(done);
});
it('fails if the default output file already exists', done => {
const archivePath = path.join(workingdir, 'optional-dependencies', 'echo-1.0.0.zip');
process.chdir(path.join(workingdir, 'optional-dependencies'));
fs.writeFileSync(archivePath, 'exists', 'utf8');
underTest({}, logger)
.then(done.fail, message => expect(message).toMatch(/echo-1\.0\.0\.zip already exists\. Use --force to overwrite it\.$/))
.then(() => expect(fs.readFileSync(archivePath, 'utf8')).toEqual('exists'))
.then(done);
});
it('fails if the specified output file already exists', done => {
const archivePath = path.join(workingdir, 'echo.zip');
fs.writeFileSync(archivePath, 'exists', 'utf8');
underTest({source: path.join(workingdir, 'optional-dependencies'), output: archivePath}, logger)
.then(done.fail, message => expect(message).toMatch(/echo\.zip already exists. Use --force to overwrite it\.$/))
.then(() => expect(fs.readFileSync(archivePath, 'utf8')).toEqual('exists'))
.then(done);
});
it('packs a project with all the production dependencies', done => {
process.chdir(path.join(workingdir, 'optional-dependencies'));
underTest({}, logger)
.then(result => decompress(result.output, unpackPath))
.then(() => {
expect(fsUtil.isFile(path.join(unpackPath, 'package.json'))).toBeTruthy();
expect(fsUtil.isFile(path.join(unpackPath, 'main.js'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'aws-sdk'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'huh'))).toBeTruthy();
})
.then(done, done.fail);
});
it('packs a project without optional dependencies if requested', done => {
process.chdir(path.join(workingdir, 'optional-dependencies'));
underTest({'optional-dependencies': false}, logger)
.then(result => decompress(result.output, unpackPath))
.then(() => {
expect(fsUtil.isFile(path.join(unpackPath, 'package.json'))).toBeTruthy();
expect(fsUtil.isFile(path.join(unpackPath, 'main.js'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'aws-sdk'))).toBeFalsy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'huh'))).toBeTruthy();
})
.then(done, done.fail);
});
it('packs a project from a source dir', done => {
process.chdir(path.join(workingdir));
underTest({'source': path.join(workingdir, 'optional-dependencies'), 'optional-dependencies': false}, logger)
.then(result => decompress(result.output, unpackPath))
.then(() => {
expect(fsUtil.isFile(path.join(unpackPath, 'package.json'))).toBeTruthy();
expect(fsUtil.isFile(path.join(unpackPath, 'main.js'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'aws-sdk'))).toBeFalsy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'huh'))).toBeTruthy();
})
.then(done, done.fail);
});
it('packs a local project to a default file name', done => {
process.chdir(path.join(workingdir, 'optional-dependencies'));
underTest({}, logger)
.then(result => {
expect(path.basename(result.output)).toEqual('echo-1.0.0.zip');
expect(fsUtil.isFile(result.output)).toBeTruthy();
})
.then(done, done.fail);
});
it('uses the specified file name', done => {
process.chdir(path.join(workingdir, 'optional-dependencies'));
underTest({output: 'xx.zip'}, logger)
.then(result => {
expect(path.basename(result.output)).toEqual('xx.zip');
expect(path.dirname(result.output)).toEqual(path.resolve(process.cwd()));
expect(fsUtil.isFile(result.output)).toBeTruthy();
})
.then(done, done.fail);
});
it('overwrites an existing file if force is set', done => {
process.chdir(path.join(workingdir, 'optional-dependencies'));
fs.writeFileSync('xx.zip', 'exists', 'utf8');
underTest({output: 'xx.zip', force: true, 'optional-dependencies': false}, logger)
.then(result => {
expect(path.basename(result.output)).toEqual('xx.zip');
expect(path.basename(result.output)).toEqual('xx.zip');
return decompress(result.output, unpackPath);
})
.then(() => {
expect(fsUtil.isFile(path.join(unpackPath, 'package.json'))).toBeTruthy();
expect(fsUtil.isFile(path.join(unpackPath, 'main.js'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules'))).toBeTruthy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'aws-sdk'))).toBeFalsy();
expect(fsUtil.isDir(path.join(unpackPath, 'node_modules', 'huh'))).toBeTruthy();
})
.then(done, done.fail);
});
});