mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: buildFileCheckCmd
This commit is contained in:
1 parent
b7ecfe3980
commit
af27780763
3 files changed
+55
No files matched your search
@@ -0,0 +1,16 @@
|
|||||||
|
const fileTypes = {
|
||||||
|
file: '-f',
|
||||||
|
folder: '-d',
|
||||||
|
link: '-L',
|
||||||
|
socket: '-S',
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line import/prefer-default-export
|
||||||
|
export function buildFileCheckCmd(files) {
|
||||||
|
return files
|
||||||
|
.map(
|
||||||
|
({ path, type, exists }) =>
|
||||||
|
`${exists ? '' : '! '}test ${fileTypes[type] || '-e'} ${path}`
|
||||||
|
)
|
||||||
|
.join(' && ')
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { buildFileCheckCmd } from './commands.js'
|
||||||
|
|
||||||
|
describe('buildFileCheckCmd function', () => {
|
||||||
|
it('builds the file check command correctly', () => {
|
||||||
|
const testcases = [
|
||||||
|
{
|
||||||
|
input: [{ path: '/etc/passwd', type: 'file', exists: true }],
|
||||||
|
output: 'test -f /etc/passwd',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: [{ path: '/etc', type: 'folder', exists: false }],
|
||||||
|
output: '! test -d /etc',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: [
|
||||||
|
{ path: '/etc/passwd', type: 'file', exists: true },
|
||||||
|
{ path: '/etc', type: 'folder', exists: false },
|
||||||
|
],
|
||||||
|
output: 'test -f /etc/passwd && ! test -d /etc',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: [
|
||||||
|
{ path: '/tmp/socket', type: 'socket', exists: true },
|
||||||
|
{ path: '/tmp/link', type: 'link', exists: false },
|
||||||
|
],
|
||||||
|
output: 'test -S /tmp/socket && ! test -L /tmp/link',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: [{ path: '/unknown', type: 'unknown', exists: true }],
|
||||||
|
output: 'test -e /unknown',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
testcases.forEach(({ input, output }) => {
|
||||||
|
expect(buildFileCheckCmd(input)).toBe(output)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1 +1,2 @@
|
|||||||
export { isValidEmail, isValidUsername } from './auth.js'
|
export { isValidEmail, isValidUsername } from './auth.js'
|
||||||
|
export { buildFileCheckCmd } from './commands.js'
|
||||||
Reference in new issue
Block a user