path 0.6.0 → 0.6.1
raw patch · 5 files changed
+119/−7 lines, 5 files
Files
- CHANGELOG +5/−1
- path.cabal +1/−1
- src/Path.hs +47/−2
- test/Posix.hs +32/−0
- test/Windows.hs +34/−3
CHANGELOG view
@@ -1,3 +1,7 @@+0.6.1:+ * Add 'addFileExtension' function and its operator form: (<.>).+ * Derive 'Eq' instance for 'PathException'.+ 0.6.0: * Deprecate PathParseException and rename it to PathException@@ -45,7 +49,7 @@ 0.2.0: - * Rename parentAbs to simply parent.+ * Rename parentAbs to simply parent. * Add dirname. 0.3.0:
path.cabal view
@@ -1,5 +1,5 @@ name: path-version: 0.6.0+version: 0.6.1 synopsis: Support for well-typed paths description: Support for well-typed paths. license: BSD3
src/Path.hs view
@@ -49,6 +49,8 @@ ,filename ,dirname ,fileExtension+ ,addFileExtension+ ,(<.>) ,setFileExtension ,(-<.>) -- * Parsing@@ -138,7 +140,7 @@ | InvalidAbsFile FilePath | InvalidRelFile FilePath | NotAProperPrefix FilePath FilePath- deriving (Show,Typeable)+ deriving (Show,Eq,Typeable) instance Exception PathException --------------------------------------------------------------------------------@@ -330,6 +332,50 @@ fileExtension :: Path b File -> String fileExtension = FilePath.takeExtension . toFilePath +-- | Add extension to given file path. Throws if the+-- resulting filename does not parse.+--+-- >>> addFileExtension "txt $(mkRelFile "foo")+-- "foo.txt"+-- >>> addFileExtension "symbols" $(mkRelFile "Data.List")+-- "Data.List.symbols"+-- >>> addFileExtension ".symbols" $(mkRelFile "Data.List")+-- "Data.List.symbols"+-- >>> addFileExtension "symbols" $(mkRelFile "Data.List.")+-- "Data.List..symbols"+-- >>> addFileExtension ".symbols" $(mkRelFile "Data.List.")+-- "Data.List..symbols"+-- >>> addFileExtension "evil/" $(mkRelFile "Data.List")+-- *** Exception: InvalidRelFile "Data.List.evil/"+--+-- @since 0.6.1+addFileExtension :: MonadThrow m+ => String -- ^ Extension to add+ -> Path b File -- ^ Old file name+ -> m (Path b File) -- ^ New file name with the desired extension added at the end+addFileExtension ext (Path path) =+ if FilePath.isAbsolute path+ then liftM coercePath (parseAbsFile (FilePath.addExtension path ext))+ else liftM coercePath (parseRelFile (FilePath.addExtension path ext))+ where coercePath :: Path a b -> Path a' b'+ coercePath (Path a) = Path a++-- | A synonym for 'addFileExtension' in the form of an operator.+-- See more examples there.+--+-- >>> $(mkRelFile "Data.List") <.> "symbols"+-- "Data.List.symbols"+-- >>> $(mkRelFile "Data.List") <.> "evil/"+-- *** Exception: InvalidRelFile "Data.List.evil/"+--+-- @since 0.6.1+infixr 7 <.>+(<.>) :: MonadThrow m+ => Path b File -- ^ Old file name+ -> String -- ^ Extension to add+ -> m (Path b File) -- ^ New file name with the desired extension added at the end+(<.>) = flip addFileExtension+ -- | Replace\/add extension to given file path. Throws if the -- resulting filename does not parse. --@@ -518,7 +564,6 @@ Left err -> error (show err) Right (Path str) -> [|Path $(return (LitE (StringL str))) :: Path Rel File|]- -------------------------------------------------------------------------------- -- Internal functions
test/Posix.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE LambdaCase #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE QuasiQuotes #-}@@ -29,6 +30,7 @@ describe "Operations: parent" operationParent describe "Operations: filename" operationFilename describe "Operations: dirname" operationDirname+ describe "Operations: addFileExtension" operationAddFileExtension describe "Operations: setFileExtension" operationSetFileExtension describe "Restrictions" restrictions describe "Aeson Instances" aesonInstances@@ -177,6 +179,36 @@ (toFilePath $(mkRelDir ".") == "./") it "show $(mkRelDir \".\") == \"\\\"./\\\"\"" (show $(mkRelDir ".") == "\"./\"")++operationAddFileExtension :: Spec+operationAddFileExtension = do+ it "adds extension if there is none" $+ addFileExtension "ext" $(mkAbsFile "/directory/path")+ `shouldReturn` $(mkAbsFile "/directory/path.ext")+ it "adds extension if there is already one" $+ addFileExtension "baz" $(mkRelFile "foo.bar")+ `shouldReturn` $(mkRelFile "foo.bar.baz")+ it "adds extension with dot" $+ addFileExtension ".bar" $(mkRelFile "foo")+ `shouldReturn` $(mkRelFile "foo.bar")+ it "adds extension with dot after dot" $+ $(mkRelFile "foo.") <.> ".bar"+ `shouldReturn` $(mkRelFile "foo..bar")+ it "adds extension without dot after dot" $+ $(mkRelFile "foo.") <.> "bar"+ `shouldReturn` $(mkRelFile "foo..bar")+ it "adds extension with separator" $ -- I'm not sure it's okay+ $(mkRelFile "foo") <.> "evil/extension"+ `shouldReturn` $(mkRelFile "foo.evil/extension")+ it "adds extension to file inside dotted directory" $+ $(mkRelFile "foo.bar/baz") <.> "txt"+ `shouldReturn` $(mkRelFile "foo.bar/baz.txt")+ it "throws InvalidRelFile extension if extenstion ends with /" $+ $(mkRelFile "foo") <.> "evil/"+ `shouldThrow` (== InvalidRelFile "foo.evil/")+ it "throws InvalidAbsFile extension if extenstion ends with /" $+ $(mkAbsFile "/home/cfg") <.> "txt/" -- No Eq instance for PathException+ `shouldThrow` (== InvalidAbsFile "/home/cfg.txt/") operationSetFileExtension :: Spec operationSetFileExtension = do
test/Windows.hs view
@@ -1,6 +1,7 @@-{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE TemplateHaskell #-} -- | Test suite. @@ -29,6 +30,7 @@ describe "Operations: parent" operationParent describe "Operations: filename" operationFilename describe "Operations: dirname" operationDirname+ describe "Operations: addFileExtension" operationAddFileExtension describe "Operations: setFileExtension" operationSetFileExtension describe "Restrictions" restrictions describe "Aeson Instances" aesonInstances@@ -122,7 +124,6 @@ it "not (\\ `isProperPrefixOf` \\)" (not (isProperPrefixOf $(mkAbsDir "C:\\") $(mkAbsDir "C:\\"))) - -- | The 'stripProperPrefix' operation. operationStripProperPrefix :: Spec operationStripProperPrefix =@@ -175,6 +176,36 @@ (toFilePath $(mkRelDir ".") == ".\\") it "show $(mkRelDir \".\") == \"\\\".\\\\\"\"" (show $(mkRelDir ".") == "\".\\\\\"")++operationAddFileExtension :: Spec+operationAddFileExtension = do+ it "adds extension if there is none" $+ addFileExtension "ext" $(mkAbsFile "C:\\directory\\path")+ `shouldReturn` $(mkAbsFile "C:\\directory\\path.ext")+ it "adds extension if there is already one" $+ addFileExtension "baz" $(mkRelFile "foo.bar")+ `shouldReturn` $(mkRelFile "foo.bar.baz")+ it "adds extension with dot" $+ addFileExtension ".bar" $(mkRelFile "foo")+ `shouldReturn` $(mkRelFile "foo.bar")+ it "adds extension with dot after dot" $+ $(mkRelFile "foo.") <.> ".bar"+ `shouldReturn` $(mkRelFile "foo..bar")+ it "adds extension without dot after dot" $+ $(mkRelFile "foo.") <.> "bar"+ `shouldReturn` $(mkRelFile "foo..bar")+ it "adds extension with separator" $ -- I'm not sure it's okay+ $(mkRelFile "foo") <.> "evil\\extension"+ `shouldReturn` $(mkRelFile "foo.evil\\extension")+ it "adds extension to file inside dotted directory" $+ $(mkRelFile "foo.bar\\baz") <.> "txt"+ `shouldReturn` $(mkRelFile "foo.bar\\baz.txt")+ it "throws InvalidRelFile extension if extenstion ends with \\" $+ $(mkRelFile "foo") <.> "evil\\"+ `shouldThrow` (== InvalidRelFile "foo.evil\\")+ it "throws InvalidAbsFile extension if extenstion ends with \\" $+ $(mkAbsFile "C:\\home\\cfg") <.> "txt\\"+ `shouldThrow` (== InvalidAbsFile "C:\\home\\cfg.txt\\") operationSetFileExtension :: Spec operationSetFileExtension = do