03083aa68bf95951ad34f7831c47b15e9e8f8322
[oota-llvm.git] / unittests / Support / ProgramTest.cpp
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>
16
17 namespace {
18
19 using namespace llvm;
20 using namespace sys;
21
22 static cl::opt<std::string>
23 ProgramTestStringArg1("program-test-string-arg1");
24 static cl::opt<std::string>
25 ProgramTestStringArg2("program-test-string-arg2");
26
27 TEST(ProgramTest, CreateProcessTrailingSlash) {
28   if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
29     if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
30         ProgramTestStringArg2 == "has\\\\ trailing\\") {
31       exit(0);  // Success!  The arguments were passed and parsed.
32     }
33     exit(1);
34   }
35
36   // FIXME: Hardcoding argv0 here since I don't know a good cross-platform way
37   // to get it.  Maybe ParseCommandLineOptions() should save it?
38   Path my_exe = Path::GetMainExecutable("SupportTests", &ProgramTestStringArg1);
39   const char *argv[] = {
40     my_exe.c_str(),
41     "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
42     "-program-test-string-arg1", "has\\\\ trailing\\",
43     "-program-test-string-arg2", "has\\\\ trailing\\",
44     0
45   };
46   const char *envp[] = { "LLVM_PROGRAM_TEST_CHILD=1", 0 };
47   std::string error;
48   bool ExecutionFailed;
49   // Redirect stdout and stdin to NUL, but let stderr through.
50 #ifdef LLVM_ON_WIN32
51   Path nul("NUL");
52 #else
53   Path nul("/dev/null");
54 #endif
55   const Path *redirects[] = { &nul, &nul, 0 };
56   int rc = Program::ExecuteAndWait(my_exe, argv, envp, redirects,
57                                    /*secondsToWait=*/10, /*memoryLimit=*/0,
58                                    &error, &ExecutionFailed);
59   EXPECT_FALSE(ExecutionFailed) << error;
60   EXPECT_EQ(0, rc);
61 }
62
63 } // end anonymous namespace