-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (81 loc) · 2.21 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/oidc-scenario-based-tester/osbt/server"
"github.com/spf13/cobra"
)
func main() {
var rootCmd = &cobra.Command{Use: "osbt"}
var runCmd = &cobra.Command{
Use: "run",
Short: "Run the tests",
Run: runTests,
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "Start the server that receives and saves the test results",
Run: func(cmd *cobra.Command, args []string) {
server.StartServer()
},
}
runCmd.Flags().StringP("file", "f", "", "Specify the test file to run")
runCmd.Flags().StringP("dir", "d", "", "Specify the test directory to run all tests")
runCmd.Flags().BoolP("recursive", "r", false, "Search directories recursively")
runCmd.Flags().StringP("timeout", "t", "30s", "Specify the timeout for running tests")
rootCmd.AddCommand(runCmd, serverCmd)
rootCmd.Execute()
}
func runTests(cmd *cobra.Command, args []string) {
file, _ := cmd.Flags().GetString("file")
dir, _ := cmd.Flags().GetString("dir")
recursive, _ := cmd.Flags().GetBool("recursive")
timeout, _ := cmd.Flags().GetString("timeout")
timeoutDuration, err := time.ParseDuration(timeout)
if err != nil {
fmt.Printf("Invalid timeout value: %v", err)
os.Exit(1)
}
if file != "" {
runTest(file, timeoutDuration)
}
if dir != "" {
runTestsInDir(dir, recursive, timeoutDuration)
}
}
func runTest(file string, timeout time.Duration) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, "python", file)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
fmt.Printf("Timeout running test: %s\n", file)
} else {
fmt.Printf("Error running test: %v\n", err)
}
} else {
fmt.Printf("Test result: %s\n", out.String())
}
}
func runTestsInDir(dir string, recursive bool, timeout time.Duration) {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(path) == ".py" {
runTest(path, timeout)
}
return nil
})
if err != nil {
fmt.Printf("Error running tests in directory: %v\n", err)
}
}