Release validation source bytes after checks
This commit is contained in:
@@ -78,6 +78,56 @@ func TestReadCandidateTreeUsesCapturedCommitAndEnumeratesGitEntries(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFileReturnsOwnedBytesAndCachesOnlySymlinkTargets(t *testing.T) {
|
||||
repository := newTreeRepository(t)
|
||||
writeTreeFile(t, repository, "source.txt", []byte("committed content"))
|
||||
if err := os.Symlink("source.txt", filepath.Join(repository, "alias.txt")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
commit := commitCandidateTree(t, repository)
|
||||
tree, err := ReadCandidateTree(context.Background(), repository, commit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cleanupCandidateTree(t, tree)
|
||||
|
||||
first, err := tree.ReadFile(context.Background(), "source.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first[0] = 'X'
|
||||
second, err := tree.ReadFile(context.Background(), "source.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(second) != "committed content" {
|
||||
t.Fatalf("second regular-file read = %q, want committed content", second)
|
||||
}
|
||||
if &first[0] == &second[0] {
|
||||
t.Fatal("regular-file reads share a backing byte slice")
|
||||
}
|
||||
|
||||
linkFirst, err := tree.ReadFile(context.Background(), "alias.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
linkFirst[0] = 'X'
|
||||
linkSecond, err := tree.ReadFile(context.Background(), "alias.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(linkSecond) != "source.txt" {
|
||||
t.Fatalf("second symlink read = %q, want source.txt", linkSecond)
|
||||
}
|
||||
if len(tree.symlinkTargets) != 1 {
|
||||
t.Fatalf("cached symlink target count = %d, want 1", len(tree.symlinkTargets))
|
||||
}
|
||||
destination, err := tree.ResolveDestination(context.Background(), "README.md", "alias.txt")
|
||||
if err != nil || destination.Kind != DestinationEntry || destination.Path != "source.txt" {
|
||||
t.Fatalf("ResolveDestination() = %#v, %v; want source.txt entry", destination, err)
|
||||
}
|
||||
}
|
||||
|
||||
func cleanupCandidateTree(tb testing.TB, tree *CandidateTree) {
|
||||
tb.Helper()
|
||||
tb.Cleanup(func() { _ = tree.Close() })
|
||||
|
||||
Reference in New Issue
Block a user