| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | var literalEmitRe = regexp.MustCompile(`(?:emitRuntimeEvent|emitRemoteEvent|EventsEmit\([^,]+,|runtimeEvents\.Emit\([^,]+,)\s*"([^"]+)"`) |
| 13 | |
| 14 | func desktopSources(t *testing.T) map[string]string { |
| 15 | t.Helper() |
| 16 | entries, err := os.ReadDir(".") |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | sources := map[string]string{} |
| 21 | for _, entry := range entries { |
| 22 | name := entry.Name() |
| 23 | if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { |
| 24 | continue |
| 25 | } |
| 26 | data, err := os.ReadFile(filepath.Join(".", name)) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | sources[name] = string(data) |
| 31 | } |
| 32 | return sources |
| 33 | } |
| 34 | |
| 35 | func TestHostEventsListIsSortedUniqueAndEmitted(t *testing.T) { |
| 36 | if !slices.IsSorted(hostEventNames) { |
| 37 | t.Fatalf("hostEventNames must be sorted: %v", hostEventNames) |
| 38 | } |
| 39 | if len(slices.Compact(slices.Clone(hostEventNames))) != len(hostEventNames) { |
| 40 | t.Fatalf("hostEventNames has duplicates: %v", hostEventNames) |
| 41 | } |
| 42 | sources := desktopSources(t) |
| 43 | for _, name := range hostEventNames { |
| 44 | literal := `"` + name + `"` |
| 45 | found := false |
| 46 | for file, src := range sources { |
| 47 | if file != "host_events.go" && strings.Contains(src, literal) { |
| 48 | found = true |
| 49 | break |
| 50 | } |
| 51 | } |
| 52 | if !found { |
| 53 | t.Errorf("hostEventNames lists %q but no desktop source emits that literal", name) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestHostEventsCoverLiteralEmitSites(t *testing.T) { |
| 59 | sources := desktopSources(t) |
| 60 | for file, src := range sources { |
| 61 | for _, match := range literalEmitRe.FindAllStringSubmatch(src, -1) { |
| 62 | name := match[1] |
| 63 | if strings.Contains(name, "%") { |
| 64 | continue |
| 65 | } |
| 66 | if !slices.Contains(hostEventNames, name) { |
| 67 | t.Errorf("%s emits %q which hostEventNames does not list", file, name) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 |