]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add test case for Args::from_gcc_atfile
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 7 Apr 2020 20:16:53 +0000 (22:16 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 7 Apr 2020 20:16:53 +0000 (22:16 +0200)
src/Args.cpp
unittest/test_Args.cpp

index ae2b598add2ca759e11640d0d24bc13421ed1ba6..639c143b68445b8c331afd9c282cfc92d94b8fce 100644 (file)
@@ -113,7 +113,7 @@ Args::from_gcc_atfile(const std::string& filename)
       // End of token
       *argpos = '\0';
       if (argbuf[0] != '\0') {
-        args_add(args, argbuf);
+        args_add(args, argbuf.c_str());
       }
       argpos = argbuf.begin();
       if (*pos == '\0') {
index 541cbc49459bbde922d8624efe97b719e1e83756..e61b0bdec99af6417de1d90f696b30f151498fb3 100644 (file)
@@ -71,6 +71,66 @@ TEST_CASE("Args::from_string")
   CHECK(args[3] == "f");
 }
 
+TEST_CASE("Args::from_gcc_atfile")
+{
+  Args args;
+
+  SECTION("Non-existing file")
+  {
+    CHECK(Args::from_gcc_atfile("at_file") == nonstd::nullopt);
+  }
+
+  SECTION("Empty")
+  {
+    Util::write_file("at_file", "");
+    args = *Args::from_gcc_atfile("at_file");
+    CHECK(args.size() == 0);
+  }
+
+  SECTION("One argument without newline")
+  {
+    Util::write_file("at_file", "foo");
+    args = *Args::from_gcc_atfile("at_file");
+    CHECK(args.size() == 1);
+    CHECK(args[0] == "foo");
+  }
+
+  SECTION("One argument with newline")
+  {
+    Util::write_file("at_file", "foo\n");
+    args = *Args::from_gcc_atfile("at_file");
+    CHECK(args.size() == 1);
+    CHECK(args[0] == "foo");
+  }
+
+  SECTION("Multiple simple arguments")
+  {
+    Util::write_file("at_file", "x y z\n");
+    args = *Args::from_gcc_atfile("at_file");
+    CHECK(args.size() == 3);
+    CHECK(args[0] == "x");
+    CHECK(args[1] == "y");
+    CHECK(args[2] == "z");
+  }
+
+  SECTION("Tricky quoting")
+  {
+    Util::write_file(
+      "at_file",
+      "first\rsec\\\tond\tthi\\\\rd\nfourth  \tfif\\ th \"si'x\\\" th\""
+      " 'seve\nth'\\");
+    args = *Args::from_gcc_atfile("at_file");
+    CHECK(args.size() == 7);
+    CHECK(args[0] == "first");
+    CHECK(args[1] == "sec\tond");
+    CHECK(args[2] == "thi\\rd");
+    CHECK(args[3] == "fourth");
+    CHECK(args[4] == "fif th");
+    CHECK(args[5] == "si'x\" th");
+    CHECK(args[6] == "seve\nth");
+  }
+}
+
 TEST_CASE("Args copy assignment operator")
 {
   Args args1 = Args::from_string("x y");