Skip to content

Commit

Permalink
tests: verify code in _examples
Browse files Browse the repository at this point in the history
A new test has been added in order to verify that all code under
_examples is up to date and has not broken due to API changes.

This test runs as the last step of the test.yml CI workflow.

Closes go-git#912.
  • Loading branch information
crazybolillo committed Feb 15, 2024
1 parent 0dfff25 commit 38f88c7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ jobs:
- name: Test
run: make test-coverage

- name: Verify Examples
env:
EXAMPLES_DIR: _examples
run: go test -test.v -test.run=^TestBuildExamples$
38 changes: 38 additions & 0 deletions build_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package git

import (
"os"
"os/exec"
"path/filepath"
"testing"
)

func TestBuildExamples(t *testing.T) {
examplesDir := os.Getenv("EXAMPLES_DIR")
if len(examplesDir) == 0 {
t.Skip("EXAMPLES_DIR not set")
}

examplesDir, err := filepath.Abs(examplesDir)
if err != nil {
t.Fatal(err.Error())
}

glob := filepath.Join(examplesDir, "/**/main.go")
matches, err := filepath.Glob(glob)
for _, match := range matches {
testName, err := filepath.Rel(examplesDir, match)
if err != nil {
t.Fatal(err.Error())
}

testName = filepath.Dir(testName)
t.Run(testName, func(t *testing.T) {
cmd := exec.Command("go", "build", "-o", os.DevNull)
cmd.Dir = filepath.Dir(match)
if err := cmd.Run(); err != nil {
t.Error(err.Error())
}
})
}
}

0 comments on commit 38f88c7

Please sign in to comment.