Skip to content

Commit 9e85628

Browse files
committed
test(engine): cover RootFS branches of cross-file resolution
codecov/patch flagged uncovered lines in the new code: resolveTargetFile's in-memory RootFS branch (the anchored-link path), resolveWorkspaceRelTarget's empty/absolute-link guard, and schemaRootFS's nil-file branch. Add focused unit tests so every new branch is exercised in its own package. https://claude.ai/code/session_01RkZnJM1JYhhioZyzfvH3nt
1 parent a42ec07 commit 9e85628

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

internal/rules/crossfilereferenceintegrity/rule_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,49 @@ func TestTargetExists_ParentTraversalViaRootFS(t *testing.T) {
707707
"a link escaping the workspace root must not resolve here")
708708
}
709709

710+
// TestResolveWorkspaceRelTarget covers every branch of the helper:
711+
// joining onto the source dir, collapsing "..", and the rejections for
712+
// empty, absolute, and root-escaping links.
713+
func TestResolveWorkspaceRelTarget(t *testing.T) {
714+
rel, ok := resolveWorkspaceRelTarget("docs/background/linters.md", "../../internal/x.md")
715+
require.True(t, ok)
716+
assert.Equal(t, "internal/x.md", rel)
717+
718+
rel, ok = resolveWorkspaceRelTarget("docs/a.md", "b.md")
719+
require.True(t, ok)
720+
assert.Equal(t, "docs/b.md", rel)
721+
722+
_, ok = resolveWorkspaceRelTarget("docs/a.md", "")
723+
assert.False(t, ok, "empty link is not a workspace target")
724+
_, ok = resolveWorkspaceRelTarget("docs/a.md", "/etc/passwd")
725+
assert.False(t, ok, "absolute link is not a workspace target")
726+
_, ok = resolveWorkspaceRelTarget("docs/a.md", "../../escape.md")
727+
assert.False(t, ok, "a link escaping the workspace root is rejected")
728+
}
729+
730+
// TestResolveTargetFile_ParentTraversalViaRootFS exercises the in-memory
731+
// RootFS branch of resolveTargetFile (the anchored-link path), which the
732+
// Session/WASM engine takes for an up-and-over target.
733+
func TestResolveTargetFile_ParentTraversalViaRootFS(t *testing.T) {
734+
root := t.TempDir()
735+
require.NoError(t, os.MkdirAll(filepath.Join(root, "internal", "rules", "x"), 0o755))
736+
writeFile(t, filepath.Join(root, "internal", "rules", "x", "README.md"), "# X\n")
737+
738+
f := &lint.File{
739+
Path: "docs/background/linters.md",
740+
FS: os.DirFS(root),
741+
RootFS: os.DirFS(root),
742+
}
743+
tf, ok := resolveTargetFile(f, "../../internal/rules/x/README.md", "")
744+
require.True(t, ok, "an existing up-and-over target must resolve via RootFS")
745+
data, err := tf.read()
746+
require.NoError(t, err)
747+
assert.Equal(t, []byte("# X\n"), data)
748+
749+
_, ok = resolveTargetFile(f, "../../internal/rules/x/MISSING.md", "")
750+
assert.False(t, ok, "a missing up-and-over target must not resolve")
751+
}
752+
710753
// =====================================================================
711754
// Additional coverage: toStringSlice with []string type
712755
// =====================================================================

internal/rules/requiredstructure/runcache_wiring_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ func TestRule_SchemaIncludeViaRootFS(t *testing.T) {
237237
}
238238
}
239239

240+
// TestSchemaRootFS covers both branches of the RootFS accessor: a nil
241+
// file (the cache primitive's struct-literal test path) yields nil, and a
242+
// file with a RootFS yields it.
243+
func TestSchemaRootFS(t *testing.T) {
244+
assert.Nil(t, schemaRootFS(nil))
245+
assert.NotNil(t, schemaRootFS(&lint.File{RootFS: fstest.MapFS{}}))
246+
}
247+
240248
// TestRule_FragmentInvalidationEvictsParsedSchema is the end-to-end
241249
// integration check for Copilot thread 1 on PR #377: after Rule.Check
242250
// reads a schema whose <?include?> reaches a fragment, calling

0 commit comments

Comments
 (0)