@@ -12,8 +12,6 @@ import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
1212
1313import { BuilderInfo } from '@docker/actions-toolkit/lib/types/buildx/builder.js' ;
1414
15- import * as context from '../src/context.js' ;
16-
1715const tmpDir = fs . mkdtempSync ( path . join ( process . env . TEMP || os . tmpdir ( ) , 'context-' ) ) ;
1816const tmpName = path . join ( tmpDir , '.tmpname-vi' ) ;
1917const fixturesDir = path . join ( __dirname , 'fixtures' ) ;
@@ -52,6 +50,53 @@ vi.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise<Buil
5250 } ;
5351} ) ;
5452
53+ describe ( 'getInputs' , ( ) => {
54+ const originalEnv = process . env ;
55+
56+ beforeEach ( ( ) => {
57+ process . env = Object . keys ( process . env ) . reduce ( ( object , key ) => {
58+ if ( ! key . startsWith ( 'INPUT_' ) ) {
59+ object [ key ] = process . env [ key ] ;
60+ }
61+ return object ;
62+ } , { } ) ;
63+ } ) ;
64+
65+ afterEach ( ( ) => {
66+ process . env = originalEnv ;
67+ } ) ;
68+
69+ function setRequiredBooleanInputs ( ) : void {
70+ setInput ( 'load' , 'false' ) ;
71+ setInput ( 'no-cache' , 'false' ) ;
72+ setInput ( 'push' , 'false' ) ;
73+ setInput ( 'pull' , 'false' ) ;
74+ }
75+
76+ test ( 'uses Build git context when context input is empty' , async ( ) => {
77+ const gitContext = 'https://github.com/docker/build-push-action.git?ref=refs/heads/master' ;
78+ const gitContextSpy = vi . spyOn ( Build . prototype , 'gitContext' ) . mockResolvedValue ( gitContext ) ;
79+ setRequiredBooleanInputs ( ) ;
80+ const context = await loadContextModule ( ) ;
81+ const inputs = await context . getInputs ( ) ;
82+ expect ( inputs . context ) . toBe ( gitContext ) ;
83+ expect ( gitContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
84+ gitContextSpy . mockRestore ( ) ;
85+ } ) ;
86+
87+ test ( 'renders defaultContext templates from Build git context' , async ( ) => {
88+ const gitContext = 'https://github.com/docker/build-push-action.git#refs/heads/master' ;
89+ const gitContextSpy = vi . spyOn ( Build . prototype , 'gitContext' ) . mockResolvedValue ( gitContext ) ;
90+ setRequiredBooleanInputs ( ) ;
91+ setInput ( 'context' , '{{defaultContext}}:subdir' ) ;
92+ const context = await loadContextModule ( ) ;
93+ const inputs = await context . getInputs ( ) ;
94+ expect ( inputs . context ) . toBe ( `${ gitContext } :subdir` ) ;
95+ expect ( gitContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
96+ gitContextSpy . mockRestore ( ) ;
97+ } ) ;
98+ } ) ;
99+
55100describe ( 'getArgs' , ( ) => {
56101 const originalEnv = process . env ;
57102 beforeEach ( ( ) => {
@@ -344,7 +389,7 @@ ccc`],
344389 'build' ,
345390 '--file' , './test/Dockerfile' ,
346391 '--iidfile' , imageIDFilePath ,
347- '--secret' , `id=MY_SECRET,src=${ tmpName } ` ,
392+ '--secret' , `id=MY_SECRET,src=${ path . join ( fixturesDir , 'secret.txt' ) } ` ,
348393 '--builder' , 'builder-git-context-2' ,
349394 '--network' , 'host' ,
350395 '--push' ,
@@ -888,6 +933,46 @@ ANOTHER_SECRET=ANOTHER_SECRET_ENV`]
888933 [ 'GITHUB_SERVER_URL' , 'https://github.cds.internal.unity3d.com' ] ,
889934 ] )
890935 ] ,
936+ [
937+ 37 ,
938+ '0.29.0' ,
939+ new Map < string , string > ( [
940+ [ 'load' , 'false' ] ,
941+ [ 'no-cache' , 'false' ] ,
942+ [ 'push' , 'false' ] ,
943+ [ 'pull' , 'false' ] ,
944+ ] ) ,
945+ [
946+ 'build' ,
947+ '--iidfile' , imageIDFilePath ,
948+ '--attest' , `type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/build-push-action/actions/runs/123456789/attempts/1` ,
949+ '--metadata-file' , metadataJson ,
950+ 'https://github.com/docker/build-push-action.git?ref=refs/heads/master'
951+ ] ,
952+ new Map < string , string > ( [
953+ [ 'BUILDX_SEND_GIT_QUERY_AS_INPUT' , 'true' ]
954+ ] )
955+ ] ,
956+ [
957+ 38 ,
958+ '0.28.0' ,
959+ new Map < string , string > ( [
960+ [ 'load' , 'false' ] ,
961+ [ 'no-cache' , 'false' ] ,
962+ [ 'push' , 'false' ] ,
963+ [ 'pull' , 'false' ] ,
964+ ] ) ,
965+ [
966+ 'build' ,
967+ '--iidfile' , imageIDFilePath ,
968+ '--attest' , `type=provenance,mode=min,inline-only=true,builder-id=https://github.com/docker/build-push-action/actions/runs/123456789/attempts/1` ,
969+ '--metadata-file' , metadataJson ,
970+ 'https://github.com/docker/build-push-action.git#refs/heads/master'
971+ ] ,
972+ new Map < string , string > ( [
973+ [ 'BUILDX_SEND_GIT_QUERY_AS_INPUT' , 'true' ]
974+ ] )
975+ ] ,
891976 ] ) (
892977 '[%d] given %o with %o as inputs, returns %o' ,
893978 async ( num : number , buildxVersion : string , inputs : Map < string , string > , expected : Array < string > , envs : Map < string , string > | undefined ) => {
@@ -903,6 +988,7 @@ ANOTHER_SECRET=ANOTHER_SECRET_ENV`]
903988 vi . spyOn ( Buildx . prototype , 'version' ) . mockImplementation ( async ( ) : Promise < string > => {
904989 return buildxVersion ;
905990 } ) ;
991+ const context = await loadContextModule ( ) ;
906992 const inp = await context . getInputs ( ) ;
907993 const res = await context . getArgs ( inp , toolkit ) ;
908994 expect ( res ) . toEqual ( expected ) ;
@@ -918,3 +1004,8 @@ function getInputName(name: string): string {
9181004function setInput ( name : string , value : string ) : void {
9191005 process . env [ getInputName ( name ) ] = value ;
9201006}
1007+
1008+ async function loadContextModule ( ) : Promise < typeof import ( '../src/context.js' ) > {
1009+ vi . resetModules ( ) ;
1010+ return await import ( '../src/context.js' ) ;
1011+ }
0 commit comments