1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 
22 #include <cctype>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 namespace android {
28 namespace gtest_extras {
29 
30 class Options {
31  public:
32   Options() = default;
33   ~Options() = default;
34 
35   bool Process(const std::vector<const char*>& args, std::vector<char*>* child_args);
36 
job_count()37   size_t job_count() const { return job_count_; }
num_iterations()38   int num_iterations() const { return num_iterations_; }
stop_on_error()39   bool stop_on_error() const { return stop_on_error_; }
error()40   const std::string& error() const { return error_; }
41 
deadline_threshold_ms()42   uint64_t deadline_threshold_ms() const { return numerics_.at("deadline_threshold_ms"); }
slow_threshold_ms()43   uint64_t slow_threshold_ms() const { return numerics_.at("slow_threshold_ms"); }
44 
shard_index()45   uint64_t shard_index() const { return numerics_.at("gtest_shard_index"); }
total_shards()46   uint64_t total_shards() const { return numerics_.at("gtest_total_shards"); }
47 
print_time()48   bool print_time() const { return bools_.at("gtest_print_time"); }
allow_disabled_tests()49   bool allow_disabled_tests() const { return bools_.at("gtest_also_run_disabled_tests"); }
list_tests()50   bool list_tests() const { return bools_.at("gtest_list_tests"); }
51 
color()52   const std::string& color() const { return strings_.at("gtest_color"); }
xml_file()53   const std::string& xml_file() const { return strings_.at("xml_file"); }
filter()54   const std::string& filter() const { return strings_.at("gtest_filter"); }
55 
56  private:
57   size_t job_count_;
58   int num_iterations_;
59   bool stop_on_error_;
60   std::string error_;
61 
62   std::unordered_map<std::string, bool> bools_;
63   std::unordered_map<std::string, std::string> strings_;
64   std::unordered_map<std::string, uint64_t> numerics_;
65 
66   enum FlagType : uint32_t {
67     FLAG_NONE = 0,
68     FLAG_CHILD = 0x1,                 // Argument preserved for forked child call.
69     FLAG_INCOMPATIBLE = 0x2,          // Not compatible with isolation mode.
70     FLAG_ENVIRONMENT_VARIABLE = 0x4,  // Can be an environment variable.
71     FLAG_REQUIRES_VALUE = 0x8,        // Flag requires a non-empty value.
72     FLAG_OPTIONAL_VALUE = 0x10,       // Flag takes an optional value.
73   };
74   static constexpr uint32_t FLAG_TAKES_VALUE = FLAG_REQUIRES_VALUE | FLAG_OPTIONAL_VALUE;
75 
76   struct ArgInfo {
77     uint32_t flags;
78     bool (Options::*func)(const std::string&, const std::string&, bool);
79   };
80 
81   bool HandleArg(const std::string& arg, const std::string& value, const ArgInfo& info,
82                  bool from_env = false);
83 
84   bool ProcessFlagfile(const std::string& file, std::vector<char*>* child_args);
85   bool ProcessSingle(const char* arg, std::vector<char*>* child_args, bool allow_flagfile);
86 
87   bool SetNumeric(const std::string&, const std::string&, bool);
88   bool SetNumericEnvOnly(const std::string&, const std::string&, bool);
89   bool SetBool(const std::string&, const std::string&, bool);
90   bool SetString(const std::string&, const std::string&, bool);
91   bool SetIterations(const std::string&, const std::string&, bool);
92   bool SetXmlFile(const std::string&, const std::string&, bool);
93   bool SetPrintTime(const std::string&, const std::string&, bool);
94 
95   const static std::unordered_map<std::string, ArgInfo> kArgs;
96 };
97 
98 }  // namespace gtest_extras
99 }  // namespace android
100