Skip to content

Commit

Permalink
Stub out a bunch of missing fs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Nov 2, 2023
1 parent a94db45 commit b895371
Showing 1 changed file with 350 additions and 11 deletions.
361 changes: 350 additions & 11 deletions test/src/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if (process.platform !== 'ios') {
})
})

test('fs.appendFile', async (t) => {})
test.skip('fs.appendFile', async (t) => { /* Not implemented */ })

if (os.platform() !== 'android' && os.platform() !== 'win32') {
test('fs.chmod', async (t) => {
Expand All @@ -74,7 +74,23 @@ if (process.platform !== 'ios') {
})
}

test('fs.chown', async (t) => {})
test('fs.chown', async (t) => {
await new Promise((resolve, reject) => {
// TODO: Figure out how to generate this.
const testUID = 1001
const testGID = 1002

fs.chown(FIXTURES + 'file.txt', testUID, testGID, (err) => {
if (err) t.fail(err)
fs.stat(FIXTURES + 'file.txt', (err, stats) => {
if (err) t.fail(err)
t.equal(stats.uid, testUID, 'file.txt UID is correct')
t.equal(stats.gid, testGID, 'file.txt GID is correct')
resolve()
})
})
})
})

test('fs.close', async (t) => {
await new Promise((resolve, reject) => {
Expand All @@ -95,7 +111,50 @@ if (process.platform !== 'ios') {
})
})

test('fs.copyFile', async (t) => {})
test('fs.copyFile', async (t) => {
await new Promise((resolve, reject) => {
const src = FIXTURES + 'source.txt'
const dest = FIXTURES + 'destination.txt'
const flags = 0

fs.stat(dest, (err) => {
if (!err) {
fs.unlink(dest, (err) => {
if (err) {
t.fail(`Failed to delete ${dest}`)
reject(err)
return
}
copySrcToFile()
})
} else {
copySrcToFile()
}
})

function copySrcToFile () {
fs.copyFile(src, dest, flags, (err) => {
if (err) {
t.fail(err)
reject(err)
return
}

// Check that the destination file exists
fs.stat(dest, (err) => {
if (err) {
t.fail(`File ${dest} does not exist after copy.`)
reject(err)
return
}

t.pass('destination.txt was created')
resolve()
})
})
}
})
})

test('fs.createReadStream', async (t) => {
if (os.platform() === 'android') {
Expand Down Expand Up @@ -161,11 +220,146 @@ if (process.platform !== 'ios') {
})
})

test('fs.fstat', async (t) => {})
test('fs.lchmod', async (t) => {})
test('fs.lchown', async (t) => {})
test('fs.lutimes', async (t) => {})
test('fs.link', async (t) => {})
test('fs.fstat', async (t) => {
await new Promise((resolve, reject) => {
fs.open(FIXTURES + 'file.txt', 'r', (err, fd) => {
if (err) {
t.fail(`Failed to open file: ${err}`)
reject(err)
return
}

fs.fstat(fd, (err, stats) => {
if (err) {
t.fail(`fstat failed: ${err}`)
reject(err)
return
}

// Ensure we received valid stats
t.ok(stats && typeof stats === 'object', 'Received valid stats')

// Close the file descriptor
fs.close(fd, (err) => {
if (err) {
t.fail(`Failed to close file descriptor: ${err}`)
reject(err)
return
}

resolve()
})
})
})
})
})

test.skip('fs.lchmod', async (t) => { /* Not implemented */ })
test('fs.lchown', async (t) => {
await new Promise((resolve, reject) => {
const originalFile = FIXTURES + 'file.txt'
const symlink = FIXTURES + 'link.txt'

// TODO: set these up
const testUID = 1001
const testGID = 1002

// Create a symbolic link
fs.symlink(originalFile, symlink, (err) => {
if (err) {
t.fail(`Failed to create symbolic link: ${err}`)
reject(err)
return
}

// Use lchown to change ownership of the link
fs.lchown(symlink, testUID, testGID, (err) => {
if (err) {
t.fail(`lchown failed: ${err}`)
reject(err)
return
}

// Check ownership of the symbolic link
fs.lstat(symlink, (err, stats) => {
if (err) {
t.fail(`Failed to get stats for symbolic link: ${err}`)
reject(err)
return
}

t.equal(stats.uid, testUID, 'Symbolic link UID is correct')
t.equal(stats.gid, testGID, 'Symbolic link GID is correct')

// Cleanup: Remove the symbolic link
fs.unlink(symlink, (err) => {
if (err) {
t.fail(`Failed to remove symbolic link: ${err}`)
reject(err)
return
}

resolve()
})
})
})
})
})
})

test.skip('fs.lutimes', async (t) => { /* Not implemented */ })
test('fs.link', async (t) => {
await new Promise((resolve, reject) => {
const originalFile = FIXTURES + 'file.txt'
const hardLink = FIXTURES + 'link.txt'

// Make sure hardLink does not exist before starting the test
fs.unlink(hardLink, (err) => {
if (err && err.code !== 'ENOENT') {
t.fail(`Failed to ensure link does not exist: ${err}`)
reject(err)
return
}

fs.link(originalFile, hardLink, (err) => {
if (err) {
t.fail(`Failed to create hard link: ${err}`)
reject(err)
return
}

fs.stat(hardLink, (err, stats) => {
if (err) {
t.fail(`Failed to stat the hard link: ${err}`)
reject(err)
return
}

fs.stat(originalFile, (err, originalStats) => {
if (err) {
t.fail(`Failed to stat the original file: ${err}`)
reject(err)
return
}

t.equal(stats.ino, originalStats.ino, 'Hard link and original file have the same inode')

fs.unlink(hardLink, (err) => {
if (err) {
t.fail(`Failed to remove hard link: ${err}`)
reject(err)
return
}

resolve()
})
})
})
})
})
})
})

test('fs.lstat', async (t) => {})
if (os.platform() !== 'android') {
test('fs.mkdir', async (t) => {
Expand Down Expand Up @@ -197,9 +391,154 @@ if (process.platform !== 'ios') {
})
})
}
test('fs.open', async (t) => {})
test('fs.opendir', async (t) => {})
test('fs.read', async (t) => {})
test('fs.open', async (t) => {
await new Promise((resolve, reject) => {
const testFile = FIXTURES + 'file.txt'

fs.writeFile(testFile, 'This is a test.', (err) => {
if (err) {
t.fail(`Failed to write to test file: ${err}`)
reject(err)
return
}

fs.open(testFile, 'r', (err, fd) => {
if (err) {
t.fail(`Failed to open file: ${err}`)
reject(err)
return
}

t.ok(fd, 'File descriptor is valid')

fs.close(fd, (err) => {
if (err) {
t.fail(`Failed to close file descriptor: ${err}`)
reject(err)
return
}

fs.unlink(testFile, (err) => {
if (err) {
t.fail(`Failed to remove test file: ${err}`)
reject(err)
return
}

resolve()
})
})
})
})
})
})

test('fs.opendir', async (t) => {
await new Promise((resolve, reject) => {
const testDir = FIXTURES + 'testDir'

fs.mkdir(testDir, (err) => {
if (err && err.code !== 'EEXIST') {
t.fail(`Failed to create test directory: ${err}`)
reject(err)
return
}

fs.opendir(testDir, (err, dir) => {
if (err) {
t.fail(`Failed to open directory: ${err}`)
reject(err)
return
}

t.ok(dir, 'Directory handle is valid')

dir.read((err, dirent) => {
if (err) {
t.fail(`Failed to read directory entries: ${err}`)
reject(err)
return
}

t.equal(dirent, null, 'No directory entries as expected')

dir.close((err) => {
if (err) {
t.fail(`Failed to close directory handle: ${err}`)
reject(err)
return
}

fs.rmdir(testDir, (err) => {
if (err) {
t.fail(`Failed to remove test directory: ${err}`)
reject(err)
return
}

resolve()
})
})
})
})
})
})
})

test('fs.read', async (t) => {
await new Promise((resolve, reject) => {
const testFile = FIXTURES + 'testFile.txt'
const content = 'Hello, fs.read!'

fs.writeFile(testFile, content, (err) => {
if (err) {
t.fail(`Failed to write to test file: ${err}`)
reject(err)
return
}

fs.open(testFile, 'r', (err, fd) => {
if (err) {
t.fail(`Failed to open test file: ${err}`)
reject(err)
return
}

const buffer = Buffer.alloc(content.length)

fs.read(fd, buffer, 0, content.length, null, (err, bytesRead, buffer) => {
if (err) {
t.fail(`Failed to read from test file: ${err}`)
reject(err)
return
}

t.equal(bytesRead, content.length, 'Read the expected number of bytes')
t.equal(buffer.toString(), content, 'Content read matches the expected content')

fs.close(fd, (err) => {
if (err) {
t.fail(`Failed to close test file: ${err}`)
reject(err)
return
}

fs.unlink(testFile, (err) => {
if (err) {
t.fail(`Failed to delete test file: ${err}`)
reject(err)
return
}

resolve()
})
})
})
})
})
})
})

test('fs.readdir', async (t) => {})
test('fs.readFile', async (t) => {
let failed = false
Expand Down

0 comments on commit b895371

Please sign in to comment.