Debian Codegraph @

test/dest-times.js

raw symbols SCIP
1 'use strict';
2
3 var fs = require('graceful-fs');
4 var File = require('vinyl');
5 var expect = require('expect');
6 var miss = require('mississippi');
7
8 var vfs = require('../');
9
10 var cleanup = require('./utils/cleanup');
11 var isWindows = require('./utils/is-windows');
12 var testConstants = require('./utils/test-constants');
13
14 var from = miss.from;
15 var pipe = miss.pipe;
16 var concat = miss.concat;
17
18 var inputBase = testConstants.inputBase;
19 var outputBase = testConstants.outputBase;
20 var inputPath = testConstants.inputPath;
21 var outputPath = testConstants.outputPath;
22 var contents = testConstants.contents;
23
24 var clean = cleanup(outputBase);
25
26 describe('.dest() with custom times', function() {
27
28 beforeEach(clean);
29 afterEach(clean);
30
31 it('does not call futimes when no mtime is provided on the vinyl stat', function(done) {
32 // Changing the time of a directory errors in Windows.
33 // Windows is treated as though it does not have permission to make this operation.
34 if (isWindows) {
35 this.skip();
36 return;
37 }
38
39 var earlier = Date.now() - 1001;
40
41 var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
42
43 var file = new File({
44 base: inputBase,
45 path: inputPath,
46 contents: new Buffer(contents),
47 stat: {},
48 });
49
50 function assert() {
51 var stats = fs.lstatSync(outputPath);
52
53 expect(futimesSpy.calls.length).toEqual(0);
54 expect(stats.atime.getTime()).toBeGreaterThan(earlier);
55 expect(stats.mtime.getTime()).toBeGreaterThan(earlier);
56 }
57
58 pipe([
59 from.obj([file]),
60 vfs.dest(outputBase, { cwd: __dirname }),
61 concat(assert),
62 ], done);
63 });
64
65 it('calls futimes when an mtime is provided on the vinyl stat', function(done) {
66 if (isWindows) {
67 this.skip();
68 return;
69 }
70
71 // Use new mtime
72 var mtime = new Date(Date.now() - 2048);
73
74 var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
75
76 var file = new File({
77 base: inputBase,
78 path: inputPath,
79 contents: new Buffer(contents),
80 stat: {
81 mtime: mtime,
82 },
83 });
84
85 function assert() {
86 expect(futimesSpy.calls.length).toEqual(1);
87
88 // Compare args instead of fs.lstats(), since mtime may be drifted in x86 Node.js
89 var mtimeSpy = futimesSpy.calls[0].arguments[2];
90
91 expect(mtimeSpy.getTime())
92 .toEqual(mtime.getTime());
93
94 expect(file.stat.mtime).toEqual(mtime);
95 }
96
97 pipe([
98 from.obj([file]),
99 vfs.dest(outputBase, { cwd: __dirname }),
100 concat(assert),
101 ], done);
102 });
103
104 it('does not call futimes when provided mtime on the vinyl stat is invalid', function(done) {
105 if (isWindows) {
106 this.skip();
107 return;
108 }
109
110 var earlier = Date.now() - 1001;
111
112 var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
113
114 var file = new File({
115 base: inputBase,
116 path: inputPath,
117 contents: new Buffer(contents),
118 stat: {
119 mtime: new Date(undefined),
120 },
121 });
122
123 function assert() {
124 var stats = fs.lstatSync(outputPath);
125
126 expect(futimesSpy.calls.length).toEqual(0);
127 expect(stats.mtime.getTime()).toBeGreaterThan(earlier);
128 }
129
130 pipe([
131 from.obj([file]),
132 vfs.dest(outputBase, { cwd: __dirname }),
133 concat(assert),
134 ], done);
135 });
136
137 it('calls futimes when provided mtime on the vinyl stat is valid but provided atime is invalid', function(done) {
138 if (isWindows) {
139 this.skip();
140 return;
141 }
142
143 // Use new mtime
144 var mtime = new Date(Date.now() - 2048);
145 var invalidAtime = new Date(undefined);
146
147 var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
148
149 var file = new File({
150 base: inputBase,
151 path: inputPath,
152 contents: new Buffer(contents),
153 stat: {
154 atime: invalidAtime,
155 mtime: mtime,
156 },
157 });
158
159 function assert() {
160 expect(futimesSpy.calls.length).toEqual(1);
161
162 var mtimeSpy = futimesSpy.calls[0].arguments[2];
163
164 expect(mtimeSpy.getTime()).toEqual(mtime.getTime());
165 }
166
167 pipe([
168 from.obj([file]),
169 vfs.dest(outputBase, { cwd: __dirname }),
170 concat(assert),
171 ], done);
172 });
173
174 it('writes file atime and mtime using the vinyl stat', function(done) {
175 if (isWindows) {
176 this.skip();
177 return;
178 }
179
180 // Use new atime/mtime
181 var atime = new Date(Date.now() - 2048);
182 var mtime = new Date(Date.now() - 1024);
183
184 var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
185
186 var file = new File({
187 base: inputBase,
188 path: inputPath,
189 contents: new Buffer(contents),
190 stat: {
191 atime: atime,
192 mtime: mtime,
193 },
194 });
195
196 function assert() {
197 expect(futimesSpy.calls.length).toEqual(1);
198
199 var atimeSpy = futimesSpy.calls[0].arguments[1];
200 var mtimeSpy = futimesSpy.calls[0].arguments[2];
201
202 expect(atimeSpy.getTime()).toEqual(atime.getTime());
203 expect(mtimeSpy.getTime()).toEqual(mtime.getTime());
204 expect(file.stat.mtime).toEqual(mtime);
205 expect(file.stat.atime).toEqual(atime);
206 };
207
208 pipe([
209 from.obj([file]),
210 vfs.dest(outputBase, { cwd: __dirname }),
211 concat(assert),
212 ], done);
213 });
214 });