blob: 1cf53d50b0241b8f932fa08acf7183ead04bd7cd [file] [log] [blame]
Reid Kleckner0b675d82013-04-22 19:03:55 +00001//===- 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"
Rafael Espindola675e0ac2013-06-13 20:25:38 +000012#include "llvm/Support/PathV1.h"
Reid Kleckner0b675d82013-04-22 19:03:55 +000013#include "llvm/Support/Program.h"
14#include "gtest/gtest.h"
15
16#include <stdlib.h>
Reid Kleckner268adf22013-04-24 17:50:30 +000017#if defined(__APPLE__)
Reid Kleckner8eca6772013-04-23 13:15:51 +000018# include <crt_externs.h>
Reid Kleckner268adf22013-04-24 17:50:30 +000019#elif !defined(_MSC_VER)
Reid Kleckner8eca6772013-04-23 13:15:51 +000020// Forward declare environ in case it's not provided by stdlib.h.
21extern char **environ;
22#endif
Reid Kleckner0b675d82013-04-22 19:03:55 +000023
Reid Kleckner2a839432013-04-30 04:30:41 +000024// From TestMain.cpp.
25extern const char *TestMainArgv0;
26
Reid Kleckner0b675d82013-04-22 19:03:55 +000027namespace {
28
29using namespace llvm;
30using namespace sys;
31
32static cl::opt<std::string>
33ProgramTestStringArg1("program-test-string-arg1");
34static cl::opt<std::string>
35ProgramTestStringArg2("program-test-string-arg2");
36
Reid Kleckner8eca6772013-04-23 13:15:51 +000037static void CopyEnvironment(std::vector<const char *> &out) {
38#ifdef __APPLE__
Reid Kleckner8eca6772013-04-23 13:15:51 +000039 char **envp = *_NSGetEnviron();
40#else
41 // environ seems to work for Windows and most other Unices.
42 char **envp = environ;
43#endif
44 while (*envp != 0) {
45 out.push_back(*envp);
46 ++envp;
47 }
48}
49
Reid Kleckner0b675d82013-04-22 19:03:55 +000050TEST(ProgramTest, CreateProcessTrailingSlash) {
51 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
52 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
53 ProgramTestStringArg2 == "has\\\\ trailing\\") {
54 exit(0); // Success! The arguments were passed and parsed.
55 }
56 exit(1);
57 }
58
Reid Kleckner2a839432013-04-30 04:30:41 +000059 Path my_exe = Path::GetMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
Reid Kleckner0b675d82013-04-22 19:03:55 +000060 const char *argv[] = {
61 my_exe.c_str(),
62 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
63 "-program-test-string-arg1", "has\\\\ trailing\\",
64 "-program-test-string-arg2", "has\\\\ trailing\\",
65 0
66 };
Reid Kleckner8eca6772013-04-23 13:15:51 +000067
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");
72 envp.push_back(0);
73
Reid Kleckner0b675d82013-04-22 19:03:55 +000074 std::string error;
75 bool ExecutionFailed;
76 // Redirect stdout and stdin to NUL, but let stderr through.
77#ifdef LLVM_ON_WIN32
Rafael Espindola675e0ac2013-06-13 20:25:38 +000078 StringRef nul("NUL");
Reid Kleckner0b675d82013-04-22 19:03:55 +000079#else
Rafael Espindola675e0ac2013-06-13 20:25:38 +000080 StringRef nul("/dev/null");
Reid Kleckner0b675d82013-04-22 19:03:55 +000081#endif
Rafael Espindola675e0ac2013-06-13 20:25:38 +000082 const StringRef *redirects[] = { &nul, &nul, 0 };
83 int rc = ExecuteAndWait(my_exe.str(), argv, &envp[0], redirects,
84 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
85 &ExecutionFailed);
Reid Kleckner0b675d82013-04-22 19:03:55 +000086 EXPECT_FALSE(ExecutionFailed) << error;
87 EXPECT_EQ(0, rc);
88}
89
90} // end anonymous namespace