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"
22 static cl::opt<std::string>
23 ProgramTestStringArg1("program-test-string-arg1");
24 static cl::opt<std::string>
25 ProgramTestStringArg2("program-test-string-arg2");
27 static void CopyEnvironment(std::vector<const char *> out) {
28 // environ appears to be pretty portable.
29 char **envp = environ;
36 TEST(ProgramTest, CreateProcessTrailingSlash) {
37 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
38 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
39 ProgramTestStringArg2 == "has\\\\ trailing\\") {
40 exit(0); // Success! The arguments were passed and parsed.
45 // FIXME: Hardcoding argv0 here since I don't know a good cross-platform way
46 // to get it. Maybe ParseCommandLineOptions() should save it?
47 Path my_exe = Path::GetMainExecutable("SupportTests", &ProgramTestStringArg1);
48 const char *argv[] = {
50 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
51 "-program-test-string-arg1", "has\\\\ trailing\\",
52 "-program-test-string-arg2", "has\\\\ trailing\\",
56 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
57 std::vector<const char *> envp;
58 CopyEnvironment(envp);
59 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
64 // Redirect stdout and stdin to NUL, but let stderr through.
68 Path nul("/dev/null");
70 const Path *redirects[] = { &nul, &nul, 0 };
71 int rc = Program::ExecuteAndWait(my_exe, argv, &envp[0], redirects,
72 /*secondsToWait=*/10, /*memoryLimit=*/0,
73 &error, &ExecutionFailed);
74 EXPECT_FALSE(ExecutionFailed) << error;
78 } // end anonymous namespace