diff --git a/core/src/main.jl b/core/src/main.jl index b9d00e6b6..d41b9112a 100644 --- a/core/src/main.jl +++ b/core/src/main.jl @@ -1,10 +1,10 @@ -function help(x)::Cint +function help(x::AbstractString)::Cint println(x) println("Usage: ribasim path/to/model/ribasim.toml") return 1 end -function main(ARGS)::Cint +function main(ARGS::Vector{AbstractString})::Cint n = length(ARGS) if n != 1 return help("Exactly 1 argument expected, got $n") @@ -25,21 +25,21 @@ function main(ARGS)::Cint # show progress bar in terminal config = Config(arg) open(results_path(config, "ribasim.log"), "w") do io - logger = Ribasim.setup_logger(; verbosity = config.verbosity, stream = io) - model = with_logger(logger) do - Ribasim.run(config) + logger = + Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io) + with_logger(logger) do + model = Ribasim.run(config) + if successful_retcode(model) + @info "The model finished successfully" + return 0 + end + + t = Ribasim.datetime_since(model.integrator.t, model.config.starttime) + retcode = model.integrator.sol.retcode + @error "The model exited at model time $t with return code $retcode.\nSee https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes" + return 1 end end - return if successful_retcode(model) - println("The model finished successfully") - 0 - else - t = Ribasim.datetime_since(model.integrator.t, model.config.starttime) - retcode = model.integrator.sol.retcode - println("The model exited at model time $t with return code $retcode") - println("See https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes") - 1 - end catch Base.invokelatest(Base.display_error, current_exceptions()) return 1 diff --git a/core/test/cli_test.jl b/core/test/cli_test.jl deleted file mode 100644 index 78bb0a727..000000000 --- a/core/test/cli_test.jl +++ /dev/null @@ -1,38 +0,0 @@ -@testitem "version" begin - using IOCapture: capture - using Logging: global_logger, ConsoleLogger - - include("../../build/ribasim_cli/src/ribasim_cli.jl") - - empty!(ARGS) - push!(ARGS, "--version") - (; value, output) = capture(ribasim_cli.julia_main) - @test value == 0 - @test output == string(pkgversion(Ribasim)) - - # the global logger is modified by ribasim_cli; set it back to the default - global_logger(ConsoleLogger()) -end - -@testitem "toml_path" begin - using IOCapture: capture - using Logging: global_logger, ConsoleLogger - - include("../../build/ribasim_cli/src/ribasim_cli.jl") - - model_path = normpath(@__DIR__, "../../generated_testmodels/basic/") - toml_path = normpath(model_path, "ribasim.toml") - @test ispath(toml_path) - empty!(ARGS) - push!(ARGS, toml_path) - (; value, output, error, backtrace) = capture(ribasim_cli.julia_main) - @test value == 0 - if value != 0 - @show output - @show error - @show backtrace - end - - # the global logger is modified by ribasim_cli; set it back to the default - global_logger(ConsoleLogger()) -end diff --git a/core/test/control_test.jl b/core/test/control_test.jl index 8067cc904..55ba03f96 100644 --- a/core/test/control_test.jl +++ b/core/test/control_test.jl @@ -4,7 +4,7 @@ toml_path = normpath(@__DIR__, "../../generated_testmodels/pump_discrete_control/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) p = model.integrator.p (; discrete_control, graph) = p @@ -49,7 +49,7 @@ end @testitem "Flow condition control" begin toml_path = normpath(@__DIR__, "../../generated_testmodels/flow_condition/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) p = model.integrator.p (; discrete_control, flow_boundary) = p @@ -73,7 +73,7 @@ end "../../generated_testmodels/level_boundary_condition/ribasim.toml", ) @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) p = model.integrator.p (; discrete_control, level_boundary) = p @@ -138,7 +138,7 @@ end "../../generated_testmodels/tabulated_rating_curve_control/ribasim.toml", ) @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) p = model.integrator.p (; discrete_control) = p # it takes some months to fill the Basin above 0.5 m @@ -157,7 +157,7 @@ end "../../generated_testmodels/level_setpoint_with_minmax/ribasim.toml", ) @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) p = model.integrator.p (; discrete_control) = p (; record, greater_than) = discrete_control @@ -187,7 +187,7 @@ end "../../generated_testmodels/discrete_control_of_pid_control/ribasim.toml", ) @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) p = model.integrator.p (; discrete_control, pid_control) = p diff --git a/core/test/equations_test.jl b/core/test/equations_test.jl index dfd700609..c50dcacfa 100644 --- a/core/test/equations_test.jl +++ b/core/test/equations_test.jl @@ -21,7 +21,7 @@ toml_path = normpath(@__DIR__, "../../generated_testmodels/linear_resistance/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) @test successful_retcode(model) p = model.integrator.p @@ -47,7 +47,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/rating_curve/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) @test successful_retcode(model) p = model.integrator.p @@ -82,7 +82,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/manning_resistance/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) @test successful_retcode(model) p = model.integrator.p (; manning_resistance) = p @@ -119,7 +119,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/pid_control_equation/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) @test successful_retcode(model) p = model.integrator.p (; basin, pid_control) = p @@ -165,7 +165,7 @@ end toml_path = normpath(@__DIR__, "../../generated_testmodels/misc_nodes/ribasim.toml") @test ispath(toml_path) - model = Ribasim.run(toml_path) + model = Ribasim.run(Ribasim.Config(toml_path)) @test successful_retcode(model) p = model.integrator.p (; flow_boundary, fractional_flow, pump) = p diff --git a/core/test/main_test.jl b/core/test/main_test.jl new file mode 100644 index 000000000..6a03b0a88 --- /dev/null +++ b/core/test/main_test.jl @@ -0,0 +1,28 @@ +@testitem "version" begin + using IOCapture: capture + using Logging: global_logger, ConsoleLogger + + (; value, output) = capture() do + Ribasim.main(["--version"]) + end + @test value == 0 + @test output == string(pkgversion(Ribasim)) +end + +@testitem "toml_path" begin + using IOCapture: capture + using Logging: global_logger, ConsoleLogger + + model_path = normpath(@__DIR__, "../../generated_testmodels/basic/") + toml_path = normpath(model_path, "ribasim.toml") + @test ispath(toml_path) + (; value, output, error, backtrace) = capture() do + Ribasim.main([toml_path]) + end + @test value == 0 + if value != 0 + @show output + @show error + @show backtrace + end +end