2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/ProgramOptions.h>
19 #include <folly/FileUtil.h>
20 #include <folly/Subprocess.h>
21 #include <folly/experimental/io/FsUtil.h>
22 #include <glog/logging.h>
23 #include <gtest/gtest.h>
25 namespace folly { namespace test {
29 std::string getHelperPath() {
30 auto path = fs::executable_path();
31 path.remove_filename() /= "program_options_test_helper";
35 std::string callHelper(ProgramOptionsStyle style,
36 std::initializer_list<std::string> args) {
37 static std::string helperPath = getHelperPath();
39 std::vector<std::string> allArgs;
40 allArgs.reserve(args.size() + 1);
41 allArgs.push_back(helperPath);
42 allArgs.insert(allArgs.end(), args.begin(), args.end());
44 std::vector<std::string> env;
46 case ProgramOptionsStyle::GNU:
47 env.push_back("PROGRAM_OPTIONS_TEST_STYLE=GNU");
49 case ProgramOptionsStyle::GFLAGS:
50 env.push_back("PROGRAM_OPTIONS_TEST_STYLE=GFLAGS");
54 Subprocess proc(allArgs, Subprocess::pipeStdout(), nullptr, &env);
55 auto p = proc.communicate();
56 EXPECT_EQ(0, proc.wait().exitStatus());
65 TEST(ProgramOptionsTest, GFlagsStyleDefaultValues) {
71 callHelper(ProgramOptionsStyle::GFLAGS, {}));
74 TEST(ProgramOptionsTest, GFlagsStyleFlagsSet) {
80 callHelper(ProgramOptionsStyle::GFLAGS, {
84 "--flag_string", "bar"}));
87 TEST(ProgramOptionsTest, GFlagsStyleBoolFlagsNegation) {
93 callHelper(ProgramOptionsStyle::GFLAGS, {
95 "--noflag_bool_false"}));
98 TEST(ProgramOptionsTest, GNUStyleDefaultValues) {
101 "flag-bool-false 0\n"
104 callHelper(ProgramOptionsStyle::GNU, {}));
107 TEST(ProgramOptionsTest, GNUStyleFlagsSet) {
110 "flag-bool-false 1\n"
113 callHelper(ProgramOptionsStyle::GNU, {
117 "--flag-string", "bar"}));
120 TEST(ProgramOptionsTest, GNUStyleBoolFlagsNegation) {
123 "flag-bool-false 0\n"
126 callHelper(ProgramOptionsStyle::GNU, {
127 "--no-flag-bool-true",
128 "--no-flag-bool-false"}));
131 TEST(ProgramOptionsTest, GNUStyleSubCommand) {
134 "flag-bool-false 1\n"
142 callHelper(ProgramOptionsStyle::GNU, {
152 TEST(ProgramOptionsTest, GNUStyleSubCommandUnrecognizedOptionFirst) {
155 "flag-bool-false 1\n"
163 callHelper(ProgramOptionsStyle::GNU, {