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

[MNG-8230] Add tests for CI-friendly versions #366

Merged
merged 4 commits into from
Oct 1, 2024
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
@@ -0,0 +1,183 @@
/*
* 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.it;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

import org.apache.maven.shared.verifier.VerificationException;
import org.apache.maven.shared.verifier.Verifier;
import org.apache.maven.shared.verifier.util.ResourceExtractor;
import org.junit.jupiter.api.Test;

/**
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8230">MNG-8230</a>.
*/
class MavenITmng8230CIFriendlyTest extends AbstractMavenIntegrationTestCase {

private static final String PROPERTIES = "target/expression.properties";

MavenITmng8230CIFriendlyTest() {
super("[4.0.0-beta-5,)");
}

/**
* Verify that CI friendly version work when using project properties
*
* @throws Exception in case of failure
*/
@Test
void testitCiFriendlyWithProjectProperties() throws Exception {
File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");

File basedir = new File(testDir, "cif-with-project-props");
Verifier verifier = newVerifier(basedir.getAbsolutePath());
verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
verifier.addCliArgument("-Dexpression.expressions=project/version");
verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
verifier.execute();
verifier.verifyErrorFreeLog();
verifier.verifyFilePresent(PROPERTIES);
Properties props = verifier.loadProperties(PROPERTIES);
assertEquals(props.getProperty("project.version"), "1.0-SNAPSHOT");
}

/**
* Verify that CI friendly version work when using project properties
*
* @throws Exception in case of failure
*/
@Test
void testitCiFriendlyWithProjectPropertiesOverride() throws Exception {
File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");

File basedir = new File(testDir, "cif-with-project-props");
Verifier verifier = newVerifier(basedir.getAbsolutePath());
verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
verifier.addCliArgument("-Dexpression.expressions=project/version");
verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
verifier.addCliArgument("-Dci-version=1.1-SNAPSHOT");
verifier.execute();
verifier.verifyErrorFreeLog();
verifier.verifyFilePresent(PROPERTIES);
Properties props = verifier.loadProperties(PROPERTIES);
assertEquals(props.getProperty("project.version"), "1.1-SNAPSHOT");
}

/**
* Verify that CI friendly version work when using user properties
*
* @throws Exception in case of failure
*/
@Test
void testitCiFriendlyWithUserProperties() throws Exception {
File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");

File basedir = new File(testDir, "cif-with-user-props");
Verifier verifier = newVerifier(basedir.getAbsolutePath());

verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
verifier.addCliArgument("-Dexpression.expressions=project/version");
verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
verifier.addCliArgument("-Dci-version=1.1-SNAPSHOT");
verifier.execute();
verifier.verifyErrorFreeLog();
verifier.verifyFilePresent(PROPERTIES);
Properties props = verifier.loadProperties(PROPERTIES);
assertEquals(props.getProperty("project.version"), "1.1-SNAPSHOT");
}

/**
* Verify that CI friendly version fails if the properties are not given
*
* @throws Exception in case of failure
*/
@Test
void testitCiFriendlyWithUserPropertiesNotGiven() throws Exception {
File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");

File basedir = new File(testDir, "cif-with-user-props");
Verifier verifier = newVerifier(basedir.getAbsolutePath());
verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
verifier.addCliArgument("-Dexpression.expressions=project/version");
verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
try {
verifier.execute();
fail("Expected failure");
} catch (VerificationException e) {
assertTrue(
e.getMessage(),
e.getMessage()
.contains(
"'version' contains an expression but should be a constant. @ myGroup:parent:${ci-version}"));
}
}

@Test
void testitExpressionInGroupId() throws Exception {
File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");

File basedir = new File(testDir, "exp-in-groupid");
Verifier verifier = newVerifier(basedir.getAbsolutePath());
verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
verifier.addCliArgument("-Dexpression.expressions=project/version");
verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
try {
verifier.execute();
fail("Expected failure");
} catch (VerificationException e) {
assertTrue(
e.getMessage(),
e.getMessage()
.contains(
"'groupId' contains an expression but should be a constant. @ ${foo}:myArtifact:1.0-SNAPSHOT"));
}
}

@Test
void testitExpressionInArtifactId() throws Exception {
File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-8230-ci-friendly-and-gav");

File basedir = new File(testDir, "exp-in-artifactid");
Verifier verifier = newVerifier(basedir.getAbsolutePath());
verifier.addCliArgument("-Dexpression.outputFile=" + new File(basedir, PROPERTIES).getPath());
verifier.addCliArgument("-Dexpression.expressions=project/version");
verifier.addCliArgument("org.apache.maven.its.plugins:maven-it-plugin-expression:2.1-SNAPSHOT:eval");
try {
verifier.execute();
fail("Expected failure");
} catch (VerificationException e) {
assertTrue(
e.getMessage(),
e.getMessage()
.contains(
"'artifactId' contains an expression but should be a constant. @ myGroup:${foo}:1.0-SNAPSHOT"));
}
}

void verifyExactLine(Verifier verifier, String line) throws Exception {
List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
if (lines.stream().noneMatch(s -> Objects.equals(s, line))) {
throw new VerificationException(
"Could not find line: '" + line + "' in output log:\n" + String.join("\n", lines));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public TestSuiteOrdering() {
* the tests are to finishing. Newer tests are also more likely to fail, so this is
* a fail fast technique as well.
*/
suite.addTestSuite(MavenITmng8230CIFriendlyTest.class);
suite.addTestSuite(MavenITmng7255InferredGroupIdTest.class);
suite.addTestSuite(MavenITmng8220ExtensionWithDITest.class);
suite.addTestSuite(MavenITmng8181CentralRepoTest.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.1.0</modelVersion>
<parent />
<artifactId>child</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project root="true">
<modelVersion>4.1.0</modelVersion>

<groupId>myGroup</groupId>
<artifactId>parent</artifactId>
<version>${ci-version}</version>
<packaging>pom</packaging>

<properties>
<ci-version>1.0-SNAPSHOT</ci-version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.1.0</modelVersion>
<parent />
<artifactId>child</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project root="true">
<modelVersion>4.1.0</modelVersion>

<groupId>myGroup</groupId>
<artifactId>parent</artifactId>
<version>${ci-version}</version>
<packaging>pom</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project root="true">
<modelVersion>4.1.0</modelVersion>

<groupId>myGroup</groupId>
<artifactId>${foo}</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project root="true">
<modelVersion>4.1.0</modelVersion>

<groupId>${foo}</groupId>
<artifactId>myArtifact</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Loading