gunk-dev/the-valley

tree_test.go

source171dbbb226c0main's published tip history
6.2 KiB
1package delta
2
3import (
4 "strings"
5 "testing"
6)
7
8// A rename is a deletion and an addition with one hash, and a change of
9// mode alone, a symlink's target and a file replacing a symlink are each
10// one line, read from trees git itself built.
11func TestBetweenReadsTreesAsWholeFiles(t *testing.T) {
12 r := newRepo(t)
13 pre := r.tree([]file{
14 {"old name.txt", ModeFile, []byte("moved\n")},
15 {"run.sh", ModeFile, []byte("#!/bin/sh\n")},
16 {"link", ModeSymlink, []byte("old name.txt")},
17 {"was-link", ModeSymlink, []byte("run.sh")},
18 {"same/deep/file", ModeFile, []byte("untouched\n")},
19 })
20 post := r.tree([]file{
21 {"new name.txt", ModeFile, []byte("moved\n")},
22 {"run.sh", ModeExec, []byte("#!/bin/sh\n")},
23 {"link", ModeSymlink, []byte("new name.txt")},
24 {"was-link", ModeFile, []byte("run.sh")},
25 {"same/deep/file", ModeFile, []byte("untouched\n")},
26 })
27 d, err := Between(r.repo(), pre, post)
28 if err != nil {
29 t.Fatal(err)
30 }
31 var got []string
32 for _, c := range d {
33 got = append(got, c.Path+": "+c.Pre.Mode+" -> "+c.Post.Mode)
34 }
35 want := []string{
36 "link: 120000 -> 120000",
37 "new name.txt: -> 100644",
38 "old name.txt: 100644 -> ",
39 "run.sh: 100644 -> 100755",
40 "was-link: 120000 -> 100644",
41 }
42 if strings.Join(got, "\n") != strings.Join(want, "\n") {
43 t.Fatalf("the delta is\n%s", strings.Join(got, "\n"))
44 }
45 if d[1].Post.Sum != d[2].Pre.Sum {
46 t.Fatal("a rename's two lines do not carry the same hash")
47 }
48 if d[3].Pre.Sum != d[3].Post.Sum || d[4].Pre.Sum != d[4].Post.Sum {
49 t.Fatal("a change of mode alone changed the hash")
50 }
51}
52
53// Trees a checkout on case-sensitive Linux could not hold faithfully are
54// refused on either side, and so is one whose entries git would not read
55// as written. Each is refused against a good tree, against the empty
56// tree in either order, and against itself, where nothing differs. An
57// alias deep in a directory both trees share is refused too: each tree
58// is read whole. So is a symlink both trees hold unchanged, and one whose
59// blob another entry names first as a file.
60func TestTreesACheckoutCannotHoldAreRefused(t *testing.T) {
61 r := newRepo(t)
62 blob := r.blob([]byte("x\n"))
63 nothing := r.blob(nil)
64 sub := r.tree([]file{{"inner", ModeFile, []byte("x\n")}})
65 empty := r.git(nil, "mktree")
66 good := r.tree([]file{{"ok", ModeFile, []byte("x\n")}})
67 cases := []struct {
68 name, says string
69 tree string
70 }{
71 {"a file and a directory at one path", "named twice",
72 r.rawTree(rawEntry{"100644", "a", blob}, rawEntry{"40000", "a", sub})},
73 {"one name twice", "named twice",
74 r.rawTree(rawEntry{"100644", "a", blob}, rawEntry{"100644", "a", blob})},
75 {"a submodule", "submodule",
76 r.rawTree(rawEntry{"160000", "mod", strings.Repeat("ab", 20)})},
77 {"a mode git does not write", "mode \"100664\"",
78 r.rawTree(rawEntry{"100664", "a", blob})},
79 {"a zero-padded directory mode", "mode \"040000\"",
80 r.rawTree(rawEntry{"040000", "a", sub})},
81 {"a name holding a slash", "slash",
82 r.rawTree(rawEntry{"100644", "a/b", blob})},
83 {"the name ..", "component ..",
84 r.rawTree(rawEntry{"100644", "..", blob})},
85 {"the name .git", "git keeps for itself",
86 r.rawTree(rawEntry{"40000", ".git", sub})},
87 {"the name .GIT", "git keeps for itself",
88 r.rawTree(rawEntry{"40000", "d", r.rawTree(rawEntry{"100644", ".GIT", blob})})},
89 {"an empty directory", "empty directory",
90 r.rawTree(rawEntry{"40000", "hollow", empty})},
91 {"entries out of order", "out of git's order",
92 r.rawTree(rawEntry{"100644", "b", blob}, rawEntry{"100644", "a", blob})},
93 {"a directory sorted as a file", "out of git's order",
94 r.rawTree(rawEntry{"40000", "a", sub}, rawEntry{"100644", "a.txt", blob})},
95 {"a file whose object is a tree", "no blob",
96 r.rawTree(rawEntry{"100644", "a", sub})},
97 {"a directory whose object is a blob", "is a blob",
98 r.rawTree(rawEntry{"40000", "a", blob})},
99 {"an alias in a shared directory", "named twice",
100 r.rawTree(rawEntry{"40000", "shared",
101 r.rawTree(rawEntry{"100644", "a", blob}, rawEntry{"40000", "a", sub})})},
102 {"a name of 256 bytes", "a component of 256 bytes, longer than Linux's limit of 255",
103 r.rawTree(rawEntry{"40000", "d", r.rawTree(rawEntry{"100644", strings.Repeat("n", 256), blob})})},
104 {"a symlink with an empty target", "\"link\" has an empty target",
105 r.rawTree(rawEntry{"120000", "link", nothing})},
106 {"a symlink whose target holds a NUL", "\"d/link\" has a target holding a NUL",
107 r.rawTree(rawEntry{"40000", "d", r.rawTree(rawEntry{"120000", "link", r.blob([]byte("a\x00b"))})})},
108 {"a symlink whose target is 4096 bytes", "a target of 4096 bytes, longer than Linux's limit of 4095",
109 r.rawTree(rawEntry{"120000", "link", r.blob([]byte(strings.Repeat("t", 4096)))})},
110 {"a symlink whose blob a file names first", "\"b\" has an empty target",
111 r.rawTree(rawEntry{"100644", "a", nothing}, rawEntry{"120000", "b", nothing})},
112 }
113 for _, c := range cases {
114 t.Run(c.name, func(t *testing.T) {
115 for _, pair := range [][2]string{{c.tree, good}, {good, c.tree}, {c.tree, empty}, {empty, c.tree}, {c.tree, c.tree}} {
116 _, err := Between(r.repo(), pair[0], pair[1])
117 if err == nil {
118 t.Fatalf("Between(%s, %s) accepted it", pair[0], pair[1])
119 }
120 if !strings.Contains(err.Error(), c.says) {
121 t.Fatalf("the refusal does not say %q: %v", c.says, err)
122 }
123 }
124 })
125 }
126}
127
128// A file and a directory at one path in two different trees is no alias:
129// the file is absent after and the directory's files absent before.
130func TestAFileMayBecomeADirectory(t *testing.T) {
131 r := newRepo(t)
132 pre := r.tree([]file{{"a", ModeFile, []byte("flat\n")}})
133 post := r.tree([]file{{"a/b", ModeFile, []byte("nested\n")}})
134 d, err := Between(r.repo(), pre, post)
135 if err != nil {
136 t.Fatal(err)
137 }
138 if len(d) != 2 || d[0].Path != "a" || !d[0].Post.Absent() || d[1].Path != "a/b" || !d[1].Pre.Absent() {
139 t.Fatalf("the delta is %q", d.Encode())
140 }
141}
142
143// Only a full object ID that names a tree or a commit is read.
144func TestBetweenTakesTreesAndCommitsByID(t *testing.T) {
145 r := newRepo(t)
146 tree := r.tree([]file{{"a", ModeFile, []byte("a\n")}})
147 blob := r.blob([]byte("a\n"))
148 for _, bad := range []string{"HEAD", tree[:12], blob, strings.Repeat("0", 40)} {
149 if _, err := Between(r.repo(), bad, tree); err == nil {
150 t.Errorf("Between took %q", bad)
151 }
152 }
153}