2013-04-22 19:03:55 +00:00
|
|
|
//===- unittest/Support/ProgramTest.cpp -----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-06-26 05:01:35 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-04-22 19:03:55 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2013-04-24 17:50:30 +00:00
|
|
|
#if defined(__APPLE__)
|
2013-04-23 13:15:51 +00:00
|
|
|
# include <crt_externs.h>
|
2013-04-24 17:50:30 +00:00
|
|
|
#elif !defined(_MSC_VER)
|
2013-04-23 13:15:51 +00:00
|
|
|
// Forward declare environ in case it's not provided by stdlib.h.
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
2013-04-22 19:03:55 +00:00
|
|
|
|
2013-04-30 04:30:41 +00:00
|
|
|
// From TestMain.cpp.
|
|
|
|
extern const char *TestMainArgv0;
|
|
|
|
|
2013-04-22 19:03:55 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
ProgramTestStringArg1("program-test-string-arg1");
|
|
|
|
static cl::opt<std::string>
|
|
|
|
ProgramTestStringArg2("program-test-string-arg2");
|
|
|
|
|
2013-04-23 13:15:51 +00:00
|
|
|
static void CopyEnvironment(std::vector<const char *> &out) {
|
|
|
|
#ifdef __APPLE__
|
|
|
|
char **envp = *_NSGetEnviron();
|
|
|
|
#else
|
|
|
|
// environ seems to work for Windows and most other Unices.
|
|
|
|
char **envp = environ;
|
|
|
|
#endif
|
|
|
|
while (*envp != 0) {
|
|
|
|
out.push_back(*envp);
|
|
|
|
++envp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 19:03:55 +00:00
|
|
|
TEST(ProgramTest, CreateProcessTrailingSlash) {
|
|
|
|
if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
|
|
|
|
if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
|
|
|
|
ProgramTestStringArg2 == "has\\\\ trailing\\") {
|
|
|
|
exit(0); // Success! The arguments were passed and parsed.
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-06-26 05:01:35 +00:00
|
|
|
std::string my_exe =
|
|
|
|
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
|
2013-04-22 19:03:55 +00:00
|
|
|
const char *argv[] = {
|
|
|
|
my_exe.c_str(),
|
|
|
|
"--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
|
|
|
|
"-program-test-string-arg1", "has\\\\ trailing\\",
|
|
|
|
"-program-test-string-arg2", "has\\\\ trailing\\",
|
|
|
|
0
|
|
|
|
};
|
2013-04-23 13:15:51 +00:00
|
|
|
|
|
|
|
// Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
|
|
|
|
std::vector<const char *> envp;
|
|
|
|
CopyEnvironment(envp);
|
|
|
|
envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
|
|
|
|
envp.push_back(0);
|
|
|
|
|
2013-04-22 19:03:55 +00:00
|
|
|
std::string error;
|
|
|
|
bool ExecutionFailed;
|
|
|
|
// Redirect stdout and stdin to NUL, but let stderr through.
|
|
|
|
#ifdef LLVM_ON_WIN32
|
2013-06-13 20:25:38 +00:00
|
|
|
StringRef nul("NUL");
|
2013-04-22 19:03:55 +00:00
|
|
|
#else
|
2013-06-13 20:25:38 +00:00
|
|
|
StringRef nul("/dev/null");
|
2013-04-22 19:03:55 +00:00
|
|
|
#endif
|
2013-06-13 20:25:38 +00:00
|
|
|
const StringRef *redirects[] = { &nul, &nul, 0 };
|
2013-06-26 05:01:35 +00:00
|
|
|
int rc = ExecuteAndWait(my_exe, argv, &envp[0], redirects,
|
2013-06-13 20:25:38 +00:00
|
|
|
/*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
|
|
|
|
&ExecutionFailed);
|
2013-04-22 19:03:55 +00:00
|
|
|
EXPECT_FALSE(ExecutionFailed) << error;
|
|
|
|
EXPECT_EQ(0, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|