forked from claudiajs/claudia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipdir-spec.js
76 lines (72 loc) · 2.4 KB
/
zipdir-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
const fs = require('fs'),
os = require('os'),
path = require('path'),
tmppath = require('../src/util/tmppath'),
fsUtil = require('../src/util/fs-util'),
trimSlash = require('../src/util/trimslash'),
childProcess = require('child_process'),
underTest = require('../src/tasks/zipdir');
describe('zipdir', () => {
'use strict';
let workingdir, zipfile, pwd;
beforeEach(() => {
workingdir = tmppath();
fs.mkdirSync(workingdir);
pwd = process.cwd();
zipfile = false;
});
afterEach(() => {
process.chdir(pwd);
fsUtil.silentRemove(workingdir);
if (zipfile) {
fsUtil.silentRemove(zipfile);
}
});
it('rejects if the path does not exist', done => {
const argpath = tmppath();
underTest(argpath).then(done.fail, reason => {
expect(reason).toEqual(argpath + ' does not exist');
done();
});
});
it('rejects if the path is not a dir', done => {
const filePath = path.join(workingdir, 'root.txt');
fs.writeFileSync(filePath, 'text1', 'utf8');
underTest(filePath).then(done.fail, (reason) => {
expect(reason).toEqual(filePath + ' is not a directory');
done();
});
});
it('zips up files and subfolders into a temporary path', done => {
const original = path.join(workingdir, 'original'),
unpacked = path.join(workingdir, 'unpacked');
fs.mkdirSync(original);
fs.writeFileSync(path.join(original, 'root.txt'), 'text1', 'utf8');
fs.mkdirSync(path.join(original, 'subdir'));
fs.writeFileSync(path.join(original, 'subdir', 'sub.txt'), 'text2', 'utf8');
underTest(original).then(argpath => {
zipfile = argpath;
fs.mkdirSync(unpacked);
return new Promise((resolve, reject) => {
childProcess.execFile('unzip', [argpath], {cwd: unpacked, env: process.env}, (error) => {
if (error) {
return reject(error);
}
resolve();
});
});
}).then(() => {
expect(trimSlash(path.dirname(zipfile))).toEqual(trimSlash(os.tmpdir()));
expect(fs.readFileSync(path.join(unpacked, 'root.txt'), 'utf8')).toEqual('text1');
expect(fs.readFileSync(path.join(unpacked, 'subdir', 'sub.txt'), 'utf8')).toEqual('text2');
}).then(done, done.fail);
});
it('removes the original dir if successful', done => {
const original = path.join(workingdir, 'original');
fs.mkdirSync(original);
fs.writeFileSync(path.join(original, 'root.txt'), 'text1', 'utf8');
underTest(original).then(() => {
expect(fs.existsSync(original)).toBeFalsy();
}).then(done, done.fail);
});
});