From 280abc97bdd5fd22509153544b84ca9c18ae0d89 Mon Sep 17 00:00:00 2001 From: Aidan Heerdegen Date: Fri, 6 Dec 2019 13:53:40 +1100 Subject: [PATCH] Added new test file. Fixed travis to run all tests --- test/test_paths.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 test/test_paths.py diff --git a/test/test_paths.py b/test/test_paths.py new file mode 100644 index 00000000..60eb1a01 --- /dev/null +++ b/test/test_paths.py @@ -0,0 +1,86 @@ +import os +from pathlib import Path +import shutil + +import pdb +import pytest +import yaml + +import payu + +from payu.laboratory import Laboratory + +from common import cd, make_random_file, get_manifests, tmpdir, ctrldir, labdir, workdir +from common import config, sweep_work, payu_init, payu_setup +from common import write_config, make_exe, make_inputs, make_restarts, make_all_files + +verbose = True + +def setup_module(module): + """ + Put any test-wide setup code in here, e.g. creating test files + """ + if verbose: + print("setup_module module:%s" % module.__name__) + + # Should be taken care of by teardown, in case remnants lying around + try: + shutil.rmtree(tmpdir) + except FileNotFoundError: + pass + + try: + tmpdir.mkdir() + labdir.mkdir() + ctrldir.mkdir() + except Exception as e: + print(e) + + write_config() + + +def teardown_module(module): + """ + Put any test-wide teardown code in here, e.g. removing test outputs + """ + if verbose: + print("teardown_module module:%s" % module.__name__) + + try: + shutil.rmtree(tmpdir) + print('removing tmp') + except Exception as e: + print(e) + + +def test_laboratory(): + + # Test instantiating a Laboratory object + with cd(ctrldir): + lab = Laboratory(None, None, None) + + assert(Path(lab.basepath).parts[0] == '..') + assert(Path(lab.basepath).parts[2] == 'lab') + + # Set a PROJECT env variable to get reproducible paths + os.environ['PROJECT'] = 'x00' + + # Repeat, but remove shortpath definition + # in config, so will fall through to default + # depending on platform + del(config['shortpath']) + write_config() + with cd(ctrldir): + lab = Laboratory(None, None, None) + + shortpath = '.' + for path in ['/short', '/scratch']: + if Path(path).exists(): + shortpath = path + break + + # pdb.set_trace() + + assert(Path(lab.basepath).parents[2] == Path(shortpath)) + assert(Path(lab.basepath).parts[2] == os.environ['PROJECT']) + assert(Path(lab.basepath).parts[-1] == 'lab') \ No newline at end of file