Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use canonical base directories to fix #138. #185

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ public void checkUnknown() throws MojoExecutionException {
public final void execute(final Callback callback) throws MojoExecutionException, MojoFailureException {
if (!skip) {

// make default base dir canonical
this.defaultBasedir = this.getCanonicalFile(this.defaultBasedir, "license.basedir");

// collect all the license sets together
final LicenseSet[] allLicenseSets;

Expand All @@ -388,6 +391,17 @@ public final void execute(final Callback callback) throws MojoExecutionException
}
}

private File getCanonicalFile(final File file, final String description) throws MojoFailureException {
if (file == null) {
return null;
}
try {
return file.getCanonicalFile();
} catch (final IOException e) {
throw new MojoFailureException("Could not get canonical path of " + description, e);
}
}

private void executeForLicenseSets(final LicenseSet[] licenseSets, final Callback callback) throws MojoFailureException, MojoExecutionException {
if (licenseSets == null || licenseSets.length == 0) {
warn("At least one licenseSet must be specified");
Expand All @@ -401,6 +415,8 @@ private void executeForLicenseSets(final LicenseSet[] licenseSets, final Callbac
warn("No header file specified to check for license in licenseSet: " + i);
return;
}
// make licenseSet baseDir canonical
licenseSet.basedir = this.getCanonicalFile(licenseSet.basedir, "licenseSet[" + i + "].basedir");
}
if (!strictCheck) {
warn("Property 'strictCheck' is not enabled. Please consider adding <strictCheck>true</strictCheck> in your pom.xml file.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package com.mycila.maven.plugin.license;

import com.mycila.maven.plugin.license.LicenseSet;
import org.apache.maven.monitor.logging.DefaultLog;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertTrue;

public class LicenseSetTest {

@Test
Expand All @@ -32,16 +36,71 @@ public void multipleLicenseSets() throws Exception {
licenseSet2.basedir = new File("src/test/resources/check/issue76");
licenseSet2.header = "src/test/resources/test-header1.txt";

final LicenseSet licenseSetWithoutBaseDir = new LicenseSet();
licenseSetWithoutBaseDir.header = "test-header1.txt";

final LicenseSet[] licenseSets = {
licenseSet1,
licenseSet2,
licenseSetWithoutBaseDir
};

final LicenseCheckMojo check = new LicenseCheckMojo();
check.licenseSets = licenseSets;
check.project = new MavenProjectStub();
check.strictCheck = false;
check.defaultBasedir = new File("src/test/resources/unknown");
final MockedLog logger = new MockedLog();
check.setLog(new DefaultLog(logger));
check.execute();

final String log = logger.getContent();
final String fileFromFirstSet = new File("src/test/resources/check/strict/space.java").getCanonicalPath().replace('\\', '/');
final String fileFromSecondSet = new File("src/test/resources/check/issue76/after.xml").getCanonicalPath().replace('\\', '/');
final String fileFromDefaultBaseDirSet = new File("src/test/resources/unknown/header.txt").getCanonicalPath().replace('\\', '/');

assertTrue(log.contains("Header OK in: " + fileFromFirstSet));
assertTrue(log.contains("Header OK in: " + fileFromSecondSet));
assertTrue(log.contains("Header OK in: " + fileFromDefaultBaseDirSet));
}

@Test
public void multipleLicenseSetsWithRelativePaths() throws Exception {
final LicenseSet licenseSet1 = new LicenseSet();
licenseSet1.basedir = new File("src/test/resources/check/def/../strict");
licenseSet1.header = "src/test/resources/test-header1-diff.txt";

final LicenseSet licenseSet2 = new LicenseSet();
licenseSet2.basedir = new File("src/test/resources/check/def/../issue76");
licenseSet2.header = "src/test/resources/test-header1.txt";

final LicenseSet licenseSetWithoutBaseDir = new LicenseSet();
licenseSetWithoutBaseDir.header = "test-header1.txt";

final LicenseSet[] licenseSets = {
licenseSet1,
licenseSet2
licenseSet2,
licenseSetWithoutBaseDir
};

final LicenseCheckMojo check = new LicenseCheckMojo();
check.licenseSets = licenseSets;
check.project = new MavenProjectStub();
check.strictCheck = false;
check.defaultBasedir = new File("src/test/resources/unknown/../unknown");
final MockedLog logger = new MockedLog();
check.setLog(new DefaultLog(logger));
check.execute();

final String log = logger.getContent();
final String fileFromFirstSet = new File("src/test/resources/check/strict/space.java").getCanonicalPath().replace('\\', '/');
final String fileFromSecondSet = new File("src/test/resources/check/issue76/after.xml").getCanonicalPath().replace('\\', '/');
final String fileFromDefaultBaseDirSet = new File("src/test/resources/unknown/header.txt").getCanonicalPath().replace('\\', '/');

assertTrue(log.contains("Header OK in: " + fileFromFirstSet));
assertTrue(log.contains("Header OK in: " + fileFromSecondSet));
assertTrue(log.contains("Header OK in: " + fileFromDefaultBaseDirSet));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public void test_not_useDefaultMapping() throws Exception {
check.execute();
assertFalse(logger.getContent().contains("header style: text"));
assertTrue(logger.getContent().contains("header style: unknown"));
String absoluteFileName = new File("src/test/resources/check/doc1.txt").getCanonicalPath().replace('\\', '/');
assertTrue(logger.getContent().contains("Unknown file extension: " + absoluteFileName));
}

@Test
Expand All @@ -61,7 +63,8 @@ public void test_useDefaultMapping() throws Exception {
fail();
} catch (MojoExecutionException e) {
assertTrue(logger.getContent().contains("header style: text"));
assertTrue(logger.getContent().contains("Header OK in: src/test/resources/check/Dockerfile"));
String absoluteDockerfileName = new File("src/test/resources/check/Dockerfile").getCanonicalPath().replace('\\', '/');
assertTrue(logger.getContent().contains("Header OK in: " + absoluteDockerfileName));
assertEquals("Some files do not have the expected license header", e.getMessage());
}
}
Expand Down