hslua-module-path 1.0.2 → 1.0.3
raw patch · 4 files changed
+74/−62 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +9/−0
- hslua-module-path.cabal +3/−3
- src/HsLua/Module/Path.hs +10/−35
- test/test-path.lua +52/−24
CHANGELOG.md view
@@ -2,6 +2,15 @@ `hslua-module-paths` uses [PVP Versioning][]. +## hslua-module-path-1.0.3++Released 2022-08-19.++- Fixed `make_relative` for longer base paths: Ensure that the+ function produces correct results in cases where the root+ (base) path has more components than the path that should be+ made relative.+ ## hslua-module-path-1.0.2 Released 2022-02-19.
hslua-module-path.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: hslua-module-path-version: 1.0.2+version: 1.0.3 synopsis: Lua module to work with file paths. description: Lua module to work with file paths in a platform independent way.@@ -22,8 +22,8 @@ , GHC == 8.6.5 , GHC == 8.8.4 , GHC == 8.10.7- , GHC == 9.0.1- , GHC == 9.2.1+ , GHC == 9.0.2+ , GHC == 9.2.3 source-repository head type: git
src/HsLua/Module/Path.hs view
@@ -1,5 +1,4 @@ {-# LANGUAGE CPP #-}-{-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} {-| Module : HsLua.Module.Path@@ -34,7 +33,6 @@ ) where -import Data.Char (toLower) #if !MIN_VERSION_base(4,11,0) import Data.Semigroup (Semigroup(..)) -- includes (<>) #endif@@ -338,40 +336,17 @@ -> FilePath -- ^ root directory from which to start -> Maybe Bool -- ^ whether to use unsafe relative paths. -> FilePath-makeRelative path root unsafe+makeRelative path root (Just True) | Path.equalFilePath root path = "."- | takeAbs root /= takeAbs path = path- | otherwise = go (dropAbs path) (dropAbs root)- where- go x "" = dropWhile Path.isPathSeparator x- go x y =- let (x1, x2) = breakPath x- (y1, y2) = breakPath y- in case () of- _ | Path.equalFilePath x1 y1 -> go x2 y2- _ | unsafe == Just True -> Path.joinPath ["..", x1, go x2 y2]- _ -> path-- breakPath = both (dropWhile Path.isPathSeparator)- . break Path.isPathSeparator- . dropWhile Path.isPathSeparator-- both f (a, b) = (f a, f b)-- leadingPathSepOnWindows = \case- "" -> False- x | Path.hasDrive x -> False- c:_ -> Path.isPathSeparator c-- dropAbs x = if leadingPathSepOnWindows x then tail x else Path.dropDrive x-- takeAbs x = if leadingPathSepOnWindows x- then [Path.pathSeparator]- else map (\y ->- if Path.isPathSeparator y- then Path.pathSeparator- else toLower y)- (Path.takeDrive x)+ | Path.takeDrive root /= Path.takeDrive path = path+ | otherwise =+ let toParts = Path.splitDirectories . Path.normalise+ go (pp:pps) (rp:rps)+ | pp == rp = go pps rps+ go pps rps+ = Path.joinPath $ replicate (length rps) ".." ++ pps+ in go (toParts path) (toParts root)+makeRelative path root _unsafe = Path.makeRelative root path -- | First published version of this library. initialVersion :: Version
test/test-path.lua view
@@ -145,29 +145,57 @@ }, group 'make_relative' {- test('just the filename if file is within path', function()- assert.are_equal(- path.make_relative('/foo/bar/file.txt', '/foo/bar'),- 'file.txt'- )- end),- test('no change if name outside of reference dir', function()- assert.are_equal(- path.make_relative('/foo/baz/file.txt', '/foo/bar'),- '/foo/baz/file.txt'- )- end),- test('use `..` when allowing unsafe operation', function()- assert.are_equal(- path.make_relative('/foo/baz/file.txt', '/foo/bar', true),- path.join{'..', 'baz', 'file.txt'}- )- end),- test('return dot if both paths are the same', function()- assert.are_equal(- path.make_relative('/one/two/three', '/one/two/three/'),- '.'- )- end)+ group 'safe' {+ test('just the filename if file is within path', function()+ assert.are_equal(+ path.make_relative('/foo/bar/file.txt', '/foo/bar'),+ 'file.txt'+ )+ end),+ test('no change if name outside of reference dir', function()+ assert.are_equal(+ path.make_relative('/foo/baz/file.txt', '/foo/bar'),+ '/foo/baz/file.txt'+ )+ end),+ test('return dot if both paths are the same', function()+ assert.are_equal(+ path.make_relative('/one/two/three', '/one/two/three/'),+ '.'+ )+ end),+ },+ group 'unsafe' {+ test('just the filename if file is within path', function()+ assert.are_equal(+ path.make_relative('/foo/bar/file.txt', '/foo/bar', true),+ 'file.txt'+ )+ end),+ test('use `..` to reach parent directory', function()+ assert.are_equal(+ path.make_relative('/foo/baz/file.txt', '/foo/bar', true),+ path.join{'..', 'baz', 'file.txt'}+ )+ end),+ test('no change if base differs', function()+ assert.are_equal(+ path.make_relative('foo/baz/file.txt', '/foo/bar', true),+ 'foo/baz/file.txt'+ )+ end),+ test('long base path ', function()+ assert.are_equal(+ path.make_relative('a/d.png', 'a/b/c', true),+ path.join{'..', '..', 'd.png'}+ )+ end),+ test('return dot if both paths are the same', function()+ assert.are_equal(+ path.make_relative('/one/two/three', '/one/two/three/', true),+ '.'+ )+ end)+ } }, }