Merge ~twom/launchpad:git-branch-picker-oops-with-no-repository into launchpad:master

Proposed by Tom Wardill
Status: Merged
Approved by: Tom Wardill
Approved revision: 00f9a6f8dda52c7de64a993a16b542ff4333c438
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~twom/launchpad:git-branch-picker-oops-with-no-repository
Merge into: launchpad:master
Diff against target: 66 lines (+24/-7)
2 files modified
lib/lp/code/vocabularies/gitref.py (+6/-7)
lib/lp/code/vocabularies/tests/test_gitref_vocabularies.py (+18/-0)
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
Review via email: mp+396678@code.launchpad.net

Commit message

Fix OOPS with gitref widget form validation and missing repositories

Description of the change

On entering a repository that doesn't exist, the validation on gitref widget will fail to set the repository details on the vocabulary.
This seems like reasonable behaviour, given that the repository doesn't exist.

In this case, we should return no results from the vocabulary, rather than an AssertionError.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/code/vocabularies/gitref.py b/lib/lp/code/vocabularies/gitref.py
2index 7d98208..3c2bf4a 100644
3--- a/lib/lp/code/vocabularies/gitref.py
4+++ b/lib/lp/code/vocabularies/gitref.py
5@@ -86,11 +86,8 @@ class GitRefVocabulary(StormVocabularyBase):
6 self.repository = None
7 self.repository_url = repository_url
8
9- def _assertHasRepository(self):
10- if self.repository is None and self.repository_url is None:
11- raise AssertionError(
12- "GitRefVocabulary cannot be used without setting a "
13- "repository or a repository URL.")
14+ def _checkHasRepository(self):
15+ return not (self.repository is None and self.repository_url is None)
16
17 @property
18 def _order_by(self):
19@@ -108,7 +105,8 @@ class GitRefVocabulary(StormVocabularyBase):
20
21 def getTermByToken(self, token):
22 """See `IVocabularyTokenized`."""
23- self._assertHasRepository()
24+ if not self._checkHasRepository():
25+ raise LookupError(token)
26 if self.repository is not None:
27 ref = self.repository.getRefByPath(token)
28 if ref is None:
29@@ -125,7 +123,8 @@ class GitRefVocabulary(StormVocabularyBase):
30
31 def searchForTerms(self, query=None, vocab_filter=None):
32 """See `IHugeVocabulary."""
33- self._assertHasRepository()
34+ if not self._checkHasRepository():
35+ return CountableIterator(0, [], self.toTerm)
36 if self.repository is not None:
37 pattern = self._makePattern(query=query)
38 results = IStore(self._table).find(
39diff --git a/lib/lp/code/vocabularies/tests/test_gitref_vocabularies.py b/lib/lp/code/vocabularies/tests/test_gitref_vocabularies.py
40index 41f5f83..9edbff9 100644
41--- a/lib/lp/code/vocabularies/tests/test_gitref_vocabularies.py
42+++ b/lib/lp/code/vocabularies/tests/test_gitref_vocabularies.py
43@@ -118,6 +118,24 @@ class TestGitRefVocabulary(TestCaseWithFactory):
44 "refs/heads/next-squared", "refs/tags/next-1.0"])
45 self.assertEqual(4, len(self.vocabulary_class(ref_master.repository)))
46
47+ def test_no_term_with_no_repository(self):
48+ # Form validation can mean that we don't always have a repository
49+ # available before a widget is rendered, which needs to not error
50+ vocab = self.vocabulary_class(
51+ self.factory.makeSnap(branch=self.factory.makeAnyBranch()))
52+ [ref] = self.factory.makeGitRefs()
53+ term = SimpleTerm(ref, ref.name, ref.name)
54+ self.assertRaises(
55+ LookupError,
56+ vocab.getTermByToken,
57+ term.token)
58+
59+ def test_no_results_with_no_repository(self):
60+ vocab = self.vocabulary_class(
61+ self.factory.makeSnap(branch=self.factory.makeAnyBranch()))
62+ results = vocab.searchForTerms("master")
63+ self.assertEqual(results.count(), 0)
64+
65
66 class TestGitBranchVocabulary(TestCaseWithFactory):
67

Subscribers

People subscribed via source and target branches

to status/vote changes: