mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add file matcher
This commit is contained in:
1 parent
b5e20766a3
commit
d612b2d6af
4 files changed
+123
No files matched your search
@@ -66,6 +66,8 @@ module.exports = {
|
||||
'@typescript-eslint/no-useless-constructor': 'error',
|
||||
'no-empty-function': 'off',
|
||||
'@typescript-eslint/no-empty-function': 'error',
|
||||
'no-shadow': 'off',
|
||||
'@typescript-eslint/no-shadow': 'error',
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { FileInput, IFileExistenceChecker } from '../types'
|
||||
|
||||
interface FileExistenceInput extends FileInput {
|
||||
exists: boolean
|
||||
}
|
||||
|
||||
export async function checkFiles(
|
||||
checker: IFileExistenceChecker,
|
||||
files: FileExistenceInput[]
|
||||
) {
|
||||
const existences = await Promise.all(
|
||||
files.map((file) => checker.exists(file))
|
||||
)
|
||||
return existences.every((exists, index) => exists === files[index].exists)
|
||||
}
|
||||
@@ -13,3 +13,17 @@ export interface ICondition {
|
||||
or?: ICondition[]
|
||||
not?: ICondition
|
||||
}
|
||||
|
||||
export enum FileType {
|
||||
FILE = 'file',
|
||||
FOLDER = 'folder',
|
||||
}
|
||||
|
||||
export interface FileInput {
|
||||
path: string
|
||||
type: FileType
|
||||
}
|
||||
|
||||
export interface IFileExistenceChecker {
|
||||
exists(file: FileInput): Promise<boolean>
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { test, expect } from '@jest/globals'
|
||||
import { IFileExistenceChecker, FileType, FileInput } from '../src/types.js'
|
||||
import { checkFiles } from '../src/condition/FileMatcher.js'
|
||||
|
||||
class MockFileChecker implements IFileExistenceChecker {
|
||||
private files: FileInput[] = [
|
||||
{
|
||||
path: '/home/user/hello.txt',
|
||||
type: FileType.FILE,
|
||||
},
|
||||
{
|
||||
path: '/home/user/world.txt',
|
||||
type: FileType.FILE,
|
||||
},
|
||||
{
|
||||
path: '/home/user/Downloads',
|
||||
type: FileType.FOLDER,
|
||||
},
|
||||
]
|
||||
|
||||
async exists(file: FileInput): Promise<boolean> {
|
||||
return this.files.some((f) => f.path === file.path && f.type === file.type)
|
||||
}
|
||||
}
|
||||
|
||||
test('file matcher', async () => {
|
||||
const checker = new MockFileChecker()
|
||||
expect(
|
||||
await checkFiles(checker, [
|
||||
{
|
||||
path: '/home/user/hello.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
])
|
||||
).toBe(true)
|
||||
expect(
|
||||
await checkFiles(checker, [
|
||||
{
|
||||
path: '/home/user/not-exists.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
])
|
||||
).toBe(false)
|
||||
expect(
|
||||
await checkFiles(checker, [
|
||||
{
|
||||
path: '/home/user/not-exists.txt',
|
||||
type: FileType.FILE,
|
||||
exists: false,
|
||||
},
|
||||
])
|
||||
).toBe(true)
|
||||
expect(
|
||||
await checkFiles(checker, [
|
||||
{
|
||||
path: '/home/user/Downloads',
|
||||
type: FileType.FOLDER,
|
||||
exists: true,
|
||||
},
|
||||
])
|
||||
).toBe(true)
|
||||
expect(
|
||||
await checkFiles(checker, [
|
||||
{
|
||||
path: '/home/user/hello.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/Downloads',
|
||||
type: FileType.FOLDER,
|
||||
exists: true,
|
||||
},
|
||||
])
|
||||
).toBe(true)
|
||||
expect(
|
||||
await checkFiles(checker, [
|
||||
{
|
||||
path: '/home/user/hello.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
{
|
||||
path: '/home/user/not-exist.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
])
|
||||
).toBe(false)
|
||||
})
|
||||
Reference in new issue
Block a user