[PATCH 1/3] Fix CachedRepo.resolve_ref unit test for missing SHA1

Lars Wirzenius lars.wirzenius at codethink.co.uk
Thu Oct 24 16:29:41 BST 2013

CachedRepo.resolve_ref does, effectively, this:

	absref = git rev-parse --verify $ref
	git log -1 --format=format:%T $absref

Roughly, "git rev-parse" takes any ref and returns the corresponding
SHA1. If the ref looks like a SHA1 (40 hex digits), it is returned
as-is, and despite --verify is not checked for existence.

"git log" then takes the SHA1 and returns the **tree** SHA1, as
opposed to the commit one, and if the commit doesn't exist, barfs.

The unit test for resolve_ref with an invalid SHA1 currently succeeds
for the wrong reason. The mocked _rev_parse fails for an unknown SHA1
(raising cliapp.AppException), which causes resolve_ref to raise
InvalidReferenceError, which the unit test expects. However, the real
implementation of _rev_parse wouldn't fail in that way, and so the
unit test doesn't test the thing it's meant to test: that resolve_ref
actually works the expected way for an unknown SHA1. What actually
happens is that resolve_ref calls _show_tree_hash, which raises
cliapp.AppException for an unknown SHA1, resulting in horror and
despair, instead of resolve_ref raising InvalidReferenceError.

This commit fixes the unit test so that it causes the right code
path in resolve_ref to be executed. This makes the unit test suite
to fail.
 morphlib/cachedrepo_tests.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/morphlib/cachedrepo_tests.py b/morphlib/cachedrepo_tests.py
index 18704f5..b1825eb 100644
--- a/morphlib/cachedrepo_tests.py
+++ b/morphlib/cachedrepo_tests.py
@@ -32,8 +32,11 @@ class CachedRepoTests(unittest.TestCase):
         "kind": "chunk"
+    bad_sha1_known_to_rev_parse = 'cafecafecafecafecafecafecafecafecafecafe'
     def rev_parse(self, ref):
         output = {
+            self.bad_sha1_known_to_rev_parse: self.bad_sha1_known_to_rev_parse,
@@ -159,7 +162,7 @@ class CachedRepoTests(unittest.TestCase):
     def test_fail_resolving_an_invalid_sha1_ref(self):
-                          '079bbfd447c8534e464ce5d40b80114c2022ebf4')
+                          self.bad_sha1_known_to_rev_parse)
     def test_cat_existing_file_in_existing_ref(self):
         data = self.repo.cat('e28a23812eadf2fce6583b8819b9c5dbd36b9fb9',

More information about the baserock-dev mailing list