2009-09-24 01:14:07 +00:00
|
|
|
//===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-09-25 21:07:20 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2009-09-24 01:14:07 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TempEnvVar {
|
|
|
|
public:
|
|
|
|
TempEnvVar(const char *name, const char *value)
|
|
|
|
: name(name) {
|
|
|
|
const char *old_value = getenv(name);
|
|
|
|
EXPECT_EQ(NULL, old_value) << old_value;
|
2009-09-25 21:07:20 +00:00
|
|
|
#if HAVE_SETENV
|
2009-09-24 01:14:07 +00:00
|
|
|
setenv(name, value, true);
|
2009-09-25 21:07:20 +00:00
|
|
|
#else
|
|
|
|
# define SKIP_ENVIRONMENT_TESTS
|
|
|
|
#endif
|
2009-09-24 01:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~TempEnvVar() {
|
2009-09-25 21:07:20 +00:00
|
|
|
#if HAVE_SETENV
|
|
|
|
// Assume setenv and unsetenv come together.
|
2009-09-24 01:14:07 +00:00
|
|
|
unsetenv(name);
|
2009-09-25 21:07:20 +00:00
|
|
|
#endif
|
2009-09-24 01:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char *const name;
|
|
|
|
};
|
|
|
|
|
2009-09-25 21:07:20 +00:00
|
|
|
#ifndef SKIP_ENVIRONMENT_TESTS
|
|
|
|
|
2009-09-24 01:14:07 +00:00
|
|
|
const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
|
|
|
|
|
|
|
|
cl::opt<std::string> EnvironmentTestOption("env-test-opt");
|
|
|
|
TEST(CommandLineTest, ParseEnvironment) {
|
|
|
|
TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
|
|
|
|
EXPECT_EQ("", EnvironmentTestOption);
|
|
|
|
cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
|
|
|
|
EXPECT_EQ("hello", EnvironmentTestOption);
|
|
|
|
}
|
|
|
|
|
2012-08-13 10:43:36 +00:00
|
|
|
// This test used to make valgrind complain
|
|
|
|
// ("Conditional jump or move depends on uninitialised value(s)")
|
|
|
|
TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
|
|
|
|
// Put cl::opt on stack to check for proper initialization of fields.
|
|
|
|
cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
|
|
|
|
TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
|
|
|
|
EXPECT_EQ("", EnvironmentTestOptionLocal);
|
|
|
|
cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
|
|
|
|
EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:07:20 +00:00
|
|
|
#endif // SKIP_ENVIRONMENT_TESTS
|
|
|
|
|
2009-09-24 01:14:07 +00:00
|
|
|
} // anonymous namespace
|