run-cts: fix command printing to escape double-quotes

If path/arg contains double-quotes, escape it with a backslash. Also
output the entire string once per command so that they don't interleave
when running multiple subcases.

Change-Id: I58a3fe9bf9d77ee48e25be3620fc118ddbfb27f2
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/156364
Kokoro: Antonio Maiorano <amaiorano@google.com>
Auto-Submit: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
diff --git a/tools/src/cmd/run-cts/main.go b/tools/src/cmd/run-cts/main.go
index 59b827f..c0c7230 100644
--- a/tools/src/cmd/run-cts/main.go
+++ b/tools/src/cmd/run-cts/main.go
@@ -1481,24 +1481,26 @@
 
 func PrintCommand(writer io.Writer, cmd *exec.Cmd) {
 	maybeQuote := func(s string) string {
-		if strings.ContainsAny(s, " ,()") {
-			return fmt.Sprintf("\"%v\"", s)
+		if strings.ContainsAny(s, ` ,()"`) {
+			s = strings.ReplaceAll(s, `"`, `\"`)
+			s = fmt.Sprintf("\"%v\"", s)
 		}
 		return s
 	}
 
-	fmt.Fprint(writer, "Running:\n")
-	fmt.Fprintf(writer, "  Cmd: %v ", maybeQuote(cmd.Path))
+	output := &strings.Builder{}
+	fmt.Fprintln(output, "Running:")
+	fmt.Fprintf(output, "  Cmd: %v ", maybeQuote(cmd.Path))
 	for i, arg := range cmd.Args[1:] {
 		if i > 0 {
-			fmt.Fprint(writer, " ")
+			fmt.Fprint(output, " ")
 		}
-		fmt.Fprint(writer, maybeQuote(arg))
+		fmt.Fprint(output, maybeQuote(arg))
 	}
-	fmt.Fprintln(writer)
-	fmt.Fprintf(writer, "  Dir: %v\n\n", cmd.Dir)
+	fmt.Fprintln(output)
+	fmt.Fprintf(output, "  Dir: %v\n\n", cmd.Dir)
 
-	fmt.Fprint(writer, "  For VS Code launch.json:\n")
+	fmt.Fprint(output, "  For VS Code launch.json:\n")
 	launchCmd := struct {
 		Program string   `json:"program"`
 		Args    []string `json:"args"`
@@ -1517,5 +1519,7 @@
 	// Remove object braces and add trailing comma
 	s = strings.TrimPrefix(s, "{\n")
 	s = strings.TrimSuffix(s, "\n}\n") + ",\n"
-	fmt.Fprintln(writer, s)
+	fmt.Fprintln(output, s)
+
+	fmt.Print(output.String())
 }