|
2 | 2 | // It will load go misc files in
|
3 | 3 | // /usr/local/go/misc/wasm
|
4 | 4 |
|
5 |
| -import {describe, it, expect, afterAll, beforeAll} from 'vitest' |
| 5 | +import { describe, it, expect, afterAll, beforeAll } from 'vitest' |
6 | 6 |
|
7 | 7 | import '../wasm_exec_node.cjs'
|
8 | 8 | import '../wasm_exec.cjs'
|
9 |
| -import {CLI, RunConfig} from '../cli' |
| 9 | +import { CLI, RunConfig } from '../cli' |
10 | 10 | import * as fs from 'fs'
|
11 |
| -import {Go} from "../wasm_exec"; |
12 |
| -import {loadWasmBinary} from "./utils"; |
| 11 | +import { Go } from '../wasm_exec' |
| 12 | +import { loadWasmBinary } from './utils' |
13 | 13 |
|
14 | 14 | const CLI_PACKAGE = 'scw'
|
15 | 15 | const CLI_CALLBACK = 'cliLoaded'
|
16 | 16 |
|
17 |
| -const emptyConfig = ( |
18 |
| - override?: { |
19 |
| - jwt?: string, |
20 |
| - defaultProjectID?: string, |
21 |
| - defaultOrganizationID?: string, |
22 |
| - apiUrl?: string |
23 |
| - } |
24 |
| -): RunConfig => ({ |
25 |
| - jwt: override?.jwt || "", |
26 |
| - defaultProjectID: override?.defaultProjectID || "", |
27 |
| - defaultOrganizationID: override?.defaultOrganizationID || "", |
28 |
| - apiUrl: override?.apiUrl || "" |
| 17 | +const emptyConfig = (override?: { |
| 18 | + jwt?: string |
| 19 | + defaultProjectID?: string |
| 20 | + defaultOrganizationID?: string |
| 21 | + apiUrl?: string |
| 22 | +}): RunConfig => ({ |
| 23 | + jwt: override?.jwt || '', |
| 24 | + defaultProjectID: override?.defaultProjectID || '', |
| 25 | + defaultOrganizationID: override?.defaultOrganizationID || '', |
| 26 | + apiUrl: override?.apiUrl || '', |
29 | 27 | })
|
30 | 28 |
|
31 | 29 | describe('With wasm CLI', async () => {
|
32 |
| - let cli: CLI |
33 |
| - |
34 |
| - beforeAll(async () => { |
35 |
| - // @ts-ignore |
36 |
| - cli = await loadWasmBinary('./cli.wasm') as CLI |
37 |
| - }) |
38 |
| - |
39 |
| - const run = async (expected: string | RegExp, command: string[], runCfg: RunConfig | null = null) => { |
40 |
| - if (runCfg === null) { |
41 |
| - runCfg = emptyConfig() |
42 |
| - } |
43 |
| - |
44 |
| - const resp = await cli.run(runCfg, command) |
45 |
| - expect(resp.exitCode).toBe(0) |
46 |
| - expect(resp.stdout).toMatch(expected) |
| 30 | + let cli: CLI |
| 31 | + |
| 32 | + beforeAll(async () => { |
| 33 | + // @ts-ignore |
| 34 | + cli = (await loadWasmBinary('./cli.wasm')) as CLI |
| 35 | + }) |
| 36 | + |
| 37 | + const run = async ( |
| 38 | + expected: string | RegExp, |
| 39 | + command: string[], |
| 40 | + runCfg: RunConfig | null = null, |
| 41 | + ) => { |
| 42 | + if (runCfg === null) { |
| 43 | + runCfg = emptyConfig() |
47 | 44 | }
|
48 | 45 |
|
49 |
| - const runWithError = async (expected: string | RegExp, command: string[], runCfg: RunConfig | null = null) => { |
50 |
| - if (runCfg === null) { |
51 |
| - runCfg = emptyConfig() |
52 |
| - } |
53 |
| - const resp = await cli.run(runCfg, command) |
54 |
| - expect(resp.exitCode).toBeGreaterThan(0) |
55 |
| - expect(resp.stderr).toMatch(expected) |
| 46 | + const resp = await cli.run(runCfg, command) |
| 47 | + expect(resp.exitCode).toBe(0) |
| 48 | + expect(resp.stdout).toMatch(expected) |
| 49 | + } |
| 50 | + |
| 51 | + const runWithError = async ( |
| 52 | + expected: string | RegExp, |
| 53 | + command: string[], |
| 54 | + runCfg: RunConfig | null = null, |
| 55 | + ) => { |
| 56 | + if (runCfg === null) { |
| 57 | + runCfg = emptyConfig() |
56 | 58 | }
|
57 |
| - |
58 |
| - const complete = async (expected: string[], command: string[], runCfg: RunConfig | null = null) => { |
59 |
| - if (runCfg === null) { |
60 |
| - runCfg = emptyConfig() |
61 |
| - } |
62 |
| - let toComplete = command.pop() || "" |
63 |
| - |
64 |
| - const suggestions = await cli.complete({ |
65 |
| - jwt: runCfg.jwt, |
66 |
| - leftWords: command, |
67 |
| - rightWords: [], |
68 |
| - selectedWord: toComplete |
69 |
| - }) |
70 |
| - expected.forEach(suggestion => expect(suggestions).toContain(suggestion)) |
| 59 | + const resp = await cli.run(runCfg, command) |
| 60 | + expect(resp.exitCode).toBeGreaterThan(0) |
| 61 | + expect(resp.stderr).toMatch(expected) |
| 62 | + } |
| 63 | + |
| 64 | + const complete = async ( |
| 65 | + expected: string[], |
| 66 | + command: string[], |
| 67 | + runCfg: RunConfig | null = null, |
| 68 | + ) => { |
| 69 | + if (runCfg === null) { |
| 70 | + runCfg = emptyConfig() |
71 | 71 | }
|
| 72 | + let toComplete = command.pop() || '' |
72 | 73 |
|
73 |
| - it('can run cli commands', async () => run(/profile.*default/, ['info'])) |
| 74 | + const suggestions = await cli.complete({ |
| 75 | + jwt: runCfg.jwt, |
| 76 | + leftWords: command, |
| 77 | + rightWords: [], |
| 78 | + selectedWord: toComplete, |
| 79 | + }) |
| 80 | + expected.forEach(suggestion => expect(suggestions).toContain(suggestion)) |
| 81 | + } |
74 | 82 |
|
75 |
| - it('can run help', async () => runWithError(/USAGE:\n.*scw <command>.*/, [])) |
| 83 | + it('can run cli commands', async () => run(/profile.*default/, ['info'])) |
76 | 84 |
|
77 |
| - it('can use jwt', async () => runWithError(/.*denied authentication.*invalid JWT.*/, ['instance', 'server', 'list'])) |
| 85 | + it('can run help', async () => runWithError(/USAGE:\n.*scw <command>.*/, [])) |
78 | 86 |
|
79 |
| - it('can complete', async () => complete(['server', 'image', 'volume'], ['instance', ''])) |
| 87 | + it('can use jwt', async () => |
| 88 | + runWithError(/.*denied authentication.*invalid JWT.*/, [ |
| 89 | + 'instance', |
| 90 | + 'server', |
| 91 | + 'list', |
| 92 | + ])) |
80 | 93 |
|
81 |
| - it('can configure terminal size', async () => { |
82 |
| - const runCfg = emptyConfig() |
| 94 | + it('can complete', async () => |
| 95 | + complete(['server', 'image', 'volume'], ['instance', ''])) |
83 | 96 |
|
84 |
| - await cli.configureOutput({width: 100, color: false}) |
85 |
| - const resp = await cli.run(runCfg, ['marketplace', 'image', 'list']) |
86 |
| - expect(resp.exitCode).toBe(0) |
| 97 | + it('can configure terminal size', async () => { |
| 98 | + const runCfg = emptyConfig() |
87 | 99 |
|
88 |
| - const lines = resp.stdout.split("\n") |
89 |
| - expect(lines.length).toBeGreaterThan(1) |
90 |
| - expect(lines[2].length).toBeLessThan(100) |
91 |
| - }) |
| 100 | + await cli.configureOutput({ width: 100, color: false }) |
| 101 | + const resp = await cli.run(runCfg, ['marketplace', 'image', 'list']) |
| 102 | + expect(resp.exitCode).toBe(0) |
92 | 103 |
|
93 |
| - it('can enable colors', async () => { |
94 |
| - const runCfg = emptyConfig() |
| 104 | + const lines = resp.stdout.split('\n') |
| 105 | + expect(lines.length).toBeGreaterThan(1) |
| 106 | + expect(lines[2].length).toBeLessThan(100) |
| 107 | + }) |
95 | 108 |
|
96 |
| - await cli.configureOutput({width: 100, color: false}) |
97 |
| - const resp = await cli.run(runCfg, ['invalid']) |
98 |
| - await cli.configureOutput({width: 100, color: true}) |
99 |
| - const coloredResp = await cli.run(runCfg, ['invalid']) |
| 109 | + it('can enable colors', async () => { |
| 110 | + const runCfg = emptyConfig() |
100 | 111 |
|
101 |
| - expect(coloredResp.stderr.length).toBeGreaterThan(resp.stderr.length) |
102 |
| - expect(coloredResp.stderr).not.toEqual(resp.stderr) |
103 |
| - }) |
| 112 | + await cli.configureOutput({ width: 100, color: false }) |
| 113 | + const resp = await cli.run(runCfg, ['invalid']) |
| 114 | + await cli.configureOutput({ width: 100, color: true }) |
| 115 | + const coloredResp = await cli.run(runCfg, ['invalid']) |
104 | 116 |
|
105 |
| - afterAll(async () => { |
106 |
| - try { |
107 |
| - await cli.stop() |
108 |
| - go._resume() |
109 |
| - } catch (e) { |
110 |
| - console.log(e) |
111 |
| - } |
112 |
| - }) |
| 117 | + expect(coloredResp.stderr.length).toBeGreaterThan(resp.stderr.length) |
| 118 | + expect(coloredResp.stderr).not.toEqual(resp.stderr) |
| 119 | + }) |
| 120 | + |
| 121 | + afterAll(async () => { |
| 122 | + try { |
| 123 | + await cli.stop() |
| 124 | + go._resume() |
| 125 | + } catch (e) { |
| 126 | + console.log(e) |
| 127 | + } |
| 128 | + }) |
113 | 129 | })
|
0 commit comments