1 //===- unittest/Support/ProgramTest.cpp -----------------------------------===//
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/Support/Path.h"
12 #include "llvm/Support/Program.h"
13 #include "gtest/gtest.h"
16 #if defined(__APPLE__)
17 # include <crt_externs.h>
18 #elif !defined(_MSC_VER)
19 // Forward declare environ in case it's not provided by stdlib.h.
20 extern char **environ;
24 extern const char *TestMainArgv0;
31 static cl::opt<std::string>
32 ProgramTestStringArg1("program-test-string-arg1");
33 static cl::opt<std::string>
34 ProgramTestStringArg2("program-test-string-arg2");
36 static void CopyEnvironment(std::vector<const char *> &out) {
38 char **envp = *_NSGetEnviron();
40 // environ seems to work for Windows and most other Unices.
41 char **envp = environ;
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.
58 Path my_exe = Path::GetMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
59 const char *argv[] = {
61 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
62 "-program-test-string-arg1", "has\\\\ trailing\\",
63 "-program-test-string-arg2", "has\\\\ trailing\\",
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");
75 // Redirect stdout and stdin to NUL, but let stderr through.
79 Path nul("/dev/null");
81 const Path *redirects[] = { &nul, &nul, 0 };
83 ExecuteAndWait(my_exe, argv, &envp[0], redirects, /*secondsToWait=*/ 10,
84 /*memoryLimit=*/ 0, &error, &ExecutionFailed);
85 EXPECT_FALSE(ExecutionFailed) << error;
89 } // end anonymous namespace