oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3import (
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.
22func TestCreditsMatchProductionDependencies(t *testing.T) {
23 // Read the embedded inventory first, and check the toolchain against
24 // it before anything else runs. Everything below depends on this
25 // binary being the Go the notices were generated with:
26 // runtime.Version() goes into the expected inventory, and
27 // runtime.GOROOT()/LICENSE is read out of it.
28 //
29 // Under any other toolchain the failure is real and says nothing
30 // about credits, and it surfaces two different misleading ways. A
31 // Homebrew keg without LICENSE at GOROOT fails the read with a bare
32 // file error that never mentions credits at all. A keg with it gets
33 // past that and fails the comparison instead, whose message sends
34 // you to `make credits` — which would rewrite notices.json to name
35 // whatever toolchain you happen to be on, burying the dependency
36 // inventory the test exists to protect. Both cost a session a while
37 // before this guard was here.
38 var embedded creditInventory
39 if err := json.Unmarshal(creditNotices, &embedded); err != nil {
40 t.Fatal(err)
41 }
42 if want := embeddedGoVersion(embedded); want != "" && want != runtime.Version() {
43 t.Fatalf("credits were generated with %s, this test is running under %s.\n"+
44 "Run `GOTOOLCHAIN=%s make ci`. Do not run `make credits` to fix this: it would\n"+
45 "rewrite notices.json to name this toolchain instead of checking the inventory.",
46 want, runtime.Version(), want)
47 }
48
49 ctx, cancel := context.WithTimeout(t.Context(), 45*time.Second)
50 defer cancel()
51 command := exec.CommandContext(ctx, "go", "list", "-buildvcs=false", "-deps", "-json", "./cmd/calls")
52 command.Dir = filepath.Join("..", "..")
53 for _, entry := range os.Environ() {
54 if !strings.HasPrefix(entry, "GOOS=") && !strings.HasPrefix(entry, "GOARCH=") && !strings.HasPrefix(entry, "CGO_ENABLED=") && !strings.HasPrefix(entry, "GOWORK=") {
55 command.Env = append(command.Env, entry)
56 }
57 }
58 command.Env = append(command.Env, "GOOS=linux", "GOARCH=arm64", "CGO_ENABLED=0", "GOWORK=off")
59 raw, err := command.Output()
60 if err != nil {
61 t.Fatal("production dependency graph unavailable", err)
62 }
63 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})
64 if err != nil {
65 t.Fatal(err)
66 }
67 goLicense, err := os.ReadFile(filepath.Join(runtime.GOROOT(), "LICENSE"))
68 if err != nil {
69 t.Fatal(err)
70 }
71 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)}}})
72 sort.Slice(inventory.Components, func(i, j int) bool { return inventory.Components[i].Name < inventory.Components[j].Name })
73 expected, _ := json.Marshal(creditInventory{Components: inventory.Components, MissingNotices: inventory.MissingNotices})
74 actual, _ := json.Marshal(embedded)
75 if !bytes.Equal(expected, actual) {
76 t.Fatal("embedded credits differ from production dependencies; run make credits " +
77 "(the toolchain is already ruled out by the check at the top of this test)")
78 }
79}
80
81// embeddedGoVersion is the Go toolchain the embedded notices name, or ""
82// when they name none — in which case there is nothing to compare against
83// and the test proceeds exactly as it did before the guard existed.
84func embeddedGoVersion(inventory creditInventory) string {
85 for _, component := range inventory.Components {
86 if component.Name == "Go" {
87 return component.Version
88 }
89 }
90 return ""
91}
92