1
1
import { test , before } from "node:test" ;
2
+ import { resolve } from "node:path" ;
2
3
import assert from "node:assert" ;
3
4
import Dockerode from "dockerode" ;
4
5
import { Stream } from "node:stream" ;
@@ -13,6 +14,7 @@ export interface AssertBinaryVersionOptions {
13
14
semver : string ;
14
15
extract ?: RegExp ;
15
16
prefix ?: string ;
17
+ suffix ?: string ;
16
18
expectStderr ?: RegExp ;
17
19
}
18
20
@@ -22,12 +24,16 @@ export async function binaryVersionTest({
22
24
semver,
23
25
extract,
24
26
prefix,
27
+ suffix,
25
28
expectStderr = / ^ $ / ,
26
29
} : AssertBinaryVersionOptions ) {
27
30
await test ( `${ name } ${ semver } is available` , async ( ) => {
28
31
const res = await runCommandInContainer ( [ binary , "--version" ] ) ;
29
- assert . ok ( res . stderr . match ( expectStderr ) , `Expected stderr to match, got: ${ res . stderr } ` ) ;
30
- assertSemver ( res . stdout , semver , { extract, prefix } ) ;
32
+ assert . ok (
33
+ res . stderr . match ( expectStderr ) ,
34
+ `Expected stderr to match, got: ${ res . stderr } ` ,
35
+ ) ;
36
+ assertSemver ( res . stdout , semver , { extract, prefix, suffix } ) ;
31
37
} ) ;
32
38
}
33
39
@@ -54,7 +60,7 @@ export function assertSemver(
54
60
prefix,
55
61
suffix,
56
62
extract,
57
- } : { prefix ?: string ; suffix ?: string ; extract ?: RegExp } = { }
63
+ } : { prefix ?: string ; suffix ?: string ; extract ?: RegExp } = { } ,
58
64
) {
59
65
actual = actual . trim ( ) ;
60
66
if ( prefix && actual . startsWith ( prefix ) ) actual = actual . slice ( prefix . length ) ;
@@ -70,7 +76,7 @@ export function assertSemver(
70
76
actual = actual . trim ( ) ;
71
77
assert . ok (
72
78
semverSatisfies ( actual , expected ) ,
73
- `Expected semver match for ${ expected } , got ${ actual } `
79
+ `Expected semver match for ${ expected } , got ${ JSON . stringify ( actual ) } ` ,
74
80
) ;
75
81
}
76
82
@@ -82,12 +88,26 @@ function ensureDocker() {
82
88
before ( ensureDocker ) ;
83
89
84
90
export async function runCommandInContainer (
85
- command : string [ ]
91
+ command : string [ ] ,
92
+ {
93
+ mounts = [ ] ,
94
+ workingDir = "/" ,
95
+ } : {
96
+ mounts ?: { host : string ; container : string } [ ] ;
97
+ workingDir ?: string ;
98
+ } = { } ,
86
99
) : Promise < { stdout : string ; stderr : string } > {
87
100
const docker = ensureDocker ( ) ;
88
101
const container = await docker . createContainer ( {
102
+ WorkingDir : workingDir ,
89
103
Image : IMAGE_TAG ,
90
104
Cmd : command ,
105
+ HostConfig : {
106
+ Binds : mounts . map (
107
+ ( { host, container } ) =>
108
+ `${ resolve ( host ) } :${ container } ` ,
109
+ ) ,
110
+ } ,
91
111
} ) ;
92
112
const stdout = new StringStream ( ) ;
93
113
const stderr = new StringStream ( ) ;
@@ -100,9 +120,11 @@ export async function runCommandInContainer(
100
120
await container . start ( ) ;
101
121
const wait = ( await container . wait ( ) ) as { StatusCode : number } ;
102
122
if ( wait . StatusCode !== 0 ) {
103
- throw new Error ( `Command failed with status code ${ wait . StatusCode } \n` +
104
- `stdout:\n${ stdout . string } \n\n` +
105
- `stderr:\n${ stderr . string } ` ) ;
123
+ throw new Error (
124
+ `Command failed with status code ${ wait . StatusCode } \n` +
125
+ `stdout:\n${ stdout . string } \n\n` +
126
+ `stderr:\n${ stderr . string } ` ,
127
+ ) ;
106
128
}
107
129
return { stdout : stdout . string , stderr : stderr . string } ;
108
130
}
0 commit comments