Add tests for revisions/current/test.submit_rule and fix subtle bug

This commit adds two tests for verifying that the input provided to
test.submit_rule makes it's way into RulesEvaluator and returns a
meaningful response.

This commit fixes a subtle bug that would make it so that we don't pass
the input to the evaluator if the project has not set up any Prolog rule
before.

Change-Id: I25445abccb02a72995abc289d169699fac2536ba
This commit is contained in:
Patrick Hiesel 2018-10-15 11:53:23 +02:00
parent 24983a0448
commit 4cec171465
3 changed files with 76 additions and 2 deletions

View File

@ -14,7 +14,9 @@
package com.google.gerrit.extensions.common;
import com.google.common.base.MoreObjects;
import java.util.Map;
import java.util.Objects;
public class TestSubmitRuleInfo {
/** @see com.google.gerrit.common.data.SubmitRecord.Status */
@ -32,4 +34,37 @@ public class TestSubmitRuleInfo {
public static None INSTANCE = new None();
}
@Override
public boolean equals(Object o) {
if (o instanceof TestSubmitRuleInfo) {
TestSubmitRuleInfo other = (TestSubmitRuleInfo) o;
return Objects.equals(status, other.status)
&& Objects.equals(errorMessage, other.errorMessage)
&& Objects.equals(ok, other.ok)
&& Objects.equals(reject, other.reject)
&& Objects.equals(need, other.need)
&& Objects.equals(may, other.may)
&& Objects.equals(impossible, other.impossible);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(status, errorMessage, ok, reject, need, may, impossible);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("status", status)
.add("errorMessage", errorMessage)
.add("ok", ok)
.add("reject", reject)
.add("need", need)
.add("may", may)
.add("impossible", impossible)
.toString();
}
}

View File

@ -39,8 +39,8 @@ public class PrologRule implements SubmitRule {
@Override
public Collection<SubmitRecord> evaluate(ChangeData cd, SubmitRuleOptions opts) {
ProjectState projectState = projectCache.get(cd.project());
// We only want to run the prolog engine if we have at least one rules.pl file to use.
if (projectState == null || !projectState.hasPrologRules()) {
// We only want to run the Prolog engine if we have at least one rules.pl file to use.
if ((projectState == null || !projectState.hasPrologRules()) && opts.rule() == null) {
return Collections.emptyList();
}

View File

@ -23,12 +23,14 @@ import static com.google.gerrit.extensions.client.SubmitType.REBASE_ALWAYS;
import static com.google.gerrit.extensions.client.SubmitType.REBASE_IF_NECESSARY;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.extensions.common.TestSubmitRuleInfo;
import com.google.gerrit.extensions.common.TestSubmitRuleInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException;
@ -255,6 +257,43 @@ public class SubmitTypeRuleIT extends AbstractDaemonTest {
}
}
@Test
public void invalidSubmitRuleWithNoRulesInProject() throws Exception {
String changeId = createChange("master", "change 1").getChangeId();
TestSubmitRuleInput in = new TestSubmitRuleInput();
in.rule = "invalid prolog rule";
// We have no rules.pl by default. The fact that the default rules are showing up here is a bug.
List<TestSubmitRuleInfo> response = gApi.changes().id(changeId).current().testSubmitRule(in);
assertThat(response).containsExactly(defaultUnsatisfiedRuleInfo(), invalidPrologRuleInfo());
}
@Test
public void invalidSubmitRuleWithRulesInProject() throws Exception {
setRulesPl(SUBMIT_TYPE_FROM_SUBJECT);
String changeId = createChange("master", "change 1").getChangeId();
TestSubmitRuleInput in = new TestSubmitRuleInput();
in.rule = "invalid prolog rule";
List<TestSubmitRuleInfo> response = gApi.changes().id(changeId).current().testSubmitRule(in);
assertThat(response).containsExactly(invalidPrologRuleInfo());
}
private static TestSubmitRuleInfo invalidPrologRuleInfo() {
TestSubmitRuleInfo info = new TestSubmitRuleInfo();
info.status = "RULE_ERROR";
info.errorMessage = "operator expected after expression at: invalid prolog rule end_of_file.";
return info;
}
private static TestSubmitRuleInfo defaultUnsatisfiedRuleInfo() {
TestSubmitRuleInfo info = new TestSubmitRuleInfo();
info.status = "NOT_READY";
info.need = ImmutableMap.of("Code-Review", TestSubmitRuleInfo.None.INSTANCE);
return info;
}
private List<RevCommit> log(String commitish, int n) throws Exception {
try (Repository repo = repoManager.openRepository(project);
Git git = new Git(repo)) {