package repo import ( "context" "errors" "os" "os/exec" "path/filepath" "strings" "testing" ) // run is git in dir, failing the test on error. func run(t *testing.T, dir string, args ...string) string { t.Helper() cmd := exec.Command("git", args...) cmd.Dir = dir cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t") out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %v: %v\n%s", args, err, out) } return strings.TrimSpace(string(out)) } // remote makes a bare repo with one commit on main, and a second clone of it // ("the laptop") for pushing from outside. func remote(t *testing.T) (bare, laptop string) { t.Helper() root := t.TempDir() bare = filepath.Join(root, "remote.git") laptop = filepath.Join(root, "laptop") run(t, root, "init", "--quiet", "--bare", "--initial-branch=main", bare) run(t, root, "clone", "--quiet", bare, laptop) os.MkdirAll(filepath.Join(laptop, "solving", "moocast"), 0o755) os.WriteFile(filepath.Join(laptop, "solving", "moocast", "main.cpp"), []byte("int main(){}\n"), 0o644) run(t, laptop, "add", "-A") run(t, laptop, "commit", "--quiet", "-m", "first") run(t, laptop, "push", "--quiet", "origin", "HEAD:main") return bare, laptop } func newRepo(t *testing.T, bare string, push bool) (*Repo, *int) { t.Helper() changes := 0 r, err := New(Config{ Dir: filepath.Join(t.TempDir(), "clone"), Remote: "file://" + bare, Branch: "main", AuthorName: "site", AuthorEmail: "site@example.com", Push: push, }, func(string) error { changes++; return nil }) if err != nil { t.Fatal(err) } return r, &changes } func laptopCommit(t *testing.T, laptop, file, msg string) { t.Helper() run(t, laptop, "pull", "--quiet", "--rebase") os.WriteFile(filepath.Join(laptop, file), []byte(msg+"\n"), 0o644) run(t, laptop, "add", "-A") run(t, laptop, "commit", "--quiet", "-m", msg) run(t, laptop, "push", "--quiet") } func TestSyncClonesThenFollowsRemote(t *testing.T) { bare, laptop := remote(t) r, changes := newRepo(t, bare, true) ctx := context.Background() if err := r.Sync(ctx); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(r.Dir(), "solving", "moocast", "main.cpp")); err != nil { t.Fatal("clone is missing the problem:", err) } laptopCommit(t, laptop, "README.md", "from laptop") // Stray local junk must not survive a sync: the remote always wins. os.WriteFile(filepath.Join(r.Dir(), "junk"), []byte("x"), 0o644) if err := r.Sync(ctx); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(r.Dir(), "README.md")); err != nil { t.Fatal("sync did not bring in the laptop's commit") } if _, err := os.Stat(filepath.Join(r.Dir(), "junk")); err == nil { t.Fatal("sync left an untracked file behind") } if *changes != 2 { t.Fatalf("onChange ran %d times, want 2", *changes) } } func TestMutateCommitsAndPushes(t *testing.T) { bare, _ := remote(t) r, _ := newRepo(t, bare, true) ctx := context.Background() err := r.Mutate(ctx, "Solve moocast", func(dir string) error { os.MkdirAll(filepath.Join(dir, "solved"), 0o755) return os.Rename(filepath.Join(dir, "solving", "moocast"), filepath.Join(dir, "solved", "moocast")) }) if err != nil { t.Fatal(err) } if got := run(t, bare, "log", "-1", "--format=%s %an", "main"); got != "Solve moocast site" { t.Fatalf("remote head = %q, want the site's commit", got) } if got := run(t, bare, "ls-tree", "-r", "--name-only", "main"); got != "solved/moocast/main.cpp" { t.Fatalf("remote tree = %q", got) } } func TestMutateNoChangeMakesNoCommit(t *testing.T) { bare, _ := remote(t) r, _ := newRepo(t, bare, true) before := run(t, bare, "rev-parse", "main") if err := r.Mutate(context.Background(), "Nothing", func(string) error { return nil }); err != nil { t.Fatal(err) } if after := run(t, bare, "rev-parse", "main"); after != before { t.Fatal("an edit that changed nothing was committed") } } func TestMutateEditErrorDiscards(t *testing.T) { bare, _ := remote(t) r, _ := newRepo(t, bare, true) boom := errors.New("boom") err := r.Mutate(context.Background(), "Broken", func(dir string) error { os.WriteFile(filepath.Join(dir, "half-done"), []byte("x"), 0o644) return boom }) if !errors.Is(err, boom) { t.Fatalf("err = %v, want the edit's own error", err) } if _, err := os.Stat(filepath.Join(r.Dir(), "half-done")); err == nil { t.Fatal("a failed edit left its files behind") } } // An outside push between the site's sync and its push is the normal race // with `usaco sync` on the laptop. The edit is replayed on the new tip. func TestMutateReplaysAfterRejectedPush(t *testing.T) { bare, laptop := remote(t) r, _ := newRepo(t, bare, true) ctx := context.Background() if err := r.Sync(ctx); err != nil { t.Fatal(err) } calls := 0 err := r.Mutate(ctx, "Update moocast", func(dir string) error { calls++ if calls == 1 { // Sneak a push in after this attempt's sync. laptopCommit(t, laptop, "laptop.txt", "laptop wins the race") } return os.WriteFile(filepath.Join(dir, "solving", "moocast", "notes.md"), []byte("notes\n"), 0o644) }) if err != nil { t.Fatal(err) } if calls != 2 { t.Fatalf("edit ran %d times, want 2 (once, then a replay)", calls) } log := run(t, bare, "log", "--format=%s", "main") if !strings.HasPrefix(log, "Update moocast\nlaptop wins the race") { t.Fatalf("remote log =\n%s\nwant the site's commit on top of the laptop's", log) } } func TestMutateGivesUpAfterTwoRejections(t *testing.T) { bare, laptop := remote(t) r, _ := newRepo(t, bare, true) ctx := context.Background() if err := r.Sync(ctx); err != nil { t.Fatal(err) } n := 0 err := r.Mutate(ctx, "Update moocast", func(dir string) error { n++ laptopCommit(t, laptop, "laptop.txt", "race "+string(rune('0'+n))) return os.WriteFile(filepath.Join(dir, "solving", "moocast", "notes.md"), []byte("notes\n"), 0o644) }) if !errors.Is(err, ErrRejected) { t.Fatalf("err = %v, want ErrRejected", err) } // And the clone is back on the remote's tip, without the lost commit. if got, want := run(t, r.Dir(), "rev-parse", "HEAD"), run(t, bare, "rev-parse", "main"); got != want { t.Fatal("clone was left on an unpushed commit") } } func TestNoPushKeepsLocalCommitsAcrossSyncs(t *testing.T) { bare, laptop := remote(t) r, _ := newRepo(t, bare, false) ctx := context.Background() before := run(t, bare, "rev-parse", "main") err := r.Mutate(ctx, "Update moocast", func(dir string) error { return os.WriteFile(filepath.Join(dir, "solving", "moocast", "notes.md"), []byte("notes\n"), 0o644) }) if err != nil { t.Fatal(err) } if run(t, bare, "rev-parse", "main") != before { t.Fatal("pushed with Push off") } laptopCommit(t, laptop, "laptop.txt", "laptop") if err := r.Sync(ctx); err != nil { t.Fatal(err) } if got := run(t, r.Dir(), "log", "--format=%s", "-2"); got != "Update moocast\nlaptop" { t.Fatalf("local log = %q, want the local commit rebased onto the laptop's", got) } }