git_test.go (3946B)
1 package external 2 3 import ( 4 "context" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 "time" 10 11 "git.prime8.dev/moncheeta/prime8dev/internal/config" 12 ) 13 14 func TestFixRelativePaths(t *testing.T) { 15 const base = "https://git.prime8.dev" 16 tests := []struct { 17 name, in, want string 18 }{ 19 {"relative link", "[docs](docs/setup.md)", "[docs](https://git.prime8.dev/janitor/file/docs/setup.md.html)"}, 20 {"dot slash link", "[lic](./LICENSE)", "[lic](https://git.prime8.dev/janitor/file/LICENSE.html)"}, 21 {"link with fragment", "[x](./a.md#usage)", "[x](https://git.prime8.dev/janitor/file/a.md.html#usage)"}, 22 {"relative image", "", ""}, 23 {"image with title", ``, ``}, 24 {"absolute link", "[site](https://prime8.dev)", "[site](https://prime8.dev)"}, 25 {"http image", "", ""}, 26 {"anchor", "[top](#top)", "[top](#top)"}, 27 {"mailto", "[mail](mailto:damian@prime8.dev)", "[mail](mailto:damian@prime8.dev)"}, 28 { 29 "linked image", 30 "[](./CONTRIBUTING.md)", 31 "[](https://git.prime8.dev/janitor/file/CONTRIBUTING.md.html)", 32 }, 33 } 34 35 for _, tt := range tests { 36 t.Run(tt.name, func(t *testing.T) { 37 if got := fixRelativePaths(base, "janitor", tt.in); got != tt.want { 38 t.Errorf("fixRelativePaths(%q)\n got %q\n want %q", tt.in, got, tt.want) 39 } 40 }) 41 } 42 } 43 44 func TestGitFetchMetadata(t *testing.T) { 45 if _, err := exec.LookPath("git"); err != nil { 46 t.Skip("git not installed") 47 } 48 49 reposDir := t.TempDir() 50 work := t.TempDir() 51 bare := filepath.Join(reposDir, "demo.git") 52 53 run := func(env []string, dir string, args ...string) { 54 t.Helper() 55 cmd := exec.Command("git", args...) 56 cmd.Dir = dir 57 cmd.Env = append(os.Environ(), 58 "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_NOSYSTEM=1", 59 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", 60 "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com") 61 cmd.Env = append(cmd.Env, env...) 62 if out, err := cmd.CombinedOutput(); err != nil { 63 t.Fatalf("git %v: %v\n%s", args, err, out) 64 } 65 } 66 67 first := time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC) 68 last := time.Date(2025, 6, 7, 8, 9, 10, 0, time.UTC) 69 at := func(ts time.Time) []string { 70 d := ts.Format(time.RFC3339) 71 return []string{"GIT_AUTHOR_DATE=" + d, "GIT_COMMITTER_DATE=" + d} 72 } 73 74 run(nil, work, "init", "-q") 75 if err := os.WriteFile(filepath.Join(work, "readme.md"), []byte("# demo\n\n\n"), 0o644); err != nil { 76 t.Fatal(err) 77 } 78 run(nil, work, "add", ".") 79 run(at(first), work, "commit", "-q", "-m", "first") 80 if err := os.WriteFile(filepath.Join(work, "main.go"), []byte("package main\n"), 0o644); err != nil { 81 t.Fatal(err) 82 } 83 run(nil, work, "add", ".") 84 run(at(last), work, "commit", "-q", "-m", "second") 85 run(nil, reposDir, "clone", "-q", "--bare", work, bare) 86 87 t.Setenv("GIT_REPOS_DIR", reposDir) 88 t.Setenv("GIT_WEB_BASE", "https://git.example.com/") 89 svc := NewGit() 90 91 md, err := svc.FetchMetadata(context.Background(), config.Project{ID: "demo", Repo: "demo"}) 92 if err != nil { 93 t.Fatalf("FetchMetadata: %v", err) 94 } 95 if want := "# demo\n\n\n"; md.Readme != want { 96 t.Errorf("Readme = %q, want %q", md.Readme, want) 97 } 98 if !md.DateCreated.Equal(first) { 99 t.Errorf("DateCreated = %v, want %v", md.DateCreated, first) 100 } 101 if !md.DateModified.Equal(last) { 102 t.Errorf("DateModified = %v, want %v", md.DateModified, last) 103 } 104 105 if _, err := svc.FetchMetadata(context.Background(), config.Project{ID: "missing", Repo: "missing"}); err == nil { 106 t.Error("expected error for missing repo") 107 } 108 if _, err := svc.FetchMetadata(context.Background(), config.Project{ID: "none"}); err == nil { 109 t.Error("expected error for project without repo") 110 } 111 }