diff --git a/interfaces/cython/cantera/test/test_composite.py b/interfaces/cython/cantera/test/test_composite.py index 2667bec4459..ac86613edcc 100644 --- a/interfaces/cython/cantera/test/test_composite.py +++ b/interfaces/cython/cantera/test/test_composite.py @@ -1,5 +1,4 @@ import sys -from pathlib import Path import numpy as np from collections import OrderedDict @@ -15,7 +14,7 @@ class TestModels(utilities.CanteraTest): @classmethod def setUpClass(cls): utilities.CanteraTest.setUpClass() - cls.yml_file = Path(cls.test_data_dir).joinpath("thermo-models.yaml") + cls.yml_file = cls.test_data_path / "thermo-models.yaml" cls.yml = utilities.load_yaml(cls.yml_file) def test_load_thermo_models(self): @@ -125,13 +124,6 @@ def setUpClass(cls): utilities.CanteraTest.setUpClass() cls.gas = ct.Solution('h2o2.yaml', transport_model=None) - def tearDown(self): - temp_files = ["solutionarray.csv", "solutionarray.h5"] - for f in temp_files: - fpath = Path(self.test_work_dir).joinpath(f) - if fpath.is_file(): - fpath.unlink() - def test_collect_data(self): states = ct.SolutionArray(self.gas) collected = states.collect_data(tabular=True) @@ -149,7 +141,7 @@ def test_write_csv(self): states.TPX = np.linspace(300, 1000, 7), 2e5, 'H2:0.5, O2:0.4' states.equilibrate('HP') - outfile = Path(self.test_work_dir).joinpath("solutionarray.csv") + outfile = self.test_work_path / "solutionarray.csv" states.write_csv(outfile) data = np.genfromtxt(outfile, names=True, delimiter=',') @@ -166,7 +158,7 @@ def test_write_csv(self): def test_write_csv_str_column(self): states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'}) - outfile = Path(self.test_work_dir).joinpath("solutionarray.csv") + outfile = self.test_work_path / "solutionarray.csv" states.write_csv(outfile) b = ct.SolutionArray(self.gas, extra={'spam'}) @@ -176,7 +168,7 @@ def test_write_csv_str_column(self): def test_write_csv_multidim_column(self): states = ct.SolutionArray(self.gas, 3, extra={'spam': np.zeros((3, 5,))}) - outfile = Path(self.test_work_dir).joinpath("solutionarray.csv") + outfile = self.test_work_path / "solutionarray.csv" with self.assertRaisesRegex(NotImplementedError, 'not supported'): states.write_csv(outfile) @@ -193,7 +185,9 @@ def test_to_pandas(self): @utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed") def test_write_hdf(self): - outfile = Path(self.test_work_dir).joinpath("solutionarray.h5") + outfile = self.test_work_path / "solutionarray.h5" + if outfile.is_file(): + outfile.unlink() extra = {'foo': range(7), 'bar': range(7)} meta = {'spam': 'eggs', 'hello': 'world'} @@ -236,7 +230,9 @@ def test_write_hdf(self): @utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed") def test_write_hdf_str_column(self): - outfile = Path(self.test_work_dir).joinpath("solutionarray.h5") + outfile = self.test_work_path / "solutionarray.h5" + if outfile.is_file(): + outfile.unlink() states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'}) states.write_hdf(outfile, mode='w') @@ -247,7 +243,9 @@ def test_write_hdf_str_column(self): @utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed") def test_write_hdf_multidim_column(self): - outfile = Path(self.test_work_dir).joinpath("solutionarray.h5") + outfile = self.test_work_path / "solutionarray.h5" + if outfile.is_file(): + outfile.unlink() states = ct.SolutionArray(self.gas, 3, extra={'spam': [[1, 2], [3, 4], [5, 6]]}) states.write_hdf(outfile, mode='w') @@ -496,7 +494,7 @@ def test_yaml_outunits(self): units = {'length': 'cm', 'quantity': 'mol', 'energy': 'cal'} gas.write_yaml('h2o2-generated.yaml', units=units) generated = utilities.load_yaml("h2o2-generated.yaml") - original = utilities.load_yaml(Path(self.cantera_data).joinpath("h2o2.yaml")) + original = utilities.load_yaml(self.cantera_data_path / "h2o2.yaml") self.assertEqual(generated['units'], units) for r1, r2 in zip(original['reactions'], generated['reactions']): diff --git a/interfaces/cython/cantera/test/test_convert.py b/interfaces/cython/cantera/test/test_convert.py index 4a71531e4d2..1aed50618c5 100644 --- a/interfaces/cython/cantera/test/test_convert.py +++ b/interfaces/cython/cantera/test/test_convert.py @@ -15,16 +15,16 @@ def convert(self, inputFile, thermo=None, transport=None, if output is None: output = Path(inputFile).stem # strip '.inp' if inputFile is not None: - inputFile = Path(self.test_data_dir).joinpath(inputFile) + inputFile = self.test_data_path / inputFile if thermo is not None: - thermo = Path(self.test_data_dir).joinpath(thermo) + thermo = self.test_data_path / thermo if transport is not None: - transport = Path(self.test_data_dir).joinpath(transport) + transport = self.test_data_path / transport if surface is not None: - surface = Path(self.test_data_dir).joinpath(surface) + surface = self.test_data_path / surface if extra is not None: - extra = Path(self.test_data_dir).joinpath(extra) - output = Path(self.test_work_dir).joinpath(output + self.ext) + extra = self.test_data_path / extra + output = self.test_work_path / (output + self.ext) if output.is_file(): output.unlink() self._convert(inputFile, thermo=thermo, transport=transport, @@ -359,14 +359,14 @@ def test_empty_reaction_section(self): def test_reaction_comments1(self): output = self.convert('pdep-test.inp') - text = Path(output).read_text() + text = output.read_text() self.assertIn('Generic mechanism header', text) self.assertIn('Single PLOG reaction', text) self.assertIn('Multiple PLOG expressions at the same pressure', text) def test_reaction_comments2(self): output = self.convert('explicit-third-bodies.inp', thermo='dummy-thermo.dat') - text = Path(output).read_text() + text = output.read_text() self.assertIn('An end of line comment', text) self.assertIn('A comment after the last reaction', text) @@ -470,7 +470,7 @@ def test_extra(self): transport='gri30_tran.dat', output='gri30_extra', extra='extra.yaml') - output = Path(self.test_work_dir).joinpath("gri30_extra" + self.ext) + output = self.test_work_path / ("gri30_extra" + self.ext) yml = utilities.load_yaml(output) desc = yml['description'].split('\n')[-1] @@ -481,7 +481,7 @@ def test_extra(self): def test_sri_zero(self): self.convert('sri_convert_test.txt') - output = Path(self.test_work_dir).joinpath("sri_convert_test" + self.ext) + output = self.test_work_path / ("sri_convert_test" + self.ext) mech = utilities.load_yaml(output) D = mech['reactions'][0]['SRI']['D'] E = mech['reactions'][0]['SRI']['E'] @@ -569,7 +569,7 @@ def test_long_source_input(self): gas = ct.Solution('pdep-test.yaml') - with open(Path(self.test_data_dir).joinpath("pdep-test.cti"), "r") as f: + with open(self.test_data_path / "pdep-test.cti", "r") as f: data = f.read() data_size_2048kB = data + ' '*2048*1024 gas2 = ct.Solution(source=data_size_2048kB) @@ -584,7 +584,7 @@ def test_short_source_input(self): gas = ct.Solution('pdep-test.yaml') - with open(Path(self.test_data_dir).joinpath("pdep-test.cti"), "r") as f: + with open(self.test_data_path / "pdep-test.cti", "r") as f: data = f.read() data_size_32kB = data + ' '*18000 gas2 = ct.Solution(source=data_size_32kB) diff --git a/interfaces/cython/cantera/test/test_equilibrium.py b/interfaces/cython/cantera/test/test_equilibrium.py index eea5cbb76ff..f0a236e580b 100644 --- a/interfaces/cython/cantera/test/test_equilibrium.py +++ b/interfaces/cython/cantera/test/test_equilibrium.py @@ -1,5 +1,4 @@ import unittest -from os.path import join as pjoin import numpy as np @@ -153,7 +152,7 @@ def test_equil_TP(self): data[i,1:] = self.mix.species_moles - self.compare(data, pjoin(self.test_data_dir, 'koh-equil-TP.csv')) + self.compare(data, self.test_data_path / "koh-equil-TP.csv") @utilities.slow_test def test_equil_HP(self): @@ -177,7 +176,7 @@ def test_equil_HP(self): data[i,1] = self.mix.T # equilibrated temperature data[i,2:] = self.mix.species_moles - self.compare(data, pjoin(self.test_data_dir, 'koh-equil-HP.csv')) + self.compare(data, self.test_data_path / "koh-equil-HP.csv") class TestEquil_GasCarbon(utilities.CanteraTest): @@ -207,7 +206,7 @@ def solve(self, solver, **kwargs): data[i,:2] = (phi[i], mix.T) data[i,2:] = mix.species_moles - self.compare(data, pjoin(self.test_data_dir, 'gas-carbon-equil.csv')) + self.compare(data, self.test_data_path / "gas-carbon-equil.csv") @utilities.slow_test def test_gibbs(self): diff --git a/interfaces/cython/cantera/test/test_kinetics.py b/interfaces/cython/cantera/test/test_kinetics.py index 55000f3c039..96f08863ee9 100644 --- a/interfaces/cython/cantera/test/test_kinetics.py +++ b/interfaces/cython/cantera/test/test_kinetics.py @@ -1,8 +1,6 @@ import numpy as np import re import itertools -from os.path import join as pjoin -import os import cantera as ct from . import utilities @@ -816,7 +814,7 @@ def cathode_curr(E): cathode_bulk.electric_potential - anode_bulk.electric_potential]) - self.compare(data, pjoin(self.test_data_dir, 'sofc-test.csv'), rtol=1e-7) + self.compare(data, self.test_data_path / "sofc-test.csv", rtol=1e-7) class TestDuplicateReactions(utilities.CanteraTest): @@ -889,8 +887,7 @@ def test_fromCti(self): def test_fromXml(self): import xml.etree.ElementTree as ET - p = os.path.dirname(__file__) - root = ET.parse(pjoin(p, '..', 'data', 'h2o2.xml')).getroot() + root = ET.parse(self.cantera_data_path / "h2o2.xml").getroot() rxn_node = root.find('.//reaction[@id="0001"]') r = ct.Reaction.fromXml(ET.tostring(rxn_node)) @@ -925,8 +922,7 @@ def test_listFromFile(self): def test_listFromCti(self): gas = ct.Solution("h2o2.xml", transport_model=None) - p = os.path.dirname(__file__) - with open(pjoin(p, '..', 'data', 'h2o2.cti')) as f: + with open(self.cantera_data_path / "h2o2.cti") as f: R = ct.Reaction.listFromCti(f.read()) eq1 = [r.equation for r in R] eq2 = [r.equation for r in gas.reactions()] @@ -934,8 +930,7 @@ def test_listFromCti(self): def test_listFromXml(self): gas = ct.Solution("h2o2.xml", transport_model=None) - p = os.path.dirname(__file__) - with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f: + with open(self.cantera_data_path / "h2o2.xml") as f: R = ct.Reaction.listFromXml(f.read()) eq1 = [r.equation for r in R] eq2 = [r.equation for r in gas.reactions()] diff --git a/interfaces/cython/cantera/test/test_onedim.py b/interfaces/cython/cantera/test/test_onedim.py index 753ce16a3e5..5cc90278a4e 100644 --- a/interfaces/cython/cantera/test/test_onedim.py +++ b/interfaces/cython/cantera/test/test_onedim.py @@ -1,8 +1,6 @@ import cantera as ct from . import utilities import numpy as np -import os -from os.path import join as pjoin from cantera.composite import _h5py @@ -510,9 +508,9 @@ def test_save_restore(self): self.create_sim(p, Tin, reactants) self.solve_fixed_T() - filename = pjoin(self.test_work_dir, 'onedim-fixed-T.xml') - if os.path.exists(filename): - os.remove(filename) + filename = self.test_work_path / "onedim-fixed-T.xml" + if filename.is_file(): + filename.unlink() Y1 = self.sim.Y u1 = self.sim.velocity @@ -577,9 +575,9 @@ def test_save_restore_add_species(self): p = 2 * ct.one_atm Tin = 400 - filename = pjoin(self.test_work_dir, 'onedim-add-species.xml') - if os.path.exists(filename): - os.remove(filename) + filename = self.test_work_path / "onedim-add-species.xml" + if filename.is_file(): + filename.unlink() self.create_sim(p, Tin, reactants, mech='h2o2.xml') gas1 = self.gas @@ -606,9 +604,9 @@ def test_save_restore_remove_species(self): p = 2 * ct.one_atm Tin = 400 - filename = pjoin(self.test_work_dir, 'onedim-add-species.xml') - if os.path.exists(filename): - os.remove(filename) + filename = self.test_work_path / "onedim-add-species.xml" + if filename.is_file(): + filename.unlink() self.create_sim(p, Tin, reactants, mech='h2o2-plus.xml') gas1 = self.gas @@ -630,9 +628,9 @@ def test_save_restore_remove_species(self): self.assertArrayNear(Y1[k1], Y2[k2]) def test_write_csv(self): - filename = pjoin(self.test_work_dir, 'onedim-write_csv.csv') - if os.path.exists(filename): - os.remove(filename) + filename = self.test_work_path / "onedim-write_csv.csv" + if filename.is_file(): + filename.unlink() self.create_sim(2e5, 350, 'H2:1.0, O2:2.0', mech='h2o2.xml') self.sim.write_csv(filename) @@ -645,9 +643,9 @@ def test_write_csv(self): @utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed") def test_write_hdf(self): - filename = pjoin(self.test_work_dir, 'onedim-write_hdf.h5') - if os.path.exists(filename): - os.remove(filename) + filename = self.test_work_path / "onedim-write_hdf.h5" + if filename.is_file(): + filename.unlink() self.run_mix(phi=1.1, T=350, width=2.0, p=2.0, refine=False) desc = 'mixture-averaged simulation' @@ -771,7 +769,7 @@ def solve_mix(self, ratio=3.0, slope=0.1, curve=0.12, prune=0.0): @utilities.slow_test def test_mixture_averaged(self, saveReference=False): - referenceFile = pjoin(self.test_data_dir, 'DiffusionFlameTest-h2-mix.csv') + referenceFile = self.test_data_path / "DiffusionFlameTest-h2-mix.csv" self.create_sim(p=ct.one_atm) nPoints = len(self.sim.grid) @@ -796,7 +794,7 @@ def test_mixture_averaged(self, saveReference=False): self.assertFalse(bad, bad) def test_auto(self, saveReference=False): - referenceFile = pjoin(self.test_data_dir, 'DiffusionFlameTest-h2-auto.csv') + referenceFile = self.test_data_path / "DiffusionFlameTest-h2-auto.csv" self.create_sim(p=ct.one_atm, mdot_fuel=2, mdot_ox=3) nPoints = [] @@ -885,7 +883,7 @@ def test_restart(self): self.assertNear(mdot[-1], -self.sim.oxidizer_inlet.mdot, 1e-4) def test_mixture_averaged_rad(self, saveReference=False): - referenceFile = pjoin(self.test_data_dir, 'DiffusionFlameTest-h2-mix-rad.csv') + referenceFile = self.test_data_path / "DiffusionFlameTest-h2-mix-rad.csv" self.create_sim(p=ct.one_atm) nPoints = len(self.sim.grid) @@ -916,12 +914,12 @@ def test_mixture_averaged_rad(self, saveReference=False): rtol=1e-2, atol=1e-8, xtol=1e-2) self.assertFalse(bad, bad) - filename = pjoin(self.test_work_dir, 'DiffusionFlameTest-h2-mix-rad.csv') + filename = self.test_work_path / "DiffusionFlameTest-h2-mix-rad.csv" self.sim.write_csv(filename) # check output - self.assertTrue(os.path.exists(filename)) + self.assertTrue(filename.is_file()) csv_data = np.genfromtxt(filename, dtype=float, delimiter=',', names=True) self.assertIn('qdot', csv_data.dtype.names) - os.remove(filename) + filename.unlink() def test_strain_rate(self): # This doesn't test that the values are correct, just that they can be @@ -1006,7 +1004,7 @@ def test_mixture_averaged(self, saveReference=False): data[:,3] = sim.T data[:,4:] = sim.Y.T - referenceFile = pjoin(self.test_data_dir, 'CounterflowPremixedFlame-h2-mix.csv') + referenceFile = self.test_data_path / "CounterflowPremixedFlame-h2-mix.csv" if saveReference: np.savetxt(referenceFile, data, '%11.6e', ', ') else: @@ -1014,12 +1012,12 @@ def test_mixture_averaged(self, saveReference=False): rtol=1e-2, atol=1e-8, xtol=1e-2) self.assertFalse(bad, bad) - filename = pjoin(self.test_work_dir, 'CounterflowPremixedFlame-h2-mix.csv') + filename = self.test_work_path / "CounterflowPremixedFlame-h2-mix.csv" sim.write_csv(filename) # check output - self.assertTrue(os.path.exists(filename)) + self.assertTrue(filename.is_file()) csv_data = np.genfromtxt(filename, dtype=float, delimiter=',', names=True) self.assertNotIn('qdot', csv_data.dtype.names) - os.remove(filename) + filename.unlink() def run_case(self, phi, T, width, P): gas = ct.Solution('h2o2.xml') @@ -1190,9 +1188,9 @@ def test_reacting_surface_case3(self): @utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed") def test_write_hdf(self): - filename = pjoin(self.test_work_dir, 'impingingjet-write_hdf.h5') - if os.path.exists(filename): - os.remove(filename) + filename = self.test_work_path / "impingingjet-write_hdf.h5" + if filename.is_file(): + filename.unlink() self.run_reacting_surface(xch4=0.095, tsurf=900.0, mdot=0.06, width=0.1) self.sim.write_hdf(filename) diff --git a/interfaces/cython/cantera/test/test_reactor.py b/interfaces/cython/cantera/test/test_reactor.py index 7ca13a34404..79157887ae8 100644 --- a/interfaces/cython/cantera/test/test_reactor.py +++ b/interfaces/cython/cantera/test/test_reactor.py @@ -1,6 +1,6 @@ import math import re -from os.path import join as pjoin +from pathlib import Path import os import numpy as np @@ -1145,8 +1145,8 @@ def test_coverages_regression1(self): surf1.coverages = C self.assertArrayNear(surf1.coverages, C) data = [] - test_file = pjoin(self.test_work_dir, 'test_coverages_regression1.csv') - reference_file = pjoin(self.test_data_dir, 'WallKinetics-coverages-regression1.csv') + test_file = self.test_work_path / "test_coverages_regression1.csv" + reference_file = self.test_data_path / "WallKinetics-coverages-regression1.csv" data = [] for t in np.linspace(1e-6, 1e-3): self.net.advance(t) @@ -1170,8 +1170,8 @@ def test_coverages_regression2(self): surf.coverages = C self.assertArrayNear(surf.coverages, C) data = [] - test_file = pjoin(self.test_work_dir, 'test_coverages_regression2.csv') - reference_file = pjoin(self.test_data_dir, 'WallKinetics-coverages-regression2.csv') + test_file = self.test_work_path / "test_coverages_regression2.csv" + reference_file = self.test_data_path / "WallKinetics-coverages-regression2.csv" data = [] for t in np.linspace(1e-6, 1e-3): self.net.advance(t) @@ -1516,7 +1516,7 @@ class CombustorTestImplementation: """ def setUp(self): - self.referenceFile = pjoin(os.path.dirname(__file__), 'data', 'CombustorTest-integrateWithAdvance.csv') + self.referenceFile = Path(__file__).parent / "data" / "CombustorTest-integrateWithAdvance.csv" self.gas = ct.Solution('h2o2.yaml', transport_model=None) # create a reservoir for the fuel inlet, and set to pure methane. @@ -1622,7 +1622,7 @@ class WallTestImplementation: """ def setUp(self): - self.referenceFile = pjoin(os.path.dirname(__file__), 'data', 'WallTest-integrateWithAdvance.csv') + self.referenceFile = Path(__file__).parent / "data" / "WallTest-integrateWithAdvance.csv" # reservoir to represent the environment self.gas0 = ct.Solution('air.xml') self.gas0.TP = 300, ct.one_atm diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index b7b34349b6b..69b2df21eba 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -1,7 +1,4 @@ -from os.path import join as pjoin -from pathlib import Path from collections import OrderedDict -import os import numpy as np import gc import warnings @@ -1113,8 +1110,7 @@ def test_fromCti(self): def test_fromXml(self): import xml.etree.ElementTree as ET - p = os.path.dirname(__file__) - root = ET.parse(pjoin(p, '..', 'data', 'h2o2.xml')).getroot() + root = ET.parse(str(self.cantera_data_path / "h2o2.xml")).getroot() h2_node = root.find('.//species[@name="H2"]') h2_string = ET.tostring(h2_node) @@ -1138,8 +1134,7 @@ def test_listfromFile_yaml(self): self.assertEqual({sp.name for sp in S}, set(self.gas.species_names)) def test_listFromCti(self): - p = os.path.dirname(__file__) - with open(pjoin(p, '..', 'data', 'h2o2.cti')) as f: + with open(self.cantera_data_path / "h2o2.cti") as f: S = ct.Species.listFromCti(f.read()) self.assertEqual(S[3].name, self.gas.species_name(3)) @@ -1160,15 +1155,14 @@ def test_listFomYaml(self): def test_listFromYaml_section(self): species = ct.Species.listFromYaml( - Path(__file__).parent.joinpath('data', 'ideal-gas.yaml').read_text(), + (self.test_data_path / "ideal-gas.yaml").read_text(), 'species') self.assertEqual(species[0].name, 'O2') self.assertEqual(species[1].composition, {'N': 1, 'O': 1}) def test_listFromXml(self): - p = os.path.dirname(__file__) - with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f: + with open(self.cantera_data_path / "h2o2.xml") as f: S = ct.Species.listFromXml(f.read()) self.assertEqual(S[4].name, self.gas.species_name(4)) diff --git a/interfaces/cython/cantera/test/utilities.py b/interfaces/cython/cantera/test/utilities.py index 34c47a3da91..583611ac923 100644 --- a/interfaces/cython/cantera/test/utilities.py +++ b/interfaces/cython/cantera/test/utilities.py @@ -6,6 +6,7 @@ import tempfile import unittest import errno +from pathlib import Path try: import ruamel_yaml as yaml @@ -31,36 +32,40 @@ def setUpClass(cls): # an in-source test, create the directory in the root # test/work directory. Otherwise, create a system level # temporary directory - root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), - '..', '..', '..', '..')) - if os.path.exists(os.path.join(root_dir, 'SConstruct')): - cls.test_work_dir = os.path.join(root_dir, 'test', 'work', 'python') + root_dir = Path(__file__).parents[4].resolve() + if (root_dir / "Sconstruct").is_file(): + cls.test_work_path = root_dir / "test" / "work" / "python" cls.using_tempfile = False try: - os.makedirs(cls.test_work_dir) - except OSError as e: - if e.errno == errno.EEXIST: - pass - elif e.errno == errno.EACCES: - cls.test_work_dir = tempfile.mkdtemp() - else: - raise + cls.test_work_path.mkdir() + except FileExistsError: + pass + except FileNotFoundError: + cls.test_work_path = Path(tempfile.mkdtemp()) + cls.using_tempfile = True + except: + raise else: - cls.test_work_dir = tempfile.mkdtemp() + cls.test_work_path = Path(tempfile.mkdtemp()) cls.using_tempfile = True cantera.make_deprecation_warnings_fatal() - cantera.add_directory(cls.test_work_dir) - cls.test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data')) - cls.cantera_data = os.path.abspath(os.path.join( - os.path.dirname(__file__), '..', 'data')) + cantera.add_directory(cls.test_work_path) + cls.test_data_path = Path(__file__).parent / "data" + cls.cantera_data_path = Path(__file__).parents[1] / "data" + + # retain old path strings + cls.test_work_dir = str(cls.test_work_path) + cls.test_data_dir = str(cls.test_data_path) + cls.cantera_data = str(cls.cantera_data_path) + @classmethod def tearDownClass(cls): # Remove the working directory after testing, but only if its a temp directory if getattr(cls, "using_tempfile", False): try: - shutil.rmtree(cls.test_work_dir) + shutil.rmtree(str(cls.test_work_path)) except OSError: pass @@ -101,7 +106,7 @@ def compare(self, data, reference_file, rtol=1e-8, atol=1e-12): file if it does not exist. """ data = np.array(data) - if os.path.exists(reference_file): + if Path(reference_file).is_file(): # Compare with existing output file ref = np.genfromtxt(reference_file) self.assertEqual(data.shape, ref.shape) @@ -110,7 +115,7 @@ def compare(self, data, reference_file, rtol=1e-8, atol=1e-12): else: # Generate the output file for the first time warnings.warn('Generating test data file:' + - os.path.abspath(reference_file)) + Path(reference_file).resolve()) np.savetxt(reference_file, data, fmt='%.10e') @@ -131,12 +136,12 @@ def compareProfiles(reference, sample, rtol=1e-5, atol=1e-12, xtol=1e-5): If the comparison succeeds, this function returns `None`. If the comparison fails, a formatted report of the differing elements is returned. """ - if isinstance(reference, str): + if isinstance(reference, (str, Path)): reference = np.genfromtxt(reference, delimiter=',').T else: reference = np.asarray(reference).T - if isinstance(sample, str): + if isinstance(sample, (str, Path)): sample = np.genfromtxt(sample, delimiter=',').T else: sample = np.asarray(sample).T