v7
1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import chalk from 'chalk'
16import { suite } from 'uvu'
17import * as assert from 'uvu/assert'
18import '../build/globals.js'
19
20const test = suite('goods')
21
22$.verbose = false
23
24function zx(script) {
25 return $`node build/cli.js --eval ${script}`.nothrow().timeout('5s')
26}
27
28test('question() works', async () => {
29 let p = $`node build/cli.js --eval "
30 let answer = await question('foo or bar? ', { choices: ['foo', 'bar'] })
31 echo('Answer is', answer)
32"`
33 p.stdin.write('foo\n')
34 p.stdin.end()
35 assert.match((await p).stdout, 'Answer is foo')
36})
37
38test('globby available', async () => {
39 assert.is(globby, glob)
40 assert.is(typeof globby, 'function')
41 assert.is(typeof globby.globbySync, 'function')
42 assert.is(typeof globby.globbyStream, 'function')
43 assert.is(typeof globby.generateGlobTasks, 'function')
44 assert.is(typeof globby.isDynamicPattern, 'function')
45 assert.is(typeof globby.isGitIgnored, 'function')
46 assert.is(typeof globby.isGitIgnoredSync, 'function')
47 assert.equal(await globby('*.md'), ['README.md'])
48})
49
50test('fetch() works', async () => {
51 assert.match(
52 await fetch('https://medv.io').then((res) => res.text()),
53 /Anton Medvedev/
54 )
55})
56
57test('echo() works', async () => {
58 let stdout = ''
59 let log = console.log
60 console.log = (...args) => {
61 stdout += args.join(' ')
62 }
63 echo(chalk.cyan('foo'), chalk.green('bar'), chalk.bold('baz'))
64 echo`${chalk.cyan('foo')} ${chalk.green('bar')} ${chalk.bold('baz')}`
65 echo(
66 await $`echo ${chalk.cyan('foo')}`,
67 await $`echo ${chalk.green('bar')}`,
68 await $`echo ${chalk.bold('baz')}`
69 )
70 console.log = log
71 assert.match(stdout, 'foo')
72})
73
74test('YAML works', async () => {
75 assert.equal(YAML.parse(YAML.stringify({ foo: 'bar' })), { foo: 'bar' })
76})
77
78test('which() available', async () => {
79 assert.is(which.sync('npm'), await which('npm'))
80})
81
82test('sleep() works', async () => {
83 const now = Date.now()
84 await sleep(100)
85 assert.ok(Date.now() >= now + 99)
86})
87
88test('retry() works', async () => {
89 const now = Date.now()
90 let p = await zx(`
91 try {
92 await retry(5, '50ms', () => $\`exit 123\`)
93 } catch (e) {
94 echo('exitCode:', e.exitCode)
95 }
96 await retry(5, () => $\`exit 0\`)
97 echo('success')
98`)
99 assert.match(p.toString(), 'exitCode: 123')
100 assert.match(p.toString(), 'success')
101 assert.ok(Date.now() >= now + 50 * (5 - 1))
102})
103
104test('retry() with expBackoff() works', async () => {
105 const now = Date.now()
106 let p = await zx(`
107 try {
108 await retry(5, expBackoff('60s', 0), () => $\`exit 123\`)
109 } catch (e) {
110 echo('exitCode:', e.exitCode)
111 }
112 echo('success')
113`)
114 assert.match(p.toString(), 'exitCode: 123')
115 assert.match(p.toString(), 'success')
116 assert.ok(Date.now() >= now + 2 + 4 + 8 + 16 + 32)
117})
118
119test('spinner() works', async () => {
120 let out = await zx(`
121 echo(await spinner(async () => {
122 await sleep(100)
123 await $\`echo hidden\`
124 return $\`echo result\`
125 }))
126 `)
127 assert.match(out.stdout, 'result')
128 assert.not.match(out.stderr, 'result')
129 assert.not.match(out.stderr, 'hidden')
130})
131
132test('spinner() with title works', async () => {
133 let out = await zx(`
134 await spinner('processing', () => sleep(100))
135 `)
136 assert.match(out.stderr, 'processing')
137})
138
139test('spinner() stops on throw', async () => {
140 let out = await zx(`
141 await spinner('processing', () => $\`wtf-cmd\`)
142 `)
143 assert.match(out.stderr, 'Error:')
144 assert.is.not(out.exitCode, 0)
145})
146
147test.run()