forked from sole/node-zip-folder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
99 lines (73 loc) · 2.33 KB
/
tests.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
var tmp = require('temporary');
var fs = require('fs');
var path = require('path');
var unzip = require('unzip');
var zipFolder = require('./index');
var txtFileName = 'file.txt';
var txtFileContents = 'this is a text file';
var zipFileName = 'archive.zip';
function emptyDirectory(dirName) {
var dirFiles = fs.readdirSync(dirName);
dirFiles.forEach(function(f) {
var entryPath = path.join(dirName, f);
var fileStats = fs.statSync(entryPath);
if(fileStats.isFile()) {
fs.unlink(entryPath);
} else {
emptyDirectory(entryPath);
}
});
}
module.exports = {
setUp: function(callback) {
this.tmpSrcDir = new tmp.Dir();
var writePath = this.tmpSrcDir.path;
fs.writeFileSync(path.join(writePath, txtFileName), txtFileContents);
this.txtFileName = txtFileName;
this.txtFileContents = txtFileContents;
this.tmpZipFile = new tmp.File();
this.tmpZipExtractionDir = new tmp.Dir();
zipFolder(writePath, this.tmpZipFile.path, function() {
callback();
});
},
tearDown: function(callback) {
emptyDirectory(this.tmpSrcDir.path);
this.tmpSrcDir.rmdir();
emptyDirectory(this.tmpZipExtractionDir.path);
this.tmpZipExtractionDir.rmdir();
this.tmpZipFile.unlink();
callback();
},
// Ensure the zip has been created
itCreatesTheZipFile: function(test) {
test.ok(fs.existsSync(this.tmpZipFile.path), 'zip exists');
test.done();
},
// Assume the zip is valid if it can be unzipped
// and the unzipped contents are not empty
theZipFileIsValid: function(test) {
test.expect(1);
var dstPath = this.tmpZipExtractionDir.path;
fs.createReadStream(this.tmpZipFile.path)
.pipe(unzip.Extract({ path: dstPath }))
.on('close', function() {
var dirList = fs.readdirSync(dstPath);
test.ok(dirList.length > 0, 'the zip contains files');
test.done();
});
},
theZipFileContainsTheRightFiles: function(test) {
var dstPath = this.tmpZipExtractionDir.path;
var txtFileName = this.txtFileName;
var txtFilePath = path.join(dstPath, txtFileName);
var txtFileContents = this.txtFileContents;
fs.createReadStream(this.tmpZipFile.path)
.pipe(unzip.Extract({ path: dstPath }))
.on('close', function() {
test.ok(fs.existsSync(txtFilePath), 'txt file exists');
test.equals(fs.readFileSync(txtFilePath), txtFileContents, 'contents are the same we put in');
test.done();
});
}
};