Skip to content

Commit

Permalink
Fixes two small layout issues and an error reporting bug in OpenFile
Browse files Browse the repository at this point in the history
  • Loading branch information
bvobart committed Jun 25, 2021
1 parent aa3e0ff commit eb1c0aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
8 changes: 4 additions & 4 deletions linters/testing/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ func isInTestsFolder(projectdir, testFile string) bool {
func (l *TestingLinter) ScoreRuleTestsPass(report *api.Report, project api.Project) {
if l.Config.Report == "" {
report.Scores[RuleTestsPass] = 0
report.Details[RuleTestsPass] = "No test report was provided. Please update the `testing.report` setting in your project's `mllint` configuration to specify the path to your project's test report.\n\n" + howToMakeJUnitXML
report.Details[RuleTestsPass] = "No test report was provided.\n\nPlease update the `testing.report` setting in your project's `mllint` configuration to specify the path to your project's test report.\n\n" + howToMakeJUnitXML
return
}

junitReportPath := path.Join(project.Dir, l.Config.Report)
if !utils.FileExists(junitReportPath) {
report.Scores[RuleTestsPass] = 0
report.Details[RuleTestsPass] = fmt.Sprintf("A test report was provided, namely `%s`, but this file could not be found. Please update the `testing.report` setting in your project's `mllint` configuration to fix the path to your project's test report. Remember that this path must be relative to the root of your project directory.", l.Config.Report)
report.Details[RuleTestsPass] = fmt.Sprintf("A test report was provided, namely `%s`, but this file could not be found.\n\nPlease update the `testing.report` setting in your project's `mllint` configuration to fix the path to your project's test report. Remember that this path must be relative to the root of your project directory.", l.Config.Report)
return
}

Expand Down Expand Up @@ -207,14 +207,14 @@ Please make sure your test report file is a valid JUnit XML file. %s`, l.Config.
func (l *TestingLinter) ScoreRuleTestCoverage(report *api.Report, project api.Project) {
if l.Config.Coverage.Report == "" {
report.Scores[RuleTestCoverage] = 0
report.Details[RuleTestCoverage] = "No test coverage report was provided. Please update the `testing.coverage.report` setting in your project's `mllint` configuration to specify the path to your project's test coverage report.\n\n" + howToMakeCoverageXML
report.Details[RuleTestCoverage] = "No test coverage report was provided.\n\nPlease update the `testing.coverage.report` setting in your project's `mllint` configuration to specify the path to your project's test coverage report.\n\n" + howToMakeCoverageXML
return
}

covReportFile, err := utils.OpenFile(project.Dir, l.Config.Coverage.Report)
if err != nil {
report.Scores[RuleTestCoverage] = 0
report.Details[RuleTestCoverage] = fmt.Sprintf("A test coverage report was provided, namely `%s`, but this file could not be found or opened (%s). Please update the `testing.coverage.report` setting in your project's `mllint` configuration to fix the path to your project's test report. Remember that this path must be relative to the root of your project directory.", l.Config.Coverage.Report, err.Error())
report.Details[RuleTestCoverage] = fmt.Sprintf("A test coverage report was provided, namely `%s`, but this file could not be found or opened (error: `%s`).\n\nPlease update the `testing.coverage.report` setting in your project's `mllint` configuration to fix the path to your project's test report. Remember that this path must be relative to the root of your project directory.", l.Config.Coverage.Report, err.Error())
return
}

Expand Down
16 changes: 7 additions & 9 deletions utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,21 @@ func FolderIsEmpty(filename string) (bool, error) {
// Will return a non-nil error when either no or more than one files match.
// Returns the opened file otherwise.
func OpenFile(folder string, pattern string) (*os.File, error) {
matches, err := filepath.Glob(folder + "/" + pattern)
matches, err := filepath.Glob(path.Join(folder, pattern))
if err != nil {
return nil, err
}

cwd, err := os.Getwd()
if err != nil {
return nil, err
if len(matches) == 1 {
return os.Open(matches[0])
}

folderpath := AbsolutePath(folder)
if len(matches) == 0 {
return nil, fmt.Errorf("did not find a file matching %s in folder %s/%s: %w", pattern, cwd, folder, os.ErrNotExist)
} else if len(matches) > 1 {
return nil, fmt.Errorf("pattern %s in folder %s/%s matches multiple files: %+v", pattern, cwd, folder, matches)
} else {
return os.Open(matches[0])
return nil, fmt.Errorf("did not find a file matching %s in folder %s: %w", pattern, folderpath, os.ErrNotExist)
}

return nil, fmt.Errorf("pattern %s in folder %s matches multiple files: %+v", pattern, folderpath, matches)
}

// AbsolutePath returns the absolute path to the given file.
Expand Down

0 comments on commit eb1c0aa

Please sign in to comment.