Skip to content

Commit

Permalink
more tests for dispatch init command
Browse files Browse the repository at this point in the history
  • Loading branch information
chicoxyzzy committed Jun 26, 2024
1 parent 687ac93 commit a33b465
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func getAppDataDir(appName string) (string, error) {
return appDataDir, nil
}

func getLatestCommitSHA(repo string) (string, error) {
url := fmt.Sprintf(githubAPIURL, repo)
func getLatestCommitSHA(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
Expand Down Expand Up @@ -305,7 +304,8 @@ func initCommand() *cobra.Command {
}

// get the latest commit SHA from the templates repository
remoteSHA, err := getLatestCommitSHA(repo)
url := fmt.Sprintf(githubAPIURL, repo)
remoteSHA, err := getLatestCommitSHA(url)
if err != nil {
cmd.Printf("failed to get latest commit SHA: %v", err)
}
Expand Down
32 changes: 29 additions & 3 deletions cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func TestInitCommand(t *testing.T) {

result, _ := directoryExists(tempFile)
assert.False(t, result)

os.Remove(tempFile)
})

t.Run("isDirectoryEmpty returns true for empty directory", func(t *testing.T) {
Expand All @@ -57,7 +55,35 @@ func TestInitCommand(t *testing.T) {

result, _ := isDirectoryEmpty(tempDir)
assert.False(t, result)
})

t.Run("downloadAndExtractTemplates downloads and extracts templates", func(t *testing.T) {
t.Parallel()

tempDir := t.TempDir()

err := downloadAndExtractTemplates(tempDir)
assert.Nil(t, err)

// Check if the templates directory was created
result, _ := isDirectoryEmpty(tempDir)
assert.False(t, result)
})

t.Run("getLatestCommitSHA returns the latest commit SHA", func(t *testing.T) {
t.Parallel()

sha, err := getLatestCommitSHA("https://api.github.com/repos/dispatchrun/dispatch/branches/main")
assert.Nil(t, err)
assert.Regexp(t, "^[a-f0-9]{40}$", sha)
})

os.Remove(tempFile)
t.Run("getLatestCommitSHA returns an error for invalid URL", func(t *testing.T) {
t.Parallel()

_, err := getLatestCommitSHA("invalidurl")
assert.NotNil(t, err)
})

// t.Run("")
}

0 comments on commit a33b465

Please sign in to comment.