This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
206 lines (181 loc) · 5.88 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { schema, CapabilityError } from '@stencila/executa'
import { Basha } from '.'
// Start a new interpreter before each test
// and stop it after each one to ensure that
// tests do not hang if they fail.
let basha: Basha
beforeEach(() => {
basha = new Basha()
})
afterEach(async () => {
await basha.stop()
})
const chunk = (text: string): schema.CodeChunk =>
schema.codeChunk({
text,
programmingLanguage: 'bash',
})
test('manifest', async () => {
await expect(basha.manifest()).resolves.not.toThrow()
})
describe('executeCode', () => {
// Commands that succeed
test('commands: ok', async () => {
expect(await basha.executeCode('echo "Hello world!"')).toBe('Hello world!')
expect(await basha.executeCode("date --date='@100000000' -u -I")).toBe(
'1973-03-03'
)
})
// Commands that do not have any output
test('commands: no output', async () => {
expect(await basha.executeCode('VAR=value')).toBe(undefined)
expect(await basha.executeCode('sleep 0.1')).toBe(undefined)
})
// Commands that fail
test('commands: bad', async () => {
await expect(basha.executeCode('foo')).rejects.toThrow(
'bash: foo: command not found'
)
await expect(basha.executeCode('cat foo.bar')).rejects.toThrow(
'cat: foo.bar: No such file or directory'
)
})
// Setting and using variables
test('variables', async () => {
expect(await basha.executeCode('VAR1=one')).toBe(undefined)
expect(await basha.executeCode('echo $VAR1')).toBe('one')
})
// Control flow
test('control flow', async () => {
expect(
await basha.executeCode(`for i in 1 2 3
do
echo "Hello $i times"
done`)
).toBe(`for i in 1 2 3
> do
> echo "Hello $i times"
> done
Hello 1 times
Hello 2 times
Hello 3 times`)
})
// Syntax errors
test('syntax errors', async () => {
await expect(basha.executeCode('for')).rejects.toThrow(
'bash: syntax error near unexpected token `newline'
)
})
})
describe('execute', () => {
test('code chunk', async () => {
const chunk = schema.codeChunk({
text: 'echo \'{"a":1}\'',
programmingLanguage: 'bash',
})
const executed = await basha.execute(chunk)
const { outputs, errors } = executed
expect(outputs).toEqual([{ a: 1 }])
expect(errors).toBeUndefined()
})
test('code expression', async () => {
const chunk = schema.codeExpression({
text: 'echo \'{"a":1}\'',
programmingLanguage: 'bash',
})
const executed = await basha.execute(chunk)
const { output, errors } = executed
expect(output).toEqual({ a: 1 })
expect(errors).toBeUndefined()
})
test('parallel code chunks', async () => {
// Should handle parallel requests
// Make two requests to execute
const p1 = basha.execute(
schema.codeChunk({ text: 'VAR=first', programmingLanguage: 'bash' })
)
const p2 = basha.execute(
schema.codeChunk({ text: 'echo $VAR', programmingLanguage: 'bash' })
)
// Wait for them and check that the second ran after the first
await p1
const { outputs } = await p2
expect(outputs).toEqual(['first'])
})
test('errors', async () => {
const chunk = schema.codeChunk({ text: 'foo', programmingLanguage: 'bash' })
const executed = await basha.execute(chunk)
const { outputs, errors } = executed
expect(outputs).toBeUndefined()
expect(errors).toEqual([
schema.codeError({
errorType: 'RuntimeError',
errorMessage: 'bash: foo: command not found',
}),
])
})
test('duration', async () => {
const { duration: duration1 } = await basha.execute(
schema.codeChunk({ text: 'sleep 0.1', programmingLanguage: 'bash' })
)
expect(duration1).toBeGreaterThanOrEqual(0.1)
expect(duration1).toBeLessThanOrEqual(0.2)
const { duration: duration2 } = await basha.execute(
schema.codeChunk({ text: 'sleep 0.2', programmingLanguage: 'bash' })
)
expect(duration2).toBeGreaterThanOrEqual(0.2)
expect(duration2).toBeLessThanOrEqual(0.3)
})
// prettier-ignore
test('incapable', async () => {
await expect(
basha.execute(null)
).rejects.toThrow(CapabilityError)
await expect(
basha.execute(schema.codeChunk({text:''}))
).rejects.toThrow(CapabilityError)
await expect(
basha.execute(schema.codeChunk({text:'', programmingLanguage: 'foo' }))
).rejects.toThrow(CapabilityError)
})
})
describe('cancel', () => {
test('code chunk', async () => {
// Can't cancel anything other than a current request
expect(await basha.cancel('job-random')).toBe(false)
// Set a var to check that we have the same Bash
// session after cancelling
await basha.execute(chunk('VAR=42'))
// Asynchronously execute a chunk that is meant to sleep for a while
const chunk2 = chunk('sleep 30')
basha
.execute(chunk2, undefined, undefined, 'job-2')
.then((chunk) => {
// When it cancels it resolves with an error (due to non-zero exit code)
expect(chunk.outputs).toEqual(['^C'])
})
.catch((error) => console.error(error))
// Wait a tiny amount for the previous chunk to finish
// writing it's input before we cancel it
await new Promise((resolve) => setTimeout(resolve, 10))
// Now cancel the chunk
expect(await basha.cancel('job-2')).toBe(true)
const chunk3 = await basha.execute(
chunk('echo $VAR'),
undefined,
undefined,
'job-3'
)
expect(chunk3.outputs).toEqual([42])
// Can't cancel it because it is already completed
expect(await basha.cancel('job-3')).toBe(false)
})
})
test('exit handling', async () => {
expect(await basha.executeCode('VAR=1')).toEqual(undefined)
expect(await basha.executeCode('echo $VAR')).toEqual(1)
expect(await basha.executeCode('exit')).toEqual('exit')
// Recovers by creating new bash process, but
// the env var is no longer set
expect(await basha.executeCode('echo $VAR')).toEqual('')
})