lib/copy/__tests__: extracted preserve-time into own file

This commit is contained in:
JP Richardson 2015-06-26 09:55:20 -05:00
parent 40c59c23e7
commit bbd6c996f7
2 changed files with 61 additions and 45 deletions

View File

@ -0,0 +1,61 @@
var assert = require('assert')
var fs = require('fs')
var os = require('os')
var path = require('path')
var fse = require('../../')
/* global beforeEach, describe, it */
describe('copy', function () {
var TEST_DIR
beforeEach(function (done) {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy')
fse.emptyDir(TEST_DIR, done)
})
describe('> modification option', function () {
var SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures')
var FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
describe('> when modified option is turned off', function () {
it('should have different timestamps on copy', function (done) {
var from = path.join(SRC_FIXTURES_DIR)
var to = path.join(TEST_DIR)
fse.copy(from, to, {preserveTimestamps: false}, function () {
FILES.forEach(testFile({preserveTimestamps: false}))
done()
})
})
})
describe('> when modified option is turned on', function () {
it('should have the same timestamps on copy', function (done) {
var from = path.join(SRC_FIXTURES_DIR)
var to = path.join(TEST_DIR)
fse.copy(from, to, {preserveTimestamps: true}, function () {
FILES.forEach(testFile({preserveTimestamps: true}))
done()
})
})
})
function testFile (options) {
return function (file) {
var a = path.join(SRC_FIXTURES_DIR, file)
var b = path.join(TEST_DIR, file)
var fromStat = fs.statSync(a)
var toStat = fs.statSync(b)
if (options.preserveTimestamps) {
assert.strictEqual(toStat.mtime.getTime(), fromStat.mtime.getTime())
assert.strictEqual(toStat.atime.getTime(), 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
}
}
}
})
})

View File

@ -179,50 +179,5 @@ describe('fs-extra', function () {
})
})
})
describe('> modification option', function () {
var SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures')
var FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
describe('> when modified option is turned off', function () {
it('should have different timestamps on copy', function (done) {
var from = path.join(SRC_FIXTURES_DIR)
var to = path.join(TEST_DIR)
fse.copy(from, to, {preserveTimestamps: false}, function () {
FILES.forEach(testFile({preserveTimestamps: false}))
done()
})
})
})
describe('> when modified option is turned on', function () {
it('should have the same timestamps on copy', function (done) {
var from = path.join(SRC_FIXTURES_DIR)
var to = path.join(TEST_DIR)
fse.copy(from, to, {preserveTimestamps: true}, function () {
FILES.forEach(testFile({preserveTimestamps: true}))
done()
})
})
})
function testFile (options) {
return function (file) {
var a = path.join(SRC_FIXTURES_DIR, file)
var b = path.join(TEST_DIR, file)
var fromStat = fs.statSync(a)
var toStat = fs.statSync(b)
if (options.preserveTimestamps) {
assert.strictEqual(toStat.mtime.getTime(), fromStat.mtime.getTime())
assert.strictEqual(toStat.atime.getTime(), 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
}
}
}
})
})
})