/
copy-preserve-time.test.js
63 lines (54 loc) · 2.33 KB
/
copy-preserve-time.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
'use strict'
const fs = require(process.cwd())
const os = require('os')
const path = require('path')
const copy = require('../copy')
const utimes = require('../../util/utimes')
const assert = require('assert')
const nodeVersion = process.versions.node
const nodeVersionMajor = parseInt(nodeVersion.split('.')[0], 10)
/* global beforeEach, afterEach, describe, it */
if (process.arch === 'ia32') console.warn('32 bit arch; skipping copy timestamp tests')
if (nodeVersionMajor < 8) console.warn(`old node version (v${nodeVersion}); skipping copy timestamp tests`)
const describeIfPractical = (process.arch === 'ia32' || nodeVersionMajor < 8) ? describe.skip : describe
describeIfPractical('copy() - preserve timestamp', () => {
let TEST_DIR, SRC, DEST, FILES
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-preserve-timestamp')
SRC = path.join(TEST_DIR, 'src')
DEST = path.join(TEST_DIR, 'dest')
FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
FILES.forEach(f => fs.ensureFileSync(path.join(SRC, f)))
done()
})
afterEach(done => fs.remove(TEST_DIR, done))
describe('> when timestamp option is true', () => {
it('should have the same timestamps on copy', done => {
copy(SRC, DEST, {preserveTimestamps: true}, () => {
FILES.forEach(testFile({preserveTimestamps: true}))
done()
})
})
})
function testFile (options) {
return function (file) {
const a = path.join(SRC, file)
const b = path.join(DEST, file)
const fromStat = fs.statSync(a)
const toStat = fs.statSync(b)
if (options.preserveTimestamps) {
// https://github.com/nodejs/io.js/issues/2069
if (process.platform !== 'win32') {
assert.strictEqual(toStat.mtime.getTime(), fromStat.mtime.getTime())
assert.strictEqual(toStat.atime.getTime(), fromStat.atime.getTime())
} else {
assert.strictEqual(toStat.mtime.getTime(), utimes.timeRemoveMillis(fromStat.mtime.getTime()))
assert.strictEqual(toStat.atime.getTime(), utimes.timeRemoveMillis(fromStat.atime.getTime()))
}
} else {
assert.notEqual(toStat.mtime.getTime(), fromStat.mtime.getTime())
// the access time might actually be the same, so check only modification time
}
}
}
})