main
1// Copyright 2024 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 assert from 'node:assert'
16import { test, describe } from 'bun:test'
17import { $, within, tmpdir } from '../../build/index.js'
18import '../../build/cli.js'
19
20describe('bun', () => {
21 test('smoke test', async () => {
22 const p = await $`echo foo`
23 assert.match(p.stdout, /foo/)
24 })
25
26 test('captures err stack', async () => {
27 const p = await $({ nothrow: true })`echo foo; exit 3`
28 assert.match(p.message, /exit code: 3/)
29 })
30
31 test('stdio: inherit', async () => {
32 await $({ stdio: 'inherit' })`ls`
33 })
34
35 test('ctx isolation', async () => {
36 await within(async () => {
37 const t1 = tmpdir()
38 const t3 = tmpdir()
39 $.cwd = t1
40 assert.equal($.cwd, t1)
41 assert.equal($.cwd, t1)
42
43 const w = within(async () => {
44 const t3 = tmpdir()
45 $.cwd = t3
46 assert.equal($.cwd, t3)
47
48 assert.ok((await $`pwd`).toString().trim().endsWith(t3))
49 assert.equal($.cwd, t3)
50 })
51
52 await $`pwd`
53 assert.ok((await $`pwd`).toString().trim().endsWith(t1))
54 assert.equal($.cwd, t1)
55 assert.ok((await $`pwd`).toString().trim().endsWith(t1))
56
57 $.cwd = t3
58 assert.ok((await $`pwd`).toString().trim().endsWith(t3))
59 assert.equal($.cwd, t3)
60
61 await w
62 })
63 })
64})