Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify cross-device move test configuration/logic #1001

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ fs-extra contains hundreds of tests.
- `npm run unit-esm`: runs tests for `fs-extra/esm` exports
- `npm test`: runs the linter and all tests

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.


### Windows

Expand Down
20 changes: 20 additions & 0 deletions lib/move/__tests__/cross-device-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('graceful-fs')
const path = require('path')

const { CROSS_DEVICE_PATH } = process.env
let runCrossDeviceTests = !!CROSS_DEVICE_PATH

if (runCrossDeviceTests) {
// make sure we have permission on device
try {
fs.writeFileSync(path.join(CROSS_DEVICE_PATH, 'file'), 'hi')
} catch {
runCrossDeviceTests = false
throw new Error(`Can't write to device ${CROSS_DEVICE_PATH}`)
}
} else console.log('Skipping cross-device move tests')

module.exports = {
differentDevice: CROSS_DEVICE_PATH,
ifCrossDeviceEnabled: (fn) => runCrossDeviceTests ? fn : fn.skip
}
25 changes: 4 additions & 21 deletions lib/move/__tests__/move-preserve-timestamp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const utimesSync = require('../../util/utimes').utimesMillisSync
const assert = require('assert')
const fse = require('../../index')
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')

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

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

describeIfPractical('move() - across different devices', () => {
let TEST_DIR, SRC, DEST, FILES
let __skipTests = false
const differentDevicePath = '/mnt'

// must set this up, if not, exit silently
if (!fs.existsSync(differentDevicePath)) {
console.log('Skipping cross-device move test')
__skipTests = true
}

// make sure we have permission on device
try {
fs.writeFileSync(path.join(differentDevicePath, 'file'), 'hi')
} catch {
console.log("Can't write to device. Skipping move test.")
__skipTests = true
}

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

const _it = __skipTests ? it.skip : it

afterEach(() => {
fse.removeSync(TEST_DIR)
fse.removeSync(SRC)
})

describe('> default behaviour', () => {
ifCrossDeviceEnabled(describe)('> default behaviour', () => {
;[
{ subcase: 'writable', readonly: false },
{ subcase: 'readonly', readonly: true }
].forEach(params => {
describe(`>> with ${params.subcase} source files`, () => {
beforeEach(() => setupFixture(params.readonly))

_it('should have the same timestamps after move', done => {
it('should have the same timestamps after move', done => {
const originalTimestamps = FILES.map(file => {
const originalPath = path.join(SRC, file)
const originalStat = fs.statSync(originalPath)
Expand Down
25 changes: 4 additions & 21 deletions lib/move/__tests__/move-sync-preserve-timestamp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const utimesSync = require('../../util/utimes').utimesMillisSync
const assert = require('assert')
const fse = require('../../index')
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')

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

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

describeIfPractical('moveSync() - across different devices', () => {
let TEST_DIR, SRC, DEST, FILES
let __skipTests = false
const differentDevicePath = '/mnt'

// must set this up, if not, exit silently
if (!fs.existsSync(differentDevicePath)) {
console.log('Skipping cross-device move test')
__skipTests = true
}

// make sure we have permission on device
try {
fs.writeFileSync(path.join(differentDevicePath, 'file'), 'hi')
} catch {
console.log("Can't write to device. Skipping move test.")
__skipTests = true
}

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

const _it = __skipTests ? it.skip : it

afterEach(() => {
fse.removeSync(TEST_DIR)
fse.removeSync(SRC)
})

describe('> default behaviour', () => {
ifCrossDeviceEnabled(describe)('> default behaviour', () => {
;[
{ subcase: 'writable', readonly: false },
{ subcase: 'readonly', readonly: true }
].forEach(params => {
describe(`>> with ${params.subcase} source files`, () => {
beforeEach(() => setupFixture(params.readonly))

_it('should have the same timestamps after move', () => {
it('should have the same timestamps after move', () => {
const originalTimestamps = FILES.map(file => {
const originalPath = path.join(SRC, file)
const originalStat = fs.statSync(originalPath)
Expand Down
26 changes: 4 additions & 22 deletions lib/move/__tests__/move-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const os = require('os')
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')

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

Expand Down Expand Up @@ -287,29 +288,10 @@ describe('moveSync()', () => {
})
})

describe('> when actually trying to move a folder across devices', () => {
const differentDevice = '/mnt'
let __skipTests = false

// must set this up, if not, exit silently
if (!fs.existsSync(differentDevice)) {
console.log('Skipping cross-device moveSync test')
__skipTests = true
}

// make sure we have permission on device
try {
fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
} catch {
console.log("Can't write to device. Skipping moveSync test.")
__skipTests = true
}

const _it = __skipTests ? it.skip : it

ifCrossDeviceEnabled(describe)('> when actually trying to move a folder across devices', () => {
describe('> just the folder', () => {
_it('should move the folder', () => {
const src = '/mnt/some/weird/dir-really-weird'
it('should move the folder', () => {
const src = path.join(differentDevice, 'some/weird/dir-really-weird')
const dest = path.join(TEST_DIR, 'device-weird')

if (!fs.existsSync(src)) fse.mkdirpSync(src)
Expand Down
26 changes: 4 additions & 22 deletions lib/move/__tests__/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const os = require('os')
const fse = require('../../')
const path = require('path')
const assert = require('assert')
const { differentDevice, ifCrossDeviceEnabled } = require('./cross-device-utils')

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

Expand Down Expand Up @@ -373,29 +374,10 @@ describe('+ move()', () => {
// tested on Linux ubuntu 3.13.0-32-generic #57-Ubuntu SMP i686 i686 GNU/Linux
// this won't trigger a bug on Mac OS X Yosimite with a USB drive (/Volumes)
// see issue #108
describe('> when actually trying to move a folder across devices', () => {
const differentDevice = '/mnt'
let __skipTests = false

// must set this up, if not, exit silently
if (!fs.existsSync(differentDevice)) {
console.log('Skipping cross-device move test')
__skipTests = true
}

// make sure we have permission on device
try {
fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
} catch {
console.log("Can't write to device. Skipping move test.")
__skipTests = true
}

const _it = __skipTests ? it.skip : it

ifCrossDeviceEnabled(describe)('> when actually trying to move a folder across devices', () => {
describe('>> just the folder', () => {
_it('should move the folder', done => {
const src = '/mnt/some/weird/dir-really-weird'
it('should move the folder', done => {
const src = path.join(differentDevice, 'some/weird/dir-really-weird')
const dest = path.join(TEST_DIR, 'device-weird')

if (!fs.existsSync(src)) {
Expand Down