Fix tempdirs not being cleaned up.
@@ -9,7 +9,7 @@ from networkml.NetworkML import NetworkML
def copy_model(model, tempdir):
src_test_dir = os.path.dirname(model)
- dst_test_dir = os.path.join(tempdir.name, os.path.basename(src_test_dir))
+ dst_test_dir = os.path.join(tempdir, os.path.basename(src_test_dir))
test_model = os.path.join(dst_test_dir, os.path.basename(model))
if not os.path.exists(dst_test_dir):
shutil.copytree(src_test_dir, dst_test_dir)
@@ -20,7 +20,7 @@ def run_networkml(args, expected_code=0,
savew=None,
model='networkml/trained_models/onelayer/OneLayerModel.pkl',
sos_model='networkml/trained_models/sos/SoSmodel'):
- tempdir = tempfile.TemporaryDirectory()
+ with tempfile.TemporaryDirectory() as tempdir:
if model:
test_model = copy_model(model, tempdir)
args.extend(['-m', test_model])
@@ -37,7 +37,8 @@ def run_networkml(args, expected_code=0,
assert pytest_wrapped_e.value.code == expected_code
return netml
else:
- return NetworkML()
+ netml = NetworkML()
+ return netml
def test_networkml_nofiles():
@@ -63,8 +64,8 @@ def test_networkml_eval_sos():
def test_networkml_train_onelayer():
- tempdir = tempfile.TemporaryDirectory()
- savew = os.path.join(tempdir.name, 'not.used')
+ with tempfile.TemporaryDirectory() as tempdir:
+ savew = os.path.join(tempdir, 'not.used')
run_networkml(['-p', 'tests/', '-o', 'train'], expected_code=1, savew=savew)
@@ -76,8 +77,8 @@ def test_networkml_train_randomforest():
def test_networkml_train_sos():
- tempdir = tempfile.TemporaryDirectory()
- savew = os.path.join(tempdir.name, 'intermediate.model')
+ with tempfile.TemporaryDirectory() as tempdir:
+ savew = os.path.join(tempdir, 'intermediate.model')
netml = run_networkml(
['-p', 'tests/', '-o', 'train', '-a', 'sos'],
model='networkml/trained_models/sos/SoSmodel',
@@ -92,8 +93,8 @@ def test_networkml_train_sos():
def test_networkml_test_randomforest():
os.environ['POSEIDON_PUBLIC_SESSIONS'] = ''
- tempdir = tempfile.TemporaryDirectory()
- savew = os.path.join(tempdir.name, 'not.used')
+ with tempfile.TemporaryDirectory() as tempdir:
+ savew = os.path.join(tempdir, 'not.used')
netml = run_networkml(
['-p', 'tests/', '-o', 'test', '-a', 'randomforest'],
savew=savew,
