From 5727fa66ff92a5f46586dc7f676c7fccaa3117c5 Mon Sep 17 00:00:00 2001 From: Tengis Dashmunkh Date: Tue, 13 Feb 2024 10:11:40 -0700 Subject: [PATCH 1/3] Adding ReTest and relevant files --- test/PowerSystemsTests.jl | 101 ++++++++++++++++++++++++++++ test/Project.toml | 2 + test/load_tests.jl | 13 ++++ test/runtests.jl | 135 +------------------------------------- 4 files changed, 118 insertions(+), 133 deletions(-) create mode 100644 test/PowerSystemsTests.jl create mode 100644 test/load_tests.jl diff --git a/test/PowerSystemsTests.jl b/test/PowerSystemsTests.jl new file mode 100644 index 0000000000..a41e5c8b4d --- /dev/null +++ b/test/PowerSystemsTests.jl @@ -0,0 +1,101 @@ +module PowerSystemsTests + +import TerminalLoggers: TerminalLogger +using ReTest +using Logging +using DataStructures +using Dates +using LinearAlgebra +using PowerSystemCaseBuilder +import TimeSeries +import InteractiveUtils +import JSON3 +import PowerSystemCaseBuilder: PSYTestSystems +import InfrastructureSystems +const IS = InfrastructureSystems +using PowerSystems +import PowerSystems: PowerSystemTableData +const PSY = PowerSystems +const PSB = PowerSystemCaseBuilder + +import Aqua +Aqua.test_unbound_args(PowerSystems) +Aqua.test_undefined_exports(PowerSystems) +Aqua.test_ambiguities(PowerSystems) +Aqua.test_stale_deps(PowerSystems) +Aqua.test_deps_compat(PowerSystems) + +const BASE_DIR = dirname(dirname(Base.find_package("PowerSystems"))) +const DATA_DIR = PSB.DATA_DIR +const TIME_SERIES_DIR = joinpath(DATA_DIR, "forecasts") +const MATPOWER_DIR = joinpath(DATA_DIR, "matpower") +const PSSE_RAW_DIR = joinpath(DATA_DIR, "psse_raw") +const PSSE_DYR_DIR = joinpath(DATA_DIR, "psse_dyr") +const PSSE_TEST_DIR = joinpath(DATA_DIR, "PSSE_test") +const RTS_GMLC_DIR = joinpath(DATA_DIR, "RTS_GMLC") +const TAMU_DIR = joinpath(DATA_DIR, "ACTIVSg2000") +const DESCRIPTORS = joinpath(RTS_GMLC_DIR, "user_descriptors.yaml") +const BAD_DATA = joinpath(DATA_DIR, "bad_data_for_tests") + +LOG_FILE = "power-systems.log" +LOG_LEVELS = Dict( + "Debug" => Logging.Debug, + "Info" => Logging.Info, + "Warn" => Logging.Warn, + "Error" => Logging.Error, +) + +include("common.jl") + +function get_logging_level_from_env(env_name::String, default) + level = get(ENV, env_name, default) + return IS.get_logging_level(level) +end + +logger = global_logger() + +function run_tests(args...; kwargs...) + logger = global_logger() + try + logging_config_filename = get(ENV, "SIENNA_LOGGING_CONFIG", nothing) + if logging_config_filename !== nothing + config = IS.LoggingConfiguration(logging_config_filename) + else + config = IS.LoggingConfiguration(; + filename = LOG_FILE, + file_level = get_logging_level_from_env("SIENNA_FILE_LOG_LEVEL", "Info"), + console_level = get_logging_level_from_env( + "SIENNA_CONSOLE_LOG_LEVEL", + "Error", + ), + ) + end + console_logger = TerminalLogger(config.console_stream, config.console_level) + + IS.open_file_logger(config.filename, config.file_level) do file_logger + levels = (Logging.Info, Logging.Warn, Logging.Error) + multi_logger = + IS.MultiLogger([console_logger, file_logger], IS.LogEventTracker(levels)) + global_logger(multi_logger) + + if !isempty(config.group_levels) + IS.set_group_levels!(multi_logger, config.group_levels) + end + + @time retest(args...; kwargs...) + @test length(IS.get_log_events(multi_logger.tracker, Logging.Error)) == 0 + @info IS.report_log_summary(multi_logger) + end + finally + # Guarantee that the global logger is reset. + global_logger(logger) + nothing + end +end + + +export run_tests +@show "hello0" +end + +using .PowerSystemsTests \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index 3f20f6e04a..20accc0296 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -14,7 +14,9 @@ NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56" PowerSystemCaseBuilder = "f00506e0-b84f-492a-93c2-c0a9afc4364e" PowerSystems = "bcd98974-b02a-5e2f-9ee0-a103f5c450dd" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +ReTest = "e0db7c4e-2690-44b9-bad6-7687da720f89" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" diff --git a/test/load_tests.jl b/test/load_tests.jl new file mode 100644 index 0000000000..56c50bebe0 --- /dev/null +++ b/test/load_tests.jl @@ -0,0 +1,13 @@ +using Revise + +# Copied from https://juliatesting.github.io/ReTest.jl/stable/#Working-with-Revise +function recursive_includet(filename) + already_included = copy(Revise.included_files) + includet(filename) + newly_included = setdiff(Revise.included_files, already_included) + for (mod, file) in newly_included + Revise.track(mod, file) + end +end + +recursive_includet("PowerSystemsTests.jl") \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 02ac00a124..a3011ca0b9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,135 +1,4 @@ -using Test -using Logging -using DataStructures -using Dates -using LinearAlgebra -using PowerSystemCaseBuilder -import TimeSeries -import InteractiveUtils -import JSON3 -import PowerSystemCaseBuilder: PSYTestSystems -import InfrastructureSystems -const IS = InfrastructureSystems using PowerSystems -import PowerSystems: PowerSystemTableData -const PSY = PowerSystems -const PSB = PowerSystemCaseBuilder -import Aqua -Aqua.test_unbound_args(PowerSystems) -Aqua.test_undefined_exports(PowerSystems) -Aqua.test_ambiguities(PowerSystems) -Aqua.test_stale_deps(PowerSystems) -Aqua.test_deps_compat(PowerSystems) - -const BASE_DIR = dirname(dirname(Base.find_package("PowerSystems"))) -const DATA_DIR = PSB.DATA_DIR -const TIME_SERIES_DIR = joinpath(DATA_DIR, "forecasts") -const MATPOWER_DIR = joinpath(DATA_DIR, "matpower") -const PSSE_RAW_DIR = joinpath(DATA_DIR, "psse_raw") -const PSSE_DYR_DIR = joinpath(DATA_DIR, "psse_dyr") -const PSSE_TEST_DIR = joinpath(DATA_DIR, "PSSE_test") -const RTS_GMLC_DIR = joinpath(DATA_DIR, "RTS_GMLC") -const TAMU_DIR = joinpath(DATA_DIR, "ACTIVSg2000") -const DESCRIPTORS = joinpath(RTS_GMLC_DIR, "user_descriptors.yaml") -const BAD_DATA = joinpath(DATA_DIR, "bad_data_for_tests") - -LOG_FILE = "power-systems.log" -LOG_LEVELS = Dict( - "Debug" => Logging.Debug, - "Info" => Logging.Info, - "Warn" => Logging.Warn, - "Error" => Logging.Error, -) - -include("common.jl") - -""" -Copied @includetests from https://github.com/ssfrr/TestSetExtensions.jl. -Ideally, we could import and use TestSetExtensions. Its functionality was broken by changes -in Julia v0.7. Refer to https://github.com/ssfrr/TestSetExtensions.jl/pull/7. -""" - -""" -Includes the given test files, given as a list without their ".jl" extensions. -If none are given it will scan the directory of the calling file and include all -the julia files. -""" -macro includetests(testarg...) - if length(testarg) == 0 - tests = [] - elseif length(testarg) == 1 - tests = testarg[1] - else - error("@includetests takes zero or one argument") - end - - quote - tests = $tests - rootfile = @__FILE__ - if length(tests) == 0 - tests = readdir(dirname(rootfile)) - tests = filter( - f -> - startswith(f, "test_") && endswith(f, ".jl") && f != basename(rootfile), - tests, - ) - else - tests = map(f -> string(f, ".jl"), tests) - end - println() - for test in tests - print(splitext(test)[1], ": ") - include(test) - println() - end - end -end - -function get_logging_level_from_env(env_name::String, default) - level = get(ENV, env_name, default) - return IS.get_logging_level(level) -end - -function run_tests() - logging_config_filename = get(ENV, "SIIP_LOGGING_CONFIG", nothing) - if logging_config_filename !== nothing - config = IS.LoggingConfiguration(logging_config_filename) - else - config = IS.LoggingConfiguration(; - filename = LOG_FILE, - file_level = get_logging_level_from_env("SIIP_FILE_LOG_LEVEL", "Info"), - console_level = get_logging_level_from_env("SIIP_CONSOLE_LOG_LEVEL", "Error"), - ) - end - console_logger = ConsoleLogger(config.console_stream, config.console_level) - - IS.open_file_logger(config.filename, config.file_level) do file_logger - levels = (Logging.Info, Logging.Warn, Logging.Error) - multi_logger = - IS.MultiLogger([console_logger, file_logger], IS.LogEventTracker(levels)) - global_logger(multi_logger) - - if !isempty(config.group_levels) - IS.set_group_levels!(multi_logger, config.group_levels) - end - - # Testing Topological components of the schema - @time @testset "Begin PowerSystems tests" begin - @includetests ARGS - end - - @test length(IS.get_log_events(multi_logger.tracker, Logging.Error)) == 0 - @info IS.report_log_summary(multi_logger) - end -end - -logger = global_logger() - -try - run_tests() -finally - # Guarantee that the global logger is reset. - global_logger(logger) - nothing -end +include("PowerSystemsTests.jl") +run_tests() \ No newline at end of file From a85317b2edc0505b61304d32abc2a871a428935c Mon Sep 17 00:00:00 2001 From: Tengis Dashmunkh Date: Tue, 13 Feb 2024 18:46:28 -0700 Subject: [PATCH 2/3] Adding a block of code back --- test/PowerSystemsTests.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/PowerSystemsTests.jl b/test/PowerSystemsTests.jl index a41e5c8b4d..f8825f12d0 100644 --- a/test/PowerSystemsTests.jl +++ b/test/PowerSystemsTests.jl @@ -47,6 +47,12 @@ LOG_LEVELS = Dict( include("common.jl") +for filename in readdir(joinpath(BASE_DIR, "test")) + if startswith(filename, "test_") && endswith(filename, ".jl") + include(filename) + end +end + function get_logging_level_from_env(env_name::String, default) level = get(ENV, env_name, default) return IS.get_logging_level(level) @@ -95,7 +101,7 @@ end export run_tests -@show "hello0" + end using .PowerSystemsTests \ No newline at end of file From 66de7e4e2e6fc173bca4056c303aba88aadef57f Mon Sep 17 00:00:00 2001 From: Tengis Dashmunkh Date: Thu, 15 Feb 2024 17:46:17 -0700 Subject: [PATCH 3/3] Ran the formatter --- test/PowerSystemsTests.jl | 3 +-- test/load_tests.jl | 2 +- test/runtests.jl | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/PowerSystemsTests.jl b/test/PowerSystemsTests.jl index f8825f12d0..b22712d840 100644 --- a/test/PowerSystemsTests.jl +++ b/test/PowerSystemsTests.jl @@ -99,9 +99,8 @@ function run_tests(args...; kwargs...) end end - export run_tests end -using .PowerSystemsTests \ No newline at end of file +using .PowerSystemsTests diff --git a/test/load_tests.jl b/test/load_tests.jl index 56c50bebe0..fed36a780a 100644 --- a/test/load_tests.jl +++ b/test/load_tests.jl @@ -10,4 +10,4 @@ function recursive_includet(filename) end end -recursive_includet("PowerSystemsTests.jl") \ No newline at end of file +recursive_includet("PowerSystemsTests.jl") diff --git a/test/runtests.jl b/test/runtests.jl index a3011ca0b9..58369c8859 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ using PowerSystems include("PowerSystemsTests.jl") -run_tests() \ No newline at end of file +run_tests()