mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: condition matcher
This commit is contained in:
1 parent
cb26e1dcb0
commit
4f3752060d
3 files changed
+37
-26
No files matched your search
@@ -64,6 +64,8 @@ module.exports = {
|
||||
'@typescript-eslint/no-unused-vars': 'error',
|
||||
'no-useless-constructor': 'off',
|
||||
'@typescript-eslint/no-useless-constructor': 'error',
|
||||
'no-empty-function': 'off',
|
||||
'@typescript-eslint/no-empty-function': 'error',
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Command } from './command'
|
||||
import { Matcher } from './matcher'
|
||||
|
||||
export class PatternMatcher {
|
||||
protected matcher: RegExp
|
||||
private matcher: RegExp
|
||||
constructor(pattern: string, args?: string) {
|
||||
this.matcher = new RegExp(pattern, args)
|
||||
}
|
||||
@@ -19,16 +19,19 @@ export class CommandMatcher extends PatternMatcher implements Matcher {
|
||||
}
|
||||
}
|
||||
|
||||
export class ErrorMatcher extends PatternMatcher implements Matcher {
|
||||
constructor(private errorPattern?: string) {
|
||||
super(errorPattern || '')
|
||||
export class ErrorMatcher implements Matcher {
|
||||
private matcher?: PatternMatcher
|
||||
constructor(errorPattern?: string) {
|
||||
if (errorPattern) {
|
||||
this.matcher = new PatternMatcher(errorPattern)
|
||||
}
|
||||
}
|
||||
|
||||
match(command: Partial<Command>): boolean {
|
||||
if (!this.errorPattern) {
|
||||
return command.error === ''
|
||||
if (!this.matcher) {
|
||||
return !command.error
|
||||
}
|
||||
return !!command.error && this.test(command.error)
|
||||
return this.matcher.test(command.error || '')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,8 +61,14 @@ export class Condition {
|
||||
|
||||
constructor({ command, output, error, pwd }: ICondition) {
|
||||
if (command) this.matchers.push(new CommandMatcher(command))
|
||||
// if (output) this.matchers.push(new OutputMatcher(output))
|
||||
if (output) this.matchers.push(new OutputMatcher(output))
|
||||
if (pwd) this.matchers.push(new PwdMatcher(pwd))
|
||||
this.matchers.push(new ErrorMatcher(error)) // always check unexpected error
|
||||
}
|
||||
|
||||
match(command: Partial<Command>): boolean {
|
||||
return this.matchers.every((matcher) => {
|
||||
return matcher.match(command)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -71,22 +71,22 @@ test('output matcher', () => {
|
||||
expect(matcher.match({ output: 'start\n' })).toBe(true)
|
||||
})
|
||||
|
||||
// test('condition match', () => {
|
||||
// const condition = new Condition({
|
||||
// command: '^echo start$',
|
||||
// pwd: '/home/user',
|
||||
// })
|
||||
test('condition match', () => {
|
||||
const condition = new Condition({
|
||||
command: '^echo start$',
|
||||
})
|
||||
|
||||
// expect(
|
||||
// condition.match({
|
||||
// command: 'echo start',
|
||||
// pwd: '/home/user',
|
||||
// })
|
||||
// ).toBe(true)
|
||||
// expect(
|
||||
// condition.match({
|
||||
// command: 'echo start',
|
||||
// pwd: '/home/user1',
|
||||
// })
|
||||
// ).toBe(false)
|
||||
// })
|
||||
expect(
|
||||
condition.match({
|
||||
command: 'echo start',
|
||||
})
|
||||
).toBe(true)
|
||||
expect(
|
||||
condition.match({
|
||||
command: 'echo hello',
|
||||
})
|
||||
).toBe(false)
|
||||
expect(
|
||||
condition.match({ command: 'echo start', error: 'permission denied' })
|
||||
).toBe(false)
|
||||
})
|
||||
Reference in new issue
Block a user