Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 1 | //===- unittest/Support/ProgramTest.cpp -----------------------------------===// |
| 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" |
| 11 | #include "llvm/Support/Path.h" |
| 12 | #include "llvm/Support/Program.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | #include <stdlib.h> |
Reid Kleckner | 268adf2 | 2013-04-24 17:50:30 +0000 | [diff] [blame] | 16 | #if defined(__APPLE__) |
Reid Kleckner | 8eca677 | 2013-04-23 13:15:51 +0000 | [diff] [blame] | 17 | # include <crt_externs.h> |
Reid Kleckner | 268adf2 | 2013-04-24 17:50:30 +0000 | [diff] [blame] | 18 | #elif !defined(_MSC_VER) |
Reid Kleckner | 8eca677 | 2013-04-23 13:15:51 +0000 | [diff] [blame] | 19 | // Forward declare environ in case it's not provided by stdlib.h. |
| 20 | extern char **environ; |
| 21 | #endif |
Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 22 | |
Reid Kleckner | 2a83943 | 2013-04-30 04:30:41 +0000 | [diff] [blame] | 23 | // From TestMain.cpp. |
| 24 | extern const char *TestMainArgv0; |
| 25 | |
Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace sys; |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | ProgramTestStringArg1("program-test-string-arg1"); |
| 33 | static cl::opt<std::string> |
| 34 | ProgramTestStringArg2("program-test-string-arg2"); |
| 35 | |
Reid Kleckner | 8eca677 | 2013-04-23 13:15:51 +0000 | [diff] [blame] | 36 | static void CopyEnvironment(std::vector<const char *> &out) { |
| 37 | #ifdef __APPLE__ |
Reid Kleckner | 8eca677 | 2013-04-23 13:15:51 +0000 | [diff] [blame] | 38 | char **envp = *_NSGetEnviron(); |
| 39 | #else |
| 40 | // environ seems to work for Windows and most other Unices. |
| 41 | char **envp = environ; |
| 42 | #endif |
| 43 | while (*envp != 0) { |
| 44 | out.push_back(*envp); |
| 45 | ++envp; |
| 46 | } |
| 47 | } |
| 48 | |
Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 49 | TEST(ProgramTest, CreateProcessTrailingSlash) { |
| 50 | if (getenv("LLVM_PROGRAM_TEST_CHILD")) { |
| 51 | if (ProgramTestStringArg1 == "has\\\\ trailing\\" && |
| 52 | ProgramTestStringArg2 == "has\\\\ trailing\\") { |
| 53 | exit(0); // Success! The arguments were passed and parsed. |
| 54 | } |
| 55 | exit(1); |
| 56 | } |
| 57 | |
Reid Kleckner | 2a83943 | 2013-04-30 04:30:41 +0000 | [diff] [blame] | 58 | Path my_exe = Path::GetMainExecutable(TestMainArgv0, &ProgramTestStringArg1); |
Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 59 | const char *argv[] = { |
| 60 | my_exe.c_str(), |
| 61 | "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild", |
| 62 | "-program-test-string-arg1", "has\\\\ trailing\\", |
| 63 | "-program-test-string-arg2", "has\\\\ trailing\\", |
| 64 | 0 |
| 65 | }; |
Reid Kleckner | 8eca677 | 2013-04-23 13:15:51 +0000 | [diff] [blame] | 66 | |
| 67 | // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child. |
| 68 | std::vector<const char *> envp; |
| 69 | CopyEnvironment(envp); |
| 70 | envp.push_back("LLVM_PROGRAM_TEST_CHILD=1"); |
| 71 | envp.push_back(0); |
| 72 | |
Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 73 | std::string error; |
| 74 | bool ExecutionFailed; |
| 75 | // Redirect stdout and stdin to NUL, but let stderr through. |
| 76 | #ifdef LLVM_ON_WIN32 |
| 77 | Path nul("NUL"); |
| 78 | #else |
| 79 | Path nul("/dev/null"); |
| 80 | #endif |
| 81 | const Path *redirects[] = { &nul, &nul, 0 }; |
Rafael Espindola | 9f1d9fd | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 82 | int rc = |
| 83 | ExecuteAndWait(my_exe, argv, &envp[0], redirects, /*secondsToWait=*/ 10, |
| 84 | /*memoryLimit=*/ 0, &error, &ExecutionFailed); |
Reid Kleckner | 0b675d8 | 2013-04-22 19:03:55 +0000 | [diff] [blame] | 85 | EXPECT_FALSE(ExecutionFailed) << error; |
| 86 | EXPECT_EQ(0, rc); |
| 87 | } |
| 88 | |
| 89 | } // end anonymous namespace |