-
Notifications
You must be signed in to change notification settings - Fork 42
[#384] Do not modify the project model in effective-pom #414
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
Open
Neil-Tomar
wants to merge
2
commits into
apache:master
Choose a base branch
from
Neil-Tomar:fix/384-effective-pom-mutation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * 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.plugins.help; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import java.util.Properties; | ||
|
|
||
| import org.apache.maven.api.plugin.testing.InjectMojo; | ||
| import org.apache.maven.api.plugin.testing.MojoParameter; | ||
| import org.apache.maven.api.plugin.testing.MojoTest; | ||
| import org.apache.maven.execution.MavenSession; | ||
| import org.apache.maven.model.Model; | ||
| import org.apache.maven.plugin.MojoExecution; | ||
| import org.apache.maven.project.MavenProject; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Test class for the effective-pom mojo of the Help Plugin. | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| @MojoTest | ||
| class EffectivePomMojoTest { | ||
|
|
||
| @Inject | ||
| private MavenProject project; | ||
|
|
||
| @Inject | ||
| private MavenSession mavenSession; | ||
|
|
||
| @Mock | ||
| private MojoExecution mojoExecution; | ||
|
|
||
| @TempDir | ||
| private Path tempDir; | ||
|
|
||
| private final Model model = new Model(); | ||
|
|
||
| private final Properties originalProperties = new Properties(); | ||
|
|
||
| @BeforeEach | ||
| void setup() throws IOException { | ||
| originalProperties.setProperty("b.property", "b-value"); | ||
| originalProperties.setProperty("a.property", "a-value"); | ||
|
|
||
| model.setProperties(originalProperties); | ||
|
|
||
| when(project.getModel()).thenReturn(model); | ||
|
|
||
| Path outputPath = Files.createTempFile(tempDir, "maven-help-plugin-test-", ".xml"); | ||
| mavenSession.getUserProperties().setProperty("outputPath", outputPath.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * The effective-pom goal only displays the model, so it must not modify the project it reads from. | ||
| * | ||
| * @throws Exception in case of errors. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really needed on test |
||
| */ | ||
| @Test | ||
| @InjectMojo(goal = "effective-pom") | ||
| @MojoParameter(name = "output", value = "${outputPath}") | ||
| void testExecuteDoesNotModifyProjectModel(EffectivePomMojo mojo) throws Exception { | ||
| // snapshot of the contents before the mojo runs, to detect in-place modification | ||
| Properties expectedProperties = new Properties(); | ||
| expectedProperties.putAll(originalProperties); | ||
|
|
||
| setVariableValueToObject(mojo, "projects", Collections.singletonList(project)); | ||
| setVariableValueToObject(mojo, "mojoExecution", mojoExecution); | ||
|
|
||
| mojo.execute(); | ||
|
|
||
| assertSame( | ||
| originalProperties, | ||
| model.getProperties(), | ||
| "effective-pom must not replace the properties of the project model"); | ||
|
|
||
| assertEquals( | ||
| expectedProperties, | ||
| model.getProperties(), | ||
| "effective-pom must not modify the properties of the project model"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how sure are we that clone works here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @elharo, sorry for late reply.
So I think
.clone()works here because in the original version thepomincleanModel(pom)has a reference to the original Model, so cleaning/sorting the properties there overwrites the original ones. That's the issue in #384. Since you mentioned the goal should be read only, I pass a clone() of the Model to cleanModel() instead, so only the copy gets changed.I am still new to this codebase, so if there is a better way I am happy to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question is more does this return a Cloneable object? What's the contract on Model?