From 51eb4aae4ece7732e5250dc6ed344e85cf29aaa8 Mon Sep 17 00:00:00 2001
From: Florian Atteneder <florian.atteneder@uni-jena.de>
Date: Tue, 6 Aug 2024 09:12:21 +0000
Subject: [PATCH] dg1d: make `main` accept paths to output directories
 (https://git.tpi.uni-jena.de/dg/dg1d.jl/-/merge_requests/196)

---
 src/main.jl                      | 19 +++++++++++++------
 src/utils.jl                     |  2 --
 test/UnitTests/src/test_utils.jl |  3 ++-
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/main.jl b/src/main.jl
index e8bc29dc..844d2eb2 100644
--- a/src/main.jl
+++ b/src/main.jl
@@ -19,16 +19,22 @@ function testparfiles()
 end
 
 
-function normalize_parfile(filename)
+function normalize_parfile(path::AbstractString)
+  filename = if isdir(path)
+    name = basename(path)
+    fname = joinpath(path, string(name, ".toml"))
+  else
+    path
+  end
   if !isfile(filename)
-    error("Cannot locate parameter file '$filename'")
+    error("Cannot locate a parameter file at $path")
   end
   return filename
 end
 function normalize_parfile(nr::Integer)
   testpars = get_testparfiles()
   if !(1 <= nr <= length(testpars))
-    error("there is no test parfile with nr $nr")
+    error("There is no test parfile with nr $nr, see testparfiles() for available parfiles")
   end
   parfile = testpars[nr]
   path = normpath(joinpath(@__DIR__, "..", "test", "IntegrationTests", "refs", parfile, "$parfile.toml"))
@@ -41,7 +47,7 @@ end
 
 Run program specified by a `parfile` in TOML format.
 """
-function main(parfile, parameter_overrides::Pair{String,String}...)
+function the_main(parfile::String, parameter_overrides::Pair{String,String}...)
 
   parfile = abspath(parfile)
   prms = TOML.parsefile(parfile)
@@ -107,8 +113,9 @@ function main(parfile, parameter_overrides::Pair{String,String}...)
 
   return
 end
-main(id::Integer, parameter_overrides::Pair{String,String}...) =
-  main(normalize_parfile(id); parameter_overrides)
+
+main(p::Union{Integer,AbstractString}, parameter_overrides::Pair{String,String}...) =
+  the_main(normalize_parfile(p), parameter_overrides...)
 
 load_parameters(parfile::AbstractString) = parse_parameters(TOML.parsefile(parfile))
 
diff --git a/src/utils.jl b/src/utils.jl
index b1acec2f..0e9a846c 100644
--- a/src/utils.jl
+++ b/src/utils.jl
@@ -199,8 +199,6 @@ function make_outputdir(path)
 
   lastext == "toml" || error("Only accept parameter files in .toml format, received '$path'")
 
-  isfile(path) || error("Parameter file '$path' not found")
-
   outputdirname = basename
   if length(firstexts) > 0
     outputdirname = "$basename.$(join(firstexts, '.'))"
diff --git a/test/UnitTests/src/test_utils.jl b/test/UnitTests/src/test_utils.jl
index 15a18fe4..f60b9490 100644
--- a/test/UnitTests/src/test_utils.jl
+++ b/test/UnitTests/src/test_utils.jl
@@ -347,7 +347,8 @@ end
     end
 
     nofilethere = joinpath(dir, "nofilethere.toml")
-    @test_throws ErrorException dg1d.make_outputdir(nofilethere)
+    dg1d.make_outputdir(nofilethere)
+    @test isdir(first(splitext(nofilethere)))
 
     wrongextensionfile = joinpath(dir, "wrongextension.toml.blabla")
     touch(wrongextensionfile)
-- 
GitLab