slawekjaranowski commented on code in PR #3575:
URL: https://github.com/apache/maven-surefire/pull/3575#discussion_r4673248318


##########
surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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.surefire.extensions.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+/**
+ * The command line of a process Surefire starts: an executable, its 
arguments, the environment and the working
+ * directory, started with {@link ProcessBuilder}. The process is a direct 
child; there is no shell in between.
+ */
+public class Commandline {
+    private static final boolean WINDOWS =
+            System.getProperty("os.name", 
"").toLowerCase(java.util.Locale.ROOT).startsWith("windows");
+
+    private static final char[] CMD_SPECIAL_CHARS = {' ', '\t', '&', '<', '>', 
'(', ')', '@', '^', '|'};
+
+    private String executable;
+    private final List<String> arguments = new ArrayList<>();
+    private final Map<String, String> addedEnvironment = new LinkedHashMap<>();
+    private File workingDirectory;
+
+    public Commandline() {}
+
+    /**
+     * @param commandLine the executable followed by its arguments, split like 
a shell would, see
+     *                    {@link #translateCommandline(String)}
+     */
+    public Commandline(String commandLine) {
+        String[] parts = translateCommandline(commandLine);
+        if (parts.length > 0) {
+            executable = parts[0];
+            arguments.addAll(java.util.Arrays.asList(parts).subList(1, 
parts.length));
+        }
+    }
+
+    public void setExecutable(String executable) {
+        this.executable = executable;
+    }
+
+    public String getExecutable() {
+        return executable;
+    }
+
+    public Argument createArg() {
+        return new Argument();
+    }
+
+    public void addArgument(String argument) {
+        arguments.add(argument);
+    }
+
+    public void addArguments(String... args) {
+        Collections.addAll(arguments, args);
+    }
+
+    /**
+     * @return the arguments, without the executable
+     */
+    public String[] getArguments() {
+        return arguments.toArray(new String[0]);
+    }
+
+    /**
+     * @return the executable followed by the arguments
+     */
+    public String[] getCommandline() {
+        List<String> all = new ArrayList<>(arguments.size() + 1);
+        all.add(executable);
+        all.addAll(arguments);
+        return all.toArray(new String[0]);
+    }
+
+    public void setWorkingDirectory(String path) {
+        this.workingDirectory = path == null ? null : new File(path);
+    }
+
+    public void setWorkingDirectory(File workingDirectory) {
+        this.workingDirectory = workingDirectory;
+    }
+
+    public File getWorkingDirectory() {
+        return workingDirectory;
+    }
+
+    public void addEnvironment(String name, String value) {
+        addedEnvironment.put(name, value);
+    }
+
+    /**
+     * The environment the process gets: the environment of this JVM, with 
{@link #addEnvironment added} variables
+     * overriding it. Subclasses may remove variables.
+     *
+     * @return a mutable copy
+     */
+    protected Map<String, String> environment() {
+        Map<String, String> env = new LinkedHashMap<>(System.getenv());
+        env.putAll(addedEnvironment);
+        return env;
+    }
+
+    /**
+     * @return the environment of the process as {@code NAME=value} entries
+     */
+    public String[] getEnvironmentVariables() {
+        Map<String, String> env = environment();
+        String[] result = new String[env.size()];
+        int i = 0;
+        for (Map.Entry<String, String> e : env.entrySet()) {
+            result[i++] = e.getKey() + "=" + e.getValue();
+        }
+        return result;
+    }
+
+    public ProcessBuilder toProcessBuilder() {
+        ProcessBuilder builder = new ProcessBuilder(getCommandline());
+        builder.directory(workingDirectory);
+        Map<String, String> env = builder.environment();
+        env.clear();
+        env.putAll(environment());
+        return builder;
+    }
+
+    public Process execute() throws IOException {
+        return toProcessBuilder().start();
+    }
+
+    /**
+     * The command line as text, for logging: on Unix every item in single 
quotes, on Windows an item in double quotes
+     * only when cmd.exe would need them (whitespace or one of {@code 
&<>()@^|}), as the shell wrappers used to print it.
+     */
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        for (String item : getCommandline()) {
+            if (sb.length() > 0) {
+                sb.append(' ');
+            }
+            sb.append(quoteForLog(item == null ? "" : item));
+        }
+        return sb.toString();
+    }
+
+    private static String quoteForLog(String item) {
+        if (!WINDOWS) {
+            return "'" + item.replace("'", "'\"'\"'") + "'";
+        }
+        if (item.isEmpty() || (item.length() > 1 && item.startsWith("\"") && 
item.endsWith("\""))) {
+            return item;
+        }
+        for (char c : CMD_SPECIAL_CHARS) {
+            if (item.indexOf(c) >= 0) {
+                return "\"" + item + "\"";
+            }
+        }
+        return item;
+    }
+
+    /**
+     * Splits a command line into arguments like a shell would: whitespace 
separates, single or double quotes
+     * group, a backslash escapes the next character.
+     *
+     * @throws IllegalArgumentException on unbalanced quotes
+     */
+    public static String[] translateCommandline(String toProcess) {

Review Comment:
   It is always buggy function, do unit test here will be appreciated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to