UNPKG

3.73 kBJavaScriptView Raw
1'use strict';
2
3/*global describe,it*/
4
5var assert = require('assert');
6var fs = require('fs');
7var path = require('path');
8var through = require('through2');
9var temp = require('temp').track();
10var vfs = require('vinyl-fs');
11var lib = require('..');
12
13describe('gulp-vinyl-zip', function () {
14 it('src should be able to read from archives', function (cb) {
15 var count = 0;
16
17 lib.src(path.join(__dirname, 'assets', 'archive.zip'))
18 .pipe(through.obj(function(chunk, enc, cb) {
19 count++;
20 cb();
21 }, function () {
22 assert.equal(7, count);
23 cb();
24 }));
25 });
26
27 it('src should be able to read from archives in streams', function (cb) {
28 var count = 0;
29
30 vfs.src(path.join(__dirname, 'assets', '*.zip'))
31 .pipe(lib.src())
32 .pipe(through.obj(function(chunk, enc, cb) {
33 count++;
34 cb();
35 }, function () {
36 assert.equal(7, count);
37 cb();
38 }));
39 });
40
41 it('dest should be able to create an archive from another archive', function (cb) {
42 var dest = temp.openSync('gulp-vinyl-zip-test').path;
43
44 lib.src(path.join(__dirname, 'assets', 'archive.zip'))
45 .pipe(lib.dest(dest))
46 .on('end', function () {
47 assert(fs.existsSync(dest));
48 cb();
49 });
50 });
51
52 it('dest should be able to create an archive\'s directory tree', function (cb) {
53 var dest = temp.mkdirSync('gulp-vinyl-zip-test');
54 var archive = path.join(dest, 'foo', 'bar', 'archive.zip');
55
56 lib.src(path.join(__dirname, 'assets', 'archive.zip'))
57 .pipe(lib.dest(archive))
58 .on('end', function () {
59 assert(fs.existsSync(archive));
60 cb();
61 });
62 });
63
64 it('should be compatible with vinyl-fs', function (cb) {
65 var dest = temp.mkdirSync('gulp-vinyl-zip-test');
66
67 lib.src(path.join(__dirname, 'assets', 'archive.zip'))
68 .pipe(vfs.dest(dest))
69 .on('end', function () {
70 assert(fs.existsSync(dest));
71
72 assert.equal(4, fs.readdirSync(dest).length);
73 cb();
74 });
75 });
76
77 it('dest should preserve stat', function (cb) {
78 var dest = temp.openSync('gulp-vinyl-zip-test').path;
79 var stats = Object.create(null);
80
81 lib.src(path.join(__dirname, 'assets', 'archive.zip'))
82 .pipe(through.obj(function (file, enc, cb) {
83 assert(file.stat);
84 stats[file.path] = file.stat;
85 cb(null, file);
86 }, function (cb) {
87 this.emit('end');
88 cb();
89 }))
90 .pipe(lib.dest(dest))
91 .on('end', function () {
92 var count = 0;
93
94 lib.src(dest)
95 .pipe(through.obj(function (file, enc, cb) {
96 count++;
97
98 if (stats[file.path].atime.valueOf() || file.stat.atime.valueOf()) {
99 assert.equal(stats[file.path].atime.getTime(), file.stat.atime.getTime());
100 }
101
102 if (stats[file.path].ctime.valueOf() || file.stat.ctime.valueOf()) {
103 assert.equal(stats[file.path].ctime.getTime(), file.stat.ctime.getTime());
104 }
105
106 if (stats[file.path].mtime.valueOf() || file.stat.mtime.valueOf()) {
107 assert.equal(stats[file.path].mtime.getTime(), file.stat.mtime.getTime());
108 }
109
110 assert.equal(stats[file.path].isFile(), file.stat.isFile());
111 assert.equal(stats[file.path].isDirectory(), file.stat.isDirectory());
112 assert.equal(stats[file.path].isSymbolicLink(), file.stat.isSymbolicLink());
113
114 cb();
115 }, function () {
116 assert.equal(7, count);
117 cb();
118 }));
119 });
120 });
121
122 it('dest should not assume files have `stat`', function (cb) {
123 var dest = temp.openSync('gulp-vinyl-zip-test').path;
124
125 lib.src(path.join(__dirname, 'assets', 'archive.zip'))
126 .pipe(through.obj(function(chunk, enc, cb) {
127 delete chunk.stat;
128 this.push(chunk);
129 cb();
130 }))
131 .pipe(lib.dest(dest))
132 .on('end', cb);
133 });
134});