gunk-dev/the-valley

flow-checker.go

sourcecfedff357a41main's published tip history
2.2 KiB
1// Command flow-checker holds what the flow checks (nix/checks/flow.nix)
2// verify beside klaus's own tests:
3//
4// flow-checker version WANT PROGRAM [ARG...]
5//
6// runs PROGRAM ARG... --version and fails unless it exits 0 having printed
7// exactly the line WANT, and nothing on stderr, and
8//
9// flow-checker sockets DIR
10//
11// fails if DIR holds a socket at any depth: a tmux server some test reached
12// through the default socket directory rather than its own. Each failure
13// names what it found and exits 1.
14package main
15
16import (
17 "bytes"
18 "errors"
19 "fmt"
20 "io/fs"
21 "os"
22 "os/exec"
23 "path/filepath"
24 "strings"
25)
26
27func main() {
28 if err := run(os.Args[1:]); err != nil {
29 fmt.Fprintln(os.Stderr, "flow-checker:", err)
30 os.Exit(1)
31 }
32}
33
34func run(args []string) error {
35 switch {
36 case len(args) >= 3 && args[0] == "version":
37 return version(args[1], args[2:])
38 case len(args) == 2 && args[0] == "sockets":
39 return sockets(args[1])
40 }
41 return errors.New("usage: flow-checker version WANT PROGRAM [ARG...] | flow-checker sockets DIR")
42}
43
44// version fails unless command, a program and its arguments, given
45// --version, exits 0 having printed exactly the line want, and nothing on
46// stderr.
47func version(want string, command []string) error {
48 var stderr bytes.Buffer
49 cmd := exec.Command(command[0], append(command[1:], "--version")...)
50 cmd.Stderr = &stderr
51 out, err := cmd.Output()
52 name := strings.Join(cmd.Args, " ")
53 if err != nil {
54 return fmt.Errorf("%s: %v: %s", name, err, strings.TrimSpace(stderr.String()))
55 }
56 if got := string(out); got != want+"\n" || stderr.Len() > 0 {
57 return fmt.Errorf("%s printed %q and %q on stderr, want %q and nothing", name, got, stderr.String(), want+"\n")
58 }
59 return nil
60}
61
62// sockets fails if dir holds a socket at any depth.
63func sockets(dir string) error {
64 var found []string
65 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
66 if err != nil {
67 return err
68 }
69 if d.Type()&fs.ModeSocket != 0 {
70 found = append(found, path)
71 }
72 return nil
73 })
74 if err != nil {
75 return err
76 }
77 if len(found) > 0 {
78 return fmt.Errorf("a test reached tmux through the default socket directory: %s", strings.Join(found, ", "))
79 }
80 return nil
81}