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