Skip to content

Commit

Permalink
Don't assume terminal components are reversible
Browse files Browse the repository at this point in the history
ComponentWalker.OnTerminal() isn't capable of returning whether a
component is reversible. Right now Builder assumes it is, whereas that
isn't necessarily safe. When joining "/hello" and "..", we may not
necessarily be able to simplify the result to "/". It should remain
"/hello/..", as "/hello" may not be a directory.
  • Loading branch information
EdSchouten committed Jun 22, 2024
1 parent 07dec22 commit fd176d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/filesystem/path/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func (cw *buildingComponentWalker) OnTerminal(name Component) (*GotSymlink, erro
}
if r == nil {
cw.b.components = append(cw.b.components, name.String())
cw.b.firstReversibleIndex = len(cw.b.components)
cw.b.suffix = ""
return nil, 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 @@ -301,4 +301,28 @@ func TestBuilder(t *testing.T) {
require.NoError(t, path.Resolve(path.NewUNIXParser("/hello/world/.."), s))
require.Equal(t, "/hello/", builder.GetUNIXString())
})

// OnTerminal() does not allow returning information whether the
// component is reversible. This means that joining and
// appending ".." can't necessarily be simplified.
t.Run("OnTerminalIsNonReversible", func(t *testing.T) {
scopeWalker1 := mock.NewMockScopeWalker(ctrl)
componentWalker1 := mock.NewMockComponentWalker(ctrl)
scopeWalker1.EXPECT().OnAbsolute().Return(componentWalker1, nil)
componentWalker1.EXPECT().OnTerminal(path.MustNewComponent("hello"))

builder1, s1 := path.EmptyBuilder.Join(scopeWalker1)
require.NoError(t, path.Resolve(path.NewUNIXParser("/hello"), s1))
require.Equal(t, "/hello", builder1.GetUNIXString())

scopeWalker2 := mock.NewMockScopeWalker(ctrl)
componentWalker2 := mock.NewMockComponentWalker(ctrl)
scopeWalker2.EXPECT().OnRelative().Return(componentWalker2, nil)
componentWalker3 := mock.NewMockComponentWalker(ctrl)
componentWalker2.EXPECT().OnUp().Return(componentWalker3, nil)

builder2, s2 := builder1.Join(scopeWalker2)
require.NoError(t, path.Resolve(path.NewUNIXParser(".."), s2))
require.Equal(t, "/hello/..", builder2.GetUNIXString())
})
}

0 comments on commit fd176d6

Please sign in to comment.