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

Add support for 'onlySpecials' option for symetry #31

Merged
merged 4 commits into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ console.log(`You have ${files.length} text files under your home directory.`)
- __`onlyFIFOs`__: (_boolean_, default: `false`) Include only FIFOs/pipes.
- __`onlyFiles`__: (_boolean_, default: `false`) Include only regular files.
- __`onlySockets`__: (_boolean_, default: `false`) Include only sockets.
- __`onlySpecials`__: (_boolean_, default: `false`) Equivalent to `noDirs`, `noFiles`, and `noSymbolicLinks`.
- __`onlySymbolicLinks`__: (_boolean_, default: `false`) Include only symbolic links.
- __`noBlockDevices`__: (_boolean_, default: `false`) Exclude block devices.
- __`noCharacterDevices`__: (_boolean_, default: `false`) Exclude character devices.
Expand Down
5 changes: 5 additions & 0 deletions src/find-plus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const find = async(params = {}) => {
params.noFIFOs = true
params.noSockets = true
}
if (params.onlySpecials === true) {
params.noDirs = true
params.noFiles = true
params.noSymbolicLinks = true
}

const {
sort = 'breadth',
Expand Down
81 changes: 37 additions & 44 deletions src/test/find-plus.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -181,45 +181,45 @@ describe('find', () => {
})
}

// fifo test
describe('finding FIFOs', () => {
let allFilesCount, fifosCount, nonFIFOsCount
describe('file types', () => {
let server
const symLinkPath = fsPath.join(symLinkDir, 'symLinkA')
const fileAPath = fsPath.join(symLinkDir, 'fileA.txt')

beforeAll(async() => {
tryExec('mkfifo ' + fifoPath)

const allFiles = await find({ root : fifoDir, sort : 'none' })
const fifos = await find({ onlyFIFOs : true, root : fifoDir, sort : 'none' })
const nonFIFOs = await find({ noFIFOs : true, root : fifoDir, sort : 'none' })
server = net.createServer((c) => {})
server.listen(socketAPath, () => {})

allFilesCount = allFiles.length
fifosCount = fifos.length
nonFIFOsCount = nonFIFOs.length
await fs.symlink(fileAPath, symLinkPath)
})

afterAll(async() => {
await fs.rm(fifoPath)
})

test('counts FIFO with all files', () => expect(allFilesCount).toBe(4))

test("'onlyFIFO' counts only FIFO files", () => expect(fifosCount).toBe(1))
await server.close()

test("'noFIFO' skips FIFO files", () => expect(nonFIFOsCount).toBe(3))
})
await fs.rm(symLinkPath)
})

describe('finding sockets', () => {
let server
// fifo test
test('counts FIFO with all files', async() => {
const allFiles = await find({ root : fifoDir, sort : 'none' })
expect(allFiles).toHaveLength(4)
})

beforeAll(() => {
server = net.createServer((c) => {})
server.listen(socketAPath, () => {})
test("'onlyFIFO' counts only FIFO files", async() => {
const fifos = await find({ onlyFIFOs : true, root : fifoDir, sort : 'none' })
expect(fifos).toHaveLength(1)
})

afterAll(async() => {
await server.close()
test("'noFIFO' skips FIFO files", async() => {
const nonFIFOs = await find({ noFIFOs : true, root : fifoDir, sort : 'none' })
expect(nonFIFOs).toHaveLength(3)
})

// socket tests
test('counts Socket with all files', async() => {
const allFiles = await find({ root : socketDirPath })
expect(allFiles).toHaveLength(3) // the root and two files
Expand All @@ -234,35 +234,28 @@ describe('find', () => {
const noSocketFiles = await find({ root : socketDirPath, noSockets : true })
expect(noSocketFiles).toEqual([socketDirPath, fsPath.join(socketDirPath, 'fileA.txt')])
})
})

// symlink test
describe('finding symlinks', () => {
const symLinkPath = fsPath.join(symLinkDir, 'symLinkA')
const fileAPath = fsPath.join(symLinkDir, 'fileA.txt')
let allFilesCount, nonSymLinksCount, symLinksCount

beforeAll(async() => {
await fs.symlink(fileAPath, symLinkPath)

// symlink test
test('counts symbolic links with all files', async() => {
const allFiles = await find({ root : symLinkDir, sort : 'none' })
const symLinks = await find({ onlySymbolicLinks : true, root : symLinkDir, sort : 'none' })
const nonSymLinks = await find({ noSymbolicLinks : true, root : symLinkDir, sort : 'none' })

allFilesCount = allFiles.length
symLinksCount = symLinks.length
nonSymLinksCount = nonSymLinks.length
expect(allFiles).toHaveLength(4)
})

afterAll(async() => {
await fs.rm(symLinkPath)
test("'onlySymbolicLinks' counts only symbolic links", async() => {
const symLinks = await find({ onlySymbolicLinks : true, root : symLinkDir, sort : 'none' })
expect(symLinks).toHaveLength(1)
})

test('counts symbolic links with all files', () => expect(allFilesCount).toBe(4))

test("'onlySymbolicLinks' counts only symbolic links", () => expect(symLinksCount).toBe(1))
test("'noSymbolicLinks' skips symbolic link files", async() => {
const nonSymLinks = await find({ noSymbolicLinks : true, root : symLinkDir, sort : 'none' })
expect(nonSymLinks).toHaveLength(3)
})

test("'noSymbolicLinks' skips symbolic link files", () => expect(nonSymLinksCount).toBe(3))
// onlySpecials test
test("'onlySpecials' ignores dirs, files, and symlinks", async() => {
const onlySpecials = await find({ root : dirDataPath, excludePaths : ['dirA/**'], onlySpecials : true })
expect(onlySpecials).toEqual([fifoPath, socketAPath])
})
})

describe('argument errors', () => {
Expand Down
Loading