gunk-dev/the-valley

shared_test.go

source6a6791929c36main's published tip history
1.6 KiB
1package git
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "testing"
10 "time"
11)
12
13// A worker can put a shared file back as it found it before finalize looks.
14// The attributes and hooks, which nothing writes in the ordinary course,
15// still differ; the config's branch tracking doesn't count.
16func TestSharedChangesSeesRestoredFiles(t *testing.T) {
17 ctx := context.Background()
18 dir := t.TempDir()
19 if out, err := exec.Command("git", "init", "-q", dir).CombinedOutput(); err != nil {
20 t.Fatalf("git init: %v: %s", err, out)
21 }
22 common := filepath.Join(dir, ".git")
23 attrs := filepath.Join(common, "info", "attributes")
24 if err := os.WriteFile(attrs, []byte("*.go text\n"), 0o644); err != nil {
25 t.Fatal(err)
26 }
27 before := SharedSnapshot(ctx, common)
28
29 later := time.Now().Add(2 * time.Second)
30 if err := os.WriteFile(attrs, []byte("*.go text\n"), 0o644); err != nil {
31 t.Fatal(err)
32 }
33 if err := os.Chtimes(attrs, later, later); err != nil {
34 t.Fatal(err)
35 }
36 hook := filepath.Join(common, "hooks", "post-checkout")
37 if err := os.WriteFile(hook, []byte("#!/bin/sh\n"), 0o755); err != nil {
38 t.Fatal(err)
39 }
40 if err := os.Remove(hook); err != nil {
41 t.Fatal(err)
42 }
43 if err := os.Chtimes(filepath.Dir(hook), later, later); err != nil {
44 t.Fatal(err)
45 }
46 if out, err := exec.Command("git", "-C", dir, "config", "branch.main.remote", "origin").CombinedOutput(); err != nil {
47 t.Fatalf("git config: %v: %s", err, out)
48 }
49
50 if got := fmt.Sprint(SharedChanges(before, SharedSnapshot(ctx, common))); got != "[hooks info/attributes]" {
51 t.Fatalf("changed = %s, want [hooks info/attributes]", got)
52 }
53}