Skip to content

Commit

Permalink
feat: refined the message display for some checks
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufhm committed Feb 14, 2024
1 parent 649c4b8 commit f6495cf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
7 changes: 3 additions & 4 deletions pkg/checks/file/filecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"path/filepath"
"strings"

"github.com/salsadigitalauorg/shipshape/pkg/config"
"github.com/salsadigitalauorg/shipshape/pkg/result"
Expand Down Expand Up @@ -64,8 +63,8 @@ func (c *FileCheck) RunCheck() {
c.AddPass("No illegal files")
return
}
c.AddBreach(&result.ValueBreach{
ValueLabel: "illegal files found",
Value: strings.Join(files, ","),
c.AddBreach(&result.KeyValuesBreach{
Key: "illegal files found",
Values: files,
})
}
11 changes: 7 additions & 4 deletions pkg/checks/file/filecheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ func TestFileCheckRunCheck(t *testing.T) {
assert.Equal(0, len(c.Result.Passes))
assert.EqualValues(
[]result.Breach{
&result.ValueBreach{
BreachType: "value",
&result.KeyValuesBreach{
BreachType: "key-values",
CheckType: "file",
CheckName: "filecheck2",
Severity: "normal",
ValueLabel: "illegal files found",
Value: "testdata/adminer.php,testdata/sub/phpmyadmin.php",
Key: "illegal files found",
Values: []string{
"testdata/adminer.php",
"testdata/sub/phpmyadmin.php",
},
},
},
c.Result.Breaches,
Expand Down
17 changes: 9 additions & 8 deletions pkg/checks/phpstan/phpstan.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,19 @@ func (c *PhpStanCheck) RunCheck() {
for file, errors := range c.phpstanResult.Files {
errLines := []string{}
for _, er := range errors.Messages {
errLines = append(errLines, fmt.Sprintf("line %d: %s", er.Line, er.Message))

errLines = append(errLines,
fmt.Sprintf("line %d: %s", er.Line,
strings.ReplaceAll(er.Message, "\n", "")))
}
c.AddBreach(&result.KeyValueBreach{
Key: fmt.Sprintf("file contains banned functions: %s", file),
Value: strings.Join(errLines, "\n"),
c.AddBreach(&result.KeyValuesBreach{
Key: fmt.Sprintf("file: %s", file),
Values: errLines,
})
}

if len(c.phpstanResult.Errors) > 0 {
c.AddBreach(&result.ValueBreach{
ValueLabel: "errors encountered when running phpstan",
Value: strings.Join(c.phpstanResult.Errors, "\n")})
c.AddBreach(&result.KeyValuesBreach{
Key: "errors encountered when running phpstan",
Values: c.phpstanResult.Errors})
}
}
16 changes: 8 additions & 8 deletions pkg/checks/phpstan/phpstan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ func TestRunCheck(t *testing.T) {
c.Result.DetermineResultStatus(false)
assert.Equal(result.Fail, c.Result.Status)
assert.EqualValues(
[]result.Breach{&result.KeyValueBreach{
BreachType: "key-value",
Key: "file contains banned functions: /app/web/themes/custom/custom/test-theme/info.php",
Value: "line 3: Calling curl_exec() is forbidden, please change the code",
[]result.Breach{&result.KeyValuesBreach{
BreachType: "key-values",
Key: "file: /app/web/themes/custom/custom/test-theme/info.php",
Values: []string{"line 3: Calling curl_exec() is forbidden, please change the code"},
}},
c.Result.Breaches,
)
Expand All @@ -289,10 +289,10 @@ func TestRunCheck(t *testing.T) {
c.Result.DetermineResultStatus(false)
assert.Equal(result.Fail, c.Result.Status)
assert.EqualValues(
[]result.Breach{&result.ValueBreach{
BreachType: "value",
ValueLabel: "errors encountered when running phpstan",
Value: "Error found in file foo",
[]result.Breach{&result.KeyValuesBreach{
BreachType: "key-values",
Key: "errors encountered when running phpstan",
Values: []string{"Error found in file foo"},
}},
c.Result.Breaches,
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/result/breach.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ type KeyValuesBreach struct {

func (b KeyValuesBreach) String() string {
if b.KeyLabel != "" && b.ValueLabel != "" {
return fmt.Sprintf("[%s:%s] %s: %s", b.KeyLabel, b.Key, b.ValueLabel,
"["+strings.Join(b.Values, ", ")+"]")
return fmt.Sprintf("[%s:%s] %s:\n - %s", b.KeyLabel, b.Key, b.ValueLabel,
strings.Join(b.Values, "\n - "))
}
return fmt.Sprintf("%s: %s", b.Key, "["+strings.Join(b.Values, ", ")+"]")
return fmt.Sprintf("%s:\n - %s", b.Key, strings.Join(b.Values, "\n - "))
}

func BreachGetKeyLabel(bIfc Breach) string {
Expand Down
4 changes: 3 additions & 1 deletion pkg/result/breach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func TestBreachKeyValuesBreachStringers(t *testing.T) {
ValueLabel: "disallowed permissions",
Values: []string{"delete the site", "delete the world"},
},
expected: "[role:admin] disallowed permissions: [delete the site, delete the world]",
expected: `[role:admin] disallowed permissions:
- delete the site
- delete the world`,
},
}

Expand Down

0 comments on commit f6495cf

Please sign in to comment.