|
| 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 | + ] |
0 commit comments