-
Notifications
You must be signed in to change notification settings - Fork 774
/
utimes.test.js
110 lines (91 loc) · 3.38 KB
/
utimes.test.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
'use strict'
const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')
const proxyquire = require('proxyquire')
let gracefulFsStub
let utimes
/* global beforeEach, describe, it */
describe('utimes', () => {
let TEST_DIR
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'utimes')
fse.emptyDir(TEST_DIR, done)
// reset stubs
gracefulFsStub = {}
utimes = proxyquire('../utimes', { 'graceful-fs': gracefulFsStub })
})
describe('hasMillisResSync()', () => {
it('should return a boolean indicating whether it has support', () => {
const res = utimes.hasMillisResSync()
assert.strictEqual(typeof res, 'boolean')
// HFS => false
if (process.platform === 'darwin') assert.strictEqual(res, false)
// does anyone use FAT anymore?
// if (process.platform === 'win32') assert.strictEqual(res, true)
// fails on appveyor... could appveyor be using FAT?
// this would fail if ext2/ext3
if (process.platform === 'linux') assert.strictEqual(res, true)
})
})
describe('timeRemoveMills()', () => {
it('should remove millisecond precision from a timestamp', () => {
const ts = 1334990868773
const ets = 1334990868000
assert.strictEqual(utimes.timeRemoveMillis(ts), ets)
assert.strictEqual(utimes.timeRemoveMillis(new Date(ts)).getTime(), ets)
})
})
describe('utimesMillis()', () => {
// see discussion https://github.com/jprichardson/node-fs-extra/pull/141
it('should set the utimes w/ millisecond precision', done => {
const tmpFile = path.join(TEST_DIR, 'someFile')
fs.writeFileSync(tmpFile, 'hello')
let stats = fs.lstatSync(tmpFile)
// Apr 21st, 2012
const awhileAgo = new Date(1334990868773)
const awhileAgoNoMillis = new Date(1334990868000)
assert.notDeepStrictEqual(stats.mtime, awhileAgo)
assert.notDeepStrictEqual(stats.atime, awhileAgo)
utimes.utimesMillis(tmpFile, awhileAgo, awhileAgo, err => {
assert.ifError(err)
stats = fs.statSync(tmpFile)
if (utimes.hasMillisResSync()) {
assert.deepStrictEqual(stats.mtime, awhileAgo)
assert.deepStrictEqual(stats.atime, awhileAgo)
} else {
assert.deepStrictEqual(stats.mtime, awhileAgoNoMillis)
assert.deepStrictEqual(stats.atime, awhileAgoNoMillis)
}
done()
})
})
it('should close open file desciptors after encountering an error', done => {
const fakeFd = Math.random()
gracefulFsStub.open = (pathIgnored, flagsIgnored, modeIgnored, callback) => {
if (typeof modeIgnored === 'function') callback = modeIgnored
process.nextTick(() => callback(null, fakeFd))
}
let closeCalled = false
gracefulFsStub.close = (fd, callback) => {
assert.strictEqual(fd, fakeFd)
closeCalled = true
if (callback) process.nextTick(callback)
}
let testError
gracefulFsStub.futimes = (fd, atimeIgnored, mtimeIgnored, callback) => {
process.nextTick(() => {
testError = new Error('A test error')
callback(testError)
})
}
utimes.utimesMillis('ignored', 'ignored', 'ignored', err => {
assert.strictEqual(err, testError)
assert(closeCalled)
done()
})
})
})
})