blob: 43c8cbd123b4bb2cc22cc899da42510625f795cd [file] [log] [blame]
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +00001//===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/CommandLine.h"
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000011#include "llvm/Config/config.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000012#include "gtest/gtest.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000013#include <stdlib.h>
Chandler Carruth5a88dda2012-12-04 10:23:08 +000014#include <string>
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000015
16using namespace llvm;
17
18namespace {
19
20class TempEnvVar {
21 public:
22 TempEnvVar(const char *name, const char *value)
23 : name(name) {
24 const char *old_value = getenv(name);
25 EXPECT_EQ(NULL, old_value) << old_value;
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000026#if HAVE_SETENV
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000027 setenv(name, value, true);
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000028#else
29# define SKIP_ENVIRONMENT_TESTS
30#endif
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000031 }
32
33 ~TempEnvVar() {
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000034#if HAVE_SETENV
35 // Assume setenv and unsetenv come together.
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000036 unsetenv(name);
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000037#endif
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000038 }
39
40 private:
41 const char *const name;
42};
43
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000044#ifndef SKIP_ENVIRONMENT_TESTS
45
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000046const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
47
48cl::opt<std::string> EnvironmentTestOption("env-test-opt");
49TEST(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);
54}
55
Alexander Kornienko8932fe42012-08-13 10:43:36 +000056// This test used to make valgrind complain
57// ("Conditional jump or move depends on uninitialised value(s)")
58TEST(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);
65}
66
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000067#endif // SKIP_ENVIRONMENT_TESTS
68
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000069} // anonymous namespace