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