diff --git a/pkg/gomason/golang_test.go b/pkg/gomason/golang_test.go index c237d75..f7b3797 100644 --- a/pkg/gomason/golang_test.go +++ b/pkg/gomason/golang_test.go @@ -2,7 +2,6 @@ package gomason import ( "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -17,15 +16,14 @@ func TestCreateGoPath(t *testing.T) { lang, _ := GetByName(LanguageGolang) _, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating gopath in %q: %s", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating gopath in %q: %s", TestTmpDir, err) } dirs := []string{"go", "go/src", "go/pkg", "go/bin"} for _, dir := range dirs { if _, err := os.Stat(filepath.Join(TestTmpDir, dir)); os.IsNotExist(err) { - t.Fail() + t.Errorf("GoPath not created.") } } } @@ -34,79 +32,69 @@ func TestCheckoutDefault(t *testing.T) { lang, _ := GetByName(LanguageGolang) gopath, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err) } - log.Printf("Checking out Master Branch") + log.Printf("Checking out Master Branch\n") err = lang.Checkout(gopath, testMetadataObj(), "") if err != nil { - log.Printf("Failed to checkout module: %s", err) - t.FailNow() + t.Errorf("Failed to checkout module: %s", err) } metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME) if _, err := os.Stat(metaPath); os.IsNotExist(err) { - log.Printf("Failed to checkout module") - t.FailNow() + t.Errorf("Failed to checkout module") } } func TestCheckoutBranch(t *testing.T) { - log.Printf("Checking out Test Branch") + log.Printf("Checking out Test Branch\n") // making a separate temp dir here cos it steps on the other tests - dir, err := ioutil.TempDir("", "gomason") + dir, err := os.MkdirTemp("", "gomason") if err != nil { - log.Fatal("Error creating temp dir\n") + t.Errorf("Error creating temp dir\n") } defer os.RemoveAll(dir) lang, _ := GetByName(LanguageGolang) gopath, err := lang.CreateWorkDir(dir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s", dir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s", dir, err) } err = lang.Checkout(gopath, testMetadataObj(), "testbranch") if err != nil { - log.Printf("Failed to checkout module: %s", err) - t.FailNow() + t.Errorf("Failed to checkout module: %s", err) } testFilePath := filepath.Join(gopath, "src", testModuleName(), "test_file") if _, err := os.Stat(testFilePath); os.IsNotExist(err) { - log.Printf("Failed to checkout branch") - t.FailNow() + t.Errorf("Failed to checkout branch") } } func TestPrep(t *testing.T) { - log.Printf("Checking out Master Branch") + log.Printf("Checking out Master Branch\n") lang, _ := GetByName(LanguageGolang) gopath, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err) } err = lang.Checkout(gopath, testMetadataObj(), "") if err != nil { - log.Printf("Failed to checkout module: %s", err) - t.FailNow() + t.Errorf("Failed to checkout module: %s", err) } metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME) if _, err := os.Stat(metaPath); os.IsNotExist(err) { - log.Printf("Failed to checkout module") - t.FailNow() + t.Errorf("Failed to checkout module") } err = lang.Prep(gopath, testMetadataObj()) if err != nil { - log.Printf("error running prep steps: %s", err) - t.FailNow() + t.Errorf("error running prep steps: %s", err) } } @@ -116,19 +104,16 @@ func TestBuildGoxInstall(t *testing.T) { log.Printf("Installing Gox\n") gopath, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err) } err = GoxInstall(gopath) if err != nil { - log.Printf("Error installing Gox: %s\n", err) - t.FailNow() + t.Errorf("Error installing Gox: %s\n", err) } if _, err := os.Stat(filepath.Join(gopath, "bin/gox")); os.IsNotExist(err) { - log.Printf("Gox failed to install.") - t.FailNow() + t.Errorf("Gox failed to install.") } } @@ -167,55 +152,49 @@ func TestBuild(t *testing.T) { t.Run(tc.name, func(t *testing.T) { lang, err := GetByName(tc.lang) if err != nil { - t.Fatalf(err.Error()) + t.Errorf(err.Error()) } log.Printf("Running Build\n") gopath, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s\n", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s\n", TestTmpDir, err) } gomodule := testMetadataObj().Package - log.Printf("Checking out Master Branch") + log.Printf("Checking out Master Branch\n") err = lang.Checkout(gopath, testMetadataObj(), "") if err != nil { - log.Printf("Failed to checkout module: %s", err) - t.FailNow() + t.Errorf("Failed to checkout module: %s", err) } metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME) if _, err := os.Stat(metaPath); os.IsNotExist(err) { - log.Printf("Failed to checkout module") - t.FailNow() + t.Errorf("Failed to checkout module") } err = lang.Prep(gopath, testMetadataObj()) if err != nil { - log.Printf("error running prep steps: %s", err) - t.FailNow() + t.Errorf("error running prep steps: %s", err) } err = lang.Build(gopath, testMetadataObj(), tc.skipTargets) if err != nil { - log.Printf("Error building: %s", err) - t.FailNow() + t.Errorf("Error building: %s", err) } for _, artifact := range tc.artifactsPresent { workdir := filepath.Join(gopath, "src", gomodule) binary := fmt.Sprintf("%s/%s", workdir, artifact) - log.Printf("Looking for binary present: %s", binary) + log.Printf("Looking for binary present: %s\n", binary) if _, err := os.Stat(binary); os.IsNotExist(err) { - log.Printf("Gox failed to build binary: %s.\n", binary) - t.FailNow() + t.Errorf("Gox failed to build binary: %s.\n", binary) } else { - log.Printf("Binary found.") + log.Printf("Binary found.\n") } } @@ -223,13 +202,12 @@ func TestBuild(t *testing.T) { workdir := filepath.Join(gopath, "src", gomodule) binary := fmt.Sprintf("%s/%s", workdir, artifact) - log.Printf("Looking for binary not present: %s", binary) + log.Printf("Looking for binary not present: %s\n", binary) if _, err := os.Stat(binary); os.IsNotExist(err) { - log.Printf("Binary not found - as intended.") + log.Printf("Binary not found - as intended.\n") } else { - log.Printf("Gox built binary: %s when it shouldn't have.\n", binary) - t.FailNow() + t.Errorf("Gox built binary: %s when it shouldn't have.\n", binary) } } }) @@ -239,35 +217,30 @@ func TestBuild(t *testing.T) { func TestTest(t *testing.T) { lang, _ := GetByName(LanguageGolang) - log.Printf("Checking out Master Branch") + log.Printf("Checking out Master Branch\n") gopath, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err) } err = lang.Checkout(gopath, testMetadataObj(), "") if err != nil { - log.Printf("Failed to checkout module: %s", err) - t.FailNow() + t.Errorf("Failed to checkout module: %s", err) } metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME) if _, err := os.Stat(metaPath); os.IsNotExist(err) { - log.Printf("Failed to checkout module") - t.FailNow() + t.Errorf("Failed to checkout module") } err = lang.Prep(gopath, testMetadataObj()) if err != nil { - log.Printf("error running prep steps: %s", err) - t.FailNow() + t.Errorf("error running prep steps: %s", err) } err = lang.Test(gopath, testMetadataObj().Package, "10m") if err != nil { - log.Printf("error running go test: %s", err) - t.FailNow() + t.Errorf("error running go test: %s", err) } } @@ -280,8 +253,7 @@ func TestSignVerifyBinary(t *testing.T) { } shellCmd, err := exec.LookPath("gpg") if err != nil { - log.Printf("Failed to check if gpg is installed:%s", err) - t.FailNow() + t.Errorf("Failed to check if gpg is installed:%s", err) } lang, _ := GetByName(LanguageGolang) @@ -289,8 +261,7 @@ func TestSignVerifyBinary(t *testing.T) { // create workspace gopath, err := lang.CreateWorkDir(TestTmpDir) if err != nil { - log.Printf("Error creating GOPATH in %s: %s\n", TestTmpDir, err) - t.FailNow() + t.Errorf("Error creating GOPATH in %s: %s\n", TestTmpDir, err) } meta := testMetadataObj() @@ -301,8 +272,7 @@ func TestSignVerifyBinary(t *testing.T) { log.Printf("Running Build\n") err = lang.Build(gopath, meta, "") if err != nil { - log.Printf("Error building: %s", err) - t.FailNow() + t.Errorf("Error building: %s", err) } // set up test keys @@ -327,25 +297,23 @@ Expire-Date: 0 %echo done ` keyFile := filepath.Join(TestTmpDir, "testkey") - err = ioutil.WriteFile(keyFile, []byte(defaultKeyText), 0644) + err = os.WriteFile(keyFile, []byte(defaultKeyText), 0644) if err != nil { - log.Printf("Error writing test key generation file: %s", err) - t.FailNow() + t.Errorf("Error writing test key generation file: %s", err) } - log.Printf("Keyring file: %s", keyring) - log.Printf("Trustdb file: %s", trustdb) - log.Printf("Test key generation file: %s", keyFile) + log.Printf("Keyring file: %s\n", keyring) + log.Printf("Trustdb file: %s\n", trustdb) + log.Printf("Test key generation file: %s\n", keyFile) // generate a test key cmd := exec.Command(shellCmd, "--trustdb", trustdb, "--no-default-keyring", "--keyring", keyring, "--batch", "--generate-key", keyFile) err = cmd.Run() if err != nil { - log.Printf("****** Error creating test key: %s *****", err) - t.FailNow() + t.Errorf("****** Error creating test key: %s *****", err) } - log.Printf("Done creating keyring and test keys") + log.Printf("Done creating keyring and test keys\n") // sign binaries parts := strings.Split(meta.Package, "/") @@ -361,46 +329,41 @@ Expire-Date: 0 binary := fmt.Sprintf("%s/%s_%s_%s", workdir, binaryPrefix, osname, archname) if _, err := os.Stat(binary); os.IsNotExist(err) { - fmt.Printf("Gox failed to build binary: %s\n", binary) - log.Printf("Failed to find binary %s", binary) - t.FailNow() + t.Errorf("Gox failed to build binary: %s\n", binary) } err = g.SignBinary(meta, binary) if err != nil { err = errors.Wrap(err, "failed to sign binary") - log.Printf("Failed to sign binary %s: %s", binary, err) - t.FailNow() + t.Errorf("Failed to sign binary %s: %s", binary, err) } // verify binaries ok, err := VerifyBinary(binary, meta) if err != nil { - log.Printf("Error verifying signature: %s", err) - //t.Fail() + t.Errorf("Error verifying signature: %s", err) } if !ok { - log.Printf("Failed to verify signature on %s", binary) - t.FailNow() + t.Errorf("Failed to verify signature on %s", binary) } } cwd, err := os.Getwd() if err != nil { - log.Fatalf("Failed to get current working directory: %s", err) + t.Errorf("Failed to get current working directory: %s", err) } fmt.Printf("Publishing\n") err = g.HandleArtifacts(meta, gopath, cwd, false, true, true, "") if err != nil { - log.Fatalf("post-build processing failed: %s", err) + t.Errorf("post-build processing failed: %s", err) } err = g.HandleExtras(meta, gopath, cwd, false, true) if err != nil { - log.Fatalf("Extra artifact processing failed: %s", err) + t.Errorf("Extra artifact processing failed: %s", err) } } diff --git a/pkg/gomason/gomason.go b/pkg/gomason/gomason.go index b621b00..be62769 100644 --- a/pkg/gomason/gomason.go +++ b/pkg/gomason/gomason.go @@ -271,7 +271,7 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo return err } -// CollectFileAndSignature grabs a file and the signature if it exists and moves it from the temp workspace into the CWD where gomason was called. +// CollectFileAndSignature grabs a file and the signature if it exists and copies it from the temp workspace into the CWD where gomason was called. func CollectFileAndSignature(cwd string, filename string) (err error) { binaryDestinationPath := fmt.Sprintf("%s/%s", cwd, filepath.Base(filename))