diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ce4f20..70a7e84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.gitignore b/.gitignore index 02fbed8..bef838a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ releases/ main dist/ bin/ +coverage.txt diff --git a/Makefile b/Makefile index d072fc6..7f34a00 100644 --- a/Makefile +++ b/Makefile @@ -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=./... ./... diff --git a/cmd/root.go b/cmd/root.go index c5a3d32..40ba213 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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"} @@ -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 } diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..bfdc987 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,8 @@ +coverage: + status: + project: + default: + informational: true + patch: + default: + informational: true diff --git a/main.go b/main.go index 4d63207..e7ca113 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/tests/crds_test.go b/tests/crds_test.go index 49864b4..662c8b4 100644 --- a/tests/crds_test.go +++ b/tests/crds_test.go @@ -15,6 +15,7 @@ package tests import ( + "bytes" "fmt" "io/fs" "os" @@ -22,6 +23,7 @@ import ( "path/filepath" "testing" + "github.com/pulumi/crd2pulumi/cmd" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -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) } @@ -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")) @@ -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()