Skip to content

Commit

Permalink
Merge pull request #795 from Elizafox/python-tests-linter
Browse files Browse the repository at this point in the history
Add python/test linter
  • Loading branch information
Elizafox authored Oct 26, 2023
2 parents 45610ae + f70c970 commit e3a2f86
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/linter/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var DefaultLinters = []string{
"opt",
"python/docs",
"python/multiple",
"python/test",
"srv",
"setuidgid",
"strip",
Expand Down
23 changes: 22 additions & 1 deletion pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ var postLinterMap = map[string]postLinter{
FailOnError: false,
Explain: "Split this package up into multiple packages and verify you are not improperly using pip install",
},
"python/test": postLinter{
LinterFunc: pythonTestPostLinter,
FailOnError: false,
Explain: "Remove all test directories from the package",
},
}

var isDevRegex = regexp.MustCompile("^dev/")
Expand Down Expand Up @@ -364,7 +369,7 @@ func pythonDocsPostLinter(_ LinterContext, fsys fs.FS) error {
for _, m := range packages {
base := filepath.Base(m)
if base == "doc" || base == "docs" {
return fmt.Errorf("Docs diretory encountered in Python directory")
return fmt.Errorf("Docs directory encountered in Python site-packages directory")
}
}

Expand Down Expand Up @@ -427,6 +432,22 @@ func pythonMultiplePackagesPostLinter(_ LinterContext, fsys fs.FS) error {
return nil
}

func pythonTestPostLinter(_ LinterContext, fsys fs.FS) error {
packages, err := getPythonSitePackages(fsys)
if err != nil {
return err
}

for _, m := range packages {
base := filepath.Base(m)
if base == "test" || base == "tests" {
return fmt.Errorf("Tests directory encountered in Python site-packages directory")
}
}

return nil
}

// Checks if the linters in the given slice are known linters
// Returns an empty slice if all linters are known, otherwise a slice with all the bad linters
func CheckValidLinters(check []string) []string {
Expand Down
47 changes: 47 additions & 0 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,53 @@ func Test_pythonMultiplePackagesLinter(t *testing.T) {
assert.True(t, called)
}

func Test_pythonTestLinter(t *testing.T) {
dir, err := os.MkdirTemp("", "melange.XXXXX")
defer os.RemoveAll(dir)
assert.NoError(t, err)

cfg := config.Configuration{
Package: config.Package{
Name: "testpythontest",
Version: "4.2.0",
Epoch: 0,
Checks: checksOnly("python/test"),
},
}

// Base dir
pythonPathdir := filepath.Join(dir, "usr", "lib", "python3.14", "site-packages")

linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"python/test"})

fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys)

// Make one "package"
packagedir := filepath.Join(pythonPathdir, "foo")
err = os.MkdirAll(packagedir, 0700)
assert.NoError(t, err)

// One package should not trip it
called := false
assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) {
called = true
}, linters))
assert.False(t, called)

// Create docs
docsdir := filepath.Join(pythonPathdir, "test")
err = os.MkdirAll(docsdir, 0700)
assert.NoError(t, err)

// This should trip
assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) {
called = true
}, linters))
assert.True(t, called)
}

func Test_srvLinter(t *testing.T) {
dir, err := os.MkdirTemp("", "melange.XXXXX")
defer os.RemoveAll(dir)
Expand Down

0 comments on commit e3a2f86

Please sign in to comment.