xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / store / store_test.go

3.7 KB
1package store
2
3import (
4	"os"
5	"os/exec"
6	"path/filepath"
7	"testing"
8)
9
10func TestSync(t *testing.T) {
11	work, root := t.TempDir(), t.TempDir()
12	commit := func(msg string) {
13		for _, args := range [][]string{{"add", "."}, {"commit", "-qm", msg}} {
14			cmd := exec.Command("git", args...)
15			cmd.Dir = work
16			cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=x", "GIT_AUTHOR_EMAIL=x@x", "GIT_COMMITTER_NAME=x", "GIT_COMMITTER_EMAIL=x@x")
17			if out, err := cmd.CombinedOutput(); err != nil {
18				t.Fatalf("git %v: %v\n%s", args, err, out)
19			}
20		}
21	}
22	if out, err := exec.Command("git", "init", "-q", "-b", "trunk", work).CombinedOutput(); err != nil {
23		t.Fatalf("%v\n%s", err, out)
24	}
25	os.WriteFile(filepath.Join(work, "a"), []byte("1"), 0o644)
26	commit("one")
27
28	url := "file://" + work
29	for i, want := range []string{"new", "same"} {
30		if got, err := Sync(root, url); err != nil || got != want {
31			t.Fatalf("sync %d = %q, %v; want %q", i, got, err, want)
32		}
33	}
34	os.WriteFile(filepath.Join(work, "a"), []byte("2"), 0o644)
35	commit("two")
36	if got, err := Sync(root, url); err != nil || got != "updated" {
37		t.Fatalf("sync after commit = %q, %v", got, err)
38	}
39	dir := Dir(root, url)
40	if head, _ := git("--git-dir", dir, "symbolic-ref", "HEAD"); head != "refs/heads/trunk" {
41		t.Errorf("HEAD = %q, want refs/heads/trunk", head)
42	}
43	if n, _ := git("--git-dir", dir, "rev-list", "--count", "HEAD"); n != "2" {
44		t.Errorf("commits = %s, want 2", n)
45	}
46}
47
48func TestSources(t *testing.T) {
49	path := filepath.Join(t.TempDir(), "sources.txt")
50	os.WriteFile(path, []byte("# comment\n\nhttps://github.com/x/a.git\n  https://gitlab.com/x/b.git  \n"), 0o644)
51	urls, err := Sources(path)
52	if err != nil || len(urls) != 2 || Dir("r", urls[1]) != filepath.Join("r", "gitlab.com/x/b.git") {
53		t.Fatalf("Sources = %v, %v", urls, err)
54	}
55}
56
57func TestEditSources(t *testing.T) {
58	local := t.TempDir()
59	exec.Command("git", "init", "-q", local).Run()
60	for in, want := range map[string]string{
61		"https://github.com/x/a": "https://github.com/x/a.git", "https://gitlab.com/g/sub/b.git/": "https://gitlab.com/g/sub/b.git",
62		"https://codeberg.org/x/c.git": "https://codeberg.org/x/c.git", local + "/": local,
63		"https://xonecas.io/d": "https://xonecas.io/d.git", "git@github.com:x/a.git": "", "https://github.com/": "", t.TempDir(): "",
64	} {
65		if got, _ := Clean(in); got != want {
66			t.Errorf("Clean(%q) = %q, want %q", in, got, want)
67		}
68	}
69	path := filepath.Join(t.TempDir(), "sources.txt")
70	os.WriteFile(path, []byte("# comment\nhttps://github.com/x/a.git\n"), 0o644)
71	if AddSource(path, "https://github.com/x/b.git") != nil || AddSource(path, "https://github.com/x/a.git") == nil {
72		t.Error("AddSource adds a new remote once")
73	}
74	if RemoveSource(path, "https://github.com/x/a.git") != nil || RemoveSource(path, "https://github.com/x/a.git") == nil {
75		t.Error("RemoveSource removes a listed remote once")
76	}
77	if b, _ := os.ReadFile(path); string(b) != "# comment\nhttps://github.com/x/b.git\n" {
78		t.Errorf("sources file is %q", b)
79	}
80}
81
82func TestHosted(t *testing.T) {
83	root := t.TempDir()
84	url := "https://xonecas.io/demo.git"
85	if err := Create(Dir(root, url), "true"); err != nil {
86		t.Fatal(err)
87	}
88	if state, err := Sync(root, url); state != "hosted" || err != nil {
89		t.Errorf("Sync of a hosted repo = %q, %v", state, err)
90	}
91	work := t.TempDir()
92	for _, args := range [][]string{
93		{"init", "--quiet", "--initial-branch=master", work},
94		{"-C", work, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "--quiet", "--allow-empty", "-m", "one"},
95		{"-C", work, "push", "--quiet", Dir(root, url), "master"},
96	} {
97		if _, err := git(args...); err != nil {
98			t.Fatal(err)
99		}
100	}
101	if head, err := git("--git-dir", Dir(root, url), "symbolic-ref", "HEAD"); head != "refs/heads/master" {
102		t.Errorf("HEAD after pushing only master = %q, %v", head, err)
103	}
104}