Skip to content

Commit

Permalink
Upload code coverage (#137)
Browse files Browse the repository at this point in the history
We were `exec`'ing a binary as part of our tests, which prevented us
from collecting coverage. Instead, invoke the command directly as part
of the test suite.
  • Loading branch information
blampe authored May 14, 2024
1 parent 5a4913e commit c7baa61
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ jobs:
**/go.sum
- name: Run Go Build
run: |
go build -ldflags="-X github.com/pulumi/crd2pulumi/gen.Version=0.0.1" -o "${{ github.workspace }}/bin/crd2pulumi" main.go
echo "${{ github.workspace }}/bin" >> $GITHUB_PATH
run: make build

- name: Run tests
run: go test -v .
working-directory: tests
run: make test

- name: Upload coverage
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Goreleaser publishing dry run
uses: goreleaser/goreleaser-action@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ releases/
main
dist/
bin/
coverage.txt
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ install::
go install -ldflags "-X ${PROJECT}/${VERSION_PATH}=${VERSION}"

test::
$(GO) test -v ./tests/
$(GO) test -v -coverprofile="coverage.txt" -covermode=atomic -coverpkg=./... ./...
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Notice that by just setting a language-specific output path (--pythonPath, --nod
still get generated, so setting -p, -n, etc becomes unnecessary.
`

func Execute() error {
func New() *cobra.Command {
dotNetSettings := &codegen.CodegenSettings{Language: "dotnet"}
goSettings := &codegen.CodegenSettings{Language: "go"}
nodejsSettings := &codegen.CodegenSettings{Language: "nodejs"}
Expand Down Expand Up @@ -134,5 +134,5 @@ func Execute() error {
f.BoolVarP(&nodejsSettings.ShouldGenerate, "nodejs", "n", false, "generate NodeJS")
f.BoolVarP(&pythonSettings.ShouldGenerate, "python", "p", false, "generate Python")
f.BoolVarP(&javaSettings.ShouldGenerate, "java", "j", false, "generate Java")
return rootCmd.Execute()
return rootCmd
}
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
informational: true
patch:
default:
informational: true
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func main() {
err := cmd.Execute()
err := cmd.New().Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
Expand Down
22 changes: 12 additions & 10 deletions tests/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package tests

import (
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/pulumi/crd2pulumi/cmd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -39,15 +41,16 @@ func execCrd2Pulumi(t *testing.T, lang, path string, additionalValidation func(t
os.RemoveAll(tmpdir)
})
langFlag := fmt.Sprintf("--%sPath", lang) // e.g. --dotnetPath
binaryPath, err := filepath.Abs("../bin/crd2pulumi")
if err != nil {
t.Fatalf("unable to create absolute path to binary: %s", err)
}

t.Logf("%s %s=%s %s: running", binaryPath, langFlag, tmpdir, path)
crdCmd := exec.Command(binaryPath, langFlag, tmpdir, "--force", path)
crdOut, err := crdCmd.CombinedOutput()
t.Logf("%s %s=%s %s: output=\n%s", binaryPath, langFlag, tmpdir, path, crdOut)
cmd := cmd.New()
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
cmd.SetArgs([]string{langFlag, tmpdir, "--force", path})
cmd.SetOut(stdout)
cmd.SetErr(stderr)

t.Logf("crd2pulumi %s=%s %s: running", langFlag, tmpdir, path)
err = cmd.Execute()
t.Logf("%s=%s %s: output=\n%s", langFlag, tmpdir, path, stdout.String()+stderr.String())
if err != nil {
t.Fatalf("expected crd2pulumi for '%s=%s %s' to succeed", langFlag, tmpdir, path)
}
Expand Down Expand Up @@ -123,7 +126,6 @@ func TestKubernetesVersionNodeJs(t *testing.T) {
validateVersion := func(t *testing.T, path string) {
// enter and build the generated package
withDir(t, path, func() {

runRequireNoError(t, exec.Command("npm", "install"))
runRequireNoError(t, exec.Command("npm", "run", "build"))

Expand Down Expand Up @@ -151,7 +153,7 @@ func withDir(t *testing.T, dir string, f func()) {

func appendFile(t *testing.T, filename, content string) {
// extract the version returned by a resource
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o600)
require.NoError(t, err)
defer f.Close()

Expand Down

0 comments on commit c7baa61

Please sign in to comment.