diff --git a/cli/init.go b/cli/init.go index 77dae08..52a23e9 100644 --- a/cli/init.go +++ b/cli/init.go @@ -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 @@ -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) } diff --git a/cli/init_test.go b/cli/init_test.go index c987f99..6517d68 100644 --- a/cli/init_test.go +++ b/cli/init_test.go @@ -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) { @@ -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("") }