OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 """ |
| 4 Copyright 2014 Google Inc. |
| 5 |
| 6 Use of this source code is governed by a BSD-style license that can be |
| 7 found in the LICENSE file. |
| 8 |
| 9 Test git_utils.py. |
| 10 """ |
| 11 |
| 12 # System-level imports |
| 13 import os |
| 14 import tempfile |
| 15 import unittest |
| 16 |
| 17 # Imports from within Skia |
| 18 import git_utils |
| 19 |
| 20 |
| 21 # A git repo we can use for tests. |
| 22 REPO = 'https://skia.googlesource.com/common' |
| 23 |
| 24 # A file in some subdirectory within REPO. |
| 25 REPO_FILE = os.path.join('py', 'utils', 'git_utils.py') |
| 26 |
| 27 |
| 28 class NewGitCheckoutTest(unittest.TestCase): |
| 29 |
| 30 def test_defaults(self): |
| 31 """Test NewGitCheckout created using default parameters.""" |
| 32 with git_utils.NewGitCheckout(repository=REPO) as checkout: |
| 33 filepath = os.path.join(checkout.root, REPO_FILE) |
| 34 self.assertTrue( |
| 35 os.path.exists(filepath), |
| 36 'file %s should exist' % filepath) |
| 37 # Confirm that NewGitCheckout cleaned up after itself. |
| 38 self.assertFalse( |
| 39 os.path.exists(filepath), |
| 40 'file %s should not exist' % filepath) |
| 41 |
| 42 def test_subdir(self): |
| 43 """Create NewGitCheckout with a specific subdirectory.""" |
| 44 subdir = os.path.dirname(REPO_FILE) |
| 45 file_within_subdir = os.path.basename(REPO_FILE) |
| 46 |
| 47 containing_dir = tempfile.mkdtemp() |
| 48 try: |
| 49 with git_utils.NewGitCheckout(repository=REPO, subdir=subdir, |
| 50 containing_dir=containing_dir) as checkout: |
| 51 self.assertTrue( |
| 52 checkout.root.startswith(containing_dir), |
| 53 'checkout.root %s should be within %s' % ( |
| 54 checkout.root, containing_dir)) |
| 55 filepath = os.path.join(checkout.root, file_within_subdir) |
| 56 self.assertTrue( |
| 57 os.path.exists(filepath), |
| 58 'file %s should exist' % filepath) |
| 59 finally: |
| 60 os.rmdir(containing_dir) |
| 61 |
| 62 def test_refspec(self): |
| 63 """Create NewGitCheckout with a specific refspec. |
| 64 |
| 65 This test depends on the fact that the whitespace.txt file was added to the |
| 66 repo in a particular commit. |
| 67 See https://skia.googlesource.com/common/+/c2200447734f13070fb3b2808dea58847
241ab0e |
| 68 ('Initial commit of whitespace.txt') |
| 69 """ |
| 70 filename = 'whitespace.txt' |
| 71 hash_without_file = 'f63e1cfff23615157e28942af5f5e8298351cb10' |
| 72 hash_with_file = 'c2200447734f13070fb3b2808dea58847241ab0e' |
| 73 |
| 74 with git_utils.NewGitCheckout( |
| 75 repository=REPO, refspec=hash_without_file) as checkout: |
| 76 filepath = os.path.join(checkout.root, filename) |
| 77 self.assertEquals( |
| 78 hash_without_file, checkout.commithash(), |
| 79 '"%s" != "%s"' % (hash_without_file, checkout.commithash())) |
| 80 self.assertFalse( |
| 81 os.path.exists(filepath), |
| 82 'file %s should not exist' % filepath) |
| 83 |
| 84 with git_utils.NewGitCheckout( |
| 85 repository=REPO, refspec=hash_with_file) as checkout: |
| 86 filepath = os.path.join(checkout.root, filename) |
| 87 self.assertEquals( |
| 88 hash_with_file, checkout.commithash(), |
| 89 '"%s" != "%s"' % (hash_with_file, checkout.commithash())) |
| 90 self.assertTrue( |
| 91 os.path.exists(filepath), |
| 92 'file %s should exist' % filepath) |
| 93 |
| 94 |
| 95 def main(test_case_class): |
| 96 """Run the unit tests within this class.""" |
| 97 suite = unittest.TestLoader().loadTestsFromTestCase(NewGitCheckoutTest) |
| 98 results = unittest.TextTestRunner(verbosity=2).run(suite) |
| 99 if not results.wasSuccessful(): |
| 100 raise Exception('failed unittest %s' % test_case_class) |
| 101 |
| 102 if __name__ == '__main__': |
| 103 main(NewGitCheckoutTest) |
OLD | NEW |