/* * Copyright (C) 2018 The Android Open Source Project * * Licensed 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. */ #include "Command.h" #include "test/Test.h" using ::testing::Eq; using namespace std::literals; namespace aapt { class TestCommand : public Command { public: explicit TestCommand() : Command("command") {} int Action(const std::vector& args) override { args_ = args; return 0; } std::vector args_; }; #ifdef _WIN32 TEST(CommandTest, LongFullyQualifiedPathWindows) { TestCommand command; std::string required_flag; command.AddRequiredFlag("--rflag", "", &required_flag, Command::kPath); std::optional optional_flag; command.AddOptionalFlag("--oflag", "", &optional_flag, Command::kPath); std::vector required_flag_list; command.AddRequiredFlagList("--rlflag", "", &required_flag_list, Command::kPath); std::vector optional_flag_list; command.AddOptionalFlagList("--olflag", "", &optional_flag_list, Command::kPath); std::string non_path_flag; command.AddRequiredFlag("--nflag", "", &non_path_flag); const std::string kLongPath = "C:\\Users\\jedo\\_bazel_jedo\\vcmdctjv\\execroot\\__main__\\_tmp" "\\6767b4778f8798efc0f784ee74fa70ee\\tests\\testApksAr8c7560a9a65" "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build" "\\intermediates\\processed_res\\minified\\processMinifiedResources" "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build" "\\intermediates\\processed_res\\minified\\processMinifiedResources" "\\out\\resources-minified.ap_"; const std::string kExpected = "\\\\?\\C:\\Users\\jedo\\_bazel_jedo\\vcmdctjv\\execroot\\__main__\\_tmp" "\\6767b4778f8798efc0f784ee74fa70ee\\tests\\testApksAr8c7560a9a65" "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build" "\\intermediates\\processed_res\\minified\\processMinifiedResources" "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build" "\\intermediates\\processed_res\\minified\\processMinifiedResources" "\\out\\resources-minified.ap_"; ASSERT_THAT(command.Execute({"--rflag", kLongPath, "--oflag", kLongPath, "--rlflag", kLongPath, "--rlflag", kLongPath, "--olflag", kLongPath, "--olflag", kLongPath, "--nflag", kLongPath, kLongPath, kLongPath}, &std::cerr), Eq(0)); ASSERT_THAT(required_flag, Eq(kExpected)); ASSERT_THAT(optional_flag, Eq(kExpected)); ASSERT_THAT(required_flag_list.size(), Eq(2)); ASSERT_THAT(required_flag_list[0], Eq(kExpected)); ASSERT_THAT(required_flag_list[1], Eq(kExpected)); ASSERT_THAT(optional_flag_list.size(), Eq(2)); ASSERT_THAT(optional_flag_list[0], Eq(kExpected)); ASSERT_THAT(optional_flag_list[1], Eq(kExpected)); // File arguments should also be converted to use the long path prefix ASSERT_THAT(command.args_.size(), Eq(2)); ASSERT_THAT(command.args_[0], Eq(kExpected)); ASSERT_THAT(command.args_[1], Eq(kExpected)); // Do not convert flags that are not marged as paths ASSERT_THAT(non_path_flag, Eq(kLongPath)); } #endif TEST(CommandTest, OptionsWithValues) { TestCommand command; std::string flag; command.AddRequiredFlag("--flag", "", &flag); ASSERT_EQ(0, command.Execute({"--flag"s, "1"s}, &std::cerr)); EXPECT_STREQ("1", flag.c_str()); ASSERT_EQ(0, command.Execute({"--flag=1"s}, &std::cerr)); EXPECT_STREQ("1", flag.c_str()); ASSERT_EQ(0, command.Execute({"--flag"s, "=2"s}, &std::cerr)); EXPECT_STREQ("=2", flag.c_str()); ASSERT_EQ(0, command.Execute({"--flag"s, "--flag"s}, &std::cerr)); EXPECT_STREQ("--flag", flag.c_str()); EXPECT_NE(0, command.Execute({"--flag"s}, &std::cerr)); EXPECT_NE(0, command.Execute({"--flag="s}, &std::cerr)); EXPECT_NE(0, command.Execute({"--flag1=2"s}, &std::cerr)); EXPECT_NE(0, command.Execute({"--flag1"s, "2"s}, &std::cerr)); } TEST(CommandTest, ShortOptions) { TestCommand command; bool flag = false; command.AddOptionalSwitch("--flag", "", &flag); ASSERT_EQ(0, command.Execute({"--flag"s}, &std::cerr)); EXPECT_TRUE(flag); // Short version of a switch should work. flag = false; ASSERT_EQ(0, command.Execute({"-f"s}, &std::cerr)); EXPECT_TRUE(flag); // Ambiguous names shouldn't parse via short options. command.AddOptionalSwitch("--flag-2", "", &flag); ASSERT_NE(0, command.Execute({"-f"s}, &std::cerr)); // But when we have a proper flag like that it should still work. flag = false; command.AddOptionalSwitch("-f", "", &flag); ASSERT_EQ(0, command.Execute({"-f"s}, &std::cerr)); EXPECT_TRUE(flag); // A regular short flag works fine as well. flag = false; command.AddOptionalSwitch("-d", "", &flag); ASSERT_EQ(0, command.Execute({"-d"s}, &std::cerr)); EXPECT_TRUE(flag); // A flag with a value only works via its long name syntax. std::optional val; command.AddOptionalFlag("--with-val", "", &val); ASSERT_EQ(0, command.Execute({"--with-val"s, "1"s}, &std::cerr)); EXPECT_TRUE(val); EXPECT_STREQ("1", val->c_str()); // Make sure the flags that require a value can't be parsed via short syntax, -w=blah // looks weird. ASSERT_NE(0, command.Execute({"-w"s, "2"s}, &std::cerr)); } } // namespace aapt