Skip to content

Commit 9e0e11f

Browse files
authored
to-directory-tree: Create parents of files when using fixpoint directory tree representation (#2653)
* to-directory-tree: Fixed --allow-path-separators for fixpointed files Before, when one uses `to-directory-tree --allow-path-separators` to create a file using the fixpoint representation of a filesystem tree, then that failed if a path with path separators was passed to the `file` or `binary-file` constructors. The reason was that we did not create the parent directories, which is intended by the use of --allow-path-separators. This commit fixes that. * Added tests * Remove generated package.dhall after test for recursive package generation
1 parent e08b1a0 commit 9e0e11f

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

dhall/src/Dhall/DirectoryTree.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,16 @@ processFilesystemEntry allowSeparators path (DirectoryEntry entry) =
296296
processEntryWith path entry $ \path' content -> do
297297
Directory.createDirectoryIfMissing allowSeparators path'
298298
processFilesystemEntryList allowSeparators path' content
299-
processFilesystemEntry _ path (BinaryFileEntry entry) =
300-
processEntryWith path entry ByteString.writeFile
301-
processFilesystemEntry _ path (TextFileEntry entry) =
302-
processEntryWith path entry Text.IO.writeFile
299+
processFilesystemEntry allowSeparators path (BinaryFileEntry entry) =
300+
processEntryWith path entry $ \path' content -> do
301+
when allowSeparators $ do
302+
Directory.createDirectoryIfMissing True (FilePath.takeDirectory path')
303+
ByteString.writeFile path' content
304+
processFilesystemEntry allowSeparators path (TextFileEntry entry) =
305+
processEntryWith path entry $ \path' content -> do
306+
when allowSeparators $ do
307+
Directory.createDirectoryIfMissing True (FilePath.takeDirectory path')
308+
Text.IO.writeFile path' content
303309

304310
-- | A helper function used by 'processFilesystemEntry'.
305311
processEntryWith

dhall/tests/Dhall/Test/DirectoryTree.hs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import System.FilePath ((</>))
1313
import Test.Tasty
1414
import Test.Tasty.HUnit
1515

16+
import qualified Data.List
1617
import qualified Data.Text.IO
1718
import qualified Dhall
1819
import qualified Dhall.Core
@@ -26,6 +27,7 @@ tests = testGroup "to-directory-tree"
2627
[ fixpointedType
2728
, fixpointedEmpty
2829
, fixpointedSimple
30+
, fixpointedAllowPathSeparators
2931
#ifndef mingw32_HOST_OS
3032
, fixpointedPermissions
3133
, fixpointedUserGroup
@@ -54,12 +56,25 @@ fixpointedSimple = testCase "simple" $ do
5456
let outDir = "./tests/to-directory-tree/fixpoint-simple.out"
5557
path = "./tests/to-directory-tree/fixpoint-simple.dhall"
5658
entries <- runDirectoryTree False outDir path
57-
entries @?=
59+
entries @?= Data.List.sort
5860
[ Directory outDir
5961
, File $ outDir </> "file"
6062
, Directory $ outDir </> "directory"
6163
]
6264

65+
fixpointedAllowPathSeparators :: TestTree
66+
fixpointedAllowPathSeparators = testCase "allow-path-separators" $ do
67+
let outDir = "./tests/to-directory-tree/fixpoint-allow-path-separators.out"
68+
path = "./tests/to-directory-tree/fixpoint-allow-path-separators.dhall"
69+
entries <- runDirectoryTree True outDir path
70+
entries @?= Data.List.sort
71+
[ Directory outDir
72+
, Directory $ outDir </> "non-existent-1"
73+
, File $ outDir </> "non-existent-1" </> "file"
74+
, Directory $ outDir </> "non-existent-2"
75+
, Directory $ outDir </> "non-existent-2" </> "directory"
76+
]
77+
6378
{-
6479
This test is disabled on Windows as it fails due to limitations of the :
6580
expected: 448
@@ -116,12 +131,12 @@ runDirectoryTree allowSeparators outDir path = do
116131

117132
toDirectoryTree allowSeparators outDir $ Dhall.Core.denote expr
118133

119-
walkFsTree outDir
134+
Data.List.sort <$> walkFsTree outDir
120135

121136
data WalkEntry
122137
= Directory FilePath
123138
| File FilePath
124-
deriving (Eq, Show)
139+
deriving (Eq, Ord, Show)
125140

126141
walkFsTree :: FilePath -> IO [WalkEntry]
127142
walkFsTree dir = do

dhall/tests/Dhall/Test/Package.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Dhall.Test.Package where
55

6-
import Control.Exception (Exception, displayException, try)
6+
import Control.Exception (Exception, displayException, finally, try)
77
import Data.List.NonEmpty (NonEmpty (..))
88
import Data.Void (Void)
99
import Dhall.Core
@@ -20,6 +20,7 @@ import Dhall.Core
2020
import qualified Dhall.Map as Map
2121
import Dhall.Package
2222
import Lens.Micro (set)
23+
import qualified System.Directory as Directory
2324
import System.FilePath ((</>))
2425
import Test.Tasty
2526
import Test.Tasty.HUnit
@@ -122,7 +123,7 @@ packageNested = testCase "nested files" $ do
122123
assertEqual "content" package expr
123124

124125
packageRecursive :: TestTree
125-
packageRecursive = testCase "recursively create subpackages" $ do
126+
packageRecursive = testCase "recursively create subpackages" $ removePackagesAfter $ do
126127
let path = "./tests/package/dir" </> "package.dhall"
127128

128129
let package :: Expr Void Import
@@ -139,6 +140,11 @@ packageRecursive = testCase "recursively create subpackages" $ do
139140
( "./tests/package/dir" :| [] )
140141
assertEqual "path" path output
141142
assertEqual "content" package expr
143+
where
144+
removePackagesAfter :: IO a -> IO a
145+
removePackagesAfter action = action `finally` do
146+
Directory.removePathForcibly "./tests/package/dir/subdirectory1/package.dhall"
147+
Directory.removePathForcibly "./tests/package/dir/subdirectory2/package.dhall"
142148

143149
packageMissingFile :: TestTree
144150
packageMissingFile = testCase "missing file" $ do
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let User = (./fixpoint-helper.dhall).User
2+
3+
let Group = (./fixpoint-helper.dhall).Group
4+
5+
let Mode = (./fixpoint-helper.dhall).Mode
6+
7+
let Make = (./fixpoint-helper.dhall).Make
8+
9+
in \(r : Type) ->
10+
\(make : Make r) ->
11+
[ make.file
12+
{ name = "non-existent-1/file"
13+
, content = ""
14+
, user = None User
15+
, group = None Group
16+
, mode = None Mode
17+
}
18+
, make.directory
19+
{ name = "non-existent-2/directory"
20+
, content = [] : List r
21+
, user = None User
22+
, group = None Group
23+
, mode = None Mode
24+
}
25+
]

0 commit comments

Comments
 (0)