Messages sent from the server to the client should not end with a \r on windows #304

This commit is contained in:
Peter Palaga
2021-01-06 23:16:02 +01:00
parent 2f7e4fdd4c
commit 5d346acda9
2 changed files with 41 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Pattern;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
@@ -35,6 +36,7 @@ import org.mvndaemon.mvnd.logging.smart.BuildEventListener;
*/
public class ClientDispatcher extends BuildEventListener {
private final Collection<Message> queue;
private static final Pattern TRAILING_EOLS_PATTERN = Pattern.compile("[\r\n]+$");
public ClientDispatcher(Collection<Message> queue) {
this.queue = queue;
@@ -78,8 +80,7 @@ public class ClientDispatcher extends BuildEventListener {
}
public void projectLogMessage(String projectId, String event) {
String msg = event.endsWith("\n") ? event.substring(0, event.length() - 1) : event;
queue.add(projectId == null ? Message.log(msg) : Message.log(projectId, msg));
queue.add(projectId == null ? Message.log(trimTrailingEols(event)) : Message.log(projectId, trimTrailingEols(event)));
}
public void projectFinished(ExecutionEvent event) {
@@ -108,7 +109,7 @@ public class ClientDispatcher extends BuildEventListener {
}
public void log(String msg) {
queue.add(Message.log(msg));
queue.add(Message.log(trimTrailingEols(msg)));
}
private MavenProject getCurrentProject(MavenSession mavenSession) {
@@ -124,4 +125,8 @@ public class ClientDispatcher extends BuildEventListener {
.orElse(mavenSession.getCurrentProject());
}
static String trimTrailingEols(String message) {
return message == null ? null : TRAILING_EOLS_PATTERN.matcher(message).replaceFirst("");
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.daemon;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ClientDispatcherTest {
@Test
void trimTrailingEols() {
Assertions.assertEquals(null, ClientDispatcher.trimTrailingEols(null));
Assertions.assertEquals("foo", ClientDispatcher.trimTrailingEols("foo"));
Assertions.assertEquals("foo\nbar", ClientDispatcher.trimTrailingEols("foo\nbar"));
Assertions.assertEquals("foo\nbar", ClientDispatcher.trimTrailingEols("foo\nbar\n"));
Assertions.assertEquals("foo\nbar", ClientDispatcher.trimTrailingEols("foo\nbar\r\n"));
Assertions.assertEquals("foo\nbar", ClientDispatcher.trimTrailingEols("foo\nbar\n\r\n"));
Assertions.assertEquals("", ClientDispatcher.trimTrailingEols("\n"));
}
}