CommandLineTest.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //===- 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. #include "llvm/Support/CommandLine.h"
  10. #include "llvm/Config/config.h"
  11. #include "gtest/gtest.h"
  12. #include <stdlib.h>
  13. #include <string>
  14. using namespace llvm;
  15. namespace {
  16. class TempEnvVar {
  17. public:
  18. TempEnvVar(const char *name, const char *value)
  19. : name(name) {
  20. const char *old_value = getenv(name);
  21. EXPECT_EQ(NULL, old_value) << old_value;
  22. #if HAVE_SETENV
  23. setenv(name, value, true);
  24. #else
  25. # define SKIP_ENVIRONMENT_TESTS
  26. #endif
  27. }
  28. ~TempEnvVar() {
  29. #if HAVE_SETENV
  30. // Assume setenv and unsetenv come together.
  31. unsetenv(name);
  32. #endif
  33. }
  34. private:
  35. const char *const name;
  36. };
  37. #ifndef SKIP_ENVIRONMENT_TESTS
  38. const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
  39. cl::opt<std::string> EnvironmentTestOption("env-test-opt");
  40. TEST(CommandLineTest, ParseEnvironment) {
  41. TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
  42. EXPECT_EQ("", EnvironmentTestOption);
  43. cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
  44. EXPECT_EQ("hello", EnvironmentTestOption);
  45. }
  46. // This test used to make valgrind complain
  47. // ("Conditional jump or move depends on uninitialised value(s)")
  48. TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
  49. // Put cl::opt on stack to check for proper initialization of fields.
  50. cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
  51. TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
  52. EXPECT_EQ("", EnvironmentTestOptionLocal);
  53. cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
  54. EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
  55. }
  56. #endif // SKIP_ENVIRONMENT_TESTS
  57. } // anonymous namespace