Skip to content

Commit 0bbf5be

Browse files
committed
Added simple test for recursive subpackage creation
1 parent 28a4909 commit 0bbf5be

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

dhall/src/Dhall/Package.hs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import Dhall.Util (_ERROR, renderExpression)
3838
import System.Directory
3939
import System.FilePath
4040

41+
-- | Whether to recursively create a package for each subdirectory or not.
42+
-- See the documentation of 'getPackagePathAndContent'.
4143
data Recurse
4244
= Recurse
4345
| Exact

dhall/tests/Dhall/Test/Package.hs

+49-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Dhall.Core
1919
)
2020
import qualified Dhall.Map as Map
2121
import Dhall.Package
22+
import Dhall.Pretty (CharacterSet(ASCII))
2223
import System.FilePath ((</>))
2324
import Test.Tasty
2425
import Test.Tasty.HUnit
@@ -31,6 +32,7 @@ tests = testGroup "Package"
3132
, packageEmptyDirectory
3233
, packageSingleDirectory
3334
, packageNested
35+
, packageRecursive
3436
, packageMissingFile
3537
, packageFilesDifferentDirs
3638
, packageIncompatibleFiles
@@ -43,7 +45,7 @@ packagePackageFile = testCase "package file" $ do
4345
let package :: Expr Void Import
4446
package = RecordLit Map.empty
4547

46-
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/package.dhall" :| [])
48+
(output, expr) <- getPackagePathAndContent ASCII Exact Nothing ("./tests/package/package.dhall" :| [])
4749
assertEqual "path" path output
4850
assertEqual "content" package expr
4951

@@ -55,7 +57,7 @@ packageCustomPackageFile = testCase "custom package file" $ do
5557
package = RecordLit $
5658
Map.singleton "package" $ makeRecordField $ Embed packageDhall
5759

58-
(output, expr) <- getPackagePathAndContent (Just "custom.dhall") ("./tests/package/package.dhall" :| [])
60+
(output, expr) <- getPackagePathAndContent ASCII Exact (Just "custom.dhall") ("./tests/package/package.dhall" :| [])
5961
assertEqual "path" path output
6062
assertEqual "content" package expr
6163

@@ -67,7 +69,7 @@ packageSingleFile = testCase "single file" $ do
6769
package = RecordLit $
6870
Map.singleton "test" $ makeRecordField $ Embed testDhall
6971

70-
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/dir/test.dhall" :| [])
72+
(output, expr) <- getPackagePathAndContent ASCII Exact Nothing ("./tests/package/dir/test.dhall" :| [])
7173
assertEqual "path" path output
7274
assertEqual "content" package expr
7375

@@ -78,7 +80,7 @@ packageEmptyDirectory = testCase "empty directory" $ do
7880
let package :: Expr Void Import
7981
package = RecordLit Map.empty
8082

81-
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/empty" :| [])
83+
(output, expr) <- getPackagePathAndContent ASCII Exact Nothing ("./tests/package/empty" :| [])
8284
assertEqual "path" path output
8385
assertEqual "content" package expr
8486

@@ -90,7 +92,7 @@ packageSingleDirectory = testCase "single directory" $ do
9092
package = RecordLit $ Map.singleton "test" $
9193
makeRecordField $ Embed testDhall
9294

93-
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/dir" :| [])
95+
(output, expr) <- getPackagePathAndContent ASCII Exact Nothing ("./tests/package/dir" :| [])
9496
assertEqual "path" path output
9597
assertEqual "content" package expr
9698

@@ -108,18 +110,34 @@ packageNested = testCase "nested files" $ do
108110
, ("test", makeRecordField $ Embed testDhall)
109111
]
110112

111-
(output, expr) <- getPackagePathAndContent Nothing
113+
(output, expr) <- getPackagePathAndContent ASCII Exact Nothing
112114
( "./tests/package/test.dhall" :|
113115
[ "./tests/package/dir/test.dhall"
114116
, "./tests/package/other/package.dhall"
115117
])
116118
assertEqual "path" path output
117119
assertEqual "content" package expr
118120

121+
packageRecursive :: TestTree
122+
packageRecursive = testCase "recursively create subpackages" $ do
123+
let path = "./tests/package/dir" </> "package.dhall"
124+
125+
let package :: Expr Void Import
126+
package = RecordLit $ Map.fromList
127+
[ ("subdirectory1", makeRecordField $ Embed subdirectoryPackageDhall1)
128+
, ("subdirectory2", makeRecordField $ Embed subdirectoryPackageDhall2)
129+
, ("test", makeRecordField $ Embed testDhall)
130+
]
131+
132+
(output, expr) <- getPackagePathAndContent ASCII Recurse Nothing
133+
( "./tests/package/dir" :| [] )
134+
assertEqual "path" path output
135+
assertEqual "content" package expr
136+
119137
packageMissingFile :: TestTree
120138
packageMissingFile = testCase "missing file" $ do
121139
let action :: IO (FilePath, Expr Void Import)
122-
action = getPackagePathAndContent Nothing ("./tests/package/missing.dhall" :| [])
140+
action = getPackagePathAndContent ASCII Exact Nothing ("./tests/package/missing.dhall" :| [])
123141

124142
assertThrow action $ \case
125143
InvalidPath "./tests/package/missing.dhall" -> True
@@ -128,7 +146,7 @@ packageMissingFile = testCase "missing file" $ do
128146
packageFilesDifferentDirs :: TestTree
129147
packageFilesDifferentDirs = testCase "files from different directories" $ do
130148
let action :: IO (FilePath, Expr Void Import)
131-
action = getPackagePathAndContent Nothing ("./tests/package/dir/test.dhall" :| ["./tests/package/test/test.dhall"])
149+
action = getPackagePathAndContent ASCII Exact Nothing ("./tests/package/dir/test.dhall" :| ["./tests/package/test/test.dhall"])
132150

133151
assertThrow action $ \case
134152
AmbiguousOutputDirectory "./tests/package/dir" "./tests/package/test" -> True
@@ -137,7 +155,7 @@ packageFilesDifferentDirs = testCase "files from different directories" $ do
137155
packageIncompatibleFiles :: TestTree
138156
packageIncompatibleFiles = testCase "files that are incompatible" $ do
139157
let action :: IO (FilePath, Expr Void Import)
140-
action = getPackagePathAndContent Nothing ("./tests/package/test.dhall" :| ["./tests/package/test/test.dhall"])
158+
action = getPackagePathAndContent ASCII Exact Nothing ("./tests/package/test.dhall" :| ["./tests/package/test/test.dhall"])
141159

142160
assertThrow action $ \case
143161
IncompatiblePaths xs -> xs == [ testDhall , testTestDhall ]
@@ -191,6 +209,28 @@ otherPackageDhall = Import
191209
, importMode = Code
192210
}
193211

212+
subdirectoryPackageDhall1 :: Import
213+
subdirectoryPackageDhall1 = Import
214+
{ importHashed = ImportHashed
215+
{ hash = Nothing
216+
, importType = Local Here $ File
217+
{ directory = Directory {components = ["subdirectory1"]}
218+
, file = "package.dhall"}
219+
}
220+
, importMode = Code
221+
}
222+
223+
subdirectoryPackageDhall2 :: Import
224+
subdirectoryPackageDhall2 = Import
225+
{ importHashed = ImportHashed
226+
{ hash = Nothing
227+
, importType = Local Here $ File
228+
{ directory = Directory {components = ["subdirectory2"]}
229+
, file = "package.dhall"}
230+
}
231+
, importMode = Code
232+
}
233+
194234
testTestDhall :: Import
195235
testTestDhall = Import
196236
{ importHashed = ImportHashed

dhall/tests/package/dir/subdirectory1/test.dhall

Whitespace-only changes.

dhall/tests/package/dir/subdirectory2/wrong-extension.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)