From 4e758baf874084d3a45a36f26675c91114041cd1 Mon Sep 17 00:00:00 2001 From: Sid Dange Date: Sun, 30 Jul 2023 00:04:58 -0400 Subject: [PATCH] Updated coverage usage docs --- README.md | 14 ++-- internal/cmd/submit/submit.go | 28 ++++---- internal/cmd/submit/submit_test.go | 105 ++++++++++++++++++++++------- 3 files changed, 104 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 4f7b1c15..c0590cd5 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,13 @@ To use `test-reporter` with another CI provider, the following environment varia | `REPOSITORY_NAME` | Name of the repository | The following are flags that can be set. Make sure to **set flags after CLI args**. -| Environment Variable | Required | Description | -|----------------------|----------------------------------|----------------------------------------------| -| `account-id` | ✓ | BuildPulse account ID (see dashboard) | -| `repository-id` | ✓ | BuildPulse repository ID (see dashboard) | -| `repository-dir` | Only if `tree` not set | Path to repository directory | -| `tree` | Only if `repository-dir` not set | Git tree SHA | -| `coverage-files` | If using BuildPulse Coverage | **Space-separated** paths to coverage files. | +| Environment Variable | Required | Description | +|----------------------|-----------------------------------|----------------------------------------------| +| `account-id` | ✓ | BuildPulse account ID (see dashboard) | +| `repository-id` | ✓ | BuildPulse repository ID (see dashboard) | +| `repository-dir` | Only if `tree` not set | Path to repository directory | +| `tree` | Only if `repository-dir` not set | Git tree SHA | +| `coverage-files` | Only if using BuildPulse Coverage | **Space-separated** paths to coverage files. | Example: ``` diff --git a/internal/cmd/submit/submit.go b/internal/cmd/submit/submit.go index 688685e3..230e15ed 100644 --- a/internal/cmd/submit/submit.go +++ b/internal/cmd/submit/submit.go @@ -67,17 +67,18 @@ type Submit struct { logger logger.Logger version *metadata.Version - envs map[string]string - paths []string - coveragePathsString string - coveragePaths []string - bucket string - accountID uint64 - repositoryID uint64 - repositoryPath string - tree string - credentials credentials - commitResolver metadata.CommitResolver + envs map[string]string + paths []string + coveragePathsString string + coveragePaths []string + bucket string + accountID uint64 + repositoryID uint64 + repositoryPath string + tree string + disableCoverageAutoDiscovery bool + credentials credentials + commitResolver metadata.CommitResolver } // NewSubmit creates a new Submit instance. @@ -94,7 +95,8 @@ func NewSubmit(version *metadata.Version, log logger.Logger) *Submit { s.fs.Uint64Var(&s.repositoryID, "repository-id", 0, "BuildPulse repository ID (required)") s.fs.StringVar(&s.repositoryPath, "repository-dir", ".", "Path to local clone of repository") s.fs.StringVar(&s.tree, "tree", "", "SHA-1 hash of git tree") - s.fs.StringVar(&s.coveragePathsString, "coverage-files", "", "Paths to coverage files or directories containing coverage files (space-separated)") + s.fs.StringVar(&s.coveragePathsString, "coverage-files", "", "Paths to coverage files (space-separated)") + s.fs.BoolVar(&s.disableCoverageAutoDiscovery, "disable-coverage-auto", false, "Disables coverage file autodiscovery") s.fs.SetOutput(io.Discard) // Disable automatic writing to STDERR s.logger.Printf("Current version: %s", s.version.String()) @@ -288,7 +290,7 @@ func (s *Submit) bundle() (string, error) { // if coverage file paths are not provided, we infer them var coveragePaths = s.coveragePaths - if len(coveragePaths) == 0 { + if len(coveragePaths) == 0 && !s.disableCoverageAutoDiscovery { coveragePaths, err = s.coveragePathsInferred() } diff --git a/internal/cmd/submit/submit_test.go b/internal/cmd/submit/submit_test.go index dc088aa3..7585672f 100644 --- a/internal/cmd/submit/submit_test.go +++ b/internal/cmd/submit/submit_test.go @@ -46,6 +46,38 @@ func TestSubmit_Init(t *testing.T) { assert.Equal(t, "Repository", s.commitResolver.Source()) }) + t.Run("WithCoveragePathString", func(t *testing.T) { + s := NewSubmit(&metadata.Version{}, logger.New()) + err := s.Init([]string{"testdata/example-reports-dir/example-*.xml", "--account-id", "42", "--repository-id", "8675309", "--coverage-files", "./dir1/**/*.xml ./dir2/**/*.xml"}, exampleEnv, new(stubCommitResolverFactory)) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"testdata/example-reports-dir/example-1.xml"}, s.paths) + assert.EqualValues(t, 42, s.accountID) + assert.EqualValues(t, 8675309, s.repositoryID) + assert.Equal(t, "buildpulse-uploads", s.bucket) + assert.Equal(t, "some-access-key-id", s.credentials.AccessKeyID) + assert.Equal(t, "some-secret-access-key", s.credentials.SecretAccessKey) + assert.Equal(t, exampleEnv, s.envs) + assert.Equal(t, ".", s.repositoryPath) + assert.Equal(t, "Repository", s.commitResolver.Source()) + assert.Equal(t, s.coveragePaths, []string{"./dir1/**/*.xml", "./dir2/**/*.xml"}) + }) + + t.Run("WithDisableCoverageAutoDiscovery", func(t *testing.T) { + s := NewSubmit(&metadata.Version{}, logger.New()) + err := s.Init([]string{"testdata/example-reports-dir/example-*.xml", "--account-id", "42", "--repository-id", "8675309", "--disable-coverage-auto"}, exampleEnv, new(stubCommitResolverFactory)) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"testdata/example-reports-dir/example-1.xml"}, s.paths) + assert.EqualValues(t, 42, s.accountID) + assert.EqualValues(t, 8675309, s.repositoryID) + assert.Equal(t, "buildpulse-uploads", s.bucket) + assert.Equal(t, "some-access-key-id", s.credentials.AccessKeyID) + assert.Equal(t, "some-secret-access-key", s.credentials.SecretAccessKey) + assert.Equal(t, exampleEnv, s.envs) + assert.Equal(t, ".", s.repositoryPath) + assert.Equal(t, "Repository", s.commitResolver.Source()) + assert.True(t, s.disableCoverageAutoDiscovery) + }) + t.Run("WithMultiplePathArgs", func(t *testing.T) { s := NewSubmit(&metadata.Version{}, logger.New()) err := s.Init( @@ -374,29 +406,6 @@ func TestSubmit_Init_invalidRepoPath(t *testing.T) { assert.Regexp(t, "invalid value for flag -repository-dir: .* is not a directory", err.Error()) } }) - - t.Run("WithCoveragePathString", func(t *testing.T) { - tmpfile, err := os.CreateTemp(os.TempDir(), "buildpulse-cli-test-fixture") - require.NoError(t, err) - defer os.Remove(tmpfile.Name()) - - s := NewSubmit(&metadata.Version{}, logger.New()) - err = s.Init([]string{ - ".", - "--account-id", "42", - "--repository-id", "8675309", - "--repository-dir", tmpfile.Name(), - "--coverage-files", "./dir1/**/*.xml ./dir2/**/*.xml", - }, - exampleEnv, - &stubCommitResolverFactory{}, - ) - if assert.Error(t, err) { - assert.Regexp(t, "invalid value for flag -repository-dir: .* is not a directory", err.Error()) - } - - assert.Equal(t, s.coveragePaths, []string{"./dir1/**/*.xml", "./dir2/**/*.xml"}) - }) } func TestSubmit_Run(t *testing.T) { @@ -550,6 +559,56 @@ func Test_bundle(t *testing.T) { require.NoError(t, err) assert.Contains(t, string(logdata), "Gathering metadata to describe the build") }) + + t.Run("bundle with disabled coverage file autodetection", func(t *testing.T) { + envs := map[string]string{ + "GITHUB_ACTIONS": "true", + "GITHUB_SHA": "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb", + } + + log := logger.New() + s := &Submit{ + logger: log, + version: &metadata.Version{Number: "v1.2.3"}, + commitResolver: metadata.NewStaticCommitResolver(&metadata.Commit{TreeSHA: "ccccccccccccccccccccdddddddddddddddddddd"}, log), + envs: envs, + paths: []string{"testdata/example-reports-dir/example-1.xml"}, + bucket: "buildpulse-uploads", + disableCoverageAutoDiscovery: true, + accountID: 42, + repositoryID: 8675309, + } + + path, err := s.bundle() + require.NoError(t, err) + + unzipDir := t.TempDir() + err = archiver.Unarchive(path, unzipDir) + require.NoError(t, err) + + // Verify buildpulse.yml is present and contains expected content + yaml, err := os.ReadFile(filepath.Join(unzipDir, "buildpulse.yml")) + require.NoError(t, err) + assert.Contains(t, string(yaml), ":ci_provider: github-actions") + assert.Contains(t, string(yaml), ":commit: aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb") + assert.Contains(t, string(yaml), ":tree: ccccccccccccccccccccdddddddddddddddddddd") + assert.Contains(t, string(yaml), ":reporter_version: v1.2.3") + + // Verify test report XML file is present and contains expected content + assertEqualContent(t, + "testdata/example-reports-dir/example-1.xml", + filepath.Join(unzipDir, "test_results/testdata/example-reports-dir/example-1.xml"), + ) + + ignoredCoverageReportPath := filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage/report.xml") + _, err = os.Stat(ignoredCoverageReportPath) + assert.True(t, os.IsNotExist(err)) + + // Verify buildpulse.log is present and contains expected content + logdata, err := os.ReadFile(filepath.Join(unzipDir, "buildpulse.log")) + require.NoError(t, err) + assert.Contains(t, string(logdata), "Gathering metadata to describe the build") + }) } func Test_upload(t *testing.T) {