Skip to content

Commit

Permalink
Remove copy test fixtures dir, refactor copy & copySync timestamp test
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed Dec 15, 2017
1 parent 80494ee commit 2ce4bff
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 65 deletions.
26 changes: 10 additions & 16 deletions lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ if (nodeVersionMajor < 8) console.warn(`old node version (v${nodeVersion}); skip
const describeIfPractical = (process.arch === 'ia32' || nodeVersionMajor < 8) ? describe.skip : describe

describeIfPractical('copySync() - preserveTimestamps option', () => {
let TEST_DIR, src, dest
let TEST_DIR, SRC, DEST, FILES

beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-preserve-time')
fs.emptyDir(TEST_DIR, done)
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))

const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]

describe('> when preserveTimestamps option is false', () => {
it('should have different timestamps on copy', done => {
src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')
FILES.forEach(f => fs.ensureFileSync(path.join(src, f)))

setTimeout(() => {
fs.copySync(src, dest, {preserveTimestamps: false})
fs.copySync(SRC, DEST, {preserveTimestamps: false})
FILES.forEach(testFile({preserveTimestamps: false}))
done()
}, 100)
Expand All @@ -43,19 +41,15 @@ describeIfPractical('copySync() - preserveTimestamps option', () => {

describe('> when preserveTimestamps option is true', () => {
it('should have the same timestamps on copy', () => {
src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')
FILES.forEach(f => fs.ensureFileSync(path.join(src, f)))

fs.copySync(src, dest, {preserveTimestamps: true})
fs.copySync(SRC, DEST, {preserveTimestamps: true})
FILES.forEach(testFile({preserveTimestamps: true}))
})
})

function testFile (options) {
return function (file) {
const a = path.join(src, file)
const b = path.join(dest, 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const assert = require('assert')

/* global afterEach, beforeEach, describe, it */

describe('copy / gh #89', () => {
describe('copy() - gh #89', () => {
const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-gh-89')

beforeEach(done => {
Expand All @@ -22,7 +22,7 @@ describe('copy / gh #89', () => {
fse.remove(TEST_DIR, done)
})

it('should...', done => {
it('should copy successfully', done => {
const A = path.join(TEST_DIR, 'A')
const B = path.join(TEST_DIR, 'B')
fs.mkdirSync(A)
Expand Down
88 changes: 44 additions & 44 deletions lib/copy/__tests__/copy-preserve-time.test.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
'use strict'

const fs = require('fs')
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, describe, it */
/* 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 describeIf64 = process.arch === 'ia32' ? describe.skip : describe
const describeIfPractical = (process.arch === 'ia32' || nodeVersionMajor < 8) ? describe.skip : describe

describeIf64('copy', () => {
let TEST_DIR
describeIfPractical('copy() - preserve timestamp', () => {
let TEST_DIR, SRC, DEST, FILES

beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy')
require(process.cwd()).emptyDir(TEST_DIR, 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()
})

describe('> modification option', () => {
const SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures')
const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
afterEach(done => fs.remove(TEST_DIR, done))

describe('> when modified option is turned off', () => {
it('should have different timestamps on copy', done => {
const from = path.join(SRC_FIXTURES_DIR)
const to = path.join(TEST_DIR)

copy(from, to, {preserveTimestamps: false}, () => {
describe('> when timestamp option is false', () => {
it('should have different timestamps on copy', done => {
setTimeout(() => {
copy(SRC, DEST, {preserveTimestamps: false}, () => {
FILES.forEach(testFile({preserveTimestamps: false}))
done()
})
})
}, 100)
})
})

describe('> when modified option is turned on', () => {
it('should have the same timestamps on copy', done => {
const from = path.join(SRC_FIXTURES_DIR)
const to = path.join(TEST_DIR)

copy(from, to, {preserveTimestamps: true}, () => {
FILES.forEach(testFile({preserveTimestamps: true}))
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_FIXTURES_DIR, file)
const b = path.join(TEST_DIR, 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()))
}
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.notEqual(toStat.mtime.getTime(), fromStat.mtime.getTime())
// the access time might actually be the same, so check only modification time
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
}
}
})
}
})
1 change: 0 additions & 1 deletion lib/copy/__tests__/fixtures/a-file

This file was deleted.

1 change: 0 additions & 1 deletion lib/copy/__tests__/fixtures/a-folder/another-file

This file was deleted.

1 change: 0 additions & 1 deletion lib/copy/__tests__/fixtures/a-folder/another-folder/file3

This file was deleted.

0 comments on commit 2ce4bff

Please sign in to comment.