Skip to content

Commit

Permalink
Fixes tests and fixes behaviour of testing/has-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bvobart committed Jun 24, 2021
1 parent 37a2f72 commit 40599ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
25 changes: 18 additions & 7 deletions linters/testing/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"

"github.com/bvobart/gocover-cobertura/cobertura"
"github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
"github.com/joshdk/go-junit"

"github.com/bvobart/mllint/api"
Expand Down Expand Up @@ -72,21 +74,30 @@ func (l *TestingLinter) ScoreRuleHasTests(report *api.Report, project api.Projec
return
}

if len(l.TestFiles) < int(l.Config.Targets.Minimum) {
numTests := len(l.TestFiles)
if numTests < int(l.Config.Targets.Minimum) {
report.Scores[RuleHasTests] = 0
report.Details[RuleHasTests] = fmt.Sprintf("There are **%d** test files in your project, but `mllint` was expecting at least **%d**.", len(l.TestFiles), l.Config.Targets.Minimum)
report.Details[RuleHasTests] = fmt.Sprintf("There %s **%d** test files in your project, but `mllint` was expecting at least **%d**.", english.PluralWord(numTests, "is", "are"), numTests, l.Config.Targets.Minimum)
return
}

// determine expected ratio of test files vs. other Python files
// determine expected and actual ratio of test files vs. other Python files
expectedRatio := float64(l.Config.Targets.Ratio.Tests) / float64(l.Config.Targets.Ratio.Other)
actualRatio := float64(len(l.TestFiles)) / float64(len(project.PythonFiles))
actualRatio := float64(numTests) / float64(len(project.PythonFiles)-numTests) // project.PythonFiles includes the test files, so we subtract them
// score is basically: actual ratio / expected ratio
report.Scores[RuleHasTests] = math.Min(100*actualRatio/expectedRatio, 100)

// add details
fileStr := english.PluralWord(numTests, "file", "")
if actualRatio < expectedRatio {
report.Details[RuleHasTests] = fmt.Sprintf("There are **%d** test files in your project, which meets the minimum of **%d** test files required.", len(l.TestFiles), l.Config.Targets.Minimum)
report.Details[RuleHasTests] += fmt.Sprintf("\n\nHowever, this only equates to **%.1f%%** of Python files in your project being tests, while `mllint` expects that **%.1f%%** of your project's Python files are tests.", 100*actualRatio, 100*expectedRatio)
report.Details[RuleHasTests] = fmt.Sprintf("There %s **%d** test %s in your project, which meets the minimum of **%d** test %s required.", english.PluralWord(numTests, "is", "are"), numTests, fileStr, l.Config.Targets.Minimum, fileStr)
report.Details[RuleHasTests] += fmt.Sprintf("\n\nHowever, this only equates to **%s%%** of Python files in your project being tests, while `mllint` expects that **%s%%** of your project's Python files are tests.", humanize.Ftoa(100*actualRatio), humanize.Ftoa(100*expectedRatio))
} else {
report.Details[RuleHasTests] = fmt.Sprintf("Great! Your project contains **%d** test files, which meets the minimum of **%d** test files required.\nThis equates to **%.1f%%** of Python files in your project being tests, which meets the target ratio of **%.1f%%**", len(l.TestFiles), l.Config.Targets.Minimum, 100*actualRatio, 100*expectedRatio)
report.Details[RuleHasTests] = fmt.Sprintf(
`Great! Your project contains **%d** test %s, which meets the minimum of **%d** test files required.
This equates to **%s%%** of Python files in your project being tests, which meets the target ratio of **%s%%**`,
numTests, fileStr, l.Config.Targets.Minimum, humanize.Ftoa(100*actualRatio), humanize.Ftoa(100*expectedRatio),
)
}
}

Expand Down
46 changes: 36 additions & 10 deletions linters/testing/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestTestingLinter(t *stdtesting.T) {
Expect: func(t *stdtesting.T, report api.Report, err error) {
require.NoError(t, err)
require.EqualValues(t, 0, report.Scores[testing.RuleHasTests])
require.Equal(t, "There are **0** test files in your project, but `mllint` was expecting at least **1**.", report.Details[testing.RuleHasTests])
require.EqualValues(t, 0, report.Scores[testing.RuleTestsPass])
require.EqualValues(t, 0, report.Scores[testing.RuleTestCoverage])
require.EqualValues(t, 0, report.Scores[testing.RuleTestsFolder])
Expand All @@ -57,6 +58,10 @@ func TestTestingLinter(t *stdtesting.T) {
Expect: func(t *stdtesting.T, report api.Report, err error) {
require.NoError(t, err)
require.EqualValues(t, 25, report.Scores[testing.RuleHasTests])
require.Contains(t, report.Details[testing.RuleHasTests], "There is **1** test file in your project")
require.Contains(t, report.Details[testing.RuleHasTests], "minimum of **1** test file required")
require.Contains(t, report.Details[testing.RuleHasTests], "equates to **6.25%** of Python files in your project being tests")
require.Contains(t, report.Details[testing.RuleHasTests], "`mllint` expects that **25%** of your project's Python files are tests")
require.EqualValues(t, 0, report.Scores[testing.RuleTestsPass])
require.EqualValues(t, 0, report.Scores[testing.RuleTestCoverage])
require.EqualValues(t, 0, report.Scores[testing.RuleTestsFolder])
Expand All @@ -69,32 +74,53 @@ func TestTestingLinter(t *stdtesting.T) {
Expect: func(t *stdtesting.T, report api.Report, err error) {
require.NoError(t, err)
require.EqualValues(t, 100, report.Scores[testing.RuleHasTests])
require.Contains(t, report.Details[testing.RuleHasTests], "Your project contains **4** test files")
require.Contains(t, report.Details[testing.RuleHasTests], "meets the minimum of **1** test files required")
require.Contains(t, report.Details[testing.RuleHasTests], "equates to **25%** of Python files in your project being tests")
require.Contains(t, report.Details[testing.RuleHasTests], "meets the target ratio of **25%**")
require.EqualValues(t, 0, report.Scores[testing.RuleTestsPass])
require.EqualValues(t, 0, report.Scores[testing.RuleTestCoverage])
require.EqualValues(t, 0, report.Scores[testing.RuleTestsFolder])
},
},
{
Name: "FourTestsSixteenFiles/InTestsFolder",
Dir: ".",
Options: testutils.NewOptions().UsePythonFiles(createPythonFilenames(16).Concat(createPythonTestFilenames(4).Prefix("tests"))),
Name: "FourTestsSixteenFiles/InTestsFolder",
Dir: ".",
Options: testutils.NewOptions().UsePythonFiles(
createPythonFilenames(16).Concat(createPythonTestFilenames(4).Prefix("tests")),
),
Expect: func(t *stdtesting.T, report api.Report, err error) {
require.NoError(t, err)
require.EqualValues(t, 100, report.Scores[testing.RuleHasTests])
require.EqualValues(t, 0, report.Scores[testing.RuleTestsPass])
require.EqualValues(t, 0, report.Scores[testing.RuleTestCoverage])
require.EqualValues(t, 100, report.Scores[testing.RuleTestsFolder])
},
},
{
Name: "FourTestsSixteenFiles/InTestsFolderAbsolute",
Dir: utils.AbsolutePath("."),
Options: testutils.NewOptions().UsePythonFiles(createPythonFilenames(16).Concat(createPythonTestFilenames(4).Prefix(path.Join(utils.AbsolutePath("."), "tests")))),
Name: "FourTestsSixteenFiles/HalfInTestsFolder",
Dir: ".",
Options: testutils.NewOptions().UsePythonFiles(
createPythonFilenames(16).
Concat(createPythonTestFilenames(2).Prefix("tests")).
Concat(createPythonTestFilenames(2)),
),
Expect: func(t *stdtesting.T, report api.Report, err error) {
require.NoError(t, err)
require.EqualValues(t, 100, report.Scores[testing.RuleHasTests])
require.EqualValues(t, 50, report.Scores[testing.RuleTestsFolder])
require.Contains(t, report.Details[testing.RuleTestsFolder], "The following test files have been detected that are **not** in the `tests` folder at the root of your project")
require.Contains(t, report.Details[testing.RuleTestsFolder], "- file0_test.py")
require.Contains(t, report.Details[testing.RuleTestsFolder], "- file1_test.py")
},
},
{
Name: "FourTestsSixteenFiles/InTestsFolderAbsolute",
Dir: utils.AbsolutePath("."),
Options: testutils.NewOptions().UsePythonFiles(
createPythonFilenames(16).Concat(createPythonTestFilenames(4).Prefix(path.Join(utils.AbsolutePath("."), "tests"))),
),
Expect: func(t *stdtesting.T, report api.Report, err error) {
require.NoError(t, err)
require.EqualValues(t, 100, report.Scores[testing.RuleHasTests])
require.EqualValues(t, 0, report.Scores[testing.RuleTestsPass])
require.EqualValues(t, 0, report.Scores[testing.RuleTestCoverage])
require.EqualValues(t, 100, report.Scores[testing.RuleTestsFolder])
},
},
Expand Down

0 comments on commit 40599ae

Please sign in to comment.