-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
repl.ts
97 lines (92 loc) · 2.44 KB
/
repl.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
import { LoadedConfig } from '@tapjs/config'
import t from 'tap'
// use clean env in tests
const originalEnv = { ...process.env }
originalEnv._TAP_REPL = '0'
t.beforeEach(t =>
t.intercept(process, 'env', { value: { ...originalEnv } })
)
t.test('do not run if already in the repl', async t => {
process.env._TAP_REPL = '1'
const errs = t.capture(console, 'error')
const { repl } = (await t.mockImport(
'../dist/repl.js'
)) as typeof import('../dist/repl.js')
await repl([], {} as unknown as LoadedConfig)
t.strictSame(errs.args(), [['you are already in the tap repl']])
t.equal(process.exitCode, 1)
process.exitCode = 0
})
class MockRepl {
parsed?: string
result?: any
running: boolean = false
displayPromptCalled = false
async parseCommand(cmd: string) {
this.parsed = cmd
return this.result
}
repl?: { displayPrompt: () => any }
start() {
this.running = true
this.repl = {
displayPrompt: () => (this.displayPromptCalled = true),
}
}
close() {
this.running = false
}
}
t.test('run a repl with no args', async t => {
const r = new MockRepl()
const logs = t.capture(console, 'log')
const errs = t.capture(console, 'error')
const { repl } = (await t.mockImport('../dist/repl.js', {
'@tapjs/processinfo': {
ProcessInfo: {
load: () => ({}),
},
},
'../dist/repl/index.js': {
Repl: class Repl {
constructor() {
return r
}
},
},
})) as typeof import('../dist/repl.js')
r.result = 'result'
await repl([], { globCwd: 'cwd' } as unknown as LoadedConfig)
t.equal(r.parsed, undefined)
t.equal(r.running, true)
t.strictSame(logs(), [])
t.strictSame(errs(), [])
})
t.test('run a repl with args', async t => {
const r = new MockRepl()
const logs = t.capture(console, 'log')
const errs = t.capture(console, 'error')
const { repl } = (await t.mockImport('../dist/repl.js', {
'@tapjs/processinfo': {
ProcessInfo: {
load: () => ({}),
},
},
'../dist/repl/index.js': {
Repl: class Repl {
constructor() {
return r
}
},
},
})) as typeof import('../dist/repl.js')
r.result = 'result'
await repl(['a', 'b', 'c'], {
globCwd: 'cwd',
} as unknown as LoadedConfig)
t.equal(r.parsed, 'a b c')
t.equal(r.running, true)
t.strictSame(logs.args(), [['result\n']])
t.strictSame(errs(), [])
t.equal(r.displayPromptCalled, true, 'displayed the prompt')
})