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',
|
'@typescript-eslint/no-unused-vars': 'error',
|
||||||
'no-useless-constructor': 'off',
|
'no-useless-constructor': 'off',
|
||||||
'@typescript-eslint/no-useless-constructor': 'error',
|
'@typescript-eslint/no-useless-constructor': 'error',
|
||||||
|
'no-empty-function': 'off',
|
||||||
|
'@typescript-eslint/no-empty-function': 'error',
|
||||||
},
|
},
|
||||||
overrides: [
|
overrides: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Command } from './command'
|
|||||||
import { Matcher } from './matcher'
|
import { Matcher } from './matcher'
|
||||||
|
|
||||||
export class PatternMatcher {
|
export class PatternMatcher {
|
||||||
protected matcher: RegExp
|
private matcher: RegExp
|
||||||
constructor(pattern: string, args?: string) {
|
constructor(pattern: string, args?: string) {
|
||||||
this.matcher = new RegExp(pattern, args)
|
this.matcher = new RegExp(pattern, args)
|
||||||
}
|
}
|
||||||
@@ -19,16 +19,19 @@ export class CommandMatcher extends PatternMatcher implements Matcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ErrorMatcher extends PatternMatcher implements Matcher {
|
export class ErrorMatcher implements Matcher {
|
||||||
constructor(private errorPattern?: string) {
|
private matcher?: PatternMatcher
|
||||||
super(errorPattern || '')
|
constructor(errorPattern?: string) {
|
||||||
|
if (errorPattern) {
|
||||||
|
this.matcher = new PatternMatcher(errorPattern)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match(command: Partial<Command>): boolean {
|
match(command: Partial<Command>): boolean {
|
||||||
if (!this.errorPattern) {
|
if (!this.matcher) {
|
||||||
return command.error === ''
|
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) {
|
constructor({ command, output, error, pwd }: ICondition) {
|
||||||
if (command) this.matchers.push(new CommandMatcher(command))
|
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))
|
if (pwd) this.matchers.push(new PwdMatcher(pwd))
|
||||||
this.matchers.push(new ErrorMatcher(error)) // always check unexpected error
|
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)
|
expect(matcher.match({ output: 'start\n' })).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
// test('condition match', () => {
|
test('condition match', () => {
|
||||||
// const condition = new Condition({
|
const condition = new Condition({
|
||||||
// command: '^echo start$',
|
command: '^echo start$',
|
||||||
// pwd: '/home/user',
|
})
|
||||||
// })
|
|
||||||
|
|
||||||
// expect(
|
expect(
|
||||||
// condition.match({
|
condition.match({
|
||||||
// command: 'echo start',
|
command: 'echo start',
|
||||||
// pwd: '/home/user',
|
})
|
||||||
// })
|
).toBe(true)
|
||||||
// ).toBe(true)
|
expect(
|
||||||
// expect(
|
condition.match({
|
||||||
// condition.match({
|
command: 'echo hello',
|
||||||
// command: 'echo start',
|
})
|
||||||
// pwd: '/home/user1',
|
).toBe(false)
|
||||||
// })
|
expect(
|
||||||
// ).toBe(false)
|
condition.match({ command: 'echo start', error: 'permission denied' })
|
||||||
// })
|
).toBe(false)
|
||||||
|
})
|
||||||
Reference in new issue
Block a user