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/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 #include "llvm/Support/PathV1.h"
14 #include "llvm/Support/Program.h"
15 #include "gtest/gtest.h"
18 #if defined(__APPLE__)
19 # include <crt_externs.h>
20 #elif !defined(_MSC_VER)
21 // Forward declare environ in case it's not provided by stdlib.h.
22 extern char **environ;
26 extern const char *TestMainArgv0;
33 static cl::opt<std::string>
34 ProgramTestStringArg1("program-test-string-arg1");
35 static cl::opt<std::string>
36 ProgramTestStringArg2("program-test-string-arg2");
38 static void CopyEnvironment(std::vector<const char *> &out) {
40 char **envp = *_NSGetEnviron();
42 // environ seems to work for Windows and most other Unices.
43 char **envp = environ;
51 TEST(ProgramTest, CreateProcessTrailingSlash) {
52 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
53 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
54 ProgramTestStringArg2 == "has\\\\ trailing\\") {
55 exit(0); // Success! The arguments were passed and parsed.
61 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
62 const char *argv[] = {
64 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
65 "-program-test-string-arg1", "has\\\\ trailing\\",
66 "-program-test-string-arg2", "has\\\\ trailing\\",
70 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
71 std::vector<const char *> envp;
72 CopyEnvironment(envp);
73 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
78 // Redirect stdout and stdin to NUL, but let stderr through.
82 StringRef nul("/dev/null");
84 const StringRef *redirects[] = { &nul, &nul, 0 };
85 int rc = ExecuteAndWait(my_exe, argv, &envp[0], redirects,
86 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
88 EXPECT_FALSE(ExecutionFailed) << error;
92 } // end anonymous namespace