Repository: maven-surefire Updated Branches: refs/heads/master 9b7ca5a2b -> cebce291f
SUREFIRE-1136 Current working directory propagation in forked mode Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/cebce291 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/cebce291 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/cebce291 Branch: refs/heads/master Commit: cebce291f454fd5df528a8da37b4e81e407b1082 Parents: 9b7ca5a Author: Norbert Wnuk <norbert.w...@sabre.com> Authored: Tue Jan 20 00:59:22 2015 +0100 Committer: Norbert Wnuk <norbert.w...@sabre.com> Committed: Tue Jan 20 01:08:21 2015 +0100 ---------------------------------------------------------------------- .../plugin/surefire/AbstractSurefireMojo.java | 7 ++ .../booterclient/ForkConfiguration.java | 18 ++++- .../booterclient/ForkConfigurationTest.java | 81 ++++++++++++++++++- ...urefire1136CwdPropagationInForkedModeIT.java | 40 ++++++++++ .../pom.xml | 83 ++++++++++++++++++++ ...CurrentWorkingDirectoryInForkedModeTest.java | 46 +++++++++++ 6 files changed, 272 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/cebce291/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index 2999a8e..5205b95 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -2199,6 +2199,13 @@ public abstract class AbstractSurefireMojo throw new MojoFailureException( "workingDirectory cannot be null" ); } + if ( isForking() ) + { + // Postpone directory creation till forked JVM creation + // see ForkConfiguration.createCommandLine + return; + } + if ( !getWorkingDirectory().exists() ) { if ( !getWorkingDirectory().mkdirs() ) http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/cebce291/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java index 5843428..e5c8a42 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java @@ -201,11 +201,27 @@ public class ForkConfiguration cli.createArg().setValue( shadefire ? new Relocator().relocate( forkedBooter ) : forkedBooter ); } - cli.setWorkingDirectory( workingDirectory.getAbsolutePath() ); + cli.setWorkingDirectory( getWorkingDirectory( threadNumber ).getAbsolutePath() ); return cli; } + private File getWorkingDirectory( int threadNumber ) + throws SurefireBooterForkException + { + File cwd = new File( replaceThreadNumberPlaceholder( workingDirectory.getAbsolutePath(), threadNumber ) ); + if ( !cwd.exists() && !cwd.mkdirs() ) + { + throw new SurefireBooterForkException( "Cannot create workingDirectory " + cwd.getAbsolutePath() ); + } + if ( !cwd.isDirectory() ) + { + throw new SurefireBooterForkException( + "WorkingDirectory " + cwd.getAbsolutePath() + " exists and is not a directory" ); + } + return cwd; + } + private String replaceThreadNumberPlaceholder( String argLine, int threadNumber ) { return argLine.replace( AbstractSurefireMojo.THREAD_NUMBER_PLACEHOLDER, http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/cebce291/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java index 5944ec8..32eaa61 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java @@ -21,9 +21,11 @@ package org.apache.maven.plugin.surefire.booterclient; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.Collections; import java.util.Properties; +import org.apache.commons.io.FileUtils; import org.apache.maven.shared.utils.StringUtils; import org.apache.maven.shared.utils.cli.Commandline; import org.apache.maven.surefire.booter.Classpath; @@ -61,6 +63,75 @@ public class ForkConfigurationTest assertTrue( commandLine.toString().contains( "abc def" ) ); } + public void testCurrentWorkingDirectoryPropagationIncludingForkNumberExpansion() + throws IOException, SurefireBooterForkException + { + // SUREFIRE-1136 + File baseDir = Files.createTempDirectory( "SUREFIRE-1136-" ).toFile(); + baseDir.deleteOnExit(); + + File cwd = new File( baseDir, "fork_${surefire.forkNumber}" ); + + ForkConfiguration config = getForkConfiguration( null, "java", cwd.getCanonicalFile() ); + Commandline commandLine = config.createCommandLine( Collections.<String>emptyList(), true, false, null, 1 ); + + File forkDirectory = new File( baseDir, "fork_1" ); + forkDirectory.deleteOnExit(); + assertTrue( forkDirectory.getCanonicalPath().equals( + commandLine.getShell().getWorkingDirectory().getCanonicalPath() ) ); + } + + public void testExceptionWhenCurrentDirectoryIsNotRealDirectory() + throws IOException, SurefireBooterForkException + { + // SUREFIRE-1136 + File baseDir = Files.createTempDirectory( "SUREFIRE-1136-" ).toFile(); + baseDir.deleteOnExit(); + + File cwd = new File( baseDir, "cwd.txt" ); + FileUtils.touch( cwd ); + cwd.deleteOnExit(); + + ForkConfiguration config = getForkConfiguration( null, "java", cwd.getCanonicalFile() ); + + try + { + config.createCommandLine( Collections.<String>emptyList(), true, false, null, 1 ); + } + catch ( SurefireBooterForkException sbfe ) + { + // To handle issue with ~ expansion on Windows + String absolutePath = cwd.getCanonicalPath(); + assertEquals( "WorkingDirectory " + absolutePath + " exists and is not a directory", sbfe.getMessage() ); + return; + } + + fail(); + } + + public void testExceptionWhenCurrentDirectoryCannotBeCreated() + throws IOException, SurefireBooterForkException + { + // SUREFIRE-1136 + File baseDir = Files.createTempDirectory( "SUREFIRE-1136-" ).toFile(); + baseDir.deleteOnExit(); + + File cwd = new File( baseDir, "\0?InvalidDirectoryName" ); + ForkConfiguration config = getForkConfiguration( null, "java", cwd.getAbsoluteFile() ); + + try + { + config.createCommandLine( Collections.<String>emptyList(), true, false, null, 1 ); + } + catch ( SurefireBooterForkException sbfe ) + { + assertEquals( "Cannot create workingDirectory " + cwd.getAbsolutePath(), sbfe.getMessage() ); + return; + } + + fail(); + } + private File getTempClasspathFile() throws IOException { @@ -70,10 +141,16 @@ public class ForkConfigurationTest } public static ForkConfiguration getForkConfiguration( String argLine, String jvm ) + throws IOException +{ + return getForkConfiguration( argLine, jvm, new File( "." ).getCanonicalFile() ); +} + + public static ForkConfiguration getForkConfiguration( String argLine, String jvm, File cwd ) throws IOException { - return new ForkConfiguration( Classpath.emptyClasspath(), null, null, jvm, new File( "." ).getCanonicalFile(), new Properties(), argLine, - null, false, 1, false ); + return new ForkConfiguration( Classpath.emptyClasspath(), null, null, jvm, cwd, new Properties(), argLine, null, + false, 1, false ); } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/cebce291/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1136CwdPropagationInForkedModeIT.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1136CwdPropagationInForkedModeIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1136CwdPropagationInForkedModeIT.java new file mode 100644 index 0000000..bcd6f2a --- /dev/null +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1136CwdPropagationInForkedModeIT.java @@ -0,0 +1,40 @@ +package org.apache.maven.surefire.its.jiras; + +/* + * 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. + */ + +import org.apache.maven.surefire.its.fixture.OutputValidator; +import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; +import org.junit.Test; + +/** + * SUREFIRE-1136 Correct current working directory propagation + * + * @author Norbert Wnuk + */ +public class Surefire1136CwdPropagationInForkedModeIT + extends SurefireJUnit4IntegrationTestCase +{ + @Test + public void testTestNgAndJUnitTogether() + { + OutputValidator outputValidator = unpack( "surefire-1136-cwd-propagation-in-forked-mode" ).executeTest(); + outputValidator.assertTestSuiteResults( 1, 0, 0, 0 ); + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/cebce291/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/pom.xml ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/pom.xml b/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/pom.xml new file mode 100644 index 0000000..c85a9ab --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/pom.xml @@ -0,0 +1,83 @@ +<!-- + ~ 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>it-parent</artifactId> + <version>1.0</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <groupId>cwd</groupId> + <artifactId>cwd</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + + <name>cwd</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.easytesting</groupId> + <artifactId>fest-assert-core</artifactId> + <version>2.0M10</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.5</source> + <target>1.5</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <!-- To override fork mode from parrent pom.xml --> + <forkMode>once</forkMode> + <forkCount>1</forkCount> + <!-- To ensure proper variables expansion for both standard maven and surefire specific variables --> + <workingDirectory>${project.name}_${surefire.forkNumber}</workingDirectory> + <systemPropertyVariables> + <maven.project.base.directory>${basedir}</maven.project.base.directory> + </systemPropertyVariables> + </configuration> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/cebce291/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/src/test/java/cwd/CurrentWorkingDirectoryInForkedModeTest.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/src/test/java/cwd/CurrentWorkingDirectoryInForkedModeTest.java b/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/src/test/java/cwd/CurrentWorkingDirectoryInForkedModeTest.java new file mode 100644 index 0000000..50cd9b0 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-1136-cwd-propagation-in-forked-mode/src/test/java/cwd/CurrentWorkingDirectoryInForkedModeTest.java @@ -0,0 +1,46 @@ +package cwd; + +/* + * 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. + */ + +import org.junit.Test; + +import java.io.File; + +import static org.fest.assertions.api.Assertions.assertThat; + +public class CurrentWorkingDirectoryInForkedModeTest +{ + + @Test + public void testCurrentWorkingDirectoryPropagation() + throws Exception + { + + File projectDirectory = new File( System.getProperty( "maven.project.base.directory" ) ); + File forkDirectory = new File( projectDirectory, "cwd_1" ); + forkDirectory.deleteOnExit(); + + assertThat( System.getProperty( "basedir" ) ).isEqualTo( projectDirectory.getCanonicalPath() ); + assertThat( System.getProperty( "user.dir" ) ).isEqualTo( forkDirectory.getCanonicalPath() ); + assertThat( new File( "." ).getCanonicalPath() ).isEqualTo( forkDirectory.getCanonicalPath() ); + + } + +}