From e81528649236ec9fb5983191767a96dc30acff54 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 8 Dec 2022 08:55:54 -0500 Subject: [PATCH] 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 Tested-by: Mike Frysinger --- run_tests | 1 - tests/conftest.py | 25 +++++++++++++++++++++++++ tests/test_git_config.py | 14 -------------- tests/test_git_superproject.py | 2 -- tests/test_manifest_xml.py | 2 -- tests/test_project.py | 10 ---------- 6 files changed, 25 insertions(+), 29 deletions(-) create mode 100644 tests/conftest.py diff --git a/run_tests b/run_tests index 7c9ff41d..573dd446 100755 --- a/run_tests +++ b/run_tests @@ -20,7 +20,6 @@ import os 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 00000000..3e43f6d3 --- /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 63c148f3..3b0aa8b4 100644 --- a/tests/test_git_config.py +++ b/tests/test_git_config.py @@ -19,7 +19,6 @@ import tempfile import unittest import git_config -import repo_trace def fixture(*paths): @@ -34,16 +33,9 @@ class GitConfigReadOnlyTests(unittest.TestCase): 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 @@ class GitConfigReadWriteTests(unittest.TestCase): """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 1425f4ce..225e98c2 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py @@ -24,7 +24,6 @@ from unittest import mock 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 @@ class SuperprojectTestCase(unittest.TestCase): """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 c7e814a3..3634701f 100644 --- a/tests/test_manifest_xml.py +++ b/tests/test_manifest_xml.py @@ -23,7 +23,6 @@ import xml.dom.minidom import error import manifest_xml -import repo_trace # Invalid paths that we don't want in the filesystem. @@ -94,7 +93,6 @@ class ManifestParseTestCase(unittest.TestCase): 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 7ab44984..66c05f6a 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -27,7 +27,6 @@ import git_command import git_config import platform_utils import project -import repo_trace @contextlib.contextmanager @@ -66,13 +65,6 @@ class FakeProject(object): 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 @@ class ManifestPropertiesFetchedCorrectly(unittest.TestCase): """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(