-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b852531
commit aa0e4fb
Showing
14 changed files
with
174 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,13 @@ | |
"*_test.go" | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "size", | ||
"args": { | ||
"module": "/internal", | ||
"max": 49.9 | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package validators | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ismailbayram/bigpicture/internal/graph" | ||
"strings" | ||
) | ||
|
||
type SizeValidatorArgs struct { | ||
Module string `json:"module" validate:"required=true"` | ||
Max float64 `json:"max" validate:"required=true,min=1"` | ||
Ignore []string `json:"ignore"` | ||
} | ||
|
||
type SizeValidator struct { | ||
args *SizeValidatorArgs | ||
tree *graph.Tree | ||
} | ||
|
||
func NewSizeValidator(args map[string]any, tree *graph.Tree) (*SizeValidator, error) { | ||
validatorArgs := &SizeValidatorArgs{} | ||
if err := validateArgs(args, validatorArgs); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(validatorArgs.Module) > 1 && strings.HasSuffix(validatorArgs.Module, "/*") { | ||
validatorArgs.Module = validatorArgs.Module[:len(validatorArgs.Module)-2] | ||
} | ||
|
||
module, err := validatePath(validatorArgs.Module, tree) | ||
if err != nil { | ||
return nil, err | ||
} | ||
validatorArgs.Module = module | ||
|
||
return &SizeValidator{ | ||
args: validatorArgs, | ||
tree: tree, | ||
}, nil | ||
} | ||
|
||
func (v *SizeValidator) Validate() error { | ||
totalSize := v.tree.Nodes[v.args.Module].LineCount | ||
|
||
for _, node := range v.tree.Nodes { | ||
if isIgnored(v.args.Ignore, node.Path) || node.Type == graph.File { | ||
continue | ||
} | ||
|
||
nodeSizePercent := float64(node.LineCount) / float64(totalSize) * 100 | ||
|
||
if node.Parent == v.args.Module && nodeSizePercent > v.args.Max { | ||
return fmt.Errorf( | ||
"Size of module '%s' is %.2f%%, but maximum allowed is %.2f%%", | ||
node.Path, | ||
nodeSizePercent, | ||
v.args.Max, | ||
) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package validators | ||
|
||
import ( | ||
"github.com/ismailbayram/bigpicture/internal/graph" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestNewSizeValidator(t *testing.T) { | ||
tree := graph.NewTree("root") | ||
tree.Nodes["a"] = graph.NewNode("a", "a", graph.Dir, []string{}) | ||
tree.Nodes["b"] = graph.NewNode("b", "b", graph.Dir, []string{}) | ||
tree.Nodes["a"].LineCount = 100 | ||
tree.Nodes["b"].LineCount = 200 | ||
|
||
args := map[string]any{} | ||
_, err := NewSizeValidator(args, nil) | ||
assert.Equal(t, "module is required and must be string", err.Error()) | ||
|
||
args = map[string]any{"module": "a"} | ||
_, err = NewSizeValidator(args, nil) | ||
assert.Equal(t, "max is required and must be float64", err.Error()) | ||
|
||
args = map[string]any{"module": "a", "max": "wrong"} | ||
_, err = NewSizeValidator(args, nil) | ||
assert.Equal(t, "max is required and must be float64", err.Error()) | ||
|
||
args = map[string]any{"module": "wrong", "max": float64(100)} | ||
_, err = NewSizeValidator(args, tree) | ||
assert.Equal(t, "'wrong' is not a valid module. Path should start with /", err.Error()) | ||
|
||
args = map[string]any{"module": "a", "max": float64(100)} | ||
validator, err := NewSizeValidator(args, tree) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "a", validator.args.Module) | ||
assert.Equal(t, float64(100), validator.args.Max) | ||
} | ||
|
||
func TestSizeValidator_Validate(t *testing.T) { | ||
tree := graph.NewTree("root") | ||
tree.Nodes["."].LineCount = 600 | ||
tree.Nodes["/dir1"] = graph.NewNode("dir1", "/dir1", graph.Dir, []string{}) | ||
tree.Nodes["/dir1"].LineCount = 100 | ||
tree.Nodes["/dir2"] = graph.NewNode("dir2", "/dir2", graph.Dir, []string{}) | ||
tree.Nodes["/dir2"].LineCount = 200 | ||
tree.Nodes["/dir3"] = graph.NewNode("dir3", "/dir3", graph.Dir, []string{}) | ||
tree.Nodes["/dir3"].LineCount = 300 | ||
|
||
args := map[string]any{"module": ".", "max": float64(40)} | ||
validator, _ := NewSizeValidator(args, tree) | ||
err := validator.Validate() | ||
assert.Equal(t, "Size of module '/dir3' is 50.00%, but maximum allowed is 40.00%", err.Error()) | ||
|
||
args = map[string]any{"module": ".", "max": float64(50)} | ||
validator, _ = NewSizeValidator(args, tree) | ||
err = validator.Validate() | ||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters