tests: clean up repo_trace._TRACE_FILE patching
Patch this automatically for all tests rather than duplicating the
boilerplate in diff testcases.
Change-Id: I391d5c859974cda3d5680d34ede2ce6e9e925838
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/354358
Reviewed-by: Joanna Wang <jojwang@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
diff --git a/run_tests b/run_tests
index 7c9ff41..573dd44 100755
--- a/run_tests
+++ b/run_tests
@@ -20,7 +20,6 @@
import shutil
import subprocess
import sys
-import repo_trace
def find_pytest():
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..3e43f6d
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,25 @@
+# Copyright 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Common fixtures for pytests."""
+
+import pytest
+
+import repo_trace
+
+
+@pytest.fixture(autouse=True)
+def disable_repo_trace(tmp_path):
+ """Set an environment marker to relax certain strict checks for test code."""
+ repo_trace._TRACE_FILE = str(tmp_path / 'TRACE_FILE_from_test')
diff --git a/tests/test_git_config.py b/tests/test_git_config.py
index 63c148f..3b0aa8b 100644
--- a/tests/test_git_config.py
+++ b/tests/test_git_config.py
@@ -19,7 +19,6 @@
import unittest
import git_config
-import repo_trace
def fixture(*paths):
@@ -34,16 +33,9 @@
def setUp(self):
"""Create a GitConfig object using the test.gitconfig fixture.
"""
-
- self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
- repo_trace._TRACE_FILE = os.path.join(self.tempdirobj.name, 'TRACE_FILE_from_test')
-
config_fixture = fixture('test.gitconfig')
self.config = git_config.GitConfig(config_fixture)
- def tearDown(self):
- self.tempdirobj.cleanup()
-
def test_GetString_with_empty_config_values(self):
"""
Test config entries with no value.
@@ -117,15 +109,9 @@
"""Read/write tests of the GitConfig class."""
def setUp(self):
- self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
- repo_trace._TRACE_FILE = os.path.join(self.tempdirobj.name, 'TRACE_FILE_from_test')
-
self.tmpfile = tempfile.NamedTemporaryFile()
self.config = self.get_config()
- def tearDown(self):
- self.tempdirobj.cleanup()
-
def get_config(self):
"""Get a new GitConfig instance."""
return git_config.GitConfig(self.tmpfile.name)
diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py
index 1425f4c..225e98c 100644
--- a/tests/test_git_superproject.py
+++ b/tests/test_git_superproject.py
@@ -24,7 +24,6 @@
import git_superproject
import git_trace2_event_log
import manifest_xml
-import repo_trace
from test_manifest_xml import sort_attributes
@@ -40,7 +39,6 @@
"""Set up superproject every time."""
self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
self.tempdir = self.tempdirobj.name
- repo_trace._TRACE_FILE = os.path.join(self.tempdir, 'TRACE_FILE_from_test')
self.repodir = os.path.join(self.tempdir, '.repo')
self.manifest_file = os.path.join(
self.repodir, manifest_xml.MANIFEST_FILE_NAME)
diff --git a/tests/test_manifest_xml.py b/tests/test_manifest_xml.py
index c7e814a..3634701 100644
--- a/tests/test_manifest_xml.py
+++ b/tests/test_manifest_xml.py
@@ -23,7 +23,6 @@
import error
import manifest_xml
-import repo_trace
# Invalid paths that we don't want in the filesystem.
@@ -94,7 +93,6 @@
def setUp(self):
self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
self.tempdir = self.tempdirobj.name
- repo_trace._TRACE_FILE = os.path.join(self.tempdir, 'TRACE_FILE_from_test')
self.repodir = os.path.join(self.tempdir, '.repo')
self.manifest_dir = os.path.join(self.repodir, 'manifests')
self.manifest_file = os.path.join(
diff --git a/tests/test_project.py b/tests/test_project.py
index 7ab4498..66c05f6 100644
--- a/tests/test_project.py
+++ b/tests/test_project.py
@@ -27,7 +27,6 @@
import git_config
import platform_utils
import project
-import repo_trace
@contextlib.contextmanager
@@ -66,13 +65,6 @@
class ReviewableBranchTests(unittest.TestCase):
"""Check ReviewableBranch behavior."""
- def setUp(self):
- self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
- repo_trace._TRACE_FILE = os.path.join(self.tempdirobj.name, 'TRACE_FILE_from_test')
-
- def tearDown(self):
- self.tempdirobj.cleanup()
-
def test_smoke(self):
"""A quick run through everything."""
with TempGitTree() as tempdir:
@@ -418,8 +410,6 @@
"""Ensure properties are fetched properly."""
def setUpManifest(self, tempdir):
- repo_trace._TRACE_FILE = os.path.join(tempdir, 'TRACE_FILE_from_test')
-
repodir = os.path.join(tempdir, '.repo')
manifest_dir = os.path.join(repodir, 'manifests')
manifest_file = os.path.join(