Skip to content

Commit 5657fc2

Browse files
IGNITE-26770 Implement escaping of space chars in mvn command path on Windows (#12438)
1 parent 86d4313 commit 5657fc2

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

modules/compatibility/src/test/java/org/apache/ignite/compatibility/testframework/util/MavenUtils.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ private static Collection<String> mavenProjectRepositories() throws Exception {
165165
return F.transform(model.getRepositories(), RepositoryBase::getUrl);
166166
}
167167
finally {
168-
Files.deleteIfExists(outPath);
168+
if (!U.isWindows())
169+
Files.deleteIfExists(outPath);
169170
}
170171
}
171172

@@ -191,7 +192,7 @@ private static void downloadArtifact(String artifact) throws Exception {
191192
}
192193

193194
/**
194-
* Executes given command in operation system.
195+
* Executes given command in operating system.
195196
*
196197
* @param cmd Command to execute.
197198
* @return Output of result of executed command.
@@ -238,12 +239,48 @@ private static String exec(String cmd) throws Exception {
238239
}
239240
}
240241

242+
/**
243+
* Adds escape characters to path elements that contain spaces.
244+
*
245+
* @param path Original path with unescaped elements.
246+
* @return Path with escaped elements.
247+
*/
248+
public static String escapeSpaceCharsInPath(String path) {
249+
int startBSlashIdx = path.indexOf('\\');
250+
int endBSlashIdx = path.indexOf('\\', startBSlashIdx + 1);
251+
252+
if (endBSlashIdx < 0)
253+
return path;
254+
255+
StringBuilder res = new StringBuilder(path.substring(0, startBSlashIdx));
256+
257+
while (endBSlashIdx > 0) {
258+
String substring = path.substring(startBSlashIdx + 1, endBSlashIdx);
259+
260+
if (substring.contains(" "))
261+
res.append("\\\"").append(substring).append("\"");
262+
else
263+
res.append("\\").append(substring);
264+
265+
startBSlashIdx = endBSlashIdx;
266+
endBSlashIdx = path.indexOf('\\', startBSlashIdx + 1);
267+
}
268+
269+
res.append("\\")
270+
.append(path.substring(startBSlashIdx + 1));
271+
272+
return res.toString();
273+
}
274+
241275
/**
242276
* @return Maven executable command.
243277
*/
244278
private static String buildMvnCommand() {
245279
String mvnCmd = resolveMavenApplicationPath();
246280

281+
if (U.isWindows())
282+
mvnCmd = escapeSpaceCharsInPath(mvnCmd);
283+
247284
Path mvnSettingsFilePath = resolveMavenSettingsFilePath();
248285

249286
if (Files.exists(mvnSettingsFilePath))
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.compatibility.testframework.util;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import static org.apache.ignite.compatibility.testframework.util.MavenUtils.escapeSpaceCharsInPath;
24+
25+
/**
26+
* Test class for {@link MavenUtils}.
27+
*/
28+
public class MavenUtilsTest {
29+
/** Remains the same. */
30+
private static final String NO_NEED_TO_ESCAPE_PATH_0 = "C:\\maven\\mvn.bat clean install";
31+
32+
/** Remains the same. */
33+
private static final String NO_NEED_TO_ESCAPE_PATH_1 = "C:\\mvn.bat clean install";
34+
35+
/** Space chars in the middle of the path require escaping. */
36+
private static final String UNESCAPED_PATH_0 = "C:\\software\\Apache Software Foundation\\maven\\bin\\mvn.bat dependency:tree";
37+
38+
/** Path with space chars escaped. */
39+
private static final String ESCAPED_PATH_0 = "C:\\software\\\"Apache Software Foundation\"\\maven\\bin\\mvn.bat dependency:tree";
40+
41+
/** Space chars in several elements of the path require escaping. */
42+
private static final String UNESCAPED_PATH_1 = "C:\\Program Files\\Apache Software Foundation\\maven\\bin\\mvn.bat clean install";
43+
44+
/** Path with space chars escaped. */
45+
private static final String ESCAPED_PATH_1 = "C:\\\"Program Files\"\\\"Apache Software Foundation\"\\maven\\bin\\mvn.bat clean install";
46+
47+
/** */
48+
@Test
49+
public void testPathsWithoutSpacesStayUnchanged() {
50+
Assert.assertEquals(NO_NEED_TO_ESCAPE_PATH_0, escapeSpaceCharsInPath(NO_NEED_TO_ESCAPE_PATH_0));
51+
52+
Assert.assertEquals(NO_NEED_TO_ESCAPE_PATH_1, escapeSpaceCharsInPath(NO_NEED_TO_ESCAPE_PATH_1));
53+
}
54+
55+
/** */
56+
@Test
57+
public void testPathsWithSpaceCharsEscaped() {
58+
Assert.assertEquals(ESCAPED_PATH_0, escapeSpaceCharsInPath(UNESCAPED_PATH_0));
59+
60+
Assert.assertEquals(ESCAPED_PATH_1, escapeSpaceCharsInPath(UNESCAPED_PATH_1));
61+
}
62+
}

modules/compatibility/src/test/java/org/apache/ignite/compatibility/testsuites/IgniteCompatibilityBasicTestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.ignite.compatibility.persistence.MoveBinaryMetadataCompatibility;
3131
import org.apache.ignite.compatibility.persistence.PersistenceBasicCompatibilityTest;
3232
import org.apache.ignite.compatibility.persistence.SnapshotCompatibilityTest;
33+
import org.apache.ignite.compatibility.testframework.util.MavenUtilsTest;
3334
import org.junit.runner.RunWith;
3435
import org.junit.runners.Suite;
3536

@@ -38,6 +39,7 @@
3839
*/
3940
@RunWith(Suite.class)
4041
@Suite.SuiteClasses({
42+
MavenUtilsTest.class,
4143
PersistenceBasicCompatibilityTest.class,
4244
InlineJavaObjectCompatibilityTest.class,
4345
IndexTypesCompatibilityTest.class,

0 commit comments

Comments
 (0)