From 0eb6225c5a2b48c0a97a52d39bc169d893a431e5 Mon Sep 17 00:00:00 2001 From: Jody Heavener Date: Fri, 5 Apr 2024 11:43:22 -0700 Subject: [PATCH] Update testing setup --- script/application.go | 7 ---- script/application_test.go | 82 ++++++++++++++++++++++++++++++++------ script/approver.go | 7 +--- script/main.go | 13 +++++- script/reviewer.go | 7 +--- script/testing.go | 7 ++++ 6 files changed, 92 insertions(+), 31 deletions(-) diff --git a/script/application.go b/script/application.go index 3f546252..ceba0fcb 100644 --- a/script/application.go +++ b/script/application.go @@ -152,13 +152,6 @@ func (a *Application) FileName() string { } func (a *Application) SetApprover() error { - if isTesting() { - a.ApproverId = 123 - a.ApproverUsername = "test-username" - - return nil - } - approverIdValue, err := getEnv("APPROVER_ID") if err != nil { return err diff --git a/script/application_test.go b/script/application_test.go index 673da046..c465812e 100644 --- a/script/application_test.go +++ b/script/application_test.go @@ -6,6 +6,23 @@ import ( "testing" ) +func setupTestDir(targetDir string) (cleanupFunc func(), err error) { + originalDir, err := os.Getwd() + if err != nil { + return nil, fmt.Errorf("failed to get current directory: %s", err) + } + + if err := os.Chdir(targetDir); err != nil { + return nil, fmt.Errorf("failed to change working directory: %s", err) + } + + return func() { + if err := os.Chdir(originalDir); err != nil { + fmt.Printf("Failed to change back to original directory: %s\n", err) + } + }, nil +} + func errNoProjectName(sectionTitle string) error { return fmt.Errorf("**%s** is missing project name", sectionTitle) } @@ -38,21 +55,13 @@ func errInvalidURL(sectionTitle string) error { return fmt.Errorf("**%s** is an invalid URL", sectionTitle) } -func TestApplication(t *testing.T) { - originalDir, err := os.Getwd() +func TestApplication_Parse(t *testing.T) { + cleanup, err := setupTestDir("../") if err != nil { - t.Fatalf("Failed to get current directory: %s", err) + t.Fatalf(err.Error()) } - defer func() { - if err := os.Chdir(originalDir); err != nil { - t.Fatalf("Failed to change back to original directory: %s", err) - } - }() - - if err := os.Chdir("../"); err != nil { - t.Fatalf("Failed to change working directory: %s", err) - } + defer cleanup() testCases := []struct { name string @@ -147,3 +156,52 @@ func TestApplication(t *testing.T) { }) } } + +func TestApplication_FileName(t *testing.T) { + testCases := []struct { + issueNumber int + projectName string + expectedFilename string + }{ + {123, "Test project", "123-test-project.json"}, + {456, "My_Team", "456-my_team.json"}, + {789, "Event: Re/Act?", "789-event-react.json"}, + {101, "Project--With---Dashes", "101-project-with-dashes.json"}, + } + + for _, tt := range testCases { + t.Run(tt.expectedFilename, func(t *testing.T) { + app := Application{ + IssueNumber: tt.issueNumber, + Project: Project{Name: tt.projectName}, + } + + filename := app.FileName() + if filename != tt.expectedFilename { + t.Errorf("FileName() is %s, expected %v", filename, tt.expectedFilename) + } + }) + } +} + +func TestApplication_SetApprover(t *testing.T) { + cleanup, err := setupTestDir("../") + if err != nil { + t.Fatalf(err.Error()) + } + + defer cleanup() + + setTestApplication("project") + app := Application{} + + app.SetApprover() + + if app.ApproverId != 123 { + t.Errorf("SetApprover() set ApproverId to %d, expected 123", app.ApproverId) + } + + if app.ApproverUsername != "wendyappleseed" { + t.Errorf("SetApprover() set ApproverUsername to %s, expected wendyappleseed", app.ApproverUsername) + } +} diff --git a/script/approver.go b/script/approver.go index 6abbb8fa..5b07ac93 100644 --- a/script/approver.go +++ b/script/approver.go @@ -13,14 +13,11 @@ import ( ) type Approver struct { - gitHub GitHub - application Application + gitHub *GitHub + application *Application } func (a *Approver) Approve() { - a.gitHub = GitHub{} - a.application = Application{} - if err := a.application.SetApprover(); err != nil { a.logErrorAndExit("could not set application approver", err) } diff --git a/script/main.go b/script/main.go index 395702f4..ab7dea16 100644 --- a/script/main.go +++ b/script/main.go @@ -50,12 +50,21 @@ func main() { printUsageAndExit() } + github := GitHub{} + application := Application{} + switch command { case "review": - reviewer := Reviewer{} + reviewer := Reviewer{ + gitHub: &github, + application: &application, + } reviewer.Review() case "approve": - approver := Approver{} + approver := Approver{ + gitHub: &github, + application: &application, + } approver.Approve() default: fmt.Printf("Invalid command: %s\n", command) diff --git a/script/reviewer.go b/script/reviewer.go index b6ff3cab..4714e14b 100644 --- a/script/reviewer.go +++ b/script/reviewer.go @@ -17,14 +17,11 @@ const ( ) type Reviewer struct { - gitHub GitHub - application Application + gitHub *GitHub + application *Application } func (r *Reviewer) Review() { - r.gitHub = GitHub{} - r.application = Application{} - if err := r.gitHub.Init(); err != nil { r.logErrorAndExit("could not initialize GitHub client", err) } diff --git a/script/testing.go b/script/testing.go index 0bb1f494..7a28fec4 100644 --- a/script/testing.go +++ b/script/testing.go @@ -193,6 +193,13 @@ func setTestApplication(testName string) { log.Fatalf(err.Error()) } + if err := os.Setenv("APPROVER_ID", "123"); err != nil { + log.Fatalf("Failed to set environment variable: %s", err) + } + if err := os.Setenv("APPROVER_USERNAME", "wendyappleseed"); err != nil { + log.Fatalf("Failed to set environment variable: %s", err) + } + testIssue = &issue }