blob: 6852ca616e08aa9d48af8d244c25e1117ade8205 [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"
Rafael Espindola50188c12013-06-26 05:01:35 +000011#include "llvm/Support/FileSystem.h"
Reid Kleckner0b675d82013-04-22 19:03:55 +000012#include "llvm/Support/Path.h"
13#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
Rafael Espindola50188c12013-06-26 05:01:35 +000059 std::string my_exe =
60 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
Reid Kleckner0b675d82013-04-22 19:03:55 +000061 const char *argv[] = {
62 my_exe.c_str(),
63 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
64 "-program-test-string-arg1", "has\\\\ trailing\\",
65 "-program-test-string-arg2", "has\\\\ trailing\\",
66 0
67 };
Reid Kleckner8eca6772013-04-23 13:15:51 +000068
69 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
70 std::vector<const char *> envp;
71 CopyEnvironment(envp);
72 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
73 envp.push_back(0);
74
Reid Kleckner0b675d82013-04-22 19:03:55 +000075 std::string error;
76 bool ExecutionFailed;
77 // Redirect stdout and stdin to NUL, but let stderr through.
78#ifdef LLVM_ON_WIN32
Rafael Espindola675e0ac2013-06-13 20:25:38 +000079 StringRef nul("NUL");
Reid Kleckner0b675d82013-04-22 19:03:55 +000080#else
Rafael Espindola675e0ac2013-06-13 20:25:38 +000081 StringRef nul("/dev/null");
Reid Kleckner0b675d82013-04-22 19:03:55 +000082#endif
Rafael Espindola675e0ac2013-06-13 20:25:38 +000083 const StringRef *redirects[] = { &nul, &nul, 0 };
Rafael Espindola50188c12013-06-26 05:01:35 +000084 int rc = ExecuteAndWait(my_exe, argv, &envp[0], redirects,
Rafael Espindola675e0ac2013-06-13 20:25:38 +000085 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
86 &ExecutionFailed);
Reid Kleckner0b675d82013-04-22 19:03:55 +000087 EXPECT_FALSE(ExecutionFailed) << error;
88 EXPECT_EQ(0, rc);
89}
90
91} // end anonymous namespace