GradleBuildOutputParser implements BuildOutputParser { @Override public boolean parse(@NotNull String line, @NotNull BuildOutputInstantReader reader, @NotNull Consumer<? super BuildEvent> messageConsumer) { String currentLine = line.trim(); if (currentLine.startsWith(STDOUT_ERROR_TAG)) { processMessage(currentLine, reader.getParentEventId(), messageConsumer); return true; } if (futureOutputMap.getOrDefault(reader.getParentEventId(), Collections.emptySet()).contains(currentLine)) { futureOutputMap.get(reader.getParentEventId()).remove(currentLine); return true; } return false; } GradleBuildOutputParser(); @Override boolean parse(@NotNull String line,
                       @NotNull BuildOutputInstantReader reader,
                       @NotNull Consumer<? super BuildEvent> messageConsumer);  }
@Test public void parseWithError() { String line = "AGPBI: {\"kind\":\"error\",\"text\":\"Error message.\",\"sources\":[{\"file\":\"/app/src/main/res/layout/activity_main.xml\",\"position\":{\"startLine\":10,\"startColumn\":31,\"startOffset\":456,\"endColumn\":44,\"endOffset\":469}}],\"tool\":\"AAPT\"}"; when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK"); ArgumentCaptor<MessageEvent> messageCaptor = ArgumentCaptor.forClass(MessageEvent.class); String detailLine = "This is a detail line"; assertTrue(myParser.parse(line, myReader, myConsumer)); assertFalse(myParser.parse(detailLine, myReader, myConsumer)); verify(myConsumer).accept(messageCaptor.capture()); List<MessageEvent> generatedMessages = messageCaptor.getAllValues(); assertThat(generatedMessages).hasSize(1); assertThat(generatedMessages.get(0)).isInstanceOf(FileMessageEvent.class); FileMessageEvent fileMessageEvent = (FileMessageEvent)generatedMessages.get(0); assertThat(fileMessageEvent.getGroup()).isEqualTo("AAPT errors"); assertThat(fileMessageEvent.getKind()).isEqualTo(MessageEvent.Kind.ERROR); assertThat(fileMessageEvent.getResult().getDetails()).isEqualTo("Error message."); }
@Test public void parseWithWarningNoSource() { String line = "AGPBI: {\"kind\":\"warning\",\"text\":\"Warning message.\",\"sources\":[{}],\"tool\":\"D8\"}"; when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK"); ArgumentCaptor<MessageEvent> messageCaptor = ArgumentCaptor.forClass(MessageEvent.class); String detailLine = "This is a detail line"; assertTrue(myParser.parse(line, myReader, myConsumer)); assertFalse(myParser.parse(detailLine, myReader, myConsumer)); verify(myConsumer).accept(messageCaptor.capture()); List<MessageEvent> generatedMessages = messageCaptor.getAllValues(); assertThat(generatedMessages).hasSize(1); assertThat(generatedMessages.get(0)).isNotInstanceOf(FileMessageEvent.class); MessageEvent messageEvent = generatedMessages.get(0); assertThat(messageEvent.getGroup()).isEqualTo("D8 warnings"); assertThat(messageEvent.getKind()).isEqualTo(MessageEvent.Kind.WARNING); assertThat(messageEvent.getResult().getDetails()).isEqualTo("Warning message."); }
@Test public void parseWithoutError() { String line = "Non AGBPI error"; assertFalse(myParser.parse(line, myReader, myConsumer)); }
@Test public void parseWithErrorIgnoreOutput() { String line = "AGPBI: {\"kind\":\"error\",\"text\":\"Error message.\",\"sources\":[{}],\"original\":\"Error line 1\\nError line 2\\nError line 3\",\"tool\":\"Dex\"}"; when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK"); ArgumentCaptor<MessageEvent> messageCaptor = ArgumentCaptor.forClass(MessageEvent.class); String[] detailLines = new String[5]; detailLines[0] = "Error line 1"; detailLines[1] = "Error line 2"; detailLines[2] = "Error line 3"; detailLines[3] = "Unrelated output"; detailLines[4] = "Error line 1"; assertTrue(myParser.parse(line, myReader, myConsumer)); assertTrue(myParser.parse(detailLines[0], myReader, myConsumer)); assertTrue(myParser.parse(detailLines[1], myReader, myConsumer)); assertTrue(myParser.parse(detailLines[2], myReader, myConsumer)); assertFalse(myParser.parse(detailLines[3], myReader, myConsumer)); assertFalse(myParser.parse(detailLines[4], myReader, myConsumer)); verify(myConsumer).accept(messageCaptor.capture()); List<MessageEvent> generatedMessages = messageCaptor.getAllValues(); assertThat(generatedMessages).hasSize(1); assertThat(generatedMessages.get(0)).isNotInstanceOf(FileMessageEvent.class); MessageEvent messageEvent = generatedMessages.get(0); assertThat(messageEvent.getGroup()).isEqualTo("Dex errors"); assertThat(messageEvent.getKind()).isEqualTo(MessageEvent.Kind.ERROR); assertThat(messageEvent.getResult().getDetails()).isEqualTo("Error line 1\nError line 2\nError line 3"); }
@Test public void parseChangeBuildId() { String line = "AGPBI: {\"kind\":\"error\",\"text\":\"Error message.\",\"sources\":[{}],\"original\":\"Error line 1\\nError line 2\\nError line 3\",\"tool\":\"Dex\"}"; when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK_1"); String[] detailLines = new String[4]; detailLines[0] = "Error line 1"; detailLines[1] = "Error line 2"; detailLines[2] = "Error line 3"; detailLines[3] = "Unrelated output"; assertTrue(myParser.parse(line, myReader, myConsumer)); when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK_2"); assertFalse(myParser.parse(detailLines[0], myReader, myConsumer)); when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK_3"); assertFalse(myParser.parse(detailLines[1], myReader, myConsumer)); when(myReader.getParentEventId()).thenReturn("BUILD_ID_MOCK_1"); assertTrue(myParser.parse(detailLines[2], myReader, myConsumer)); assertFalse(myParser.parse(detailLines[3], myReader, myConsumer)); }