1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Command.h"
18 
19 #include "test/Test.h"
20 
21 using ::testing::Eq;
22 
23 namespace aapt {
24 
25 class TestCommand : public Command {
26  public:
TestCommand()27   explicit TestCommand() : Command("command") {}
Action(const std::vector<std::string> & args)28   int Action(const std::vector<std::string>& args) override {
29     args_ = args;
30     return 0;
31   }
32 
33   std::vector<std::string> args_;
34 };
35 
36 #ifdef _WIN32
TEST(CommandTest,LongFullyQualifiedPathWindows)37 TEST(CommandTest, LongFullyQualifiedPathWindows) {
38   TestCommand command;
39   std::string required_flag;
40   command.AddRequiredFlag("--rflag", "", &required_flag, Command::kPath);
41   Maybe<std::string> optional_flag;
42   command.AddOptionalFlag("--oflag", "", &optional_flag, Command::kPath);
43   std::vector<std::string> required_flag_list;
44   command.AddRequiredFlagList("--rlflag", "", &required_flag_list, Command::kPath);
45   std::vector<std::string> optional_flag_list;
46   command.AddOptionalFlagList("--olflag", "", &optional_flag_list, Command::kPath);
47   std::string non_path_flag;
48   command.AddRequiredFlag("--nflag", "", &non_path_flag);
49 
50   const std::string kLongPath =
51       "C:\\Users\\jedo\\_bazel_jedo\\vcmdctjv\\execroot\\__main__\\_tmp"
52       "\\6767b4778f8798efc0f784ee74fa70ee\\tests\\testApksAr8c7560a9a65"
53       "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build"
54       "\\intermediates\\processed_res\\minified\\processMinifiedResources"
55       "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build"
56       "\\intermediates\\processed_res\\minified\\processMinifiedResources"
57       "\\out\\resources-minified.ap_";
58 
59   const std::string kExpected =
60       "\\\\?\\C:\\Users\\jedo\\_bazel_jedo\\vcmdctjv\\execroot\\__main__\\_tmp"
61       "\\6767b4778f8798efc0f784ee74fa70ee\\tests\\testApksAr8c7560a9a65"
62       "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build"
63       "\\intermediates\\processed_res\\minified\\processMinifiedResources"
64       "\\1346ee7c014a089fb55d8c46cf3d9\\project\\baseModule\\build"
65       "\\intermediates\\processed_res\\minified\\processMinifiedResources"
66       "\\out\\resources-minified.ap_";
67 
68 
69   ASSERT_THAT(command.Execute({"--rflag", kLongPath,
70                                "--oflag", kLongPath,
71                                "--rlflag", kLongPath,
72                                "--rlflag", kLongPath,
73                                "--olflag", kLongPath,
74                                "--olflag", kLongPath,
75                                "--nflag", kLongPath,
76                                kLongPath, kLongPath}, &std::cerr), Eq(0));
77 
78   ASSERT_THAT(required_flag, Eq(kExpected));
79   ASSERT_THAT(optional_flag, Eq(kExpected));
80   ASSERT_THAT(required_flag_list.size(), Eq(2));
81   ASSERT_THAT(required_flag_list[0], Eq(kExpected));
82   ASSERT_THAT(required_flag_list[1], Eq(kExpected));
83   ASSERT_THAT(optional_flag_list.size(), Eq(2));
84   ASSERT_THAT(optional_flag_list[0], Eq(kExpected));
85   ASSERT_THAT(optional_flag_list[1], Eq(kExpected));
86 
87   // File arguments should also be converted to use the long path prefix
88   ASSERT_THAT(command.args_.size(), Eq(2));
89   ASSERT_THAT(command.args_[0], Eq(kExpected));
90   ASSERT_THAT(command.args_[1], Eq(kExpected));
91 
92   // Do not convert flags that are not marged as paths
93   ASSERT_THAT(non_path_flag, Eq(kLongPath));
94 }
95 #endif
96 
97 }  // namespace aapt