Skip to content

Commit 5c7b07c

Browse files
IGNITE-26770 Implement escaping of space chars in path for Windows
1 parent d45d2be commit 5c7b07c

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

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

Lines changed: 36 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

@@ -190,6 +191,39 @@ private static void downloadArtifact(String artifact) throws Exception {
190191
X.println("Download is finished");
191192
}
192193

194+
/**
195+
* Adds escape characters to path elements that contain spaces.
196+
*
197+
* @param path Original path with unescaped elements.
198+
* @return Path with escaped elements.
199+
*/
200+
public static String escapeSpaceCharsInPath(String path) {
201+
int startBSlashIdx = path.indexOf('\\');
202+
int endBSlashIdx = path.indexOf('\\', startBSlashIdx + 1);
203+
204+
if (endBSlashIdx < 0)
205+
return path;
206+
207+
StringBuilder res = new StringBuilder(path.substring(0, startBSlashIdx));
208+
209+
while (endBSlashIdx > 0) {
210+
String substring = path.substring(startBSlashIdx + 1, endBSlashIdx);
211+
212+
if (substring.contains(" "))
213+
res.append("\\\"").append(substring).append("\"");
214+
else
215+
res.append("\\").append(substring);
216+
217+
startBSlashIdx = endBSlashIdx;
218+
endBSlashIdx = path.indexOf('\\', startBSlashIdx + 1);
219+
}
220+
221+
res.append("\\")
222+
.append(path.substring(startBSlashIdx + 1));
223+
224+
return res.toString();
225+
}
226+
193227
/**
194228
* Executes given command in operation system.
195229
*
@@ -202,7 +236,7 @@ private static String exec(String cmd) throws Exception {
202236
pb.redirectErrorStream(true);
203237

204238
pb.command(U.isWindows() ?
205-
new String[] {"cmd", "/c", cmd} :
239+
new String[] {"cmd", "/c", escapeSpaceCharsInPath(cmd)} :
206240
new String[] {"/bin/bash", "-c", cmd});
207241

208242
final Process p = pb.start();
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)