@@ -666,6 +666,79 @@ func TestNoMethodFails(t *testing.T) {
666
666
require .ErrorContains (t , err , envbuilder .ErrNoFallbackImage .Error ())
667
667
}
668
668
669
+ func TestDockerfileBuildContext (t * testing.T ) {
670
+ t .Parallel ()
671
+
672
+ inclFile := "myfile"
673
+ dockerfile := fmt .Sprintf (`FROM scratch
674
+ COPY %s .` , inclFile )
675
+
676
+ tests := []struct {
677
+ name string
678
+ files map [string ]string
679
+ dockerfilePath string
680
+ buildContextPath string
681
+ expectedErr string
682
+ }{
683
+ {
684
+ // Dockerfile & build context are in the same dir, copying inclFile should work.
685
+ name : "same build context (default)" ,
686
+ files : map [string ]string {
687
+ "Dockerfile" : dockerfile ,
688
+ inclFile : "..." ,
689
+ },
690
+ dockerfilePath : "Dockerfile" ,
691
+ buildContextPath : "" , // use default
692
+ expectedErr : "" , // expect no errors
693
+ },
694
+ {
695
+ // Dockerfile & build context are not in the same dir, build context is still the default; this should fail
696
+ // to copy inclFile since it is not in the same dir as the Dockerfile.
697
+ name : "different build context (default)" ,
698
+ files : map [string ]string {
699
+ "a/Dockerfile" : dockerfile ,
700
+ "a/" + inclFile : "..." ,
701
+ },
702
+ dockerfilePath : "a/Dockerfile" ,
703
+ buildContextPath : "" , // use default
704
+ expectedErr : inclFile + ": no such file or directory" ,
705
+ },
706
+ {
707
+ // Dockerfile is not in the default build context dir, but the build context has been overridden; this should
708
+ // allow inclFile to be copied.
709
+ name : "different build context (custom)" ,
710
+ files : map [string ]string {
711
+ "a/Dockerfile" : dockerfile ,
712
+ "a/" + inclFile : "..." ,
713
+ },
714
+ dockerfilePath : "a/Dockerfile" ,
715
+ buildContextPath : "a/" ,
716
+ expectedErr : "" ,
717
+ },
718
+ }
719
+
720
+ for _ , tc := range tests {
721
+ tc := tc
722
+
723
+ t .Run (tc .name , func (t * testing.T ) {
724
+ url := createGitServer (t , gitServerOptions {
725
+ files : tc .files ,
726
+ })
727
+ _ , err := runEnvbuilder (t , options {env : []string {
728
+ "GIT_URL=" + url ,
729
+ "DOCKERFILE_PATH=" + tc .dockerfilePath ,
730
+ "BUILD_CONTEXT_PATH=" + tc .buildContextPath ,
731
+ }})
732
+
733
+ if tc .expectedErr == "" {
734
+ require .NoError (t , err )
735
+ } else {
736
+ require .ErrorContains (t , err , tc .expectedErr )
737
+ }
738
+ })
739
+ }
740
+ }
741
+
669
742
// TestMain runs before all tests to build the envbuilder image.
670
743
func TestMain (m * testing.M ) {
671
744
cleanOldEnvbuilders ()
0 commit comments