v6
  1// Copyright 2022 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 { test } from 'uvu'
 16import * as assert from 'uvu/assert'
 17import '../build/globals.js'
 18
 19import {
 20  echo,
 21  retry,
 22  startSpinner,
 23  withTimeout,
 24  ctx,
 25  log,
 26} from '../build/experimental.js'
 27
 28import chalk from 'chalk'
 29
 30$.verbose = false
 31
 32const nodev = +process.versions.node.split('.')[0]
 33
 34test('retry works', async () => {
 35  let exitCode = 0
 36  let now = Date.now()
 37  try {
 38    await retry(5, 50)`exit 123`
 39  } catch (p) {
 40    exitCode = p.exitCode
 41  }
 42  assert.is(exitCode, 123)
 43  assert.ok(Date.now() >= now + 50 * (5 - 1))
 44})
 45
 46test('withTimeout works', async () => {
 47  let exitCode = 0
 48  let signal
 49  try {
 50    await withTimeout(100, 'SIGKILL')`sleep 9999`
 51  } catch (p) {
 52    exitCode = p.exitCode
 53    signal = p.signal
 54  }
 55  assert.is(exitCode, null)
 56  assert.is(signal, 'SIGKILL')
 57
 58  let p = await withTimeout(0)`echo 'test'`
 59  assert.is(p.stdout.trim(), 'test')
 60})
 61
 62test('echo works', async () => {
 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})
 71
 72test('spinner works', async () => {
 73  let s = startSpinner('waiting')
 74  await sleep(1000)
 75  s()
 76})
 77
 78test('ctx() provides isolates running scopes', async () => {
 79  $.verbose = true
 80
 81  await ctx(async ($) => {
 82    $.verbose = false
 83    await $`echo a`
 84
 85    $.verbose = true
 86    await $`echo b`
 87
 88    $.verbose = false
 89    await ctx(async ($) => {
 90      await $`echo d`
 91
 92      await ctx(async ($) => {
 93        assert.ok($.verbose === false)
 94
 95        await $`echo e`
 96        $.verbose = true
 97      })
 98      $.verbose = true
 99    })
100
101    await $`echo c`
102  })
103
104  await $`echo f`
105
106  await ctx(async ($) => {
107    assert.is($.verbose, true)
108    $.verbose = false
109    await $`echo g`
110  })
111
112  assert.is($.verbose, true)
113  $.verbose = false
114})
115
116test('ctx accepts optional ref', () => {
117  const ref = $.bind(null)
118
119  ctx(($) => {
120    assert.is(ref, $)
121  }, ref)
122})
123
124;(nodev < 24) && test('bound ctx is attached to Promise', async () => {
125  const pSymbols = Object.getOwnPropertySymbols(new Promise(() => {}))
126  const [_, __, kResourceStoreSymbol] = pSymbols
127  assert.is(new Promise(() => {})[kResourceStoreSymbol], undefined)
128
129  await ctx(async ($) => {
130    await ctx(async ($) => {
131      assert.is(new Promise(() => {})[kResourceStoreSymbol], $)
132    })
133
134    assert.is(new Promise(() => {})[kResourceStoreSymbol], $)
135  })
136
137  assert.is(new Promise(() => {})[kResourceStoreSymbol], undefined)
138})
139
140test('log() API is available', () => {
141  assert.ok(typeof log === 'function')
142})
143
144test.run()