Skip to content

Commit 0eb6225

Browse files
committed
Update testing setup
1 parent 6a13f5c commit 0eb6225

File tree

6 files changed

+92
-31
lines changed

6 files changed

+92
-31
lines changed

script/application.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ func (a *Application) FileName() string {
152152
}
153153

154154
func (a *Application) SetApprover() error {
155-
if isTesting() {
156-
a.ApproverId = 123
157-
a.ApproverUsername = "test-username"
158-
159-
return nil
160-
}
161-
162155
approverIdValue, err := getEnv("APPROVER_ID")
163156
if err != nil {
164157
return err

script/application_test.go

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import (
66
"testing"
77
)
88

9+
func setupTestDir(targetDir string) (cleanupFunc func(), err error) {
10+
originalDir, err := os.Getwd()
11+
if err != nil {
12+
return nil, fmt.Errorf("failed to get current directory: %s", err)
13+
}
14+
15+
if err := os.Chdir(targetDir); err != nil {
16+
return nil, fmt.Errorf("failed to change working directory: %s", err)
17+
}
18+
19+
return func() {
20+
if err := os.Chdir(originalDir); err != nil {
21+
fmt.Printf("Failed to change back to original directory: %s\n", err)
22+
}
23+
}, nil
24+
}
25+
926
func errNoProjectName(sectionTitle string) error {
1027
return fmt.Errorf("**%s** is missing project name", sectionTitle)
1128
}
@@ -38,21 +55,13 @@ func errInvalidURL(sectionTitle string) error {
3855
return fmt.Errorf("**%s** is an invalid URL", sectionTitle)
3956
}
4057

41-
func TestApplication(t *testing.T) {
42-
originalDir, err := os.Getwd()
58+
func TestApplication_Parse(t *testing.T) {
59+
cleanup, err := setupTestDir("../")
4360
if err != nil {
44-
t.Fatalf("Failed to get current directory: %s", err)
61+
t.Fatalf(err.Error())
4562
}
4663

47-
defer func() {
48-
if err := os.Chdir(originalDir); err != nil {
49-
t.Fatalf("Failed to change back to original directory: %s", err)
50-
}
51-
}()
52-
53-
if err := os.Chdir("../"); err != nil {
54-
t.Fatalf("Failed to change working directory: %s", err)
55-
}
64+
defer cleanup()
5665

5766
testCases := []struct {
5867
name string
@@ -147,3 +156,52 @@ func TestApplication(t *testing.T) {
147156
})
148157
}
149158
}
159+
160+
func TestApplication_FileName(t *testing.T) {
161+
testCases := []struct {
162+
issueNumber int
163+
projectName string
164+
expectedFilename string
165+
}{
166+
{123, "Test project", "123-test-project.json"},
167+
{456, "My_Team", "456-my_team.json"},
168+
{789, "Event: Re/Act?", "789-event-react.json"},
169+
{101, "Project--With---Dashes", "101-project-with-dashes.json"},
170+
}
171+
172+
for _, tt := range testCases {
173+
t.Run(tt.expectedFilename, func(t *testing.T) {
174+
app := Application{
175+
IssueNumber: tt.issueNumber,
176+
Project: Project{Name: tt.projectName},
177+
}
178+
179+
filename := app.FileName()
180+
if filename != tt.expectedFilename {
181+
t.Errorf("FileName() is %s, expected %v", filename, tt.expectedFilename)
182+
}
183+
})
184+
}
185+
}
186+
187+
func TestApplication_SetApprover(t *testing.T) {
188+
cleanup, err := setupTestDir("../")
189+
if err != nil {
190+
t.Fatalf(err.Error())
191+
}
192+
193+
defer cleanup()
194+
195+
setTestApplication("project")
196+
app := Application{}
197+
198+
app.SetApprover()
199+
200+
if app.ApproverId != 123 {
201+
t.Errorf("SetApprover() set ApproverId to %d, expected 123", app.ApproverId)
202+
}
203+
204+
if app.ApproverUsername != "wendyappleseed" {
205+
t.Errorf("SetApprover() set ApproverUsername to %s, expected wendyappleseed", app.ApproverUsername)
206+
}
207+
}

script/approver.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ import (
1313
)
1414

1515
type Approver struct {
16-
gitHub GitHub
17-
application Application
16+
gitHub *GitHub
17+
application *Application
1818
}
1919

2020
func (a *Approver) Approve() {
21-
a.gitHub = GitHub{}
22-
a.application = Application{}
23-
2421
if err := a.application.SetApprover(); err != nil {
2522
a.logErrorAndExit("could not set application approver", err)
2623
}

script/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,21 @@ func main() {
5050
printUsageAndExit()
5151
}
5252

53+
github := GitHub{}
54+
application := Application{}
55+
5356
switch command {
5457
case "review":
55-
reviewer := Reviewer{}
58+
reviewer := Reviewer{
59+
gitHub: &github,
60+
application: &application,
61+
}
5662
reviewer.Review()
5763
case "approve":
58-
approver := Approver{}
64+
approver := Approver{
65+
gitHub: &github,
66+
application: &application,
67+
}
5968
approver.Approve()
6069
default:
6170
fmt.Printf("Invalid command: %s\n", command)

script/reviewer.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ const (
1717
)
1818

1919
type Reviewer struct {
20-
gitHub GitHub
21-
application Application
20+
gitHub *GitHub
21+
application *Application
2222
}
2323

2424
func (r *Reviewer) Review() {
25-
r.gitHub = GitHub{}
26-
r.application = Application{}
27-
2825
if err := r.gitHub.Init(); err != nil {
2926
r.logErrorAndExit("could not initialize GitHub client", err)
3027
}

script/testing.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ func setTestApplication(testName string) {
193193
log.Fatalf(err.Error())
194194
}
195195

196+
if err := os.Setenv("APPROVER_ID", "123"); err != nil {
197+
log.Fatalf("Failed to set environment variable: %s", err)
198+
}
199+
if err := os.Setenv("APPROVER_USERNAME", "wendyappleseed"); err != nil {
200+
log.Fatalf("Failed to set environment variable: %s", err)
201+
}
202+
196203
testIssue = &issue
197204
}
198205

0 commit comments

Comments
 (0)