1
1
import { readableStreamToText , spawn } from "bun" ;
2
- import { afterEach , expect , it } from "bun:test" ;
3
- import { readFile , unlink } from "fs/promises" ;
2
+ import { expect , it } from "bun:test" ;
3
+ import { readFile , unlink } from "node: fs/promises" ;
4
4
5
5
export const runContainer = async (
6
6
image : string ,
@@ -21,7 +21,8 @@ export const runContainer = async (
21
21
"-c" ,
22
22
init ,
23
23
] ) ;
24
- let containerID = await readableStreamToText ( proc . stdout ) ;
24
+
25
+ const containerID = await readableStreamToText ( proc . stdout ) ;
25
26
const exitCode = await proc . exited ;
26
27
if ( exitCode !== 0 ) {
27
28
throw new Error ( containerID ) ;
@@ -36,7 +37,7 @@ export const runContainer = async (
36
37
export const executeScriptInContainer = async (
37
38
state : TerraformState ,
38
39
image : string ,
39
- shell : string = "sh" ,
40
+ shell = "sh" ,
40
41
) : Promise < {
41
42
exitCode : number ;
42
43
stdout : string [ ] ;
@@ -116,6 +117,9 @@ export interface CoderScriptAttributes {
116
117
url : string ;
117
118
}
118
119
120
+ export type ResourceInstance < T extends string = string > =
121
+ T extends "coder_script" ? CoderScriptAttributes : Record < string , string > ;
122
+
119
123
/**
120
124
* finds the first instance of the given resource type in the given state. If
121
125
* name is specified, it will only find the instance with the given name.
@@ -124,10 +128,7 @@ export const findResourceInstance = <T extends string>(
124
128
state : TerraformState ,
125
129
type : T ,
126
130
name ?: string ,
127
- // if type is "coder_script" return CoderScriptAttributes
128
- ) : T extends "coder_script"
129
- ? CoderScriptAttributes
130
- : Record < string , string > => {
131
+ ) : ResourceInstance < T > => {
131
132
const resource = state . resources . find (
132
133
( resource ) =>
133
134
resource . type === type && ( name ? resource . name === name : true ) ,
@@ -140,7 +141,8 @@ export const findResourceInstance = <T extends string>(
140
141
`Resource ${ type } has ${ resource . instances . length } instances` ,
141
142
) ;
142
143
}
143
- return resource . instances [ 0 ] . attributes as any ;
144
+
145
+ return resource . instances [ 0 ] . attributes as ResourceInstance < T > ;
144
146
} ;
145
147
146
148
/**
@@ -157,15 +159,15 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
157
159
} ) ;
158
160
159
161
const varNames = Object . keys ( vars ) ;
160
- varNames . forEach ( ( varName ) => {
162
+ for ( const varName of varNames ) {
161
163
// Ensures that every variable provided is required!
162
- it ( " missing variable " + varName , async ( ) => {
164
+ it ( ` missing variable: ${ varName } ` , async ( ) => {
163
165
const localVars : TerraformVariables = { } ;
164
- varNames . forEach ( ( otherVarName ) => {
166
+ for ( const otherVarName of varNames ) {
165
167
if ( otherVarName !== varName ) {
166
168
localVars [ otherVarName ] = vars [ otherVarName ] ;
167
169
}
168
- } ) ;
170
+ }
169
171
170
172
try {
171
173
await runTerraformApply ( dir , localVars ) ;
@@ -181,7 +183,7 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
181
183
}
182
184
throw new Error ( `${ varName } is not a required variable!` ) ;
183
185
} ) ;
184
- } ) ;
186
+ }
185
187
} ;
186
188
187
189
/**
0 commit comments