Skip to content

Commit

Permalink
Don't discard the drive letter when joining with an absolute path
Browse files Browse the repository at this point in the history
When encountering a symlink that contains an absolute target without a
drive letter, assume the target resides on the same drive.
  • Loading branch information
EdSchouten committed Jun 22, 2024
1 parent 0554e2f commit 221b636
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/filesystem/path/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ func (w *buildingScopeWalker) OnAbsolute() (ComponentWalker, error) {
return nil, err
}
*w.b = Builder{
absolute: true,
components: w.b.components[:0],
suffix: "/",
absolute: true,
driveLetter: w.b.driveLetter,
components: w.b.components[:0],
suffix: "/",
}
return w.b.getComponentWalker(componentWalker), nil
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/filesystem/path/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,28 @@ func TestBuilder(t *testing.T) {
require.NoError(t, path.Resolve(path.NewUNIXParser(".."), s2))
require.Equal(t, "/hello/..", builder2.GetUNIXString())
})

// When encountering a symlink target that is an absolute path
// without a drive letter, we should assume the path resolves to
// a location on the same drive.
t.Run("DriveLetterWithAbsoluteSymlink", func(t *testing.T) {
scopeWalker1 := mock.NewMockScopeWalker(ctrl)
componentWalker1 := mock.NewMockComponentWalker(ctrl)
scopeWalker1.EXPECT().OnDriveLetter('C').Return(componentWalker1, nil)
scopeWalker2 := mock.NewMockScopeWalker(ctrl)
componentWalker1.EXPECT().OnTerminal(path.MustNewComponent("hello")).Return(
&path.GotSymlink{
Parent: scopeWalker2,
Target: path.NewWindowsParser("\\world"),
},
nil,
)
componentWalker2 := mock.NewMockComponentWalker(ctrl)
scopeWalker2.EXPECT().OnAbsolute().Return(componentWalker2, nil)
componentWalker2.EXPECT().OnTerminal(path.MustNewComponent("world"))

builder1, s1 := path.EmptyBuilder.Join(scopeWalker1)
require.NoError(t, path.Resolve(path.NewWindowsParser("C:\\hello"), s1))
require.Equal(t, "C:\\world", mustGetWindowsString(builder1))
})
}

0 comments on commit 221b636

Please sign in to comment.