Skip to content

Commit

Permalink
[MSHARED-1351] Fix console message when origin is baseDir (#93)
Browse files Browse the repository at this point in the history
Print alternative message replacing the relative origin path
by the literal 'base directory'.
  • Loading branch information
abelsromero committed Feb 26, 2024
1 parent b514783 commit 1a51fee
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Resource;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.Scanner;
Expand Down Expand Up @@ -254,9 +255,15 @@ private File getTargetFile(File file) throws MavenFilteringException {
Path destination = getDestinationFile(outputDirectory, targetPath, "", mavenResourcesExecution)
.getAbsoluteFile()
.toPath();
String origin = basedir.relativize(
resourceDirectory.getAbsoluteFile().toPath())
.toString();
if (StringUtils.isEmpty(origin)) {
origin = ".";
}
LOGGER.info("Copying " + includedFiles.size() + " resource" + (includedFiles.size() > 1 ? "s" : "")
+ " from "
+ basedir.relativize(resourceDirectory.getAbsoluteFile().toPath())
+ origin
+ " to "
+ basedir.relativize(destination));
} catch (Exception e) {
Expand Down
88 changes: 88 additions & 0 deletions src/test/java/org/apache/maven/shared/filtering/ConsoleHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.maven.shared.filtering;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
* Helping class to capture console input and output for tests.
*
* @author abelsromero
* @since 3.3.2
*/
class ConsoleHolder {

private PrintStream originalOut;
private PrintStream originalErr;

private ByteArrayOutputStream newOut;
private ByteArrayOutputStream newErr;

private ConsoleHolder() {}

static ConsoleHolder start() {
final ConsoleHolder holder = new ConsoleHolder();

holder.originalOut = System.out;
holder.originalErr = System.err;

holder.newOut = new DoubleOutputStream(holder.originalOut);
holder.newErr = new DoubleOutputStream(holder.originalErr);

System.setOut(new PrintStream(holder.newOut));
System.setErr(new PrintStream(holder.newErr));

return holder;
}

void release() {
System.setOut(originalOut);
System.setOut(originalErr);
}

String getOutput() {
return new String(newOut.toByteArray());
}

String getError() {
return new String(newErr.toByteArray());
}

static class DoubleOutputStream extends ByteArrayOutputStream {

final OutputStream other;

DoubleOutputStream(final OutputStream os) {
other = os;
}

@Override
public synchronized void write(final byte[] b, final int off, final int len) {
try {
other.write(b, off, len);
} catch (IOException e) {
throw new RuntimeException(e);
}
super.write(b, off, len);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,57 @@ void noFiltering() throws Exception {
assertTrue(filesAreIdentical(initialImageFile, imageFile));
}

@Test
void messageWhenCopyingFromSubDirectory() throws Exception {

String subDirectory = "src/test/units-files/maven-resources-filtering";
String unitFilesDir = String.format("%s/%s", getBasedir(), subDirectory);

assertMessage(
unitFilesDir,
"Copying (\\d)+ resources from " + subDirectory + " to target/DefaultMavenResourcesFilteringTest");
}

@Test
void messageWhenCopyingFromBaseDir() throws Exception {

String unitFilesDir = getBasedir();

assertMessage(unitFilesDir, "Copying (\\d)+ resources from . to target/DefaultMavenResourcesFilteringTest");
}

private void assertMessage(String directory, String expectedMessagePattern) throws Exception {
Resource resource = new Resource();
List<Resource> resources = new ArrayList<>();
resources.add(resource);

resource.setDirectory(directory);
resource.setFiltering(false);

MavenResourcesExecution mre = new MavenResourcesExecution();
mre.setResources(resources);
mre.setOutputDirectory(outputDirectory);
mre.setEncoding("UTF-8");
mre.setMavenProject(mavenProject);
mre.setFilters(null);
mre.setNonFilteredFileExtensions(Collections.emptyList());
mre.setMavenSession(new StubMavenSession());

ConsoleHolder console = ConsoleHolder.start();

mavenResourcesFiltering.filterResources(mre);

String output = console.getError();
String marker = DefaultMavenResourcesFiltering.class.getSimpleName();
String message = output.substring(output.indexOf(marker) + marker.length() + 3)
.trim()
.replaceAll("\\\\", "/");

boolean matches = message.matches(expectedMessagePattern);
assertTrue(matches, "expected: '" + expectedMessagePattern + "' does not match actual: '" + message + "'");
console.release();
}

private static boolean filesAreIdentical(File expected, File current) throws IOException {
if (expected.length() != current.length()) {
return false;
Expand Down

0 comments on commit 1a51fee

Please sign in to comment.