| 1 | // knotcheck holds the Go validator of the plan graph to the CUE schema and |
| 2 | // the knowledge lint (the knot-agree check). It writes graphs, shared |
| 3 | // fixtures and generated ones, each with the verdict both validators must |
| 4 | // give, and it gives the Go validator's verdict on a graph on disk. The |
| 5 | // check runs the lint over the same graphs and fails on any disagreement. |
| 6 | // |
| 7 | // knotcheck fixtures DIR write the shared fixtures under DIR |
| 8 | // knotcheck generate DIR N SEED write N generated graphs under DIR |
| 9 | // knotcheck validate DIR the Go verdict on the graph at DIR |
| 10 | // knotcheck rules the rules both validators share |
| 11 | // knotcheck contract the constants the lint must share |
| 12 | // |
| 13 | // fixtures and generate write each case as DIR/<case>/graph, the whole |
| 14 | // graph, and DIR/<case>/tree/knots, the same graph less its comments, |
| 15 | // which the lint does not read. Each adds a line to DIR/cases.tsv: the |
| 16 | // case, accept or reject, and for a rejection the rule it breaks. |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "fmt" |
| 22 | "io/fs" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "sort" |
| 26 | "strconv" |
| 27 | "strings" |
| 28 | |
| 29 | "the-valley/knot" |
| 30 | ) |
| 31 | |
| 32 | func main() { |
| 33 | os.Exit(run(os.Args[1:])) |
| 34 | } |
| 35 | |
| 36 | func run(args []string) int { |
| 37 | if len(args) == 0 { |
| 38 | fmt.Fprintln(os.Stderr, "usage: knotcheck fixtures DIR | generate DIR N SEED | validate DIR | rules | contract") |
| 39 | return 2 |
| 40 | } |
| 41 | var err error |
| 42 | switch { |
| 43 | case args[0] == "fixtures" && len(args) == 2: |
| 44 | err = write(args[1], Fixtures()) |
| 45 | case args[0] == "generate" && len(args) == 4: |
| 46 | n, nerr := strconv.Atoi(args[2]) |
| 47 | seed, serr := strconv.ParseUint(args[3], 10, 64) |
| 48 | if nerr != nil || serr != nil { |
| 49 | fmt.Fprintln(os.Stderr, "knotcheck: generate takes a count and a seed") |
| 50 | return 2 |
| 51 | } |
| 52 | err = write(args[1], Generate(n, seed)) |
| 53 | case args[0] == "validate" && len(args) == 2: |
| 54 | files, rerr := readDir(args[1]) |
| 55 | if rerr != nil { |
| 56 | fmt.Fprintln(os.Stderr, "knotcheck:", rerr) |
| 57 | return 2 |
| 58 | } |
| 59 | if verr := validate(files); verr != nil { |
| 60 | fmt.Printf("reject %s\n", ruled(verr)) |
| 61 | return 1 |
| 62 | } |
| 63 | fmt.Println("accept") |
| 64 | return 0 |
| 65 | case args[0] == "rules" && len(args) == 1: |
| 66 | for _, r := range knot.SharedRules { |
| 67 | fmt.Println(r) |
| 68 | } |
| 69 | case args[0] == "contract" && len(args) == 1: |
| 70 | dirs := map[string]string{} |
| 71 | for _, t := range knot.Types { |
| 72 | dirs[t.Name] = t.Directory |
| 73 | } |
| 74 | err = json.NewEncoder(os.Stdout).Encode(map[string]any{"cutoff": knot.IDRuleCutoff, "directories": dirs}) |
| 75 | default: |
| 76 | fmt.Fprintln(os.Stderr, "usage: knotcheck fixtures DIR | generate DIR N SEED | validate DIR | rules | contract") |
| 77 | return 2 |
| 78 | } |
| 79 | if err != nil { |
| 80 | fmt.Fprintln(os.Stderr, "knotcheck:", err) |
| 81 | return 1 |
| 82 | } |
| 83 | return 0 |
| 84 | } |
| 85 | |
| 86 | // Case is one graph, and the verdict both validators must give it. |
| 87 | type Case struct { |
| 88 | Name string |
| 89 | Accept bool |
| 90 | // Rule is the shared rule a rejected graph breaks. |
| 91 | Rule knot.Rule |
| 92 | // Files maps each file's path in the graph to its content. |
| 93 | Files map[string]string |
| 94 | } |
| 95 | |
| 96 | // validate is the Go validator's verdict on a graph's files: its layout, |
| 97 | // and then every knot and comment. |
| 98 | func validate(files map[string]file) error { |
| 99 | var entries []knot.Entry |
| 100 | dirs := map[string]bool{} |
| 101 | for p, f := range files { |
| 102 | entries = append(entries, knot.Entry{Path: p, Mode: f.mode, OID: p}) |
| 103 | for d := filepath.Dir(p); d != "."; d = filepath.Dir(d) { |
| 104 | dirs[d] = true |
| 105 | } |
| 106 | } |
| 107 | for d := range dirs { |
| 108 | entries = append(entries, knot.Entry{Path: d, Mode: "40000"}) |
| 109 | } |
| 110 | sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path }) |
| 111 | idx, err := knot.Layout(entries) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | read := func(oid string, max int) ([]byte, error) { |
| 116 | data := files[oid].data |
| 117 | if len(data) > max { |
| 118 | return nil, fmt.Errorf("is %d bytes, more than %d", len(data), max) |
| 119 | } |
| 120 | return data, nil |
| 121 | } |
| 122 | return knot.Validate(idx, read) |
| 123 | } |
| 124 | |
| 125 | func ruled(err error) error { |
| 126 | if e, ok := err.(*knot.Error); ok { |
| 127 | return fmt.Errorf("%s: %s", e.Rule, e.Error()) |
| 128 | } |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | type file struct { |
| 133 | mode string |
| 134 | data []byte |
| 135 | } |
| 136 | |
| 137 | // readDir reads every file under dir, with the mode git would give it. |
| 138 | func readDir(dir string) (map[string]file, error) { |
| 139 | files := map[string]file{} |
| 140 | err := filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error { |
| 141 | if err != nil || d.IsDir() { |
| 142 | return err |
| 143 | } |
| 144 | rel, _ := filepath.Rel(dir, p) |
| 145 | info, err := os.Lstat(p) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | mode := "100644" |
| 150 | switch { |
| 151 | case info.Mode()&fs.ModeSymlink != 0: |
| 152 | mode = "120000" |
| 153 | case info.Mode()&0o111 != 0: |
| 154 | mode = "100755" |
| 155 | } |
| 156 | data, err := os.ReadFile(p) |
| 157 | if err != nil && mode != "120000" { |
| 158 | return err |
| 159 | } |
| 160 | files[filepath.ToSlash(rel)] = file{mode: mode, data: data} |
| 161 | return nil |
| 162 | }) |
| 163 | return files, err |
| 164 | } |
| 165 | |
| 166 | // write lays every case out under dir, and lists them in dir/cases.tsv. |
| 167 | func write(dir string, cases []Case) error { |
| 168 | var index strings.Builder |
| 169 | for _, c := range cases { |
| 170 | verdict, rule := "accept", "" |
| 171 | if !c.Accept { |
| 172 | verdict, rule = "reject", string(c.Rule) |
| 173 | } |
| 174 | fmt.Fprintf(&index, "%s\t%s\t%s\n", c.Name, verdict, rule) |
| 175 | for p, content := range c.Files { |
| 176 | targets := []string{filepath.Join(dir, c.Name, "graph", p)} |
| 177 | if !strings.HasPrefix(p, knot.CommentsDirectory+"/") { |
| 178 | targets = append(targets, filepath.Join(dir, c.Name, "tree", "knots", p)) |
| 179 | } |
| 180 | for _, t := range targets { |
| 181 | if err := os.MkdirAll(filepath.Dir(t), 0o755); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | if err := os.WriteFile(t, []byte(content), 0o644); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return os.WriteFile(filepath.Join(dir, "cases.tsv"), []byte(index.String()), 0o644) |
| 191 | } |