Skip to content

Commit 86ae15b

Browse files
authored
Merge pull request #26 from sol/main
Add test command
2 parents cf78a94 + a8eb8c3 commit 86ae15b

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/MicroCabal/Backend/GHC.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ setupStdArgs env flds = do
101101
binGhc :: FilePath
102102
binGhc = "bin" </> "ghc"
103103

104-
ghcBuildExe :: Env -> Section -> Section -> IO ()
104+
ghcBuildExe :: Env -> Section -> Section -> IO FilePath
105105
ghcBuildExe env _ (Section _ name flds) = do
106106
initDB env
107107
let mainIs = getFieldString flds "main-is"
@@ -114,6 +114,7 @@ ghcBuildExe env _ (Section _ name flds) = do
114114
[ ">/dev/null" | verbose env <= 0 ]
115115
message env 0 $ "Building executable " ++ bin ++ " with ghc"
116116
ghc env args
117+
return bin
117118

118119
findMainIs :: Env -> [FilePath] -> FilePath -> IO FilePath
119120
findMainIs _ [] fn = error $ "cannot find " ++ show fn

src/MicroCabal/Backend/MHS.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ setupStdArgs env flds = do
118118
binMhs :: String
119119
binMhs = "bin" </> "mhs"
120120

121-
mhsBuildExe :: Env -> Section -> Section -> IO ()
121+
mhsBuildExe :: Env -> Section -> Section -> IO FilePath
122122
mhsBuildExe env (Section _ _ gflds) (Section _ name flds) = do
123123
initDB env
124124
let mainIs = getFieldString flds "main-is"
@@ -135,6 +135,7 @@ mhsBuildExe env (Section _ _ gflds) (Section _ name flds) = do
135135
["-z", "-a.","-o" ++ bin, mainIs']
136136
message env 0 $ "Build " ++ bin ++ " with mhs"
137137
mhs env args
138+
return bin
138139

139140
mhs :: Env -> String -> IO ()
140141
mhs env args = do

src/MicroCabal/Env.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ data Env = Env {
2525
subDir :: Maybe String -- subdirectory of git repo
2626
}
2727

28-
data Target = TgtLib | TgtFor | TgtExe
28+
data Target = TgtLib | TgtFor | TgtExe | TgtTst
2929
deriving (Eq)
3030

3131
data Backend = Backend {
@@ -36,7 +36,7 @@ data Backend = Backend {
3636
doesPkgExist :: Env -> PackageName -> IO Bool, -- is the package available in the database?
3737
patchDepends :: Env -> Cabal -> Cabal, -- patch dependencies
3838
patchName :: Env -> (Name, Version) -> (Name, Version), -- patch package name
39-
buildPkgExe :: Env -> Section -> Section -> IO (), -- build executable the current directory
39+
buildPkgExe :: Env -> Section -> Section -> IO FilePath, -- build executable the current directory
4040
buildPkgLib :: Env -> Section -> Section -> IO (), -- build the package in the current directory
4141
buildPkgForLib :: Env -> Section -> Section -> IO (), -- build the foreign-library in the current directory
4242
installPkgExe :: Env -> Section -> Section -> IO (), -- install the package from the current directory

src/MicroCabal/Main.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ main = do
3030
[] -> usage
3131
["--version"] -> putStrLn version
3232
"build" : as -> decodeGit cmdBuild env as
33+
"test" : as -> cmdTest env as
3334
"clean" : as -> cmdClean env as
3435
"fetch" : as -> decodeGit cmdFetch env as
3536
"help" : as -> cmdHelp env as
@@ -257,6 +258,10 @@ cmdBuild env [apkg] = do
257258
cmdBuild env []
258259
cmdBuild _ _ = usage
259260

261+
cmdTest :: Env -> [String] -> IO ()
262+
cmdTest env [] = build env { targets = TgtTst : targets env }
263+
cmdTest _ _ = usage
264+
260265
getGlobal :: Cabal -> Section
261266
getGlobal (Cabal sects) =
262267
fromMaybe (error "no global section") $ listToMaybe [ s | s@(Section "global" _ _) <- sects ]
@@ -296,19 +301,22 @@ build env = do
296301
sectLib s@(Section "library" _ _) | TgtLib `elem` targets env && isBuildable s = buildLib env glob s
297302
sectLib s@(Section "foreign-library" _ _) | TgtFor `elem` targets env && isBuildable s = buildForeignLib env glob s
298303
sectLib _ = return Nothing
299-
sectExe ll s@(Section "executable" _ _) | TgtExe `elem` targets env && isBuildable s = buildExe env glob s ll
304+
sectExe ll s@(Section "executable" _ _) | TgtExe `elem` targets env && isBuildable s = void $ buildExe env glob s ll
300305
sectExe _ _ = return ()
306+
sectTst ll s@(Section "test-suite" _ _) | TgtTst `elem` targets env && isBuildable s = buildExe env glob s ll >>= cmd env
307+
sectTst _ _ = return ()
301308
sects' = addMissing sects
302309
message env 3 $ "Unnormalized Cabal file:\n" ++ show cbl
303310
message env 2 $ "Normalized Cabal file:\n" ++ show ncbl
304311
-- Build libs first, then exes
305-
localLibs <- mapM sectLib sects'
306-
mapM_ (sectExe $ catMaybes localLibs) sects'
312+
localLibs <- catMaybes <$> mapM sectLib sects'
313+
mapM_ (sectExe localLibs) sects'
314+
mapM_ (sectTst localLibs) sects'
307315

308316
isBuildable :: Section -> Bool
309317
isBuildable (Section _ _ flds) = getFieldBool True flds "buildable"
310318

311-
buildExe :: Env -> Section -> Section -> [Name] -> IO ()
319+
buildExe :: Env -> Section -> Section -> [Name] -> IO FilePath
312320
buildExe env glob@(Section _ _ sglob) sect@(Section _ name flds) localLibs = do
313321
message env 0 $ "Building executable " ++ name
314322
createPathFile env glob sect
@@ -469,6 +477,7 @@ cmdHelp :: Env -> [String] -> IO ()
469477
cmdHelp _ _ = putStrLn "\
470478
\Available commands:\n\
471479
\ mcabal [FLAGS] build [--git=URL [--dir=DIR]] [PKG] build in current directory, or the package PKG\n\
480+
\ mcabal [FLAGS] test build and run tests in current directory\n\
472481
\ mcabal [FLAGS] clean clean in the current directory\n\
473482
\ mcabal [FLAGS] fetch [--git=URL [--dir=DIR]] PKG fetch files for package PKG\n\
474483
\ mcabal [FLAGS] help show this message\n\

0 commit comments

Comments
 (0)