diff options
author | Mike Frysinger <vapier@google.com> | 2022-05-20 06:26:50 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2022-05-20 11:38:10 +0000 |
commit | 74737da1ab9b5a25c38616d563a973af267277f7 (patch) | |
tree | 23b1758d149942868de03eed6b27c57bbdf9ce51 /tests/test_project.py | |
parent | 0ddb6776110aaefee3f93cc171d11dcdc12d082b (diff) | |
download | git-repo-74737da1ab9b5a25c38616d563a973af267277f7.tar.gz |
tests: switch to tempfile.TemporaryDirectory
Now that we don't need to support Python 2, we can switch to this
API for better contextmanager logic.
Change-Id: I2d03e391121886547e7808a3b5c3b470c411533f
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/337515
Reviewed-by: LaMont Jones <lamontjones@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'tests/test_project.py')
-rw-r--r-- | tests/test_project.py | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/tests/test_project.py b/tests/test_project.py index 8829637e..acd44ccc 100644 --- a/tests/test_project.py +++ b/tests/test_project.py | |||
@@ -17,7 +17,6 @@ | |||
17 | import contextlib | 17 | import contextlib |
18 | import os | 18 | import os |
19 | from pathlib import Path | 19 | from pathlib import Path |
20 | import shutil | ||
21 | import subprocess | 20 | import subprocess |
22 | import tempfile | 21 | import tempfile |
23 | import unittest | 22 | import unittest |
@@ -32,11 +31,7 @@ import project | |||
32 | @contextlib.contextmanager | 31 | @contextlib.contextmanager |
33 | def TempGitTree(): | 32 | def TempGitTree(): |
34 | """Create a new empty git checkout for testing.""" | 33 | """Create a new empty git checkout for testing.""" |
35 | # TODO(vapier): Convert this to tempfile.TemporaryDirectory once we drop | 34 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
36 | # Python 2 support entirely. | ||
37 | try: | ||
38 | tempdir = tempfile.mkdtemp(prefix='repo-tests') | ||
39 | |||
40 | # Tests need to assume, that main is default branch at init, | 35 | # Tests need to assume, that main is default branch at init, |
41 | # which is not supported in config until 2.28. | 36 | # which is not supported in config until 2.28. |
42 | cmd = ['git', 'init'] | 37 | cmd = ['git', 'init'] |
@@ -50,8 +45,6 @@ def TempGitTree(): | |||
50 | cmd += ['--template', templatedir] | 45 | cmd += ['--template', templatedir] |
51 | subprocess.check_call(cmd, cwd=tempdir) | 46 | subprocess.check_call(cmd, cwd=tempdir) |
52 | yield tempdir | 47 | yield tempdir |
53 | finally: | ||
54 | platform_utils.rmtree(tempdir) | ||
55 | 48 | ||
56 | 49 | ||
57 | class FakeProject(object): | 50 | class FakeProject(object): |
@@ -124,14 +117,15 @@ class CopyLinkTestCase(unittest.TestCase): | |||
124 | """ | 117 | """ |
125 | 118 | ||
126 | def setUp(self): | 119 | def setUp(self): |
127 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | 120 | self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests') |
121 | self.tempdir = self.tempdirobj.name | ||
128 | self.topdir = os.path.join(self.tempdir, 'checkout') | 122 | self.topdir = os.path.join(self.tempdir, 'checkout') |
129 | self.worktree = os.path.join(self.topdir, 'git-project') | 123 | self.worktree = os.path.join(self.topdir, 'git-project') |
130 | os.makedirs(self.topdir) | 124 | os.makedirs(self.topdir) |
131 | os.makedirs(self.worktree) | 125 | os.makedirs(self.worktree) |
132 | 126 | ||
133 | def tearDown(self): | 127 | def tearDown(self): |
134 | shutil.rmtree(self.tempdir, ignore_errors=True) | 128 | self.tempdirobj.cleanup() |
135 | 129 | ||
136 | @staticmethod | 130 | @staticmethod |
137 | def touch(path): | 131 | def touch(path): |