mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add pwdmatcher and update error matcher
This commit is contained in:
1 parent
3b4a808131
commit
9eb08b45f4
3 files changed
+81
-15
No files matched your search
@@ -62,6 +62,8 @@ module.exports = {
|
|||||||
],
|
],
|
||||||
'no-unused-vars': 'off',
|
'no-unused-vars': 'off',
|
||||||
'@typescript-eslint/no-unused-vars': 'error',
|
'@typescript-eslint/no-unused-vars': 'error',
|
||||||
|
'no-useless-constructor': 'off',
|
||||||
|
'@typescript-eslint/no-useless-constructor': 'error',
|
||||||
},
|
},
|
||||||
overrides: [
|
overrides: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,15 +3,12 @@ import { Command } from './command'
|
|||||||
import { Matcher } from './matcher'
|
import { Matcher } from './matcher'
|
||||||
|
|
||||||
export class PatternMatcher {
|
export class PatternMatcher {
|
||||||
protected matcher: RegExp | null
|
protected matcher: RegExp
|
||||||
constructor(pattern?: string, args?: string) {
|
constructor(pattern: string, args?: string) {
|
||||||
this.matcher = pattern ? new RegExp(pattern, args) : null
|
this.matcher = new RegExp(pattern, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
test(input: string): boolean {
|
test(input: string): boolean {
|
||||||
if (!this.matcher) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return this.matcher.test(input)
|
return this.matcher.test(input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,14 +20,40 @@ export class CommandMatcher extends PatternMatcher implements Matcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ErrorMatcher extends PatternMatcher implements Matcher {
|
export class ErrorMatcher extends PatternMatcher implements Matcher {
|
||||||
|
constructor(private errorPattern?: string) {
|
||||||
|
super(errorPattern || '')
|
||||||
|
}
|
||||||
|
|
||||||
match(command: Partial<Command>): boolean {
|
match(command: Partial<Command>): boolean {
|
||||||
if (!this.matcher) {
|
if (!this.errorPattern) {
|
||||||
return command.error === ''
|
return command.error === ''
|
||||||
}
|
}
|
||||||
return !!command.error && this.test(command.error)
|
return !!command.error && this.test(command.error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class PwdMatcher implements Matcher {
|
||||||
|
constructor(private pwd: string) {}
|
||||||
|
|
||||||
|
match(command: Partial<Command>): boolean {
|
||||||
|
return command.pwd === this.pwd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICondition {
|
||||||
|
command?: string
|
||||||
|
output?: string
|
||||||
|
error?: string
|
||||||
|
pwd?: string
|
||||||
|
}
|
||||||
|
|
||||||
export class Condition {
|
export class Condition {
|
||||||
private matchers: Matcher[] = []
|
private matchers: Matcher[] = []
|
||||||
|
|
||||||
|
constructor({ command, output, error, pwd }: ICondition) {
|
||||||
|
if (command) this.matchers.push(new CommandMatcher(command))
|
||||||
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
import { test, expect } from '@jest/globals'
|
import { test, expect } from '@jest/globals'
|
||||||
import { PatternMatcher, CommandMatcher, ErrorMatcher } from '../src/condition'
|
import {
|
||||||
|
PatternMatcher,
|
||||||
|
CommandMatcher,
|
||||||
|
ErrorMatcher,
|
||||||
|
PwdMatcher,
|
||||||
|
Condition,
|
||||||
|
} from '../src/condition'
|
||||||
import { Command } from '../src/command'
|
import { Command } from '../src/command'
|
||||||
|
|
||||||
test('pattern match', () => {
|
test('pattern match', () => {
|
||||||
@@ -22,12 +28,6 @@ test('empty pattern return true', () => {
|
|||||||
expect(matcher.test('echo start')).toBe(true)
|
expect(matcher.test('echo start')).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('empty error return false', () => {
|
|
||||||
const matcher = new ErrorMatcher()
|
|
||||||
expect(matcher.match({ error: '' })).toBe(true)
|
|
||||||
expect(matcher.match({ error: 'permission denied' })).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('command match', () => {
|
test('command match', () => {
|
||||||
const command: Command = {
|
const command: Command = {
|
||||||
command: 'echo start',
|
command: 'echo start',
|
||||||
@@ -40,4 +40,45 @@ test('command match', () => {
|
|||||||
expect(matcher.match(command)).toBe(true)
|
expect(matcher.match(command)).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('condition match', () => {})
|
test('error matcher', () => {
|
||||||
|
const matcher = new ErrorMatcher()
|
||||||
|
expect(matcher.match({ error: '' })).toBe(true)
|
||||||
|
expect(matcher.match({ error: 'permission denied' })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('error matcher with specific error', () => {
|
||||||
|
const matcher = new ErrorMatcher('permission denied')
|
||||||
|
expect(matcher.match({ error: '' })).toBe(false)
|
||||||
|
expect(matcher.match({ error: 'file not found' })).toBe(false)
|
||||||
|
expect(matcher.match({ error: 'cannot open files: permission denied' })).toBe(
|
||||||
|
true
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('pwd matcher', () => {
|
||||||
|
const matcher = new PwdMatcher('/home/user')
|
||||||
|
expect(matcher.match({ pwd: '/home/user' })).toBe(true)
|
||||||
|
expect(matcher.match({ pwd: '/home/user1' })).toBe(false)
|
||||||
|
expect(matcher.match({ pwd: '/home/user/anotherpath' })).toBe(false)
|
||||||
|
expect(matcher.match({ pwd: '/var/home/user' })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
// test('condition match', () => {
|
||||||
|
// const condition = new Condition({
|
||||||
|
// command: '^echo start$',
|
||||||
|
// pwd: '/home/user',
|
||||||
|
// })
|
||||||
|
|
||||||
|
// expect(
|
||||||
|
// condition.match({
|
||||||
|
// command: 'echo start',
|
||||||
|
// pwd: '/home/user',
|
||||||
|
// })
|
||||||
|
// ).toBe(true)
|
||||||
|
// expect(
|
||||||
|
// condition.match({
|
||||||
|
// command: 'echo start',
|
||||||
|
// pwd: '/home/user1',
|
||||||
|
// })
|
||||||
|
// ).toBe(false)
|
||||||
|
// })
|
||||||
Reference in new issue
Block a user