mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add stage condition checks
This commit is contained in:
1 parent
77fe2d37b3
commit
5bea2ea78c
3 files changed
+60
-13
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { IStage, ICommand } from './types'
|
||||
import { IStage, ICommand, IFileExistenceChecker } from './types'
|
||||
import { Condition } from './condition/Condition'
|
||||
|
||||
export class Stage {
|
||||
@@ -12,7 +12,10 @@ export class Stage {
|
||||
this.condition = new Condition(condition)
|
||||
}
|
||||
|
||||
satisfies(command: ICommand): boolean {
|
||||
return this.condition.match(command)
|
||||
async satisfies(
|
||||
command: ICommand,
|
||||
checker: IFileExistenceChecker
|
||||
): Promise<boolean> {
|
||||
return this.condition.match(command) && this.condition.check(checker)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { IFileExistenceChecker, FileType, FileInput } from '../src/types.js'
|
||||
import { checkFiles } from '../src/condition/FileMatcher.js'
|
||||
import { Condition } from '../src/condition/Condition.js'
|
||||
|
||||
class MockFileChecker implements IFileExistenceChecker {
|
||||
export class MockFileChecker implements IFileExistenceChecker {
|
||||
private files: FileInput[] = [
|
||||
{
|
||||
path: '/home/user/hello.txt',
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { test, expect } from '@jest/globals'
|
||||
import { Stage } from '../src/Stage'
|
||||
import { FileType } from '../src/types'
|
||||
import { MockFileChecker } from './asyncCondition.test'
|
||||
|
||||
test('check stage condition', () => {
|
||||
const checker = new MockFileChecker()
|
||||
|
||||
test('check stage condition', async () => {
|
||||
const stage = new Stage({
|
||||
id: 'stage1',
|
||||
name: 'List the files using `ls`',
|
||||
@@ -11,15 +15,55 @@ test('check stage condition', () => {
|
||||
},
|
||||
})
|
||||
expect(
|
||||
stage.satisfies({
|
||||
command: 'ls',
|
||||
pwd: '/home/user',
|
||||
})
|
||||
await stage.satisfies(
|
||||
{
|
||||
command: 'ls',
|
||||
pwd: '/home/user',
|
||||
},
|
||||
checker
|
||||
)
|
||||
).toBe(true)
|
||||
expect(
|
||||
stage.satisfies({
|
||||
command: 'ls',
|
||||
pwd: '/home/user/Downloads',
|
||||
})
|
||||
await stage.satisfies(
|
||||
{
|
||||
command: 'ls',
|
||||
pwd: '/home/user/Downloads',
|
||||
},
|
||||
checker
|
||||
)
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
test('check stage with async condition truthy', async () => {
|
||||
const stage = new Stage({
|
||||
id: 'stage1',
|
||||
name: 'Create file',
|
||||
condition: {
|
||||
files: [
|
||||
{
|
||||
path: '/home/user/hello.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
expect(await stage.satisfies({}, checker)).toBe(true)
|
||||
})
|
||||
|
||||
test('check stage with async condition falsy', async () => {
|
||||
const stage = new Stage({
|
||||
id: 'stage1',
|
||||
name: 'Create file',
|
||||
condition: {
|
||||
files: [
|
||||
{
|
||||
path: '/home/user/not-exists.txt',
|
||||
type: FileType.FILE,
|
||||
exists: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
expect(await stage.satisfies({}, checker)).toBe(false)
|
||||
})
|
||||
Reference in new issue
Block a user