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   *
19   * @since 1.0
20   */
21  final class ArgfileTest {
22  
23      @Test
24      void writesQuotedTokensOnePerLine(@TempDir final File dir) throws Exception {
25          final File path = new File(dir, "args.txt");
26          new Argfile(
27              path,
28              List.of("-classpath", "C:\\one\\two.jar;C:\\Program Files\\x.jar")
29          ).save();
30          MatcherAssert.assertThat(
31              "argfile must escape backslashes and keep one token per line",
32              Files.readString(path.toPath(), StandardCharsets.UTF_8),
33              Matchers.equalTo(
34                  String.join(
35                      System.lineSeparator(),
36                      "\"-classpath\"",
37                      "\"C:\\\\one\\\\two.jar;C:\\\\Program Files\\\\x.jar\"",
38                      ""
39                  )
40              )
41          );
42      }
43  
44      @Test
45      void escapesEmbeddedDoubleQuotes(@TempDir final File dir) throws Exception {
46          final File path = new File(dir, "args.txt");
47          new Argfile(path, List.of("a\"b")).save();
48          MatcherAssert.assertThat(
49              "embedded double quotes must be backslash-escaped",
50              Files.readString(path.toPath(), StandardCharsets.UTF_8),
51              Matchers.equalTo(
52                  String.join(System.lineSeparator(), "\"a\\\"b\"", "")
53              )
54          );
55      }
56  }