diff options
-rw-r--r-- | tests/test_git_superproject.py | 6 | ||||
-rw-r--r-- | tests/test_manifest_xml.py | 6 | ||||
-rw-r--r-- | tests/test_project.py | 14 | ||||
-rw-r--r-- | tests/test_wrapper.py | 34 |
4 files changed, 20 insertions, 40 deletions
diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py index 1e7b1201..603694d2 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py | |||
@@ -24,7 +24,6 @@ from unittest import mock | |||
24 | import git_superproject | 24 | import git_superproject |
25 | import git_trace2_event_log | 25 | import git_trace2_event_log |
26 | import manifest_xml | 26 | import manifest_xml |
27 | import platform_utils | ||
28 | from test_manifest_xml import sort_attributes | 27 | from test_manifest_xml import sort_attributes |
29 | 28 | ||
30 | 29 | ||
@@ -38,7 +37,8 @@ class SuperprojectTestCase(unittest.TestCase): | |||
38 | 37 | ||
39 | def setUp(self): | 38 | def setUp(self): |
40 | """Set up superproject every time.""" | 39 | """Set up superproject every time.""" |
41 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | 40 | self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests') |
41 | self.tempdir = self.tempdirobj.name | ||
42 | self.repodir = os.path.join(self.tempdir, '.repo') | 42 | self.repodir = os.path.join(self.tempdir, '.repo') |
43 | self.manifest_file = os.path.join( | 43 | self.manifest_file = os.path.join( |
44 | self.repodir, manifest_xml.MANIFEST_FILE_NAME) | 44 | self.repodir, manifest_xml.MANIFEST_FILE_NAME) |
@@ -75,7 +75,7 @@ class SuperprojectTestCase(unittest.TestCase): | |||
75 | 75 | ||
76 | def tearDown(self): | 76 | def tearDown(self): |
77 | """Tear down superproject every time.""" | 77 | """Tear down superproject every time.""" |
78 | platform_utils.rmtree(self.tempdir) | 78 | self.tempdirobj.cleanup() |
79 | 79 | ||
80 | def getXmlManifest(self, data): | 80 | def getXmlManifest(self, data): |
81 | """Helper to initialize a manifest for testing.""" | 81 | """Helper to initialize a manifest for testing.""" |
diff --git a/tests/test_manifest_xml.py b/tests/test_manifest_xml.py index ede41547..85c20733 100644 --- a/tests/test_manifest_xml.py +++ b/tests/test_manifest_xml.py | |||
@@ -17,7 +17,6 @@ | |||
17 | import os | 17 | import os |
18 | import platform | 18 | import platform |
19 | import re | 19 | import re |
20 | import shutil | ||
21 | import tempfile | 20 | import tempfile |
22 | import unittest | 21 | import unittest |
23 | import xml.dom.minidom | 22 | import xml.dom.minidom |
@@ -92,7 +91,8 @@ class ManifestParseTestCase(unittest.TestCase): | |||
92 | """TestCase for parsing manifests.""" | 91 | """TestCase for parsing manifests.""" |
93 | 92 | ||
94 | def setUp(self): | 93 | def setUp(self): |
95 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | 94 | self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests') |
95 | self.tempdir = self.tempdirobj.name | ||
96 | self.repodir = os.path.join(self.tempdir, '.repo') | 96 | self.repodir = os.path.join(self.tempdir, '.repo') |
97 | self.manifest_dir = os.path.join(self.repodir, 'manifests') | 97 | self.manifest_dir = os.path.join(self.repodir, 'manifests') |
98 | self.manifest_file = os.path.join( | 98 | self.manifest_file = os.path.join( |
@@ -111,7 +111,7 @@ class ManifestParseTestCase(unittest.TestCase): | |||
111 | """) | 111 | """) |
112 | 112 | ||
113 | def tearDown(self): | 113 | def tearDown(self): |
114 | shutil.rmtree(self.tempdir, ignore_errors=True) | 114 | self.tempdirobj.cleanup() |
115 | 115 | ||
116 | def getXmlManifest(self, data): | 116 | def getXmlManifest(self, data): |
117 | """Helper to initialize a manifest for testing.""" | 117 | """Helper to initialize a manifest for testing.""" |
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): |
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index e9a1f64a..8447bec1 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py | |||
@@ -14,11 +14,9 @@ | |||
14 | 14 | ||
15 | """Unittests for the wrapper.py module.""" | 15 | """Unittests for the wrapper.py module.""" |
16 | 16 | ||
17 | import contextlib | ||
18 | from io import StringIO | 17 | from io import StringIO |
19 | import os | 18 | import os |
20 | import re | 19 | import re |
21 | import shutil | ||
22 | import sys | 20 | import sys |
23 | import tempfile | 21 | import tempfile |
24 | import unittest | 22 | import unittest |
@@ -26,22 +24,9 @@ from unittest import mock | |||
26 | 24 | ||
27 | import git_command | 25 | import git_command |
28 | import main | 26 | import main |
29 | import platform_utils | ||
30 | import wrapper | 27 | import wrapper |
31 | 28 | ||
32 | 29 | ||
33 | @contextlib.contextmanager | ||
34 | def TemporaryDirectory(): | ||
35 | """Create a new empty git checkout for testing.""" | ||
36 | # TODO(vapier): Convert this to tempfile.TemporaryDirectory once we drop | ||
37 | # Python 2 support entirely. | ||
38 | try: | ||
39 | tempdir = tempfile.mkdtemp(prefix='repo-tests') | ||
40 | yield tempdir | ||
41 | finally: | ||
42 | platform_utils.rmtree(tempdir) | ||
43 | |||
44 | |||
45 | def fixture(*paths): | 30 | def fixture(*paths): |
46 | """Return a path relative to tests/fixtures. | 31 | """Return a path relative to tests/fixtures. |
47 | """ | 32 | """ |
@@ -336,19 +321,19 @@ class NeedSetupGnuPG(RepoWrapperTestCase): | |||
336 | 321 | ||
337 | def test_missing_dir(self): | 322 | def test_missing_dir(self): |
338 | """The ~/.repoconfig tree doesn't exist yet.""" | 323 | """The ~/.repoconfig tree doesn't exist yet.""" |
339 | with TemporaryDirectory() as tempdir: | 324 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
340 | self.wrapper.home_dot_repo = os.path.join(tempdir, 'foo') | 325 | self.wrapper.home_dot_repo = os.path.join(tempdir, 'foo') |
341 | self.assertTrue(self.wrapper.NeedSetupGnuPG()) | 326 | self.assertTrue(self.wrapper.NeedSetupGnuPG()) |
342 | 327 | ||
343 | def test_missing_keyring(self): | 328 | def test_missing_keyring(self): |
344 | """The keyring-version file doesn't exist yet.""" | 329 | """The keyring-version file doesn't exist yet.""" |
345 | with TemporaryDirectory() as tempdir: | 330 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
346 | self.wrapper.home_dot_repo = tempdir | 331 | self.wrapper.home_dot_repo = tempdir |
347 | self.assertTrue(self.wrapper.NeedSetupGnuPG()) | 332 | self.assertTrue(self.wrapper.NeedSetupGnuPG()) |
348 | 333 | ||
349 | def test_empty_keyring(self): | 334 | def test_empty_keyring(self): |
350 | """The keyring-version file exists, but is empty.""" | 335 | """The keyring-version file exists, but is empty.""" |
351 | with TemporaryDirectory() as tempdir: | 336 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
352 | self.wrapper.home_dot_repo = tempdir | 337 | self.wrapper.home_dot_repo = tempdir |
353 | with open(os.path.join(tempdir, 'keyring-version'), 'w'): | 338 | with open(os.path.join(tempdir, 'keyring-version'), 'w'): |
354 | pass | 339 | pass |
@@ -356,7 +341,7 @@ class NeedSetupGnuPG(RepoWrapperTestCase): | |||
356 | 341 | ||
357 | def test_old_keyring(self): | 342 | def test_old_keyring(self): |
358 | """The keyring-version file exists, but it's old.""" | 343 | """The keyring-version file exists, but it's old.""" |
359 | with TemporaryDirectory() as tempdir: | 344 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
360 | self.wrapper.home_dot_repo = tempdir | 345 | self.wrapper.home_dot_repo = tempdir |
361 | with open(os.path.join(tempdir, 'keyring-version'), 'w') as fp: | 346 | with open(os.path.join(tempdir, 'keyring-version'), 'w') as fp: |
362 | fp.write('1.0\n') | 347 | fp.write('1.0\n') |
@@ -364,7 +349,7 @@ class NeedSetupGnuPG(RepoWrapperTestCase): | |||
364 | 349 | ||
365 | def test_new_keyring(self): | 350 | def test_new_keyring(self): |
366 | """The keyring-version file exists, and is up-to-date.""" | 351 | """The keyring-version file exists, and is up-to-date.""" |
367 | with TemporaryDirectory() as tempdir: | 352 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
368 | self.wrapper.home_dot_repo = tempdir | 353 | self.wrapper.home_dot_repo = tempdir |
369 | with open(os.path.join(tempdir, 'keyring-version'), 'w') as fp: | 354 | with open(os.path.join(tempdir, 'keyring-version'), 'w') as fp: |
370 | fp.write('1000.0\n') | 355 | fp.write('1000.0\n') |
@@ -376,7 +361,7 @@ class SetupGnuPG(RepoWrapperTestCase): | |||
376 | 361 | ||
377 | def test_full(self): | 362 | def test_full(self): |
378 | """Make sure it works completely.""" | 363 | """Make sure it works completely.""" |
379 | with TemporaryDirectory() as tempdir: | 364 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
380 | self.wrapper.home_dot_repo = tempdir | 365 | self.wrapper.home_dot_repo = tempdir |
381 | self.wrapper.gpg_dir = os.path.join(self.wrapper.home_dot_repo, 'gnupg') | 366 | self.wrapper.gpg_dir = os.path.join(self.wrapper.home_dot_repo, 'gnupg') |
382 | self.assertTrue(self.wrapper.SetupGnuPG(True)) | 367 | self.assertTrue(self.wrapper.SetupGnuPG(True)) |
@@ -426,7 +411,8 @@ class GitCheckoutTestCase(RepoWrapperTestCase): | |||
426 | @classmethod | 411 | @classmethod |
427 | def setUpClass(cls): | 412 | def setUpClass(cls): |
428 | # Create a repo to operate on, but do it once per-class. | 413 | # Create a repo to operate on, but do it once per-class. |
429 | cls.GIT_DIR = tempfile.mkdtemp(prefix='repo-rev-tests') | 414 | cls.tempdirobj = tempfile.TemporaryDirectory(prefix='repo-rev-tests') |
415 | cls.GIT_DIR = cls.tempdirobj.name | ||
430 | run_git = wrapper.Wrapper().run_git | 416 | run_git = wrapper.Wrapper().run_git |
431 | 417 | ||
432 | remote = os.path.join(cls.GIT_DIR, 'remote') | 418 | remote = os.path.join(cls.GIT_DIR, 'remote') |
@@ -455,10 +441,10 @@ class GitCheckoutTestCase(RepoWrapperTestCase): | |||
455 | 441 | ||
456 | @classmethod | 442 | @classmethod |
457 | def tearDownClass(cls): | 443 | def tearDownClass(cls): |
458 | if not cls.GIT_DIR: | 444 | if not cls.tempdirobj: |
459 | return | 445 | return |
460 | 446 | ||
461 | shutil.rmtree(cls.GIT_DIR) | 447 | cls.tempdirobj.cleanup() |
462 | 448 | ||
463 | 449 | ||
464 | class ResolveRepoRev(GitCheckoutTestCase): | 450 | class ResolveRepoRev(GitCheckoutTestCase): |