Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/util/__tests__/stat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('../..')
const os = require('os')
const path = require('path')
const assert = require('assert')
const proxyquire = require('proxyquire')
const stat = require('../stat.js')

/* global beforeEach, afterEach, describe, it */
Expand Down Expand Up @@ -66,13 +67,41 @@ describe('util/stat', () => {

describe('should get correct identical result when stat.ino or stat.dev is 0n', () => {
it('stat.areIdentical', () => {
assert.ok(stat.areIdentical({ ino: 0n, dev: 0n }, { ino: 0n, dev: 0n }))
assert.ok(!stat.areIdentical({ ino: 0n, dev: 0n }, { ino: 0n, dev: 0n }))
assert.ok(stat.areIdentical({ ino: 1n, dev: 0n }, { ino: 1n, dev: 0n }))
assert.ok(stat.areIdentical({ ino: 0n, dev: 1n }, { ino: 0n, dev: 1n }))
assert.ok(!stat.areIdentical({ ino: 0n, dev: 1n }, { ino: 0n, dev: 1n }))
assert.ok(stat.areIdentical({ ino: 1n, dev: 1n }, { ino: 1n, dev: 1n }))
assert.ok(!stat.areIdentical({ ino: 2n, dev: 0n }, { ino: 1n, dev: 0n }))
assert.ok(!stat.areIdentical({ ino: 0n, dev: 2n }, { ino: 0n, dev: 1n }))
assert.ok(!stat.areIdentical({ ino: 2n, dev: 1n }, { ino: 1n, dev: 2n }))
})
})

describe('when the filesystem reports 0n for every inode', () => {
const zeroInode = st => Object.assign(st, { ino: 0n })
const statZero = proxyquire('../stat.js', {
'../fs': {
lstat: (file, opts) => fs.lstat(file, opts).then(zeroInode),
lstatSync: (file, opts) => zeroInode(fs.lstatSync(file, opts))
}
})

it('stat.checkPaths() does not treat different files as the same', async () => {
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
const { destStat } = await statZero.checkPaths(src, dest, 'move', {})
assert.strictEqual(destStat.ino, 0n)
})

it('stat.checkPathsSync() does not treat different files as the same', () => {
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fs.ensureFileSync(src)
fs.ensureFileSync(dest)
const { destStat } = statZero.checkPathsSync(src, dest, 'move', {})
assert.strictEqual(destStat.ino, 0n)
})
})
})
3 changes: 2 additions & 1 deletion lib/util/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {

function areIdentical (srcStat, destStat) {
// stat.dev can be 0n on windows when node version >= 22.x.x
return destStat.ino !== undefined && destStat.dev !== undefined && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev
// stat.ino is 0n on filesystems without inode numbers, so it cannot identify a file
return !!destStat.ino && destStat.dev !== undefined && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev
}

// return true if dest is a subdir of src, otherwise false.
Expand Down