Support writing CodeOwnerConfigs to find-owners syntax This change adds a new 'format' method to CodeOwnerConfigParser that allows to format a CodeOwnerConfig as string that represents the code owner config in an 'OWNER' file. This method can be used to create code owner configuration files programmatically, which we will need to do during integration tests. The tests in CodeOwnerConfigParserTest are rewritten so that they do: 1. parse a given code owner config 2. assert the parsed code owner config 3. format the parsed code owner config as string 4. assert the formatted string Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: Id9e48e680bc1eb37f7ad2e9e7edce97dca38381b
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java b/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java index 45cec8f..044b773 100644 --- a/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java +++ b/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java
@@ -14,10 +14,13 @@ package com.google.gerrit.plugins.codeowners.backend.findowners; +import static java.util.stream.Collectors.joining; + import com.google.common.base.Splitter; import com.google.common.collect.Streams; import com.google.common.flogger.FluentLogger; import com.google.gerrit.plugins.codeowners.backend.CodeOwnerConfig; +import com.google.gerrit.plugins.codeowners.backend.CodeOwnerReference; import com.google.gerrit.server.mail.send.OutgoingEmailValidator; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -66,6 +69,12 @@ * * <p>Comment lines, invalid lines and invalid emails are silently ignored. * + * <p><strong>Note:</strong> Parsing a code owner config by using the {@link + * #parse(CodeOwnerConfig.Key, String)} and then formatting the parsed code owner config back to a + * string by using {@link #formatAsString(CodeOwnerConfig)} is not guaranteed to result in the + * exact same code owner config file (e.g. comment lines, invalid lines and invalid emails are + * dropped and emails may be reordered). + * * @param codeOwnerConfigKey the key of the code owner config that should be parsed * @param codeOwnerConfigFileContent string that represents the content of an {@code OWNERS} file * @return the parsed {@link CodeOwnerConfig} @@ -83,6 +92,22 @@ return codeOwnerConfig.build(); } + /** + * Formats the given code owner config as string that represents the code owner config in an + * {@code OWNERS} file. + * + * @param codeOwnerConfig the code owner config that should be formatted + * @return the code owner config as string that represents the code owner config in an {@code + * OWNERS} file + */ + String formatAsString(CodeOwnerConfig codeOwnerConfig) { + return codeOwnerConfig.codeOwners().stream() + .map(CodeOwnerReference::email) + .sorted() + .distinct() + .collect(joining("\n")); + } + private static boolean isComment(String trimmedLine) { return trimmedLine.startsWith("#"); }
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java index cfaecaa..2d2a02f 100644 --- a/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java +++ b/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java
@@ -14,6 +14,7 @@ package com.google.gerrit.plugins.codeowners.backend.findowners; +import static com.google.common.truth.Truth.assertThat; import static com.google.gerrit.plugins.codeowners.testing.CodeOwnerConfigSubject.assertThat; import com.google.gerrit.acceptance.LightweightPluginDaemonTest; @@ -26,6 +27,12 @@ /** Tests for {@link CodeOwnerConfigParser}. */ @TestPlugin(name = "code-owners", sysModule = "com.google.gerrit.plugins.codeowners.Module") public class CodeOwnerConfigParserTest extends LightweightPluginDaemonTest { + /** Callback interface that allows to assert a code owner config. */ + @FunctionalInterface + interface CodeOwnerConfigAsserter { + void doAssert(CodeOwnerConfig codeOwnerConfig); + } + // Emails for testing, sorted alphabetically. private static final String EMAIL_1 = "admin@test.com"; private static final String EMAIL_2 = "jdoe@test.com"; @@ -40,106 +47,161 @@ @Test public void emptyCodeOwnerConfig() throws Exception { - CodeOwnerConfig codeOwnerConfig = parse(""); - assertThat(codeOwnerConfig).hasCodeOwnersThat().isEmpty(); + assertParseAndFormat( + "", codeOwnerConfig -> assertThat(codeOwnerConfig).hasCodeOwnersThat().isEmpty()); } @Test public void codeOwnerConfigWithOneEmail() throws Exception { - CodeOwnerConfig codeOwnerConfig = parse(EMAIL_1); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1); + assertParseAndFormat( + EMAIL_1, + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1)); } @Test public void codeOwnerConfigWithMultipleEmails() throws Exception { - CodeOwnerConfig codeOwnerConfig = parse(String.format("%s\n%s\n%s", EMAIL_1, EMAIL_2, EMAIL_3)); - assertThat(codeOwnerConfig) - .hasCodeOwnersEmailsThat() - .containsExactly(EMAIL_1, EMAIL_2, EMAIL_3); + assertParseAndFormat( + String.format("%s\n%s\n%s", EMAIL_1, EMAIL_2, EMAIL_3), + codeOwnerConfig -> + assertThat(codeOwnerConfig) + .hasCodeOwnersEmailsThat() + .containsExactly(EMAIL_1, EMAIL_2, EMAIL_3)); } @Test public void codeOwnerConfigWithTrailingLineBreak() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("%s\n%s\n%s\n", EMAIL_1, EMAIL_2, EMAIL_3)); - assertThat(codeOwnerConfig) - .hasCodeOwnersEmailsThat() - .containsExactly(EMAIL_1, EMAIL_2, EMAIL_3); + assertParseAndFormat( + String.format("%s\n%s\n%s\n", EMAIL_1, EMAIL_2, EMAIL_3), + codeOwnerConfig -> + assertThat(codeOwnerConfig) + .hasCodeOwnersEmailsThat() + .containsExactly(EMAIL_1, EMAIL_2, EMAIL_3), + String.format("%s\n%s\n%s", EMAIL_1, EMAIL_2, EMAIL_3)); } @Test public void codeOwnerConfigWithWindowsLineBreaks() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("%s\r\n%s\r\n%s", EMAIL_1, EMAIL_2, EMAIL_3)); - assertThat(codeOwnerConfig) - .hasCodeOwnersEmailsThat() - .containsExactly(EMAIL_1, EMAIL_2, EMAIL_3); + assertParseAndFormat( + String.format("%s\r\n%s\r\n%s", EMAIL_1, EMAIL_2, EMAIL_3), + codeOwnerConfig -> + assertThat(codeOwnerConfig) + .hasCodeOwnersEmailsThat() + .containsExactly(EMAIL_1, EMAIL_2, EMAIL_3), + String.format("%s\n%s\n%s", EMAIL_1, EMAIL_2, EMAIL_3)); } @Test public void codeOwnerConfigWithEmptyLines() throws Exception { - CodeOwnerConfig codeOwnerConfig = parse(String.format("\n%s\n\n%s", EMAIL_1, EMAIL_2)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("\n%s\n\n%s", EMAIL_1, EMAIL_2), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithWhitespace() throws Exception { - CodeOwnerConfig codeOwnerConfig = parse(String.format("\t %s\t \n \t%s\t ", EMAIL_1, EMAIL_2)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("\t %s\t \n \t%s\t ", EMAIL_1, EMAIL_2), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithDuplicateEmails() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("%s\n%s\n%s\n%s", EMAIL_1, EMAIL_1, EMAIL_2, EMAIL_1)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("%s\n%s\n%s\n%s", EMAIL_1, EMAIL_1, EMAIL_2, EMAIL_1), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithNonSortedEmails() throws Exception { - CodeOwnerConfig codeOwnerConfig = parse(String.format("%s\n%s", EMAIL_2, EMAIL_1)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("%s\n%s", EMAIL_2, EMAIL_1), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithInvalidEmails_InvalidEmailsAreIgnored() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("%s\n@test.com\nadmin@\nadmin@test@com\n%s", EMAIL_1, EMAIL_2)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("%s\n@test.com\nadmin@\nadmin@test@com\n%s", EMAIL_1, EMAIL_2), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithInvalidLines_InvalidLinesAreIgnored() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("%s\nINVALID\nNOT_AN_EMAIL\n%s", EMAIL_1, EMAIL_2)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("%s\nINVALID\nNOT_AN_EMAIL\n%s", EMAIL_1, EMAIL_2), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithCommentLines_CommentLinesAreIgnored() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("# a@test.com\n%s\n # b@test.com\n%s\n#c@test.com", EMAIL_1, EMAIL_2)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("# a@test.com\n%s\n # b@test.com\n%s\n#c@test.com", EMAIL_1, EMAIL_2), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } @Test public void codeOwnerConfigWithInlineComments_LinesWithInlineCommentsAreConsideredAsInvalidAndAreIgnored() throws Exception { - CodeOwnerConfig codeOwnerConfig = - parse(String.format("foo.bar@test.com # Foo Bar\n%s\n%s", EMAIL_1, EMAIL_2)); - assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + assertParseAndFormat( + String.format("foo.bar@test.com # Foo Bar\n%s\n%s", EMAIL_1, EMAIL_2), + codeOwnerConfig -> + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2), + String.format("%s\n%s", EMAIL_1, EMAIL_2)); } /** - * Parses the given code owner config. + * Parses the given code owner config, asserts the parsed code owner config, formats the parsed + * code owner configuration back into a string and asserts the result of the formatting. + * + * @param codeOwnerConfig the code owner config that should be parsed and that is expected as + * result of formatting the parsed code owner config + * @param codeOwnerConfigAsserter asserter that asserts the parsed code owner config + */ + private void assertParseAndFormat( + String codeOwnerConfig, CodeOwnerConfigAsserter codeOwnerConfigAsserter) { + assertParseAndFormat(codeOwnerConfig, codeOwnerConfigAsserter, codeOwnerConfig); + } + + /** + * Parses the given code owner config, asserts the parsed code owner config, formats the parsed + * code owner configuration back into a string and asserts the result of the formatting. * * @param codeOwnerConfig the code owner config that should be parsed - * @return the parsed code owner config + * @param codeOwnerConfigAsserter asserter that asserts the parsed code owner config + * @param expectedConfig the code owner config that is expected as result of formatting the parsed + * code owner config */ - private CodeOwnerConfig parse(String codeOwnerConfig) { + private void assertParseAndFormat( + String codeOwnerConfig, + CodeOwnerConfigAsserter codeOwnerConfigAsserter, + String expectedConfig) { + // Parse the provided code owner config. CodeOwnerConfig.Key codeOwnerConfigKey = CodeOwnerConfig.Key.create(Project.nameKey("project"), "master", "/"); - return codeOwnerConfigParser.parse(codeOwnerConfigKey, codeOwnerConfig); + CodeOwnerConfig parsedCodeOwnerConfig = + codeOwnerConfigParser.parse(codeOwnerConfigKey, codeOwnerConfig); + + // Assert the parsed code owner config. + codeOwnerConfigAsserter.doAssert(parsedCodeOwnerConfig); + + // Format the parsed code owner config and assert the formatted code owner config. + assertThat(codeOwnerConfigParser.formatAsString(parsedCodeOwnerConfig)) + .isEqualTo(expectedConfig); } }