-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add file/should/have_permission expression, replace some panics…
… with errors
Showing
22 changed files
with
287 additions
and
85 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
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
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,76 @@ | ||
package should | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"goarkitect/internal/arch/rule" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
ErrInvalidPermissions = errors.New( | ||
"permissions must only contain the following characters: 'd', 'r', 'w', 'x', '-'", | ||
) | ||
) | ||
|
||
func HavePermissions(want string, opts ...Option) *havePermissionsExpression { | ||
rx := regexp.MustCompile("[d-][rwx-]{9}") | ||
|
||
errs := make([]error, 0) | ||
if !rx.MatchString(want) { | ||
errs = append(errs, ErrInvalidPermissions) | ||
} | ||
|
||
expr := &havePermissionsExpression{ | ||
want: want, | ||
baseExpression: baseExpression{ | ||
errors: errs, | ||
}, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt.apply(&expr.options) | ||
} | ||
|
||
return expr | ||
} | ||
|
||
type havePermissionsExpression struct { | ||
baseExpression | ||
|
||
want string | ||
} | ||
|
||
func (e havePermissionsExpression) Evaluate(rb rule.Builder) []rule.Violation { | ||
return e.evaluate(rb, e.doEvaluate, e.getViolation) | ||
} | ||
|
||
func (e havePermissionsExpression) doEvaluate(rb rule.Builder, filePath string) bool { | ||
info, err := os.Stat(filePath) | ||
if err != nil { | ||
rb.AddError(err) | ||
|
||
return true | ||
} | ||
|
||
return e.want != info.Mode().String() | ||
} | ||
|
||
func (e havePermissionsExpression) getViolation(filePath string) rule.Violation { | ||
iNodeType := "file" | ||
if info, _ := os.Stat(filePath); info.IsDir() { | ||
iNodeType = "directory" | ||
} | ||
|
||
format := "%s '%s' does not have permissions matching '%s'" | ||
|
||
if e.options.negated { | ||
format = "%s '%s' does have permissions matching '%s'" | ||
} | ||
|
||
return rule.NewViolation( | ||
fmt.Sprintf(format, iNodeType, filepath.Base(filePath), e.want), | ||
) | ||
} |
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,113 @@ | ||
package should_test | ||
|
||
import ( | ||
"goarkitect/internal/arch/file" | ||
"goarkitect/internal/arch/file/should" | ||
"goarkitect/internal/arch/rule" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
func Test_HavePermissions(t *testing.T) { | ||
basePath, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
testCases := []struct { | ||
desc string | ||
ruleBuilder *file.RuleBuilder | ||
permissions string | ||
options []should.Option | ||
want []rule.Violation | ||
wantErrs []error | ||
}{ | ||
{ | ||
desc: "wrong permissions string", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions/0700.txt")), | ||
permissions: "foobarbaz-", | ||
want: nil, | ||
wantErrs: []error{should.ErrInvalidPermissions}, | ||
}, | ||
{ | ||
desc: "permissions of directory 'test/permissions' match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions")), | ||
permissions: "drwxr-xr-x", | ||
want: nil, | ||
}, | ||
{ | ||
desc: "permissions of file '0700.txt' match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions/0700.txt")), | ||
permissions: "-rwx------", | ||
want: nil, | ||
}, | ||
{ | ||
desc: "permissions of directory 'test/permissions' do not match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions")), | ||
permissions: "dr--r--r--", | ||
want: []rule.Violation{ | ||
rule.NewViolation("directory 'permissions' does not have permissions matching 'dr--r--r--'"), | ||
}, | ||
}, | ||
{ | ||
desc: "permissions of file '0700.txt' do not match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions/0700.txt")), | ||
permissions: "-rwxrwxrwx", | ||
want: []rule.Violation{ | ||
rule.NewViolation("file '0700.txt' does not have permissions matching '-rwxrwxrwx'"), | ||
}, | ||
}, | ||
{ | ||
desc: "negated: permissions of directory 'test/permissions' match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions")), | ||
permissions: "drwxr-xr-x", | ||
options: []should.Option{should.Negated{}}, | ||
want: []rule.Violation{ | ||
rule.NewViolation("directory 'permissions' does have permissions matching 'drwxr-xr-x'"), | ||
}, | ||
}, | ||
{ | ||
desc: "negated: permissions of file '0700.txt' match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions/0700.txt")), | ||
permissions: "-rwx------", | ||
options: []should.Option{should.Negated{}}, | ||
want: []rule.Violation{ | ||
rule.NewViolation("file '0700.txt' does have permissions matching '-rwx------'"), | ||
}, | ||
}, | ||
{ | ||
desc: "negated: permissions of directory 'test/permissions' do not match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions")), | ||
permissions: "dr--r--r--", | ||
options: []should.Option{should.Negated{}}, | ||
want: nil, | ||
}, | ||
{ | ||
desc: "negated: permissions of file '0700.txt' do not match expected one", | ||
ruleBuilder: file.One(filepath.Join(basePath, "test/permissions/0700.txt")), | ||
permissions: "-rwxrwxrwx", | ||
options: []should.Option{should.Negated{}}, | ||
want: nil, | ||
}, | ||
} | ||
|
||
for _, tC := range testCases { | ||
t.Run(tC.desc, func(t *testing.T) { | ||
hcm := should.HavePermissions(tC.permissions, tC.options...) | ||
got := hcm.Evaluate(tC.ruleBuilder) | ||
gotErrs := hcm.GetErrors() | ||
|
||
if !cmp.Equal(gotErrs, tC.wantErrs, cmpopts.EquateErrors(), cmpopts.EquateEmpty()) { | ||
t.Errorf("wantErr = %+v, gotErr = %+v", tC.wantErrs, gotErrs) | ||
} | ||
|
||
if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) { | ||
t.Errorf("want = %+v, got = %+v", tC.want, got) | ||
} | ||
}) | ||
} | ||
} |
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 @@ | ||
0700 |
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