Skip to content

Commit 69a430f

Browse files
author
Brad Richardson
committed
Enable tests with fpm
1 parent 038ac85 commit 69a430f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

app/Main.hs

+48-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ data TomlSettings = TomlSettings {
4141
, tomlSettingsProjectName :: String
4242
, tomlSettingsLibrary :: (Maybe Library)
4343
, tomlSettingsExecutables :: [Executable]
44+
, tomlSettingsTests :: [Executable]
4445
}
4546

4647
data AppSettings = AppSettings {
@@ -50,6 +51,7 @@ data AppSettings = AppSettings {
5051
, appSettingsFlags :: [String]
5152
, appSettingsLibrary :: (Maybe Library)
5253
, appSettingsExecutables :: [Executable]
54+
, appSettingsTests :: [Executable]
5355
}
5456

5557
data Library = Library { librarySourceDir :: String }
@@ -87,7 +89,17 @@ app args settings = case command' args of
8789
(appSettingsExecutables settings)
8890
let executables = map (buildPrefix </>) executableNames
8991
mapM_ runCommand executables
90-
Test -> putStrLn "Test"
92+
Test -> do
93+
build settings
94+
let buildPrefix = appSettingsBuildPrefix settings
95+
let
96+
executableNames = map
97+
(\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } ->
98+
sourceDir </> name
99+
)
100+
(appSettingsTests settings)
101+
let executables = map (buildPrefix </>) executableNames
102+
mapM_ runCommand executables
91103

92104
build :: AppSettings -> IO ()
93105
build settings = do
@@ -96,6 +108,7 @@ build settings = do
96108
let buildPrefix = appSettingsBuildPrefix settings
97109
let flags = appSettingsFlags settings
98110
let executables = appSettingsExecutables settings
111+
let tests = appSettingsTests settings
99112
executableDepends <- case appSettingsLibrary settings of
100113
Just librarySettings -> do
101114
let librarySourceDir' = librarySourceDir librarySettings
@@ -122,6 +135,19 @@ build settings = do
122135
mainFile
123136
)
124137
executables
138+
mapM_
139+
(\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } ->
140+
do
141+
buildProgram sourceDir
142+
executableDepends
143+
[".f90", ".f", ".F", ".F90", ".f95", ".f03"]
144+
(buildPrefix </> sourceDir)
145+
compiler
146+
flags
147+
name
148+
mainFile
149+
)
150+
tests
125151

126152
getArguments :: IO Arguments
127153
getArguments = execParser
@@ -169,6 +195,8 @@ settingsCodec =
169195
.= tomlSettingsLibrary
170196
<*> Toml.list executableCodec "executable"
171197
.= tomlSettingsExecutables
198+
<*> Toml.list executableCodec "test"
199+
.= tomlSettingsTests
172200

173201
libraryCodec :: TomlCodec Library
174202
libraryCodec = Library <$> Toml.string "source-dir" .= librarySourceDir
@@ -190,6 +218,7 @@ toml2AppSettings tomlSettings release = do
190218
executableSettings <- getExecutableSettings
191219
(tomlSettingsExecutables tomlSettings)
192220
projectName
221+
testSettings <- getTestSettings $ tomlSettingsTests tomlSettings
193222
return AppSettings
194223
{ appSettingsCompiler = tomlSettingsCompiler tomlSettings
195224
, appSettingsProjectName = projectName
@@ -222,6 +251,7 @@ toml2AppSettings tomlSettings release = do
222251
]
223252
, appSettingsLibrary = librarySettings
224253
, appSettingsExecutables = executableSettings
254+
, appSettingsTests = testSettings
225255
}
226256

227257
getLibrarySettings :: Maybe Library -> IO (Maybe Library)
@@ -249,3 +279,20 @@ getExecutableSettings [] projectName = do
249279
else return []
250280
else return []
251281
getExecutableSettings executables _ = return executables
282+
283+
getTestSettings :: [Executable] -> IO [Executable]
284+
getTestSettings [] = do
285+
defaultDirectoryExists <- doesDirectoryExist "test"
286+
if defaultDirectoryExists
287+
then do
288+
defaultMainExists <- doesFileExist ("test" </> "main.f90")
289+
if defaultMainExists
290+
then return
291+
[ Executable { executableSourceDir = "test"
292+
, executableMainFile = "main.f90"
293+
, executableName = "runTests"
294+
}
295+
]
296+
else return []
297+
else return []
298+
getTestSettings tests = return tests

example_project/test/main.f90

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
program runTests
2+
print *, "Running Tests"
3+
end program runTests

0 commit comments

Comments
 (0)