Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions hackage-security/src/Hackage/Security/Util/Path.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module Hackage.Security.Util.Path (
, fromURIPath
, uriPath
, modifyUriPath
-- * Internals
, mkPathNative
, unPathNative
-- * Re-exports
, IOMode(..)
, BufferMode(..)
Expand Down Expand Up @@ -118,10 +121,17 @@ newtype Path a = Path FilePath -- always a Posix style path internally
deriving (Show, Eq, Ord)

mkPathNative :: FilePath -> Path a
mkPathNative = Path . FP.Posix.joinPath . FP.Native.splitDirectories
mkPathNative = Path . canonicalizePathSeparator

unPathNative :: Path a -> FilePath
unPathNative (Path fp) = FP.Native.joinPath . FP.Posix.splitDirectories $ fp
unPathNative (Path fp) = fp

canonicalizePathSeparator :: FilePath -> FilePath
canonicalizePathSeparator = map (replaceSeparator)
where
replaceSeparator c
| FP.Native.isPathSeparator c = '/'
| otherwise = c

mkPathPosix :: FilePath -> Path a
mkPathPosix = Path
Expand Down
13 changes: 12 additions & 1 deletion hackage-security/tests/TestSuite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Data.Time ( UTCTime, getCurrentTime )
import Network.URI ( URI, parseURI )
import Test.Tasty ( defaultMain, testGroup, TestTree )
import Test.Tasty.HUnit ( testCase, (@?=), assertEqual, assertFailure, Assertion )
import Test.Tasty.QuickCheck ( testProperty )
import Test.Tasty.QuickCheck ( testProperty, Property, (===), property )
import System.IO.Temp (withSystemTempDirectory)
import qualified Codec.Archive.Tar.Entry as Tar
import qualified Data.ByteString.Lazy.Char8 as BS
Expand Down Expand Up @@ -72,6 +72,9 @@ tests = testGroup "hackage-security" [
, testProperty "prop_canonical_pretty" JSON.prop_canonical_pretty
, testProperty "prop_aeson_canonical" JSON.prop_aeson_canonical
]
, testGroup "Path" [
testProperty "Hackage.Security.Util.Path.mkPathNative" prop_mkPathNative
]
]

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -547,3 +550,11 @@ checkExpiry = Just `fmap` getCurrentTime
mkPackageName :: String -> PackageName
mkPackageName = PackageName
#endif

{-------------------------------------------------------------------------------
Path tests
-------------------------------------------------------------------------------}

prop_mkPathNative :: Property
prop_mkPathNative = property $ \(fp :: FilePath) -> (mkPathNative . unPathNative . mkPathNative) fp === mkPathNative fp

Loading