Skip to content

Commit

Permalink
ensure executable is up-to-date on running smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 6, 2024
1 parent 71d6bb1 commit f789e22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
- '**'
pull_request:
workflow_dispatch:
env:
CGO_ENABLED: 0

jobs:
tests:
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/private-repo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Private Repository
on:
push:
branches: [master]
env:
CGO_ENABLED: 0

jobs:
private-repo-test:
Expand All @@ -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
Expand Down
41 changes: 19 additions & 22 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f789e22

Please sign in to comment.