integrate_test.go raw
1 package integrate
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8 )
9
10 func TestTargetFile_PackageMain(t *testing.T) {
11 src := "package main\n\nfunc DoSomething() {}\n"
12 got := TargetFile(src, "/project")
13 if !strings.Contains(got, "cmd/dendrite") {
14 t.Errorf("expected cmd/dendrite for package main, got: %s", got)
15 }
16 if !strings.HasSuffix(got, "do_something.go") {
17 t.Errorf("expected do_something.go, got: %s", got)
18 }
19 }
20
21 func TestTargetFile_PackageLattice(t *testing.T) {
22 src := "package lattice\n\ntype NodeCount struct{}\n"
23 got := TargetFile(src, "/project")
24 if !strings.Contains(got, "lattice") {
25 t.Errorf("expected lattice dir, got: %s", got)
26 }
27 if !strings.HasSuffix(got, "node_count.go") {
28 t.Errorf("expected node_count.go, got: %s", got)
29 }
30 }
31
32 func TestTargetFile_ParseError(t *testing.T) {
33 got := TargetFile("not go source", "/project")
34 if !strings.HasSuffix(got, "generated.go") {
35 t.Errorf("expected generated.go fallback, got: %s", got)
36 }
37 }
38
39 func TestToSnake(t *testing.T) {
40 tests := []struct {
41 in, want string
42 }{
43 {"CountNodes", "count_nodes"},
44 {"foo", "foo"},
45 {"A", "a"},
46 {"HTMLParser", "h_t_m_l_parser"},
47 }
48 for _, tt := range tests {
49 got := toSnake(tt.in)
50 if got != tt.want {
51 t.Errorf("toSnake(%q) = %q, want %q", tt.in, got, tt.want)
52 }
53 }
54 }
55
56 func TestCopyDir(t *testing.T) {
57 // Create a minimal project.
58 src := t.TempDir()
59 os.WriteFile(filepath.Join(src, "go.mod"), []byte("module test\n\ngo 1.24\n"), 0o644)
60 os.MkdirAll(filepath.Join(src, "pkg"), 0o755)
61 os.WriteFile(filepath.Join(src, "pkg", "foo.go"), []byte("package pkg\n"), 0o644)
62 os.WriteFile(filepath.Join(src, "pkg", "readme.txt"), []byte("skip me\n"), 0o644)
63 os.MkdirAll(filepath.Join(src, ".git"), 0o755)
64 os.WriteFile(filepath.Join(src, ".git", "config"), []byte("git config\n"), 0o644)
65
66 dst := t.TempDir()
67 if err := copyDir(src, dst); err != nil {
68 t.Fatal(err)
69 }
70
71 // go.mod should be copied.
72 if _, err := os.Stat(filepath.Join(dst, "go.mod")); err != nil {
73 t.Error("go.mod not copied")
74 }
75 // .go file should be copied.
76 if _, err := os.Stat(filepath.Join(dst, "pkg", "foo.go")); err != nil {
77 t.Error("pkg/foo.go not copied")
78 }
79 // .txt should NOT be copied.
80 if _, err := os.Stat(filepath.Join(dst, "pkg", "readme.txt")); err == nil {
81 t.Error("readme.txt should not be copied")
82 }
83 // .git should NOT be copied.
84 if _, err := os.Stat(filepath.Join(dst, ".git", "config")); err == nil {
85 t.Error(".git should not be copied")
86 }
87 }
88
89 func TestRollback(t *testing.T) {
90 dir := t.TempDir()
91 path := filepath.Join(dir, "test.go")
92 os.WriteFile(path, []byte("package test\n"), 0o644)
93
94 plan := Plan{SourceFile: path}
95 if err := Rollback(plan, dir); err != nil {
96 t.Fatal(err)
97 }
98 if _, err := os.Stat(path); !os.IsNotExist(err) {
99 t.Error("file should have been deleted")
100 }
101 }
102