Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: include err value in test errors #85

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions cmd/notary/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,32 @@ pebble_notices: false`
func TestMain(m *testing.M) {
cmd := exec.Command("go", "install", "./...")
if err := cmd.Run(); err != nil {
log.Fatalf("couldn't install the notary CLI")
log.Fatalf("couldn't install the notary CLI: %v", err)
}

testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
log.Fatalf("couldn't create temp directory: %v", err)
}
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
err = os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %v", err)
}
err = os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %v", err)
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
log.Fatalf("couldn't enter testing directory: %v", err)
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
log.Fatalf("couldn't change back to parent directory: %v", err)
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
log.Fatalf("couldn't remove temp testing directory: %v", err)
}
os.Exit(exitval)
}
Expand All @@ -145,22 +148,22 @@ func TestNotaryFail(t *testing.T) {
{"database not connectable", []string{"-config", "config.yaml"}, invalidDBConfig, "Couldn't connect to database:"},
}
for _, tc := range cases {
writeConfigErr := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if writeConfigErr != nil {
t.Errorf("Failed writing config file")
err := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if err != nil {
t.Errorf("Failed writing config file: %v", err)
}
flag.CommandLine = flag.NewFlagSet(tc.Name, flag.ExitOnError)
cmd := exec.Command("notary", tc.Args...)
stdout, _ := cmd.StdoutPipe()

if err := cmd.Start(); err != nil {
t.Errorf("Failed running command")
t.Errorf("Failed running command: %v", err)
}

slurp, _ := io.ReadAll(stdout)

if err := cmd.Wait(); err == nil {
t.Errorf("Command did not fail")
t.Errorf("Command did not fail: %s", tc.Name)
}
if !strings.Contains(string(slurp), tc.ExpectedOutput) {
t.Errorf("%s: Expected error not found: %s", tc.Name, slurp)
Expand Down
27 changes: 15 additions & 12 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ yaml.here`
func TestMain(m *testing.M) {
testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
log.Fatalf("couldn't create temp directory: %s", err)
}
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
err = os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
err = os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
log.Fatalf("couldn't enter testing directory: %s", err)
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
log.Fatalf("couldn't change back to parent directory: %s", err)
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
log.Fatalf("couldn't remove temp testing directory: %s", err)
}
os.Exit(exitval)
}
Expand Down Expand Up @@ -104,11 +107,11 @@ func TestBadConfigFail(t *testing.T) {
}

for _, tc := range cases {
writeConfigErr := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if writeConfigErr != nil {
t.Errorf("Failed writing config file")
err := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if err != nil {
t.Errorf("Failed writing config file: %v", err)
}
_, err := config.Validate("config.yaml")
_, err = config.Validate("config.yaml")
if err == nil {
t.Errorf("Expected error, got nil")
}
Expand Down
19 changes: 11 additions & 8 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,27 @@ D7DC34n8CH9+avz9sCRwxpjxKnYW/BeyK0c4n9uZpjI8N4sOVqy6yWBUseww
func TestMain(m *testing.M) {
testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
log.Fatalf("couldn't create temp directory: %s", err)
}
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
err = os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
err = os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
log.Fatalf("couldn't enter testing directory: %s", err)
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
log.Fatalf("couldn't change back to parent directory: %s", err)
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
log.Fatalf("couldn't remove temp testing directory: %s", err)
}
os.Exit(exitval)
}
Expand Down
Loading