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"
17 # include <crt_externs.h>
19 // Forward declare environ in case it's not provided by stdlib.h.
20 extern char **environ;
28 static cl::opt<std::string>
29 ProgramTestStringArg1("program-test-string-arg1");
30 static cl::opt<std::string>
31 ProgramTestStringArg2("program-test-string-arg2");
33 static void CopyEnvironment(std::vector<const char *> &out) {
35 // _NSGetEnviron() only works from the main exe on Mac. Fortunately the test
36 // should be in the executable.
37 char **envp = *_NSGetEnviron();
39 // environ seems to work for Windows and most other Unices.
40 char **envp = environ;
48 TEST(ProgramTest, CreateProcessTrailingSlash) {
49 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
50 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
51 ProgramTestStringArg2 == "has\\\\ trailing\\") {
52 exit(0); // Success! The arguments were passed and parsed.
57 // FIXME: Hardcoding argv0 here since I don't know a good cross-platform way
58 // to get it. Maybe ParseCommandLineOptions() should save it?
59 Path my_exe = Path::GetMainExecutable("SupportTests", &ProgramTestStringArg1);
60 const char *argv[] = {
62 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
63 "-program-test-string-arg1", "has\\\\ trailing\\",
64 "-program-test-string-arg2", "has\\\\ trailing\\",
68 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
69 std::vector<const char *> envp;
70 CopyEnvironment(envp);
71 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
76 // Redirect stdout and stdin to NUL, but let stderr through.
80 Path nul("/dev/null");
82 const Path *redirects[] = { &nul, &nul, 0 };
83 int rc = Program::ExecuteAndWait(my_exe, argv, &envp[0], redirects,
84 /*secondsToWait=*/10, /*memoryLimit=*/0,
85 &error, &ExecutionFailed);
86 EXPECT_FALSE(ExecutionFailed) << error;
90 } // end anonymous namespace