Skip to content

Commit

Permalink
[#197] Some path canonicalization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aeqz committed Dec 13, 2022
1 parent c6c48a0 commit dd17047
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Xrefcheck/System.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ getDirsBetweenRootAndFile root@(UnsafeCanonicalPath rootPath) file =

getRelativeChild :: CanonicalPath -> CanonicalPath -> Maybe FilePath
getRelativeChild (UnsafeCanonicalPath root) (UnsafeCanonicalPath child) =
dropWhile FP.isPathSeparator <$> stripPrefix root child
prepare <$> stripPrefix root child
where
prepare :: FilePath -> FilePath
prepare path = case dropWhile FP.isPathSeparator path of
"" -> "./"
other -> other

getRelativeOrAbsoluteChild :: CanonicalPath -> CanonicalPath -> FilePath
getRelativeOrAbsoluteChild root child@(UnsafeCanonicalPath c) =
Expand Down
47 changes: 47 additions & 0 deletions tests/Test/Xrefcheck/CanonicalPathSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
-
- SPDX-License-Identifier: MPL-2.0
-}

module Test.Xrefcheck.CanonicalPathSpec where

import Universum

import System.Directory (getCurrentDirectory)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=))

import Xrefcheck.System

test_canonicalPath :: TestTree
test_canonicalPath = testGroup "Path canonicalization"
[ testCase "Trailing separator" $ do
path <- canonicalizePath "./example/dir/"
current <- getCurrentDirectory >>= canonicalizePath
getRelativeOrAbsoluteChild current path @?= "example/dir",
testCase "Parent directory indirection" $ do
path <- canonicalizePath "dir1/../dir2"
current <- getCurrentDirectory >>= canonicalizePath
getRelativeOrAbsoluteChild current path @?= "dir2",
testCase "Through parent directory indirection" $ do
path <- canonicalizePath "dir1/../../../dir2"
current <- getCurrentDirectory >>= canonicalizePath
root <- current </ "../.."
getRelativeOrAbsoluteChild root path @?= "dir2",
testCase "Current directory indirection" $ do
path <- canonicalizePath "././dir1/./././dir2/././"
current <- getCurrentDirectory >>= canonicalizePath
getRelativeOrAbsoluteChild current path @?= "dir1/dir2",
testCase "Mixed indirections result in current directory" $ do
path <- canonicalizePath "././dir1/./.././dir2/./../"
current <- getCurrentDirectory >>= canonicalizePath
getRelativeOrAbsoluteChild current path @?= "./",
testCase "Child directory" $ do
path <- canonicalizePath "./dir1/dir2/"
current <- getCurrentDirectory >>= canonicalizePath
getRelativeChild current path @?= Just "dir1/dir2",
testCase "Not a child directory" $ do
root <- canonicalizePath "./dir1/dir2/"
path <- canonicalizePath "./dir1/dir3/"
getRelativeChild root path @?= Nothing
]
2 changes: 1 addition & 1 deletion tests/Test/Xrefcheck/RedirectRequestsSpec.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{- SPDX-FileCopyrightText: 2021 Serokell <https://serokell.io>
{- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
-
- SPDX-License-Identifier: MPL-2.0
-}
Expand Down

0 comments on commit dd17047

Please sign in to comment.