Skip to content

Commit

Permalink
Update testing setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jodyheavener committed Apr 5, 2024
1 parent 6a13f5c commit 0eb6225
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 31 deletions.
7 changes: 0 additions & 7 deletions script/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 70 additions & 12 deletions script/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
7 changes: 2 additions & 5 deletions script/approver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 11 additions & 2 deletions script/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions script/reviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions script/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 0eb6225

Please sign in to comment.