From 3497710d749a8acf2478721bcdbdf88f575fa8e8 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 31 Aug 2021 07:50:03 +0200 Subject: [PATCH] Use tempfile.TemporaryDirectory() with with statement instead of gettempdir to avoid races when tests are launched in parallel --- src/ConnectorAPI/Test/TestExportToGEOM.py | 6 ---- .../TestExportToGEOMAllGroupsAndFields.py | 35 +++++++------------ .../Test/TestExportToGEOMWholeResult.py | 32 ++++++++--------- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/src/ConnectorAPI/Test/TestExportToGEOM.py b/src/ConnectorAPI/Test/TestExportToGEOM.py index 5d3809d49..cdbb74f89 100644 --- a/src/ConnectorAPI/Test/TestExportToGEOM.py +++ b/src/ConnectorAPI/Test/TestExportToGEOM.py @@ -66,12 +66,6 @@ def dumpShaper(fileName): model.end() pass -def getTmpFileName(ext): - tempdir = tempfile.gettempdir() - tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False) - tmp_filename = tmp_file.name - return tmp_filename - # Create 2 boxes # Create a group of faces # Create a field of faces diff --git a/src/ConnectorAPI/Test/TestExportToGEOMAllGroupsAndFields.py b/src/ConnectorAPI/Test/TestExportToGEOMAllGroupsAndFields.py index 27ed03a6f..b954a79b6 100644 --- a/src/ConnectorAPI/Test/TestExportToGEOMAllGroupsAndFields.py +++ b/src/ConnectorAPI/Test/TestExportToGEOMAllGroupsAndFields.py @@ -70,12 +70,6 @@ def dumpShaper(fileName): model.end() pass -def getTmpFileName(ext): - tempdir = tempfile.gettempdir() - tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False) - tmp_filename = tmp_file.name - return tmp_filename - def testGroupsAndFieldsExportToGEOM(): model.begin() partSet = model.moduleDocument() @@ -184,26 +178,23 @@ def checkResultInGEOM(): def checkDump(): # Dump the salome study (only CORBA modules, SHAPER dump is not in it) - tempdir = tempfile.gettempdir() - dumpFileGeomBase = "dump_test_geom" - dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase) - salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False) - - # Dump SHAPER - dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py") - dumpShaper(dumpFileShaper) + with tempfile.TemporaryDirectory() as tempdir: + dumpFileGeomBase = "dump_test_geom" + dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase) + salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False) - # Load SHAPER dump - exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec')) + # Dump SHAPER + dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py") + dumpShaper(dumpFileShaper) - # Load GEOM dump - exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec')) + # Load SHAPER dump + exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec')) - # Clean files - files = [dumpFileGeom, dumpFileShaper] - for f in files: - os.remove(f) + # Load GEOM dump + exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec')) + # Clean files + files = [dumpFileGeom, dumpFileShaper] pass if __name__ == '__main__': diff --git a/src/ConnectorAPI/Test/TestExportToGEOMWholeResult.py b/src/ConnectorAPI/Test/TestExportToGEOMWholeResult.py index 6bccc592b..3c3a63cf3 100644 --- a/src/ConnectorAPI/Test/TestExportToGEOMWholeResult.py +++ b/src/ConnectorAPI/Test/TestExportToGEOMWholeResult.py @@ -102,27 +102,25 @@ def testExportToGEOM(): # Check that the group has 6+6 faces geomGroup_2 = getSubObject(geomObject_1, 2) assert geompy.NumberOfFaces(geomGroup_2) == 12 + + with tempfile.TemporaryDirectory() as tempdir: + # Dump the salome study (only CORBA modules, SHAPER dump is not in it) + dumpFileGeomBase = "dump_test_geom" + dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase) + salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False) - # Dump the salome study (only CORBA modules, SHAPER dump is not in it) - tempdir = tempfile.gettempdir() - dumpFileGeomBase = "dump_test_geom" - dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase) - salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False) + # Dump SHAPER + dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py") + dumpShaper(dumpFileShaper) - # Dump SHAPER - dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py") - dumpShaper(dumpFileShaper) + # Load SHAPER dump + exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec')) - # Load SHAPER dump - exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec')) + # Load GEOM dump + exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec')) - # Load GEOM dump - exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec')) - - # Clean files - files = [dumpFileGeom, dumpFileShaper] - for f in files: - os.remove(f) + # Clean files + files = [dumpFileGeom, dumpFileShaper] pass -- 2.39.2