Skip to content

Commit 511e5d0

Browse files
committed
[#197] Some path canonicalization tests
1 parent 8a37305 commit 511e5d0

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/Xrefcheck/System.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ getDirsBetweenRootAndFile root@(UnsafeCanonicalPath rootPath) file =
9292

9393
getRelativeChild :: CanonicalPath -> CanonicalPath -> Maybe FilePath
9494
getRelativeChild (UnsafeCanonicalPath root) (UnsafeCanonicalPath child) =
95-
dropWhile FP.isPathSeparator <$> stripPrefix root child
95+
prepare <$> stripPrefix root child
96+
where
97+
prepare :: FilePath -> FilePath
98+
prepare path = case dropWhile FP.isPathSeparator path of
99+
"" -> "./"
100+
other -> other
96101

97102
getRelativeOrAbsoluteChild :: CanonicalPath -> CanonicalPath -> FilePath
98103
getRelativeOrAbsoluteChild root child@(UnsafeCanonicalPath c) =
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
2+
-
3+
- SPDX-License-Identifier: MPL-2.0
4+
-}
5+
6+
module Test.Xrefcheck.CanonicalPathSpec where
7+
8+
import Universum
9+
10+
import System.Directory (getCurrentDirectory)
11+
import Test.Tasty (TestTree, testGroup)
12+
import Test.Tasty.HUnit (testCase, (@?=))
13+
14+
import Xrefcheck.System
15+
16+
test_canonicalPath :: TestTree
17+
test_canonicalPath = testGroup "Path canonicalization"
18+
[ testCase "Trailing separator" $ do
19+
path <- canonicalizePath "./example/dir/"
20+
current <- getCurrentDirectory >>= canonicalizePath
21+
getRelativeOrAbsoluteChild current path @?= "example/dir",
22+
testCase "Parent directory indirection" $ do
23+
path <- canonicalizePath "dir1/../dir2"
24+
current <- getCurrentDirectory >>= canonicalizePath
25+
getRelativeOrAbsoluteChild current path @?= "dir2",
26+
testCase "Through parent directory indirection" $ do
27+
path <- canonicalizePath "dir1/../../../dir2"
28+
current <- getCurrentDirectory >>= canonicalizePath
29+
root <- current </ "../.."
30+
getRelativeOrAbsoluteChild root path @?= "dir2",
31+
testCase "Current directory indirection" $ do
32+
path <- canonicalizePath "././dir1/./././dir2/././"
33+
current <- getCurrentDirectory >>= canonicalizePath
34+
getRelativeOrAbsoluteChild current path @?= "dir1/dir2",
35+
testCase "Mixed indirections result in current directory" $ do
36+
path <- canonicalizePath "././dir1/./.././dir2/./../"
37+
current <- getCurrentDirectory >>= canonicalizePath
38+
getRelativeOrAbsoluteChild current path @?= "./",
39+
testCase "Child directory" $ do
40+
path <- canonicalizePath "./dir1/dir2/"
41+
current <- getCurrentDirectory >>= canonicalizePath
42+
getRelativeChild current path @?= Just "dir1/dir2",
43+
testCase "Not a child directory" $ do
44+
root <- canonicalizePath "./dir1/dir2/"
45+
path <- canonicalizePath "./dir1/dir3/"
46+
getRelativeChild root path @?= Nothing
47+
]

tests/Test/Xrefcheck/RedirectRequestsSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{- SPDX-FileCopyrightText: 2021 Serokell <https://serokell.io>
1+
{- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
22
-
33
- SPDX-License-Identifier: MPL-2.0
44
-}

0 commit comments

Comments
 (0)