From: Michael R. Crusoe <crusoe@debian.org>
Subject: Use pytest tmp_dir fixture for safe test paths
Forwarded: https://github.com/DataBiosphere/toil/pull/4287

As running the tests post-installation can result in errors otherwise

--- toil.orig/src/toil/test/cwl/cwlTest.py
+++ toil/src/toil/test/cwl/cwlTest.py
@@ -927,31 +927,15 @@
 
 @needs_cwl
 class CWLSmallLogDir(ToilTest):
-    @classmethod
-    def setUpClass(cls):
-        """Runs anew before each test to create farm fresh temp dirs."""
-        cls.out_dir = f"/tmp/cwl-out-dir-{str(uuid.uuid4())}"
-        cls.log_dir = Path(os.path.join(os.path.dirname(__file__), "cwl-logs"))
-        os.makedirs(cls.out_dir)
-        os.makedirs(cls.log_dir)
-
-    def tearDown(self):
-        """Clean up outputs."""
-        if os.path.exists(self.out_dir):
-            shutil.rmtree(self.out_dir)
-        if os.path.exists(self.log_dir):
-            shutil.rmtree(self.log_dir)
-
-        unittest.TestCase.tearDown(self)
-
-    def test_workflow_echo_string_scatter_stderr_log_dir(self):
+    def test_workflow_echo_string_scatter_stderr_log_dir(self, tmp_path: Path):
+        log_dir = tmp_path / "cwl-logs"
         job_store = 'test_workflow_echo_string_scatter_stderr_log_dir'
         toil = "toil-cwl-runner"
         jobstore = f"--jobStore={job_store}"
         option_1 = "--strict-memory-limit"
         option_2 = "--force-docker-pull"
         option_3 = "--clean=always"
-        option_4 = f"--log-dir={self.log_dir}"
+        option_4 = f"--log-dir={log_dir}"
         cwl = os.path.join(
             os.path.dirname(__file__), "echo_string_scatter_capture_stdout.cwl"
         )
@@ -976,32 +960,33 @@
         assert b"Finished toil run successfully" in stderr
         assert p.returncode == 0
 
-        assert os.path.exists(self.log_dir)
-        scatter_0 = os.path.join(self.log_dir, "echo-test-scatter.0.scatter")
-        scatter_1 = os.path.join(self.log_dir, "echo-test-scatter.1.scatter")
-        list_0 = os.path.join(self.log_dir, "echo-test-scatter.0.list")
-        list_1 = os.path.join(self.log_dir, "echo-test-scatter.1.list")
-        assert os.path.exists(scatter_0)
-        assert os.path.exists(scatter_1)
-        assert os.path.exists(list_0)
-        assert os.path.exists(list_1)
-
-    def test_log_dir_echo_no_output(self) -> None:
-        job_store  = 'test_log_dir_echo_no_output'
+        assert log_dir.exists()
+        scatter_0 = log_dir / "echo-test-scatter.0.scatter"
+        scatter_1 = log_dir / "echo-test-scatter.1.scatter"
+        list_0 = log_dir / "echo-test-scatter.0.list"
+        list_1 = log_dir / "echo-test-scatter.1.list"
+        assert scatter_0.exists()
+        assert scatter_1.exists()
+        assert list_0.exists()
+        assert list_1.exists()
+
+    def test_log_dir_echo_no_output(self, tmp_path: Path) -> None:
+        log_dir = tmp_path / "cwl-logs"
+        job_store = 'test_log_dir_echo_no_output'
         toil = "toil-cwl-runner"
         jobstore = f"--jobStore={job_store}"
         option_1 = "--strict-memory-limit"
         option_2 = "--force-docker-pull"
         option_3 = "--clean=always"
-        option_4 = f"--log-dir={self.log_dir}"
+        option_4 = f"--log-dir={log_dir}"
         cwl = os.path.join(os.path.dirname(__file__), "echo-stdout-log-dir.cwl")
         cmd = [toil, jobstore, option_1, option_2, option_3, option_4, cwl]
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         stdout, stderr = p.communicate()
 
-        tmp_path = self.log_dir
+        tmp_path = log_dir
 
-        assert os.path.exists(self.log_dir)
+        assert log_dir.exists()
         assert len(list(tmp_path.iterdir())) == 1
 
         subdir = next(tmp_path.iterdir())
@@ -1012,7 +997,9 @@
         assert result.name == "out.txt"
         output = open(result).read()
         assert "hello" in output
-    def test_log_dir_echo_stderr(self) -> None:
+
+    def test_log_dir_echo_stderr(self, tmp_path: Path) -> None:
+        log_dir = tmp_path / "cwl-logs"
 
         job_store  = 'test_log_dir_echo_stderr'
         toil = "toil-cwl-runner"
@@ -1020,12 +1007,12 @@
         option_1 = "--strict-memory-limit"
         option_2 = "--force-docker-pull"
         option_3 = "--clean=always"
-        option_4 = f"--log-dir={self.log_dir}"
+        option_4 = f"--log-dir={log_dir}"
         cwl = os.path.join(os.path.dirname(__file__), "echo-stderr.cwl")
         cmd = [toil, jobstore, option_1, option_2, option_3, option_4, cwl]
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         stdout, stderr = p.communicate()
-        tmp_path = self.log_dir
+        tmp_path = log_dir
 
         assert len(list(tmp_path.iterdir())) == 1
 
@@ -1038,17 +1025,17 @@
         output = open(result).read()
         assert output == "hello\n"
 
-    def test_filename_conflict_resolution(self):
+    def test_filename_conflict_resolution(self, tmp_path: Path):
+        out_dir = tmp_path / "cwl-out-dir"
         toil = "toil-cwl-runner"
         options = [
-            f"--outdir={self.out_dir}",
+            f"--outdir={out_dir}",
             "--clean=always",
         ]
         cwl = os.path.join(
             os.path.dirname(__file__), "test_filename_conflict_resolution.cwl"
         )
         input = os.path.join(os.path.dirname(__file__), "test_filename_conflict_resolution.ms")
-        output = input + '.sector_*'
         cwl_inputs = [
             "--msin", input
         ]
