| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/estilo.git/credits" |
| 17 | "amadan.net/oficina/estilo.git/credits/catalog" |
| 18 | ) |
| 19 | |
| 20 | // Check the production package graph, not the test binary's dependencies. |
| 21 | // A dependency or notice change must update the embedded attribution too. |
| 22 | func TestCreditsMatchProductionDependencies(t *testing.T) { |
| 23 | ctx, cancel := context.WithTimeout(t.Context(), 45*time.Second) |
| 24 | defer cancel() |
| 25 | command := exec.CommandContext(ctx, "go", "list", "-buildvcs=false", "-deps", "-json", "./cmd/calendar") |
| 26 | command.Dir = filepath.Join("..", "..") |
| 27 | for _, entry := range os.Environ() { |
| 28 | if !strings.HasPrefix(entry, "GOOS=") && !strings.HasPrefix(entry, "GOARCH=") && !strings.HasPrefix(entry, "CGO_ENABLED=") && !strings.HasPrefix(entry, "GOWORK=") { |
| 29 | command.Env = append(command.Env, entry) |
| 30 | } |
| 31 | } |
| 32 | command.Env = append(command.Env, "GOOS=linux", "GOARCH=arm64", "CGO_ENABLED=0", "GOWORK=off") |
| 33 | var stderr bytes.Buffer |
| 34 | command.Stderr = &stderr |
| 35 | raw, err := command.Output() |
| 36 | if err != nil { |
| 37 | t.Fatalf("production dependency graph unavailable: %v\n%s", err, stderr.String()) |
| 38 | } |
| 39 | inventory, err := catalog.Collect(bytes.NewReader(raw), map[string]bool{"amadan.net/oficina/estilo.git": true, "amadan.net/oficina/home/handoff": true, "amadan.net/oficina/home/connect": true, "amadan.net/rastrillo/native/server": true}) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | goLicense, err := os.ReadFile(filepath.Join(runtime.GOROOT(), "LICENSE")) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | inventory.Components = append(inventory.Components, credits.Lucide(), credits.Onest(), credits.Component{Name: "Go", Version: runtime.Version(), SourceURL: "https://go.dev/LICENSE", Notices: []credits.Notice{{Name: "LICENSE", Text: string(goLicense)}}}) |
| 48 | sort.Slice(inventory.Components, func(i, j int) bool { return inventory.Components[i].Name < inventory.Components[j].Name }) |
| 49 | var embedded creditInventory |
| 50 | if err := json.Unmarshal(creditNotices, &embedded); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | expected, _ := json.Marshal(creditInventory{Components: inventory.Components, MissingNotices: inventory.MissingNotices}) |
| 54 | actual, _ := json.Marshal(embedded) |
| 55 | if !bytes.Equal(expected, actual) { |
| 56 | t.Fatal("embedded credits differ from production dependencies; run make credits") |
| 57 | } |
| 58 | } |
| 59 | |