@@ -592,7 +592,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
592592 plugin PluginDef
593593 defaults DefaultsConfig
594594 mockDownload func (* exec.Cmd ) error
595- mockInstall func (* exec.Cmd ) error
595+ mockBuild func (* exec.Cmd ) error
596596 expectError bool
597597 expectedErrMsg string
598598 }{
@@ -613,7 +613,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
613613 }
614614 return nil
615615 },
616- mockInstall : func (cmd * exec.Cmd ) error {
616+ mockBuild : func (cmd * exec.Cmd ) error {
617617 return nil
618618 },
619619 expectError : false ,
@@ -629,7 +629,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
629629 mockDownload : func (cmd * exec.Cmd ) error {
630630 return fmt .Errorf ("failed to download module" )
631631 },
632- mockInstall : func (cmd * exec.Cmd ) error {
632+ mockBuild : func (cmd * exec.Cmd ) error {
633633 return nil
634634 },
635635 expectError : true ,
@@ -650,7 +650,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
650650 }
651651 return nil
652652 },
653- mockInstall : func (cmd * exec.Cmd ) error {
653+ mockBuild : func (cmd * exec.Cmd ) error {
654654 return fmt .Errorf ("failed to install plugin" )
655655 },
656656 expectError : true ,
@@ -684,7 +684,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
684684 }
685685 return nil
686686 },
687- mockInstall : func (cmd * exec.Cmd ) error {
687+ mockBuild : func (cmd * exec.Cmd ) error {
688688 if len (cmd .Args ) < 2 {
689689 return fmt .Errorf ("install command has too few arguments: %v" , cmd .Args )
690690 }
@@ -713,7 +713,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
713713 }
714714 return nil
715715 },
716- mockInstall : func (cmd * exec.Cmd ) error {
716+ mockBuild : func (cmd * exec.Cmd ) error {
717717 if len (cmd .Args ) < 2 {
718718 return fmt .Errorf ("install command has too few arguments: %v" , cmd .Args )
719719 }
@@ -731,7 +731,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
731731 for _ , tc := range tests {
732732 t .Run (tc .name , func (t * testing.T ) {
733733 // Mock the command execution
734- var downloadCalled , installCalled bool
734+ var downloadCalled , buildCalled bool
735735 execCommand = func (cmd * exec.Cmd ) error {
736736 cmdLine := strings .Join (cmd .Args , " " )
737737
@@ -740,10 +740,10 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
740740 if tc .mockDownload != nil {
741741 return tc .mockDownload (cmd )
742742 }
743- } else if strings .Contains (cmdLine , "go install " ) {
744- installCalled = true
745- if tc .mockInstall != nil {
746- return tc .mockInstall (cmd )
743+ } else if strings .Contains (cmdLine , "go build " ) {
744+ buildCalled = true
745+ if tc .mockBuild != nil {
746+ return tc .mockBuild (cmd )
747747 }
748748 }
749749 return nil
@@ -768,7 +768,7 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
768768 if ! downloadCalled && tc .mockDownload != nil {
769769 t .Error ("Download command was not called" )
770770 }
771- if ! installCalled && tc .mockInstall != nil {
771+ if ! buildCalled && tc .mockBuild != nil {
772772 t .Error ("Install command was not called" )
773773 }
774774 }
@@ -851,6 +851,177 @@ func TestFlags(t *testing.T) {
851851 })
852852}
853853
854+ func TestEnvVars (t * testing.T ) {
855+ t .Run ("plugin specific env vars are appended to default env vars" , func (t * testing.T ) {
856+ // Save the original execCommand to restore it after the test
857+ originalExecCommand := execCommand
858+ defer func () { execCommand = originalExecCommand }()
859+
860+ // Create a temporary directory for test files
861+ tempDir , err := os .MkdirTemp ("" , "plugin-envvars-test" )
862+ if err != nil {
863+ t .Fatalf ("Failed to create temp dir: %v" , err )
864+ }
865+ defer os .RemoveAll (tempDir )
866+
867+ mockDownload := func (cmd * exec.Cmd ) error {
868+ if stdout , ok := cmd .Stdout .(* bytes.Buffer ); ok {
869+ parts := strings .Split ("github.com/example/test" , "/" )
870+ moduleDir := filepath .Join (append ([]string {tempDir , "modules" }, parts ... )... )
871+ stdout .WriteString (fmt .Sprintf (`{"Dir":"%s"}` , moduleDir ))
872+ }
873+ return nil
874+ }
875+
876+ mockBuild := func (cmd * exec.Cmd ) error {
877+ // Verify environment variables are set correctly
878+ foundGOOS := false
879+ foundGOARCH := false
880+ foundCGOEnabled := false
881+
882+ for _ , env := range cmd .Env {
883+ if env == "GOOS=linux" {
884+ foundGOOS = true
885+ }
886+ if env == "GOARCH=amd64" {
887+ foundGOARCH = true
888+ }
889+ if env == "CGO_ENABLED=0" {
890+ foundCGOEnabled = true
891+ }
892+ }
893+
894+ if ! foundGOOS {
895+ return fmt .Errorf ("expected GOOS=linux in environment, not found" )
896+ }
897+ if ! foundGOARCH {
898+ return fmt .Errorf ("expected GOARCH=amd64 in environment, not found" )
899+ }
900+ if ! foundCGOEnabled {
901+ return fmt .Errorf ("expected CGO_ENABLED=0 in environment, not found" )
902+ }
903+
904+ return nil
905+ }
906+
907+ // Mock the command execution
908+ execCommand = func (cmd * exec.Cmd ) error {
909+ cmdLine := strings .Join (cmd .Args , " " )
910+ if strings .Contains (cmdLine , "go mod download" ) {
911+ return mockDownload (cmd )
912+ } else if strings .Contains (cmdLine , "go build" ) {
913+ return mockBuild (cmd )
914+ }
915+ return nil
916+ }
917+
918+ defaults := DefaultsConfig {
919+ EnvVars : []string {"GOOS=linux" },
920+ }
921+
922+ plugin := PluginDef {
923+ ModuleURI : "github.com/example/test" ,
924+ GitRef : "v1.0.0" ,
925+ InstallPath : "./cmd/test" ,
926+ EnvVars : []string {"GOARCH=amd64" , "CGO_ENABLED=0" },
927+ }
928+
929+ // Call the function
930+ err = downloadAndInstallPlugin ("test" , 0 , plugin , defaults )
931+
932+ // Check results
933+ if err != nil {
934+ t .Errorf ("Expected no error, got %v" , err )
935+ }
936+ })
937+
938+ t .Run ("env vars replace existing values instead of duplicating" , func (t * testing.T ) {
939+ // Save the original execCommand to restore it after the test
940+ originalExecCommand := execCommand
941+ defer func () { execCommand = originalExecCommand }()
942+
943+ // Create a temporary directory for test files
944+ tempDir , err := os .MkdirTemp ("" , "plugin-envvars-replace-test" )
945+ if err != nil {
946+ t .Fatalf ("Failed to create temp dir: %v" , err )
947+ }
948+ defer os .RemoveAll (tempDir )
949+
950+ mockDownload := func (cmd * exec.Cmd ) error {
951+ if stdout , ok := cmd .Stdout .(* bytes.Buffer ); ok {
952+ parts := strings .Split ("github.com/example/test" , "/" )
953+ moduleDir := filepath .Join (append ([]string {tempDir , "modules" }, parts ... )... )
954+ stdout .WriteString (fmt .Sprintf (`{"Dir":"%s"}` , moduleDir ))
955+ }
956+ return nil
957+ }
958+
959+ mockBuild := func (cmd * exec.Cmd ) error {
960+ // Count occurrences of GOOS
961+ goosCount := 0
962+ correctValue := false
963+
964+ for _ , env := range cmd .Env {
965+ if strings .HasPrefix (env , "GOOS=" ) {
966+ goosCount ++
967+ if env == "GOOS=linux" {
968+ correctValue = true
969+ }
970+ }
971+ }
972+
973+ if goosCount != 1 {
974+ return fmt .Errorf ("expected exactly 1 GOOS env var, found %d" , goosCount )
975+ }
976+ if ! correctValue {
977+ return fmt .Errorf ("expected GOOS=linux, but found different value" )
978+ }
979+
980+ return nil
981+ }
982+
983+ // Mock the command execution
984+ execCommand = func (cmd * exec.Cmd ) error {
985+ cmdLine := strings .Join (cmd .Args , " " )
986+ if strings .Contains (cmdLine , "go mod download" ) {
987+ return mockDownload (cmd )
988+ } else if strings .Contains (cmdLine , "go build" ) {
989+ return mockBuild (cmd )
990+ }
991+ return nil
992+ }
993+
994+ // Set GOOS in the environment to a different value
995+ oldGOOS := os .Getenv ("GOOS" )
996+ os .Setenv ("GOOS" , "darwin" )
997+ defer func () {
998+ if oldGOOS == "" {
999+ os .Unsetenv ("GOOS" )
1000+ } else {
1001+ os .Setenv ("GOOS" , oldGOOS )
1002+ }
1003+ }()
1004+
1005+ defaults := DefaultsConfig {
1006+ EnvVars : []string {"GOOS=linux" }, // Override with linux
1007+ }
1008+
1009+ plugin := PluginDef {
1010+ ModuleURI : "github.com/example/test" ,
1011+ GitRef : "v1.0.0" ,
1012+ InstallPath : "./cmd/test" ,
1013+ }
1014+
1015+ // Call the function
1016+ err = downloadAndInstallPlugin ("test" , 0 , plugin , defaults )
1017+
1018+ // Check results
1019+ if err != nil {
1020+ t .Errorf ("Expected no error, got %v" , err )
1021+ }
1022+ })
1023+ }
1024+
8541025// TestSetupOutputFile tests the setupOutputFile function
8551026func TestSetupOutputFile (t * testing.T ) {
8561027 // Test with relative path
0 commit comments