Skip to content

Commit f7b03a6

Browse files
Amended isExtensionOf and its haddock, updated tests
1 parent 64f23c5 commit f7b03a6

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

System/FilePath/Internal.hs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module System.FilePath.MODULE_NAME
105105

106106
import Data.Char(toLower, toUpper, isAsciiLower, isAsciiUpper)
107107
import Data.Maybe(isJust)
108-
import Data.List(stripPrefix)
108+
import Data.List(stripPrefix, isSuffixOf)
109109

110110
import System.Environment(getEnv)
111111

@@ -313,14 +313,28 @@ hasExtension :: FilePath -> Bool
313313
hasExtension = any isExtSeparator . takeFileName
314314

315315

316-
-- | Is the given string the final extension of the filename?
317-
-- The extension should not include the separator.
316+
-- | Does the given filename have the specified extension?
318317
--
319-
-- > "png" `isExtensionOf` "/directory/file.png" == True
318+
-- The extension may exclude the separator
319+
-- > "png" `isExtensionOf` "/directory/file.png" == True
320+
--
321+
-- And it may include it
322+
-- > ".png" `isExtensionOf` "/directory/file.png" == True
323+
--
324+
-- Multiple extensions are allowed
325+
-- > ".tar.gz" `isExtensionOf` "bar/foo.tar.gz" == True
326+
--
327+
-- But partial matches are not
328+
-- > "ar.gz" `isExtensionOf` "bar/foo.tar.gz" == False
329+
--
330+
-- Extensions are matched from the end, so the following yields @False@
320331
-- > "png" `isExtensionOf` "/directory/file.png.jpg" == False
321-
-- > "csv" `isExtensionOf` "/directory/data.csv" == True
332+
333+
-- The argument cannot simply be a suffix, it has to be to a valid extension
334+
-- > "csv/table.csv" `isExtensionOf` "/data/csv/table.csv" == False
322335
isExtensionOf :: String -> FilePath -> Bool
323-
isExtensionOf ext = (== ext) . drop 1 . takeExtension
336+
isExtensionOf ext@('.':_) = isSuffixOf ext . takeExtensions
337+
isExtensionOf ext = isSuffixOf ('.':ext) . takeExtensions
324338

325339
-- | Drop the given extension from a FilePath, and the @\".\"@ preceding it.
326340
-- Returns 'Nothing' if the FilePath does not have the given extension, or

tests/TestGen.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,20 @@ tests =
106106
,("W.hasExtension \"/directory/path.ext\" == True", property $ W.hasExtension "/directory/path.ext" == True)
107107
,("P.hasExtension \"/directory/path\" == False", property $ P.hasExtension "/directory/path" == False)
108108
,("W.hasExtension \"/directory/path\" == False", property $ W.hasExtension "/directory/path" == False)
109-
,("W.isExtensionOf \"png\" \"/directory/file.png\" == True", property $ W.isExtensionOf "png" "/directory/file.png" == True)
110-
,("P.isExtensionOf \"png\" \"/directory/file.png\" == True", property $ P.isExtensionOf "png" "/directory/file.png" == True)
111-
,("W.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png" "/directory/file.png.jpg" == False)
112-
,("P.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ P.isExtensionOf "png" "/directory/file.png.jpg" == False)
113-
,("W.isExtensionOf \"csv\" \"/directory/data.csv\" == True", property $ W.isExtensionOf "csv" "/directory/data.csv" == True)
114-
,("P.isExtensionOf \"csv\" \"/directory/data.csv\" == True", property $ P.isExtensionOf "csv" "/directory/data.csv" == True)
109+
110+
,("W.isExtensionOf \"png\" \"/directory/file.png\" == True", property $ W.isExtensionOf "png" "/directory/file.png" == True)
111+
,("P.isExtensionOf \"png\" \"/directory/file.png\" == True", property $ W.isExtensionOf "png" "/directory/file.png" == True)
112+
,("W.isExtensionOf \".png\" \"/directory/file.png\" == True", property $ W.isExtensionOf ".png" "/directory/file.png" == True)
113+
,("P.isExtensionOf \".png\" \"/directory/file.png\" == True", property $ W.isExtensionOf ".png" "/directory/file.png" == True)
114+
,("W.isExtensionOf \".tar.gz\" \"bar/foo.tar.gz\" == True", property $ W.isExtensionOf ".tar.gz" "bar/foo.tar.gz" == True)
115+
,("P.isExtensionOf \".tar.gz\" \"bar/foo.tar.gz\" == True", property $ W.isExtensionOf ".tar.gz" "bar/foo.tar.gz" == True)
116+
,("W.isExtensionOf \"ar.gz\" \"bar/foo.tar.gz\" == False", property $ W.isExtensionOf "ar.gz" "bar/foo.tar.gz" == False)
117+
,("P.isExtensionOf \"ar.gz\" \"bar/foo.tar.gz\" == False", property $ W.isExtensionOf "ar.gz" "bar/foo.tar.gz" == False)
118+
,("W.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png" "/directory/file.png.jpg" == False)
119+
,("P.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png" "/directory/file.png.jpg" == False)
120+
,("W.isExtensionOf \"csv/table.csv\" \"/data/csv/table.csv\" == False", property $ W.isExtensionOf "csv/table.csv" "/data/csv/table.csv" == False)
121+
,("P.isExtensionOf \"csv/table.csv\" \"/data/csv/table.csv\" == False", property $ W.isExtensionOf "csv/table.csv" "/data/csv/table.csv" == False)
122+
115123
,("null (P.takeExtension x) == not (P.hasExtension x)", property $ \(QFilePath x) -> null (P.takeExtension x) == not (P.hasExtension x))
116124
,("null (W.takeExtension x) == not (W.hasExtension x)", property $ \(QFilePath x) -> null (W.takeExtension x) == not (W.hasExtension x))
117125
,("P.stripExtension \"hs.o\" \"foo.x.hs.o\" == Just \"foo.x\"", property $ P.stripExtension "hs.o" "foo.x.hs.o" == Just "foo.x")

0 commit comments

Comments
 (0)