1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main

import (
	"reflect"
	"strings"
	"testing"
)

// buildTree builds a treeIndex from a list of paths, marking as a directory
// every path that is a prefix of another (matching what the tar loop records).
func buildTree(paths ...string) *treeIndex {
	isDir := map[string]bool{}
	for _, p := range paths {
		for _, other := range paths {
			if strings.HasPrefix(other, p+"/") {
				isDir[p] = true
			}
		}
	}
	tree := newTreeIndex()
	for _, p := range paths {
		tree.add(p, isDir[p])
	}
	return tree
}

// Listing composition: automatic collapse (unchanged behaviour) plus the
// additive `shortcut` entries a directory may declare.
func TestListingEntries(t *testing.T) {
	for _, tc := range []struct {
		name  string
		paths []string
		metas map[string]*dirMeta
		want  map[string][]string
	}{
		{
			name:  "directories sort before files, then by name",
			paths: []string{"README.md", "nix", "nix/a", "nix/b", "CLAUDE.md", "users", "users/x", "users/y"},
			want: map[string][]string{
				"": {"nix", "users", "CLAUDE.md", "README.md"},
			},
		},
		{
			name:  "a single-child chain collapses, as before",
			paths: []string{"users", "users/Profpatsch", "users/Profpatsch/blog", "users/Profpatsch/e.nix"},
			want: map[string][]string{
				"": {"users/Profpatsch"},
			},
		},
		{
			name: "a branch point stops the collapse (the regression)",
			paths: []string{
				"users", "users/packages.nix",
				"users/Profpatsch", "users/Profpatsch/blog", "users/Profpatsch/e.nix",
			},
			want: map[string][]string{
				"": {"users"},
			},
		},
		{
			name: "a shortcut adds an entry without removing one",
			paths: []string{
				"users", "users/packages.nix",
				"users/Profpatsch", "users/Profpatsch/blog", "users/Profpatsch/e.nix",
			},
			metas: map[string]*dirMeta{
				"": {shortcuts: []string{"users/Profpatsch"}},
			},
			want: map[string][]string{
				"": {"users", "users/Profpatsch"},
			},
		},
		{
			name: "several shortcuts keep their declared order, under their base entry",
			paths: []string{
				"nix", "nix/a",
				"users", "users/packages.nix",
				"users/Profpatsch", "users/Profpatsch/e.nix",
				"users/Profpatsch/git-blimey", "users/Profpatsch/git-blimey/main.go",
			},
			metas: map[string]*dirMeta{
				"": {shortcuts: []string{"users/Profpatsch", "users/Profpatsch/git-blimey"}},
			},
			want: map[string][]string{
				// Spliced after "users", NOT appended after "nix".
				"": {"nix/a", "users", "users/Profpatsch", "users/Profpatsch/git-blimey"},
			},
		},
		{
			name: "a shortcut under an automatically collapsed entry still attaches",
			paths: []string{
				"users", "users/Profpatsch",
				"users/Profpatsch/blog", "users/Profpatsch/blog/post.md",
				"users/Profpatsch/e.nix",
			},
			metas: map[string]*dirMeta{
				"": {shortcuts: []string{"users/Profpatsch/blog"}},
			},
			want: map[string][]string{
				"": {"users/Profpatsch", "users/Profpatsch/blog"},
			},
		},
		{
			name:  "a shortcut naming the collapse target is not duplicated",
			paths: []string{"users", "users/Profpatsch", "users/Profpatsch/a.nix", "users/Profpatsch/b.nix"},
			metas: map[string]*dirMeta{
				"": {shortcuts: []string{"users/Profpatsch"}},
			},
			want: map[string][]string{
				"": {"users/Profpatsch"},
			},
		},
		{
			name: "a shortcut declared in a subdirectory affects that listing only",
			paths: []string{
				"users", "users/packages.nix",
				"users/Profpatsch", "users/Profpatsch/blog", "users/Profpatsch/blog/post.md",
				"users/Profpatsch/e.nix",
			},
			metas: map[string]*dirMeta{
				"users": {dir: "users", shortcuts: []string{"Profpatsch/blog"}},
			},
			want: map[string][]string{
				"":      {"users"},
				"users": {"users/Profpatsch", "users/Profpatsch/blog", "users/packages.nix"},
			},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			got := listingEntries(buildTree(tc.paths...), tc.metas)
			for dir, want := range tc.want {
				if !reflect.DeepEqual(got[dir], want) {
					t.Errorf("listing of %q =\n  %q\nwant\n  %q", dir, got[dir], want)
				}
			}
		})
	}
}

// Every path in the tree must stay reachable by walking the listings from the
// root: shortcuts are additive and collapse only ever folds single-child
// chains, so no declaration can strand part of the tree.
func TestListingReachesEveryPath(t *testing.T) {
	paths := []string{
		"README.md",
		"nix", "nix/buildGo", "nix/buildGo/default.nix",
		"users", "users/packages.nix",
		"users/Profpatsch", "users/Profpatsch/packages.nix",
		"users/Profpatsch/git-blimey", "users/Profpatsch/git-blimey/main.go",
		"users/Profpatsch/blog", "users/Profpatsch/blog/post.md",
	}
	tree := buildTree(paths...)
	entries := listingEntries(tree, map[string]*dirMeta{
		"": {shortcuts: []string{"users/Profpatsch", "users/Profpatsch/git-blimey"}},
	})

	// Walk from the root, following entries; a directory entry that is not a
	// direct child still has its own listing, so recursion covers the gap that
	// collapsing skipped over.
	seen := map[string]bool{}
	var walk func(dir string)
	walk = func(dir string) {
		for _, target := range entries[dir] {
			if seen[target] {
				continue
			}
			seen[target] = true
			if tree.isDir[target] {
				walk(target)
			}
		}
	}
	walk("")

	for _, p := range paths {
		if tree.isDir[p] {
			// An intermediate directory of a collapsed chain is legitimately
			// not listed anywhere (that is what collapsing means); it stays
			// reachable via the breadcrumb.
			continue
		}
		if !seen[p] {
			t.Errorf("file %q is not reachable from the root listing", p)
		}
	}
}