diff options
Diffstat (limited to 'tests/test_wrapper.py')
| -rw-r--r-- | tests/test_wrapper.py | 34 |
1 files changed, 10 insertions, 24 deletions
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): |
