View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.errorprone;
6   
7   import java.io.File;
8   import java.nio.charset.StandardCharsets;
9   import java.nio.file.Files;
10  import java.util.List;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.api.io.TempDir;
15  
16  /**
17   * Test case for {@link Argfile}.
18   * @since 1.0
19   */
20  final class ArgfileTest {
21  
22      @Test
23      void writesQuotedTokensOnePerLine(@TempDir final File dir) throws Exception {
24          final File path = new File(dir, "args.txt");
25          new Argfile(
26              path,
27              List.of("-classpath", "C:\\one\\two.jar;C:\\Program Files\\x.jar")
28          ).save();
29          MatcherAssert.assertThat(
30              "argfile must escape backslashes and keep one token per line",
31              Files.readString(path.toPath(), StandardCharsets.UTF_8),
32              Matchers.equalTo(
33                  String.join(
34                      System.lineSeparator(),
35                      "\"-classpath\"",
36                      "\"C:\\\\one\\\\two.jar;C:\\\\Program Files\\\\x.jar\"",
37                      ""
38                  )
39              )
40          );
41      }
42  
43      @Test
44      void escapesEmbeddedDoubleQuotes(@TempDir final File dir) throws Exception {
45          final File path = new File(dir, "args.txt");
46          new Argfile(path, List.of("a\"b")).save();
47          MatcherAssert.assertThat(
48              "embedded double quotes must be backslash-escaped",
49              Files.readString(path.toPath(), StandardCharsets.UTF_8),
50              Matchers.equalTo(
51                  String.join(System.lineSeparator(), "\"a\\\"b\"", "")
52              )
53          );
54      }
55  }