Skip to content

Commit

Permalink
Implements config structure for custom rule definitions, adds api met…
Browse files Browse the repository at this point in the history
…hod to make api.Rule definition from config.CustomRule
  • Loading branch information
bvobart committed Jul 15, 2021
1 parent 5aa8e36 commit bdbfb6d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
18 changes: 18 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/bvobart/mllint/api"
"github.com/bvobart/mllint/categories"
"github.com/bvobart/mllint/config"
)

func TestCategoryString(t *testing.T) {
Expand Down Expand Up @@ -67,3 +68,20 @@ func TestMergeReports(t *testing.T) {

require.Equal(t, expectedReport, finalReport)
}

func TestNewCustomRule(t *testing.T) {
cr := config.CustomRule{
Name: "Custom Test Rule",
Slug: "custom/test-rule",
Details: "Tests whether parsing a custom rule from a YAML config works",
Weight: 420,
Run: "python ./scripts/mllint-test-rule.py",
}
rule := api.NewCustomRule(cr)

require.Equal(t, cr.Name, rule.Name)
require.Equal(t, cr.Slug, rule.Slug)
require.Equal(t, cr.Details, rule.Details)
require.Equal(t, cr.Weight, rule.Weight)
require.False(t, rule.Disabled)
}
13 changes: 13 additions & 0 deletions api/rule.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package api

import "github.com/bvobart/mllint/config"

// Rule is a struct for defining what a rule looks like that `mllint` will check.
type Rule struct {
// Slug should be a lowercased, dashed reference code, e.g. 'git-no-big-files'
Expand Down Expand Up @@ -27,3 +29,14 @@ func (r *Rule) Disable() {
func (r *Rule) Enable() {
r.Disabled = false
}

// NewCustomRule creates a new rule based on a custom rule definition as can be configured in `mllint`'s config
func NewCustomRule(cr config.CustomRule) Rule {
return Rule{
Slug: cr.Slug,
Name: cr.Name,
Details: cr.Details,
Weight: cr.Weight,
Disabled: false,
}
}
33 changes: 29 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,42 @@ type Config struct {
Testing TestingConfig `yaml:"testing" toml:"testing"`
}

//---------------------------------------------------------------------------------------

// RuleConfig contains info about which rules are enabled / disabled.
type RuleConfig struct {
Disabled []string `yaml:"disabled" toml:"disabled"`
Disabled []string `yaml:"disabled" toml:"disabled"`
Custom []CustomRule `yaml:"custom" toml:"custom"`
}

// CustomRule contains the configuration for custom rules
type CustomRule struct {
Name string `yaml:"name" toml:"name"`
Slug string `yaml:"slug" toml:"slug"`
Details string `yaml:"details" toml:"details"`
Weight float64 `yaml:"weight" toml:"weight"`
Run string `yaml:"run" toml:"run"`
}

//---------------------------------------------------------------------------------------

// GitConfig contains the configuration for the Git linters.
type GitConfig struct {
// Maximum size of files in bytes tolerated by the 'git-no-big-files' linter
// Default is 10 MB
MaxFileSize uint64 `yaml:"maxFileSize" toml:"maxFileSize"`
}

//---------------------------------------------------------------------------------------

// CodeQualityConfig contains the configuration for the CQ linters used in the Code Quality category
type CodeQualityConfig struct {
// Defines all code linters to use in the Code Quality category
Linters []string `yaml:"linters" toml:"linters"`
}

//---------------------------------------------------------------------------------------

// TestingConfig contains the configuration for the rules in the Testing category.
type TestingConfig struct {
// Filename of the project's test execution report, either absolute or relative to the project's root.
Expand Down Expand Up @@ -88,9 +106,16 @@ type TestCoverageTargets struct {

func Default() *Config {
return &Config{
Rules: RuleConfig{Disabled: []string{}},
Git: GitConfig{MaxFileSize: 10_000_000}, // 10 MB
CodeQuality: CodeQualityConfig{Linters: []string{"pylint", "mypy", "black", "isort", "bandit"}},
Rules: RuleConfig{
Disabled: []string{},
Custom: []CustomRule{},
},
Git: GitConfig{
MaxFileSize: 10_000_000, // 10 MB
},
CodeQuality: CodeQualityConfig{
Linters: []string{"pylint", "mypy", "black", "isort", "bandit"},
},
Testing: TestingConfig{
Targets: TestingTargets{
Minimum: 1,
Expand Down
66 changes: 66 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ testing:
line: 50 # percent line coverage.
`

const yamlCustomRule = `
rules:
custom:
- name: Custom Test Rule
slug: custom/test-rule
details: Tests whether parsing a custom rule from a YAML config works
weight: 420
run: python ./scripts/mllint-test-rule.py
- name: Custom Test Rule
slug: custom/test-rule
details: Tests whether parsing a custom rule from a YAML config works
weight: 420
run: python ./scripts/mllint-test-rule.py
`

const yamlInvalid = `
rules:
disabled: nothing
Expand All @@ -69,6 +84,23 @@ report = "tests-report.xml"
targets = { minimum = 2, ratio = { tests = 2, other = 8 }}
coverage = { report = "coverage.xml", targets = { line = 100.0 }}
`
const tomlCustomRule = `
[tool.mllint.rules]
[[tool.mllint.rules.custom]]
name = "Custom Test Rule"
slug = "custom/test-rule"
details = "Tests whether parsing a custom rule from a YAML config works"
weight = 420.0
run = "python ./scripts/mllint-test-rule.py"
[[tool.mllint.rules.custom]]
name = "Custom Test Rule"
slug = "custom/test-rule"
details = "Tests whether parsing a custom rule from a YAML config works"
weight = 420.0
run = "python ./scripts/mllint-test-rule.py"
`

const tomlInvalid = `
[tool.mllint.rules]
Expand Down Expand Up @@ -133,6 +165,23 @@ func TestParseYAML(t *testing.T) {
}(),
Err: nil,
},
{
Name: "YamlCustomRule",
File: strings.NewReader(yamlCustomRule),
Expected: func() *config.Config {
c := config.Default()
customRule := config.CustomRule{
Name: "Custom Test Rule",
Slug: "custom/test-rule",
Details: "Tests whether parsing a custom rule from a YAML config works",
Weight: 420,
Run: "python ./scripts/mllint-test-rule.py",
}
c.Rules.Custom = []config.CustomRule{customRule, customRule}
return c
}(),
Err: nil,
},
{
Name: "YamlError",
File: strings.NewReader(yamlInvalid),
Expand Down Expand Up @@ -217,6 +266,23 @@ func TestParseTOML(t *testing.T) {
}(),
Err: nil,
},
{
Name: "TomlCustomRule",
File: strings.NewReader(tomlCustomRule),
Expected: func() *config.Config {
c := config.Default()
customRule := config.CustomRule{
Name: "Custom Test Rule",
Slug: "custom/test-rule",
Details: "Tests whether parsing a custom rule from a YAML config works",
Weight: 420,
Run: "python ./scripts/mllint-test-rule.py",
}
c.Rules.Custom = []config.CustomRule{customRule, customRule}
return c
}(),
Err: nil,
},
{
Name: "TomlError",
File: strings.NewReader(tomlInvalid),
Expand Down

0 comments on commit bdbfb6d

Please sign in to comment.