main
 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 assert from 'node:assert'
16import { Readable, Writable } from 'node:stream'
17import { expectError, expectType } from 'tsd'
18import { $, ProcessPromise, ProcessOutput, within } from 'zx'
19
20let p = $`cmd`
21assert(p instanceof ProcessPromise)
22expectType<ProcessPromise>(p)
23expectType<Writable>(p.stdin)
24expectType<Readable>(p.stdout)
25expectType<Readable>(p.stderr)
26expectType<ProcessPromise>(p.nothrow())
27expectType<ProcessPromise>(p.quiet())
28expectType<ProcessPromise>(p.pipe($`cmd`))
29expectType<ProcessPromise>(p.pipe`cmd`)
30expectType<
31  Writable & PromiseLike<ProcessOutput & Writable> & { run: () => void }
32>(p.pipe('file'))
33expectType<
34  typeof process.stdout &
35    PromiseLike<ProcessOutput & typeof process.stdout> & { run: () => void }
36>(p.pipe(process.stdout))
37expectType<ProcessPromise>(p.stdio('pipe'))
38expectType<ProcessPromise>(p.timeout('1s'))
39expectType<Promise<void>>(p.kill())
40expectType<Promise<ProcessOutput>>(p.then((p) => p))
41expectType<Promise<ProcessOutput>>(p.catch((p) => p))
42expectType<Promise<any>>(p.then((p) => p).catch((p) => p))
43
44let o = await p
45assert(o instanceof ProcessOutput)
46expectType<ProcessOutput>(o)
47expectType<string>(o.stdout)
48expectType<string>(o.stderr)
49expectType<number | null>(o.exitCode)
50expectType<NodeJS.Signals | null>(o.signal)
51// prettier-ignore
52expectType<ProcessOutput>(new ProcessOutput({
53  code: null,
54  signal: null,
55  duration: 0,
56  store: { stdout: [], stderr: [], stdall: [] },
57  error: null,
58  from: ''
59}))
60
61expectType<ProcessOutput>(new ProcessOutput(null, null, '', '', '', '', 1))
62expectType<ProcessOutput>(new ProcessOutput(null, null, '', '', '', ''))
63expectError(new ProcessOutput('1'))
64
65expectType<'banana'>(within(() => 'apple' as 'banana'))
66
67expectType<ProcessPromise>($`cmd`)
68expectType<ProcessPromise>($({ sync: false })`cmd`)
69expectType<ProcessOutput>($({ sync: true })`cmd`)
70expectType<ProcessOutput>($.sync`cmd`)