Skip to content

Commit 11290b1

Browse files
authored
Merge pull request #31 from zanerock/work-liquid-labs/find-plus/30
Add support for 'onlySpecials' option for symetry
2 parents 6931ad4 + 38b2313 commit 11290b1

File tree

3 files changed

+43
-44
lines changed

3 files changed

+43
-44
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ console.log(`You have ${files.length} text files under your home directory.`)
4747
- __`onlyFIFOs`__: (_boolean_, default: `false`) Include only FIFOs/pipes.
4848
- __`onlyFiles`__: (_boolean_, default: `false`) Include only regular files.
4949
- __`onlySockets`__: (_boolean_, default: `false`) Include only sockets.
50+
- __`onlySpecials`__: (_boolean_, default: `false`) Equivalent to `noDirs`, `noFiles`, and `noSymbolicLinks`.
5051
- __`onlySymbolicLinks`__: (_boolean_, default: `false`) Include only symbolic links.
5152
- __`noBlockDevices`__: (_boolean_, default: `false`) Exclude block devices.
5253
- __`noCharacterDevices`__: (_boolean_, default: `false`) Exclude character devices.

src/find-plus.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const find = async(params = {}) => {
1212
params.noFIFOs = true
1313
params.noSockets = true
1414
}
15+
if (params.onlySpecials === true) {
16+
params.noDirs = true
17+
params.noFiles = true
18+
params.noSymbolicLinks = true
19+
}
1520

1621
const {
1722
sort = 'breadth',

src/test/find-plus.test.mjs

+37-44
Original file line numberDiff line numberDiff line change
@@ -181,45 +181,45 @@ describe('find', () => {
181181
})
182182
}
183183

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

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

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

195-
allFilesCount = allFiles.length
196-
fifosCount = fifos.length
197-
nonFIFOsCount = nonFIFOs.length
195+
await fs.symlink(fileAPath, symLinkPath)
198196
})
199197

200198
afterAll(async() => {
201199
await fs.rm(fifoPath)
202-
})
203-
204-
test('counts FIFO with all files', () => expect(allFilesCount).toBe(4))
205200

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

208-
test("'noFIFO' skips FIFO files", () => expect(nonFIFOsCount).toBe(3))
209-
})
203+
await fs.rm(symLinkPath)
204+
})
210205

211-
describe('finding sockets', () => {
212-
let server
206+
// fifo test
207+
test('counts FIFO with all files', async() => {
208+
const allFiles = await find({ root : fifoDir, sort : 'none' })
209+
expect(allFiles).toHaveLength(4)
210+
})
213211

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

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

222+
// socket tests
223223
test('counts Socket with all files', async() => {
224224
const allFiles = await find({ root : socketDirPath })
225225
expect(allFiles).toHaveLength(3) // the root and two files
@@ -234,35 +234,28 @@ describe('find', () => {
234234
const noSocketFiles = await find({ root : socketDirPath, noSockets : true })
235235
expect(noSocketFiles).toEqual([socketDirPath, fsPath.join(socketDirPath, 'fileA.txt')])
236236
})
237-
})
238-
239-
// symlink test
240-
describe('finding symlinks', () => {
241-
const symLinkPath = fsPath.join(symLinkDir, 'symLinkA')
242-
const fileAPath = fsPath.join(symLinkDir, 'fileA.txt')
243-
let allFilesCount, nonSymLinksCount, symLinksCount
244-
245-
beforeAll(async() => {
246-
await fs.symlink(fileAPath, symLinkPath)
247237

238+
// symlink test
239+
test('counts symbolic links with all files', async() => {
248240
const allFiles = await find({ root : symLinkDir, sort : 'none' })
249-
const symLinks = await find({ onlySymbolicLinks : true, root : symLinkDir, sort : 'none' })
250-
const nonSymLinks = await find({ noSymbolicLinks : true, root : symLinkDir, sort : 'none' })
251-
252-
allFilesCount = allFiles.length
253-
symLinksCount = symLinks.length
254-
nonSymLinksCount = nonSymLinks.length
241+
expect(allFiles).toHaveLength(4)
255242
})
256243

257-
afterAll(async() => {
258-
await fs.rm(symLinkPath)
244+
test("'onlySymbolicLinks' counts only symbolic links", async() => {
245+
const symLinks = await find({ onlySymbolicLinks : true, root : symLinkDir, sort : 'none' })
246+
expect(symLinks).toHaveLength(1)
259247
})
260248

261-
test('counts symbolic links with all files', () => expect(allFilesCount).toBe(4))
262-
263-
test("'onlySymbolicLinks' counts only symbolic links", () => expect(symLinksCount).toBe(1))
249+
test("'noSymbolicLinks' skips symbolic link files", async() => {
250+
const nonSymLinks = await find({ noSymbolicLinks : true, root : symLinkDir, sort : 'none' })
251+
expect(nonSymLinks).toHaveLength(3)
252+
})
264253

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

268261
describe('argument errors', () => {

0 commit comments

Comments
 (0)