@@ -673,6 +673,75 @@ func Test_Build_createBuildArgExpander(t *testing.T) {
673673 g .Expect (value ).To (Equal ("from-file" ))
674674 })
675675
676+ t .Run ("should provide built-in platform args by default" , func (t * testing.T ) {
677+ c := & Build {
678+ Params : & BuildParams {},
679+ }
680+
681+ expander , err := c .createBuildArgExpander ()
682+ g .Expect (err ).ToNot (HaveOccurred ())
683+
684+ // Check that all built-in platform args are available
685+ platformArgs := []string {
686+ "TARGETPLATFORM" , "TARGETOS" , "TARGETARCH" , "TARGETVARIANT" ,
687+ "BUILDPLATFORM" , "BUILDOS" , "BUILDARCH" , "BUILDVARIANT" ,
688+ }
689+
690+ for _ , arg := range platformArgs {
691+ value , err := expander (arg )
692+ // TARGETVARIANT and BUILDVARIANT can be empty on non-ARM platforms
693+ if arg == "TARGETVARIANT" || arg == "BUILDVARIANT" {
694+ g .Expect (err ).ToNot (HaveOccurred ())
695+ } else {
696+ g .Expect (err ).ToNot (HaveOccurred ())
697+ g .Expect (value ).ToNot (BeEmpty (), "arg %s should not be empty" , arg )
698+ }
699+ }
700+ })
701+
702+ t .Run ("should allow file args to override built-in platform args" , func (t * testing.T ) {
703+ tempDir := t .TempDir ()
704+ testutil .WriteFileTree (t , tempDir , map [string ]string {
705+ "build-args" : "TARGETOS=custom-os\n TARGETARCH=custom-arch\n " ,
706+ })
707+
708+ c := & Build {
709+ Params : & BuildParams {
710+ BuildArgsFile : filepath .Join (tempDir , "build-args" ),
711+ },
712+ }
713+
714+ expander , err := c .createBuildArgExpander ()
715+ g .Expect (err ).ToNot (HaveOccurred ())
716+
717+ value , err := expander ("TARGETOS" )
718+ g .Expect (err ).ToNot (HaveOccurred ())
719+ g .Expect (value ).To (Equal ("custom-os" ))
720+
721+ value , err = expander ("TARGETARCH" )
722+ g .Expect (err ).ToNot (HaveOccurred ())
723+ g .Expect (value ).To (Equal ("custom-arch" ))
724+ })
725+
726+ t .Run ("should allow CLI args to override built-in platform args" , func (t * testing.T ) {
727+ c := & Build {
728+ Params : & BuildParams {
729+ BuildArgs : []string {"TARGETOS=custom-os" , "TARGETARCH=custom-arch" },
730+ },
731+ }
732+
733+ expander , err := c .createBuildArgExpander ()
734+ g .Expect (err ).ToNot (HaveOccurred ())
735+
736+ value , err := expander ("TARGETOS" )
737+ g .Expect (err ).ToNot (HaveOccurred ())
738+ g .Expect (value ).To (Equal ("custom-os" ))
739+
740+ value , err = expander ("TARGETARCH" )
741+ g .Expect (err ).ToNot (HaveOccurred ())
742+ g .Expect (value ).To (Equal ("custom-arch" ))
743+ })
744+
676745 t .Run ("should return error for undefined build arg" , func (t * testing.T ) {
677746 c := & Build {
678747 Params : & BuildParams {},
0 commit comments