Fix internal errors when 'destination:' refers to non-existing destination

When querying with the destination: predicate using a destination name
that does not exist, it is supposed to return an error:

  "Unknown named destination"

However it was not, because of two problems:

- When the revision is null, VersionedAccountDestinations's call to
  getPathInfos results in NPE due to dereferencing the revision.

- When no destinations exist, DestinationList.getDestinations returns
  an empty list, but the ChangeQueryBuilder is only testing for null

Fix both of the bugs and add a test that querying "destination:" with
a non-existing destination results in the correct response.

Adding tests for existing destinations is not within the scope of this
change.

Change-Id: I7787a90a508f61ef19586f327c6be427a0870e69
This commit is contained in:
David Pursehouse
2018-04-16 16:35:27 +02:00
parent a32cb27478
commit ace569abb1
3 changed files with 14 additions and 1 deletions

View File

@@ -53,6 +53,9 @@ public class VersionedAccountDestinations extends VersionedMetaData {
@Override
protected void onLoad() throws IOException, ConfigInvalidException {
if (revision == null) {
return;
}
String prefix = DestinationList.DIR_NAME + "/";
for (PathInfo p : getPathInfos(true)) {
if (p.fileMode == FileMode.REGULAR_FILE) {

View File

@@ -1047,7 +1047,7 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
VersionedAccountDestinations d = VersionedAccountDestinations.forUser(self());
d.load(git);
Set<Branch.NameKey> destinations = d.getDestinationList().getDestinations(name);
if (destinations != null) {
if (destinations != null && !destinations.isEmpty()) {
return new DestinationPredicate(destinations, name);
}
} catch (RepositoryNotFoundException e) {

View File

@@ -2105,6 +2105,16 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
assertQuery("-assignee:" + user.getUserName(), change2);
}
@Test
public void userDestination() throws Exception {
TestRepository<Repo> repo = createProject("repo");
insert(repo, newChange(repo));
assertThatQueryException("destination:foo")
.hasMessageThat()
.isEqualTo("Unknown named destination: foo");
}
protected ChangeInserter newChange(TestRepository<Repo> repo) throws Exception {
return newChange(repo, null, null, null, null);
}