blob: 7886761c9aa582fd89d00d075746add0c386707b [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"
Rafael Espindola675e0ac2013-06-13 20:25:38 +000013#include "llvm/Support/PathV1.h"
Reid Kleckner0b675d82013-04-22 19:03:55 +000014#include "llvm/Support/Program.h"
15#include "gtest/gtest.h"
16
17#include <stdlib.h>
Reid Kleckner268adf22013-04-24 17:50:30 +000018#if defined(__APPLE__)
Reid Kleckner8eca6772013-04-23 13:15:51 +000019# include <crt_externs.h>
Reid Kleckner268adf22013-04-24 17:50:30 +000020#elif !defined(_MSC_VER)
Reid Kleckner8eca6772013-04-23 13:15:51 +000021// Forward declare environ in case it's not provided by stdlib.h.
22extern char **environ;
23#endif
Reid Kleckner0b675d82013-04-22 19:03:55 +000024
Reid Kleckner2a839432013-04-30 04:30:41 +000025// From TestMain.cpp.
26extern const char *TestMainArgv0;
27
Reid Kleckner0b675d82013-04-22 19:03:55 +000028namespace {
29
30using namespace llvm;
31using namespace sys;
32
33static cl::opt<std::string>
34ProgramTestStringArg1("program-test-string-arg1");
35static cl::opt<std::string>
36ProgramTestStringArg2("program-test-string-arg2");
37
Reid Kleckner8eca6772013-04-23 13:15:51 +000038static void CopyEnvironment(std::vector<const char *> &out) {
39#ifdef __APPLE__
Reid Kleckner8eca6772013-04-23 13:15:51 +000040 char **envp = *_NSGetEnviron();
41#else
42 // environ seems to work for Windows and most other Unices.
43 char **envp = environ;
44#endif
45 while (*envp != 0) {
46 out.push_back(*envp);
47 ++envp;
48 }
49}
50
Reid Kleckner0b675d82013-04-22 19:03:55 +000051TEST(ProgramTest, CreateProcessTrailingSlash) {
52 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
53 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
54 ProgramTestStringArg2 == "has\\\\ trailing\\") {
55 exit(0); // Success! The arguments were passed and parsed.
56 }
57 exit(1);
58 }
59
Rafael Espindola50188c12013-06-26 05:01:35 +000060 std::string my_exe =
61 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
Reid Kleckner0b675d82013-04-22 19:03:55 +000062 const char *argv[] = {
63 my_exe.c_str(),
64 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
65 "-program-test-string-arg1", "has\\\\ trailing\\",
66 "-program-test-string-arg2", "has\\\\ trailing\\",
67 0
68 };
Reid Kleckner8eca6772013-04-23 13:15:51 +000069
70 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
71 std::vector<const char *> envp;
72 CopyEnvironment(envp);
73 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
74 envp.push_back(0);
75
Reid Kleckner0b675d82013-04-22 19:03:55 +000076 std::string error;
77 bool ExecutionFailed;
78 // Redirect stdout and stdin to NUL, but let stderr through.
79#ifdef LLVM_ON_WIN32
Rafael Espindola675e0ac2013-06-13 20:25:38 +000080 StringRef nul("NUL");
Reid Kleckner0b675d82013-04-22 19:03:55 +000081#else
Rafael Espindola675e0ac2013-06-13 20:25:38 +000082 StringRef nul("/dev/null");
Reid Kleckner0b675d82013-04-22 19:03:55 +000083#endif
Rafael Espindola675e0ac2013-06-13 20:25:38 +000084 const StringRef *redirects[] = { &nul, &nul, 0 };
Rafael Espindola50188c12013-06-26 05:01:35 +000085 int rc = ExecuteAndWait(my_exe, argv, &envp[0], redirects,
Rafael Espindola675e0ac2013-06-13 20:25:38 +000086 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
87 &ExecutionFailed);
Reid Kleckner0b675d82013-04-22 19:03:55 +000088 EXPECT_FALSE(ExecutionFailed) << error;
89 EXPECT_EQ(0, rc);
90}
91
92} // end anonymous namespace