package main import ( "os" "path/filepath" "strings" "testing" ) // writeModule creates a minimal module directory (one non-test Go file). func writeModule(t *testing.T, dir, name string) { t.Helper() d := filepath.Join(dir, name) if err := os.MkdirAll(d, 0o755); err != nil { t.Fatalf("mkdir %s: %v", d, err) } if err := os.WriteFile(filepath.Join(d, "app.go"), []byte("package "+name+"\n"), 0o644); err != nil { t.Fatalf("write %s: %v", d, err) } } const importBase = "example.com/mod/internal/modules" // TestGenerateSortedAndDeterministic: the aggregator is deterministic and the // imports are sorted (AC-1/AC-2). func TestGenerateSortedAndDeterministic(t *testing.T) { dir := t.TempDir() for _, n := range []string{"time", "home", "expense"} { writeModule(t, dir, n) } src, err := Generate(dir, importBase) if err != nil { t.Fatalf("Generate: %v", err) } got := string(src) if !strings.Contains(got, "Code generated") { t.Error("the header is missing") } // Sorted, regardless of creation order. iHome := strings.Index(got, importBase+"/home") iExpense := strings.Index(got, importBase+"/expense") iTime := strings.Index(got, importBase+"/time") if iHome < 0 || iExpense < 0 || iTime < 0 { t.Fatalf("not all modules were imported:\n%s", got) } if !(iExpense < iHome && iHome < iTime) { t.Errorf("imports are not sorted (expense