diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d9db4a..f006f88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,8 @@ on: - '**' pull_request: workflow_dispatch: +env: + CGO_ENABLED: 0 jobs: tests: @@ -24,15 +26,10 @@ jobs: - uses: actions/setup-go@v5 with: go-version: '>=1.19.0' - - name: Build sources - run: go build - env: - # Note: -race requires cgo - CGO_ENABLED: 0 - - name: Run smoke tests + - run: go build + - run: go test -v env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: go test -v linter: name: Run staticcheck runs-on: ubuntu-latest diff --git a/.github/workflows/private-repo.yml b/.github/workflows/private-repo.yml index e186941..1bdc58f 100644 --- a/.github/workflows/private-repo.yml +++ b/.github/workflows/private-repo.yml @@ -2,6 +2,8 @@ name: Private Repository on: push: branches: [master] +env: + CGO_ENABLED: 0 jobs: private-repo-test: @@ -11,11 +13,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version: '>=1.19.0' - - name: Build sources - run: go build - env: - # Note: -race requires cgo - CGO_ENABLED: 0 + - run: go build - name: Generate run: | ./changelog-from-release -r 'https://github.com/rhysd/private-repo-test' > OUTPUT.md diff --git a/main_test.go b/main_test.go index deee1e4..b44e611 100644 --- a/main_test.go +++ b/main_test.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "regexp" "runtime" "strings" @@ -13,30 +12,28 @@ import ( "github.com/google/go-cmp/cmp" ) -func validateExecutable(t *testing.T) string { - exe := "changelog-from-release" - if runtime.GOOS == "windows" { - exe = exe + ".exe" - } +var testExecutablePath = "" - if _, err := os.Stat(exe); err != nil { - t.Fatal("Executable not found:", exe) - } +func ensureExecutable(t *testing.T) string { + t.Helper() - if s, err := os.Stat(".git"); err != nil || !s.IsDir() { - t.Fatal("Test did not run at root of repository") - } - - cwd, err := os.Getwd() - if err != nil { - panic(err) + if testExecutablePath == "" { + b, err := exec.Command("go", "build").CombinedOutput() + if err != nil { + t.Fatal("Compile error while building `changelog-from-release` executable:", err, ":", string(b)) + } + if runtime.GOOS == "windows" { + testExecutablePath = `.\changelog-from-release.exe` + } else { + testExecutablePath = `./changelog-from-release` + } } - return filepath.Join(cwd, exe) + return testExecutablePath } func TestGenerateChangelog(t *testing.T) { - exe := validateExecutable(t) + exe := ensureExecutable(t) b, err := os.ReadFile("CHANGELOG.md") if err != nil { @@ -56,7 +53,7 @@ func TestGenerateChangelog(t *testing.T) { } func TestVersion(t *testing.T) { - exe := validateExecutable(t) + exe := ensureExecutable(t) b, err := exec.Command(exe, "-v").CombinedOutput() out := string(b) @@ -71,7 +68,7 @@ func TestVersion(t *testing.T) { } func TestGenerateWithRemoteURL(t *testing.T) { - exe := validateExecutable(t) + exe := ensureExecutable(t) b, err := exec.Command(exe, "-r", "https://github.com/rhysd/action-setup-vim").CombinedOutput() out := string(b) if err != nil { @@ -112,7 +109,7 @@ func TestGenerateWithRemoteURL(t *testing.T) { } func TestInvalidRemoteURL(t *testing.T) { - exe := validateExecutable(t) + exe := ensureExecutable(t) tests := []struct { what string input string @@ -150,7 +147,7 @@ func TestInvalidRemoteURL(t *testing.T) { } func TestInvalidGitHubToken(t *testing.T) { - exe := validateExecutable(t) + exe := ensureExecutable(t) c := exec.Command(exe) c.Env = append(c.Environ(), "GITHUB_TOKEN=invalid")