Skip to content

Commit

Permalink
#2826: Add tests for config options
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-alistairp committed Jan 13, 2025
1 parent ffb388f commit 4eaeef1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/psyclone/psyad/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def main(args):
been invoked with.
'''

# Make sure we have the supported APIs defined in the Config singleton,
# but postpone loading the config file till the command line was parsed
# in case that the user specifies a different config file.
Config.get(do_not_load_file=True)

# pylint: disable=too-many-statements, too-many-branches
# TODO #1863 - expose line-length limiting as a command-line flag.
line_length_limit = True
Expand Down Expand Up @@ -110,13 +116,13 @@ def msg():

args = parser.parse_args(args)

# If no config file name is specified, args.config is none
# and config will load the default config file.
Config.get().load(args.config)

if args.verbose:
logging.basicConfig(level=logging.DEBUG)

# Create a config file so we don't trigger the 'LFRicConstants' created
# before config file was read exception
Config.get()

# Specifying an output file for the test harness is taken to mean that
# the user wants us to generate it.
generate_test = args.gen_test or args.test_filename
Expand Down
40 changes: 40 additions & 0 deletions src/psyclone/tests/psyad/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import pytest

from psyclone.psyad import main
from psyclone.configuration import Config

TEST_PROG = (
"program test\n"
Expand Down Expand Up @@ -472,6 +473,45 @@ def test_main_t_option(tmpdir, capsys):
assert EXPECTED_HARNESS_CODE in output.lower()


def test_config_flag(tmpdir):
''' Test that -c/--config take precedence over the configuration
file references in the environment variable.
'''
# filename_in = str(tmpdir.join("tl_foo_kernel_mod.f90"))
filename_in = str(tmpdir.join("tl.f90"))

with open(filename_in, "w", encoding='utf-8') as my_file:
my_file.write(TEST_LFRIC_KERNEL)

# dummy_config has a non-default REPORD_PAD_SIZE of 7
config_name = os.path.join(
os.path.split(os.path.dirname(os.path.abspath(__file__)))[0],
"test_files", "dummy_config.cfg")

# Test with no option
Config._HAS_CONFIG_BEEN_INITIALISED = False
main([filename_in, "-a", "field", "-api", "lfric"])
assert Config.get().api == "lfric"
assert Config.has_config_been_initialised() is True
print(Config.get().reprod_pad_size)
assert Config.get().reprod_pad_size == 8

# Test with with -c
Config._HAS_CONFIG_BEEN_INITIALISED = False
main([filename_in, "-a", "field", "-c", config_name, "-api", "lfric"])
assert Config.get().api == "lfric"
assert Config.has_config_been_initialised() is True
assert Config.get().reprod_pad_size == 7

# Test with with --config
Config._HAS_CONFIG_BEEN_INITIALISED = False
main([filename_in, "-a", "field", "--config", config_name,
"-api", "lfric"])
assert Config.get().api == "lfric"
assert Config.has_config_been_initialised() is True
assert Config.get().reprod_pad_size == 7


@pytest.mark.parametrize("extra_args", [[], ["-t"]])
def test_main_otest_option(tmpdir, capsys, extra_args):
''' Test that the -otest option switches on test-harness generation and
Expand Down

0 comments on commit 4eaeef1

Please sign in to comment.