Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit be8dd1f

Browse files
committed
Add parser tests for jenga config file
1 parent 39d6ea8 commit be8dd1f

File tree

13 files changed

+173
-9
lines changed

13 files changed

+173
-9
lines changed

main/jenga.hs

+25-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ main =
4343
data Command
4444
= Initialize JengaConfig
4545
| Update
46-
| Parse StackFilePath
46+
| ParseStack StackFilePath
47+
| ParseJenga FilePath
4748

4849

4950
pCommand :: Parser Command
@@ -62,10 +63,14 @@ pCommand = O.subparser $ mconcat
6263
<> "or other things) and want to build is the new updated project. This sub command"
6364
<> "expects a '.jenga' file in the same directory as the 'stack.yaml' file.")
6465
(pure Update)
65-
, subCommand "parse"
66-
( "Parse the 'stack.yaml' file and dump out the relevant bits as JSON. This is "
66+
, subCommand "parse-stack"
67+
( "Parse a 'stack.yaml' file and dump out the relevant bits as JSON. This is "
6768
<> "mostly useful for testing and debugging.")
68-
(Parse <$> stackFilePathP)
69+
(ParseStack <$> stackFilePathP)
70+
, subCommand "parse-jenga"
71+
( "Parse the '.jenga' file and dump out the relevant bits as JSON. This is "
72+
<> "mostly useful for testing and debugging.")
73+
(ParseJenga <$> jengaFilePathP)
6974
]
7075
where
7176
subCommand :: String -> Doc -> Parser a -> Mod CommandFields a
@@ -110,6 +115,15 @@ stackFilePathP =
110115
<> help "The 'stack.yaml' file. (defaults to './stack.yaml')."
111116
)
112117

118+
jengaFilePathP :: Parser FilePath
119+
jengaFilePathP =
120+
fmap (fromMaybe ".jenga") <$> O.optional $ strOption
121+
( short 'j'
122+
<> long "jenga-file"
123+
<> metavar "JENGA_FILE"
124+
<> help "The '.jenga' file. (defaults to './.jenga')."
125+
)
126+
113127
-- -----------------------------------------------------------------------------
114128

115129
commandHandler :: Command -> IO ()
@@ -118,7 +132,8 @@ commandHandler cmd =
118132
case cmd of
119133
Initialize cfg -> initialize cfg
120134
Update -> update
121-
Parse scfg -> dumpStackToJSON scfg
135+
ParseStack scfg -> dumpStackToJSON scfg
136+
ParseJenga jcfg -> dumpJengaToJSON jcfg
122137

123138
initialize :: JengaConfig -> EitherT JengaError IO ()
124139
initialize jcfg = do
@@ -165,6 +180,11 @@ dumpStackToJSON stackFile = do
165180
scfg <- readStackConfig stackFile
166181
liftIO . BS.putStrLn $ renderStackConfig scfg
167182

183+
dumpJengaToJSON :: FilePath -> EitherT JengaError IO ()
184+
dumpJengaToJSON file = do
185+
jcfg <- readJengaConfigFrom file
186+
liftIO . BS.putStrLn $ renderJengaConfig jcfg
187+
168188
-- -----------------------------------------------------------------------------
169189

170190
checkModulesDirPath :: ModulesDirPath -> EitherT JengaError IO ()

src/Jenga/Config.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import qualified Data.ByteString.Lazy as LBS
2222
import qualified Data.List as List
2323
import Data.Text (Text)
2424
import qualified Data.Text as Text
25-
import Data.YAML (FromYAML(..), Node (..), Parser, Scalar (..), (.:))
25+
import Data.YAML (FromYAML(..), Node (..), Parser, Scalar (..), (.:), (.:?))
2626
import qualified Data.YAML as Yaml
2727

2828
import Jenga.Stack
@@ -57,7 +57,7 @@ instance FromYAML JengaConfig where
5757
<$> o .: "submodule-dir"
5858
<*> ((o .: "mafia-lock") >>= toLockFormat)
5959
<*> ((o .: "drop-deps") >>= parseDropDeps)
60-
<*> ((o .: "submodules") >>= parseSubmodules)
60+
<*> (maybe (pure []) parseSubmodules =<< (o .:? "submodules"))
6161

6262
instance FromYAML ModulesDirPath where
6363
parseYAML = Yaml.withStr "ModulesDirPath" (pure . ModulesDirPath . Text.unpack)
@@ -120,7 +120,7 @@ readJengaConfig :: EitherT JengaError IO JengaConfig
120120
readJengaConfig =
121121
readJengaConfigFrom configFilePath
122122

123-
-- Mainly for debugging.
123+
-- Mainly for debugging and testing.
124124
readJengaConfigFrom :: FilePath -> EitherT JengaError IO JengaConfig
125125
readJengaConfigFrom path = do
126126
bs <- handleIOEitherT handler $ BS.readFile path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
submodule-dir: submods
2+
mafia-lock: true
3+
drop-deps: [ directory ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
submodule-dir: lib
2+
mafia-lock: false
3+
drop-deps: [ ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"submodule-dir": "submods",
3+
"mafia-lock": true,
4+
"submodules": [],
5+
"drop-deps": [
6+
"directory"
7+
]
8+
}
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"submodule-dir": "lib",
3+
"mafia-lock": false,
4+
"submodules": [],
5+
"drop-deps": []
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"packages": [
3+
"."
4+
],
5+
"extra-deps": [],
6+
"resolver": "lts-9.0"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"packages": [
3+
"first",
4+
"second",
5+
{
6+
"location": {
7+
"git": "https://github.com/well-typed/cborg",
8+
"commit": "c7db82bfd93923f5b08ed51a4cd53e30bd445924"
9+
},
10+
"subdirs": [
11+
"cborg"
12+
],
13+
"extra-dep": true
14+
},
15+
{
16+
"location": {
17+
"git": "https://github.com/thoughtpolice/hs-ed25519",
18+
"commit": "da4247b5b3420120e20451e6a252e2a2ca15b43c"
19+
},
20+
"subdirs": [],
21+
"extra-dep": true
22+
}
23+
],
24+
"extra-deps": [
25+
"transformers-0.5.5.0",
26+
"concurrent-extra-0.7.0.10"
27+
],
28+
"resolver": "lts-9.1"
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"packages": [],
3+
"extra-deps": [],
4+
"resolver": "lts-9.0"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"packages": [],
3+
"extra-deps": [
4+
"transformers-0.5.5.0",
5+
"concurrent-extra-0.7.0.10",
6+
{
7+
"git": "https://github.com/primetype/inspector",
8+
"commit": "674c5d3a4733088f14164b0924824b7433e06428"
9+
}
10+
],
11+
"resolver": "lts-9.0"
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"packages": [
3+
".",
4+
{
5+
"location": {
6+
"git": "https://github.com/well-typed/cborg",
7+
"commit": "c7db82bfd93923f5b08ed51a4cd53e30bd445924"
8+
},
9+
"subdirs": [
10+
"cborg"
11+
],
12+
"extra-dep": true
13+
},
14+
{
15+
"location": {
16+
"git": "https://github.com/input-output-hk/engine.io.git",
17+
"commit": "d3c55f51bb81cee7d0d551de930ce65fe7d76756"
18+
},
19+
"subdirs": [
20+
"socket-io",
21+
"engine-io",
22+
"engine-io-wai"
23+
],
24+
"extra-dep": true
25+
}
26+
],
27+
"extra-deps": [],
28+
"resolver": "lts-11.13"
29+
}

test/cli/parse-jenga-config/run

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh -eu
2+
3+
. $(dirname $0)/../core/runner
4+
5+
PARSER="${JENGA} parse-jenga --jenga-file"
6+
7+
testname="parse-jenga-config"
8+
9+
banner "${testname}"
10+
#----------
11+
12+
INPUT_DIR=$(dirname $0)/data
13+
OUTPUT_DIR=${TEST}
14+
EXPECTED_DIR=$(dirname $0)/expected
15+
16+
mkdir -p ${OUTPUT_DIR}
17+
18+
error_count=0
19+
20+
for input in ${INPUT_DIR}/* ; do
21+
outname=$(basename $input | sed 's/\.yaml$/\.json/')
22+
output=${OUTPUT_DIR}/${outname}
23+
expected=${EXPECTED_DIR}/${outname}
24+
${PARSER} ${input} > ${output}
25+
if ! test -f ${expected} ; then
26+
cp ${output} ${expected}
27+
fi
28+
error=0
29+
diff -q ${expected} ${output} || error=1
30+
if test "${error}" = "1"; then
31+
echo "Error in file:" ${input}
32+
${DIFF} ${expected} ${output}
33+
fi
34+
done
35+
36+
if test "${error_count}" = "0"; then
37+
pass_test
38+
else
39+
fail_test
40+
fi

test/cli/parse-stack-config/run

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
. $(dirname $0)/../core/runner
44

5-
PARSER="${JENGA} parse --stack-file"
5+
PARSER="${JENGA} parse-stack --stack-file"
66

77
testname="parse-stack-config"
88

0 commit comments

Comments
 (0)