Skip to content

Commit

Permalink
Merge pull request #90 from buildpulse/fixcov
Browse files Browse the repository at this point in the history
Create blocklist to filter out unwanted matched files
  • Loading branch information
siddhantdange authored Jul 30, 2023
2 parents f7d4739 + a486242 commit 9013f99
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 16 deletions.
143 changes: 137 additions & 6 deletions internal/cmd/submit/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,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 {
coveragePaths, err = coveragePathsInferred()
coveragePaths, err = s.coveragePathsInferred()
}

if err == nil && len(coveragePaths) > 0 {
Expand Down Expand Up @@ -452,11 +452,15 @@ func xmlPathsFromArgs(args []string) ([]string, error) {
return paths, nil
}

func globPatternFromPattern(pattern string) string {
return fmt.Sprintf("./**/%s", pattern)
func globPatternFromPattern(repositoryPath string, pattern string) string {
if len(repositoryPath) == 0 {
repositoryPath = "."
}

return fmt.Sprintf("%s/**/%s", repositoryPath, pattern)
}

func coveragePathsInferred() ([]string, error) {
func (s *Submit) coveragePathsInferred() ([]string, error) {
coverageFileTypes := []string{
"*coverage*.*",
"nosetests.xml",
Expand All @@ -481,9 +485,120 @@ func coveragePathsInferred() ([]string, error) {
"test_cov.xml",
}

fileBlocklistMatchers := []string{
`__pycache__`,
`node_modules/.*`,
`vendor`,
`\.circleci`,
`\.git`,
`\.gitignore`,
`\.nvmrc`,
`\.nyc_output`,
`\.tox`,
`.*\.am$`,
`.*\.bash$`,
`.*\.bat$`,
`.*\.bw$`,
`.*\.cfg$`,
`.*\.class$`,
`.*\.cmake$`,
`.*\.cmake$`,
`.*\.conf$`,
`.*\.coverage$`,
`.*\.cp$`,
`.*\.cpp$`,
`.*\.crt$`,
`.*\.css$`,
`.*\.csv$`,
`.*\.csv$`,
`.*\.data$`,
`.*\.db$`,
`.*\.dox$`,
`.*\.ec$`,
`.*\.ec$`,
`.*\.egg$`,
`.*\.egg-info$`,
`.*\.el$`,
`.*\.env$`,
`.*\.erb$`,
`.*\.exe$`,
`.*\.ftl$`,
`.*\.gif$`,
`.*\.go$`,
`.*\.gradle$`,
`.*\.gz$`,
`.*\.h$`,
`.*\.html$`,
`.*\.in$`,
`.*\.jade$`,
`.*\.jar.*$`,
`.*\.jpeg$`,
`.*\.jpg$`,
`.*\.js$`,
`.*\.less$`,
`.*\.log$`,
`.*\.m4$`,
`.*\.mak.*$`,
`.*\.map$`,
`.*\.marker$`,
`.*\.md$`,
`.*\.o$`,
`.*\.p12$`,
`.*\.pem$`,
`.*\.png$`,
`.*\.pom.*$`,
`.*\.profdata$`,
`.*\.proto$`,
`.*\.ps1$`,
`.*\.pth$`,
`.*\.py$`,
`.*\.pyc$`,
`.*\.pyo$`,
`.*\.rb$`,
`.*\.rsp$`,
`.*\.rst$`,
`.*\.ru$`,
`.*\.sbt$`,
`.*\.scss$`,
`.*\.scss$`,
`.*\.serialized$`,
`.*\.sh$`,
`.*\.snapshot$`,
`.*\.sql$`,
`.*\.svg$`,
`.*\.tar\.tz$`,
`.*\.template$`,
`.*\.ts$`,
`.*\.whl$`,
`.*\.xcconfig$`,
`.*\.xcoverage\..*$`,
`.*/classycle/report\.xml$`,
`.*codecov\.yml$`,
`.*~$`,
`.*\.coveragerc$`,
`\.coverage.*$`,
`codecov\.SHA256SUM$`,
`codecov\.SHA256SUM\.sig$`,
`coverage-summary\.json$`,
`createdFiles\.lst$`,
`fullLocaleNames\.lst$`,
`include\.lst$`,
`inputFiles\.lst$`,
`phpunit-code-coverage\.xml$`,
`phpunit-coverage\.xml$`,
`remapInstanbul\.coverage.*\.json$`,
`scoverage\.measurements\..*$`,
`test-result-.*-codecoverage\.json$`,
`test_.*_coverage\.txt$`,
`testrunner-coverage.*$`,
`.*\..*js$`,
`\.yarn$`,
`.*\.zip$`,
}

filePaths := []string{}
for _, filePattern := range coverageFileTypes {
fullPattern := globPatternFromPattern(filePattern)
fullPattern := globPatternFromPattern(s.repositoryPath, filePattern)
candidates, err := filepathx.Glob(fullPattern)

if err != nil {
Expand All @@ -493,7 +608,23 @@ func coveragePathsInferred() ([]string, error) {
filePaths = append(filePaths, candidates...)
}

return filePaths, nil
sanitized := []string{}
for _, filepath := range filePaths {
var matched = false
for _, blocklistPattern := range fileBlocklistMatchers {
regex, _ := regexp.Compile(blocklistPattern)
if regex.MatchString(filepath) {
matched = true
break
}
}

if !matched {
sanitized = append(sanitized, filepath)
}
}

return sanitized, nil
}

// xmlPathsFromDir returns a list of all the XML files in the given directory
Expand Down
24 changes: 14 additions & 10 deletions internal/cmd/submit/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func Test_bundle(t *testing.T) {
commitResolver: metadata.NewStaticCommitResolver(&metadata.Commit{TreeSHA: "ccccccccccccccccccccdddddddddddddddddddd"}, log),
envs: envs,
paths: []string{"testdata/example-reports-dir/example-1.xml"},
coveragePaths: []string{"testdata/example-reports-dir/coverage-files/report.xml", "testdata/example-reports-dir/coverage-files/report-2.xml"},
coveragePaths: []string{"testdata/example-reports-dir/coverage/report.xml", "testdata/example-reports-dir/coverage/report-2.xml"},
bucket: "buildpulse-uploads",
accountID: 42,
repositoryID: 8675309,
Expand Down Expand Up @@ -477,13 +477,13 @@ func Test_bundle(t *testing.T) {

// Verify coverage files are present and contains expected content
assertEqualContent(t,
"testdata/example-reports-dir/coverage-files/report.xml",
filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage-files/report.xml"),
"testdata/example-reports-dir/coverage/report.xml",
filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage/report.xml"),
)

assertEqualContent(t,
"testdata/example-reports-dir/coverage-files/report-2.xml",
filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage-files/report-2.xml"),
"testdata/example-reports-dir/coverage/report-2.xml",
filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage/report-2.xml"),
)

// Verify buildpulse.log is present and contains expected content
Expand Down Expand Up @@ -533,14 +533,18 @@ func Test_bundle(t *testing.T) {

// Verify coverage file is present and contains expected content
assertEqualContent(t,
"testdata/example-reports-dir/coverage-files/report.xml",
filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage-files/report.xml"),
"testdata/example-reports-dir/coverage/report.xml",
filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage/report.xml"),
)

ignoredCoverageReportPath := filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage-files/report-2.xml")
ignoredCoverageReportPath := filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/coverage/report-2.xml")
_, err = os.Stat(ignoredCoverageReportPath)
assert.True(t, os.IsNotExist(err))

ignoredSourceFilePath := filepath.Join(unzipDir, "coverage/testdata/example-reports-dir/vendor/simplecov/coverage_statistic.go")
_, err = os.Stat(ignoredSourceFilePath)
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)
Expand Down Expand Up @@ -662,8 +666,8 @@ func Test_xmlPathsFromDir(t *testing.T) {
name: "DirectoryWithFilesAtRootAndInSubDirectories",
path: "testdata/example-reports-dir",
want: []string{
"testdata/example-reports-dir/coverage-files/report.xml",
"testdata/example-reports-dir/coverage-files/report-2.xml",
"testdata/example-reports-dir/coverage/report.xml",
"testdata/example-reports-dir/coverage/report-2.xml",
"testdata/example-reports-dir/example-1.xml",
"testdata/example-reports-dir/example-2.XML",
"testdata/example-reports-dir/dir-with-xml-files/browserstack/example-1.xml",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}

0 comments on commit 9013f99

Please sign in to comment.