Skip to content

Commit

Permalink
Merge pull request #91 from buildpulse/fixdocs
Browse files Browse the repository at this point in the history
Updated coverage usage docs
  • Loading branch information
siddhantdange authored Jul 30, 2023
2 parents 9013f99 + 4e758ba commit 3dee63d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 43 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
28 changes: 15 additions & 13 deletions internal/cmd/submit/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())
Expand Down Expand Up @@ -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()
}

Expand Down
105 changes: 82 additions & 23 deletions internal/cmd/submit/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3dee63d

Please sign in to comment.