1 //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/Config/config.h"
12 #include "gtest/gtest.h"
22 TempEnvVar(const char *name, const char *value)
24 const char *old_value = getenv(name);
25 EXPECT_EQ(NULL, old_value) << old_value;
27 setenv(name, value, true);
29 # define SKIP_ENVIRONMENT_TESTS
35 // Assume setenv and unsetenv come together.
41 const char *const name;
44 #ifndef SKIP_ENVIRONMENT_TESTS
46 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
48 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
49 TEST(CommandLineTest, ParseEnvironment) {
50 TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
51 EXPECT_EQ("", EnvironmentTestOption);
52 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
53 EXPECT_EQ("hello", EnvironmentTestOption);
56 // This test used to make valgrind complain
57 // ("Conditional jump or move depends on uninitialised value(s)")
58 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
59 // Put cl::opt on stack to check for proper initialization of fields.
60 cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
61 TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
62 EXPECT_EQ("", EnvironmentTestOptionLocal);
63 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
64 EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
67 #endif // SKIP_ENVIRONMENT_TESTS
69 } // anonymous namespace