/
plugin_pytest.go
79 lines (64 loc) · 1.93 KB
/
plugin_pytest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package wat
import (
"context"
"os/exec"
"fmt"
"regexp"
)
var testRegexps = []*regexp.Regexp{
regexp.MustCompile("test_.*\\.py"),
regexp.MustCompile(".*_test\\.py"),
}
type PluginPytest struct{}
func (PluginPytest) name() string { return "python+pytest populate" }
func (PluginPytest) run(ctx context.Context, root string) ([]WatCommand, error) {
// 1. is pytest relevant?
if !projUsesPytest(ctx, root) {
return nil, nil
}
// 2. where are the test files? what are the commands?
cmds, err := findTestFiles(ctx, root)
if err != nil {
return nil, fmt.Errorf("findTestFiles: %v", err)
}
watCmds := []WatCommand{}
for _, c := range cmds {
watCmds = append(watCmds, WatCommand{Command: c})
}
// 3. which tests --> which files?
// TBD ( ⚆ _ ⚆ )
return watCmds, nil
}
// Here's the naive implementation. Other possibilities:
// a. are there py files?
// b. is some other framework configured here that takes precedence?
// c. pytest-related files in .gitignore, requirements.txt, setup.py?
// d. config files
// + path/pytest.ini
// + path/setup.cfg (must also contain [tool:pytest] section to match)
// + path/tox.ini (must also contain [pytest] section to match)
// + pytest.ini
func projUsesPytest(ctx context.Context, root string) bool {
// is there a pytest executable?
cmd := exec.CommandContext(ctx, "python", "-c", "import pytest")
err := cmd.Run()
if err != nil {
return false
}
return true
}
// This is the naive function that just finds test_*.py files, returns their
// invocations (NOT caring about associated code)
func findTestFiles(ctx context.Context, root string) ([]string, error) {
allFiles, err := walkDir(root)
if err != nil {
return nil, fmt.Errorf("walkDirWithRegexp: %v", err)
}
testFiles := filterFilesMatchAny(allFiles, testRegexps)
cmds := []string{}
for _, info := range testFiles {
cmds = append(cmds, fmt.Sprintf("pytest %s", info.name))
}
return cmds, nil
}
var _ plugin = PluginPytest{}