Debian Codegraph @

tools/antithesis-go-toolexec/catalog_test.go

raw
1 package main
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 qt "github.com/go-quicktest/qt"
10 )
11
12 func TestFlagValue(t *testing.T) {
13 // Both spellings appear in real compile invocations: -importcfg takes a
14 // separate argument, while -lang is written with an equals sign.
15 args := strings.Fields("-o out.a -p app -lang=go1.24 -complete -importcfg /w/importcfg -pack a.go")
16
17 qt.Assert(t, qt.Equals(flagValue(args, "-importcfg"), "/w/importcfg"))
18 qt.Assert(t, qt.Equals(flagValue(args, "-lang"), "go1.24"))
19 qt.Assert(t, qt.Equals(flagValue(args, "-p"), "app"))
20 qt.Assert(t, qt.Equals(flagValue(args, "-embedcfg"), ""))
21 // A boolean flag has no value, and must not swallow the next argument.
22 qt.Assert(t, qt.Equals(flagValue(args, "-complete"), "-importcfg"))
23 }
24
25 func TestLanguageVersion(t *testing.T) {
26 // go/types wants a "go1.N" string; the compiler is passed either form.
27 qt.Assert(t, qt.Equals(languageVersion([]string{"-lang=go1.24"}), "go1.24"))
28 qt.Assert(t, qt.Equals(languageVersion([]string{"-lang", "1.24"}), "go1.24"))
29 // Absent -lang means "no constraint", which go/types spells as empty.
30 qt.Assert(t, qt.Equals(languageVersion([]string{"-complete"}), ""))
31 }
32
33 func TestModuleRoot(t *testing.T) {
34 root := t.TempDir()
35 nested := filepath.Join(root, "a", "b")
36 qt.Assert(t, qt.IsNil(os.MkdirAll(nested, 0o755)))
37 qt.Assert(t, qt.IsNil(os.WriteFile(filepath.Join(root, "go.mod"), []byte("module m\n"), 0o644)))
38
39 // Found from the module root itself and from any depth beneath it.
40 qt.Assert(t, qt.Equals(moduleRoot(root), root))
41 qt.Assert(t, qt.Equals(moduleRoot(nested), root))
42 }
43
44 func TestModuleRootAbsentIsEmpty(t *testing.T) {
45 // With no go.mod anywhere above, callers fall back to absolute paths rather
46 // than inventing a root.
47 dir := t.TempDir()
48 if moduleRoot(dir) != "" {
49 t.Skip("the temporary directory is inside a module; nothing to assert")
50 }
51 qt.Assert(t, qt.Equals(moduleRoot(dir), ""))
52 }
53
54 func TestCatalogFileMatchesTheWholeTreeName(t *testing.T) {
55 // The two workflows must agree on the name, so that a catalog committed by
56 // either one is recognised by the other.
57 qt.Assert(t, qt.Equals(catalogFile, "antithesis_catalog.go"))
58 }