Skip to content

Commit 055e0b8

Browse files
authoredMar 16, 2023
Unify cross-device move test configuration/logic (#1001)
1 parent 0e7de32 commit 055e0b8

6 files changed

+38
-86
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ fs-extra contains hundreds of tests.
227227
- `npm run unit-esm`: runs tests for `fs-extra/esm` exports
228228
- `npm test`: runs the linter and all tests
229229

230+
When running unit tests, set the environment variable `CROSS_DEVICE_PATH` to the absolute path of an empty directory on another device (like a thumb drive) to enable cross-device move tests.
231+
230232

231233
### Windows
232234

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const fs = require('graceful-fs')
2+
const path = require('path')
3+
4+
const { CROSS_DEVICE_PATH } = process.env
5+
let runCrossDeviceTests = !!CROSS_DEVICE_PATH
6+
7+
if (runCrossDeviceTests) {
8+
// make sure we have permission on device
9+
try {
10+
fs.writeFileSync(path.join(CROSS_DEVICE_PATH, 'file'), 'hi')
11+
} catch {
12+
runCrossDeviceTests = false
13+
throw new Error(`Can't write to device ${CROSS_DEVICE_PATH}`)
14+
}
15+
} else console.log('Skipping cross-device move tests')
16+
17+
module.exports = {
18+
differentDevice: CROSS_DEVICE_PATH,
19+
ifCrossDeviceEnabled: (fn) => runCrossDeviceTests ? fn : fn.skip
20+
}

‎lib/move/__tests__/move-preserve-timestamp.test.js

+4-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require('path')
66
const utimesSync = require('../../util/utimes').utimesMillisSync
77
const assert = require('assert')
88
const fse = require('../../index')
9+
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')
910

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

@@ -15,26 +16,10 @@ const describeIfPractical = process.arch === 'ia32' ? describe.skip : describe
1516

1617
describeIfPractical('move() - across different devices', () => {
1718
let TEST_DIR, SRC, DEST, FILES
18-
let __skipTests = false
19-
const differentDevicePath = '/mnt'
20-
21-
// must set this up, if not, exit silently
22-
if (!fs.existsSync(differentDevicePath)) {
23-
console.log('Skipping cross-device move test')
24-
__skipTests = true
25-
}
26-
27-
// make sure we have permission on device
28-
try {
29-
fs.writeFileSync(path.join(differentDevicePath, 'file'), 'hi')
30-
} catch {
31-
console.log("Can't write to device. Skipping move test.")
32-
__skipTests = true
33-
}
3419

3520
function setupFixture (readonly) {
3621
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-sync-preserve-timestamp')
37-
SRC = path.join(differentDevicePath, 'some/weird/dir-really-weird')
22+
SRC = path.join(differentDevice, 'some/weird/dir-really-weird')
3823
DEST = path.join(TEST_DIR, 'dest')
3924
FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
4025
const timestamp = Date.now() / 1000 - 5
@@ -50,22 +35,20 @@ describeIfPractical('move() - across different devices', () => {
5035
})
5136
}
5237

53-
const _it = __skipTests ? it.skip : it
54-
5538
afterEach(() => {
5639
fse.removeSync(TEST_DIR)
5740
fse.removeSync(SRC)
5841
})
5942

60-
describe('> default behaviour', () => {
43+
ifCrossDeviceEnabled(describe)('> default behaviour', () => {
6144
;[
6245
{ subcase: 'writable', readonly: false },
6346
{ subcase: 'readonly', readonly: true }
6447
].forEach(params => {
6548
describe(`>> with ${params.subcase} source files`, () => {
6649
beforeEach(() => setupFixture(params.readonly))
6750

68-
_it('should have the same timestamps after move', done => {
51+
it('should have the same timestamps after move', done => {
6952
const originalTimestamps = FILES.map(file => {
7053
const originalPath = path.join(SRC, file)
7154
const originalStat = fs.statSync(originalPath)

‎lib/move/__tests__/move-sync-preserve-timestamp.test.js

+4-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require('path')
66
const utimesSync = require('../../util/utimes').utimesMillisSync
77
const assert = require('assert')
88
const fse = require('../../index')
9+
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')
910

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

@@ -15,26 +16,10 @@ const describeIfPractical = process.arch === 'ia32' ? describe.skip : describe
1516

1617
describeIfPractical('moveSync() - across different devices', () => {
1718
let TEST_DIR, SRC, DEST, FILES
18-
let __skipTests = false
19-
const differentDevicePath = '/mnt'
20-
21-
// must set this up, if not, exit silently
22-
if (!fs.existsSync(differentDevicePath)) {
23-
console.log('Skipping cross-device move test')
24-
__skipTests = true
25-
}
26-
27-
// make sure we have permission on device
28-
try {
29-
fs.writeFileSync(path.join(differentDevicePath, 'file'), 'hi')
30-
} catch {
31-
console.log("Can't write to device. Skipping move test.")
32-
__skipTests = true
33-
}
3419

3520
function setupFixture (readonly) {
3621
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-sync-preserve-timestamp')
37-
SRC = path.join(differentDevicePath, 'some/weird/dir-really-weird')
22+
SRC = path.join(differentDevice, 'some/weird/dir-really-weird')
3823
DEST = path.join(TEST_DIR, 'dest')
3924
FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
4025
const timestamp = Date.now() / 1000 - 5
@@ -50,22 +35,20 @@ describeIfPractical('moveSync() - across different devices', () => {
5035
})
5136
}
5237

53-
const _it = __skipTests ? it.skip : it
54-
5538
afterEach(() => {
5639
fse.removeSync(TEST_DIR)
5740
fse.removeSync(SRC)
5841
})
5942

60-
describe('> default behaviour', () => {
43+
ifCrossDeviceEnabled(describe)('> default behaviour', () => {
6144
;[
6245
{ subcase: 'writable', readonly: false },
6346
{ subcase: 'readonly', readonly: true }
6447
].forEach(params => {
6548
describe(`>> with ${params.subcase} source files`, () => {
6649
beforeEach(() => setupFixture(params.readonly))
6750

68-
_it('should have the same timestamps after move', () => {
51+
it('should have the same timestamps after move', () => {
6952
const originalTimestamps = FILES.map(file => {
7053
const originalPath = path.join(SRC, file)
7154
const originalStat = fs.statSync(originalPath)

‎lib/move/__tests__/move-sync.test.js

+4-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const os = require('os')
77
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
10+
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')
1011

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

@@ -287,29 +288,10 @@ describe('moveSync()', () => {
287288
})
288289
})
289290

290-
describe('> when actually trying to move a folder across devices', () => {
291-
const differentDevice = '/mnt'
292-
let __skipTests = false
293-
294-
// must set this up, if not, exit silently
295-
if (!fs.existsSync(differentDevice)) {
296-
console.log('Skipping cross-device moveSync test')
297-
__skipTests = true
298-
}
299-
300-
// make sure we have permission on device
301-
try {
302-
fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
303-
} catch {
304-
console.log("Can't write to device. Skipping moveSync test.")
305-
__skipTests = true
306-
}
307-
308-
const _it = __skipTests ? it.skip : it
309-
291+
ifCrossDeviceEnabled(describe)('> when actually trying to move a folder across devices', () => {
310292
describe('> just the folder', () => {
311-
_it('should move the folder', () => {
312-
const src = '/mnt/some/weird/dir-really-weird'
293+
it('should move the folder', () => {
294+
const src = path.join(differentDevice, 'some/weird/dir-really-weird')
313295
const dest = path.join(TEST_DIR, 'device-weird')
314296

315297
if (!fs.existsSync(src)) fse.mkdirpSync(src)

‎lib/move/__tests__/move.test.js

+4-22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const os = require('os')
55
const fse = require('../../')
66
const path = require('path')
77
const assert = require('assert')
8+
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')
89

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

@@ -373,29 +374,10 @@ describe('+ move()', () => {
373374
// tested on Linux ubuntu 3.13.0-32-generic #57-Ubuntu SMP i686 i686 GNU/Linux
374375
// this won't trigger a bug on Mac OS X Yosimite with a USB drive (/Volumes)
375376
// see issue #108
376-
describe('> when actually trying to move a folder across devices', () => {
377-
const differentDevice = '/mnt'
378-
let __skipTests = false
379-
380-
// must set this up, if not, exit silently
381-
if (!fs.existsSync(differentDevice)) {
382-
console.log('Skipping cross-device move test')
383-
__skipTests = true
384-
}
385-
386-
// make sure we have permission on device
387-
try {
388-
fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
389-
} catch {
390-
console.log("Can't write to device. Skipping move test.")
391-
__skipTests = true
392-
}
393-
394-
const _it = __skipTests ? it.skip : it
395-
377+
ifCrossDeviceEnabled(describe)('> when actually trying to move a folder across devices', () => {
396378
describe('>> just the folder', () => {
397-
_it('should move the folder', done => {
398-
const src = '/mnt/some/weird/dir-really-weird'
379+
it('should move the folder', done => {
380+
const src = path.join(differentDevice, 'some/weird/dir-really-weird')
399381
const dest = path.join(TEST_DIR, 'device-weird')
400382

401383
if (!fs.existsSync(src)) {

0 commit comments

Comments
 (0)
Please sign in to comment.