summaryrefslogtreecommitdiff
path: root/unittests/Support/ProgramTest.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2018-07-28 10:51:19 +0000
committerDimitry Andric <dim@FreeBSD.org>2018-07-28 10:51:19 +0000
commiteb11fae6d08f479c0799db45860a98af528fa6e7 (patch)
tree44d492a50c8c1a7eb8e2d17ea3360ec4d066f042 /unittests/Support/ProgramTest.cpp
parentb8a2042aa938069e862750553db0e4d82d25822c (diff)
Notes
Diffstat (limited to 'unittests/Support/ProgramTest.cpp')
-rw-r--r--unittests/Support/ProgramTest.cpp75
1 files changed, 30 insertions, 45 deletions
diff --git a/unittests/Support/ProgramTest.cpp b/unittests/Support/ProgramTest.cpp
index 3c272bb980c5..ec1c85a9f25a 100644
--- a/unittests/Support/ProgramTest.cpp
+++ b/unittests/Support/ProgramTest.cpp
@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/Program.h"
+#include "llvm/Config/llvm-config.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/FileSystem.h"
@@ -26,7 +27,7 @@ extern char **environ;
void sleep_for(unsigned int seconds) {
sleep(seconds);
}
-#elif defined(LLVM_ON_WIN32)
+#elif defined(_WIN32)
#include <windows.h>
void sleep_for(unsigned int seconds) {
Sleep(seconds * 1000);
@@ -59,13 +60,13 @@ static cl::opt<std::string>
ProgramTestStringArg2("program-test-string-arg2");
class ProgramEnvTest : public testing::Test {
- std::vector<const char *> EnvTable;
+ std::vector<StringRef> EnvTable;
std::vector<std::string> EnvStorage;
protected:
void SetUp() override {
auto EnvP = [] {
-#if defined(LLVM_ON_WIN32)
+#if defined(_WIN32)
_wgetenv(L"TMP"); // Populate _wenviron, initially is null
return _wenviron;
#elif defined(__APPLE__)
@@ -76,8 +77,8 @@ protected:
}();
ASSERT_TRUE(EnvP);
- auto prepareEnvVar = [this](decltype(*EnvP) Var) {
-#if defined(LLVM_ON_WIN32)
+ auto prepareEnvVar = [this](decltype(*EnvP) Var) -> StringRef {
+#if defined(_WIN32)
// On Windows convert UTF16 encoded variable to UTF8
auto Len = wcslen(Var);
ArrayRef<char> Ref{reinterpret_cast<char const *>(Var),
@@ -85,10 +86,10 @@ protected:
EnvStorage.emplace_back();
auto convStatus = convertUTF16ToUTF8String(Ref, EnvStorage.back());
EXPECT_TRUE(convStatus);
- return EnvStorage.back().c_str();
+ return EnvStorage.back();
#else
(void)this;
- return Var;
+ return StringRef(Var);
#endif
};
@@ -103,19 +104,12 @@ protected:
EnvStorage.clear();
}
- void addEnvVar(const char *Var) {
- ASSERT_TRUE(EnvTable.empty() || EnvTable.back()) << "Env table sealed";
- EnvTable.emplace_back(Var);
- }
+ void addEnvVar(StringRef Var) { EnvTable.emplace_back(Var); }
- const char **getEnviron() {
- if (EnvTable.back() != nullptr)
- EnvTable.emplace_back(nullptr); // Seal table.
- return &EnvTable[0];
- }
+ ArrayRef<StringRef> getEnviron() const { return EnvTable; }
};
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
TEST_F(ProgramEnvTest, CreateProcessLongPath) {
if (getenv("LLVM_PROGRAM_TEST_LONG_PATH"))
exit(0);
@@ -128,11 +122,8 @@ TEST_F(ProgramEnvTest, CreateProcessLongPath) {
MyExe.append("\\\\?\\");
MyExe.append(MyAbsExe);
- const char *ArgV[] = {
- MyExe.c_str(),
- "--gtest_filter=ProgramEnvTest.CreateProcessLongPath",
- nullptr
- };
+ StringRef ArgV[] = {MyExe,
+ "--gtest_filter=ProgramEnvTest.CreateProcessLongPath"};
// Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
addEnvVar("LLVM_PROGRAM_TEST_LONG_PATH=1");
@@ -172,13 +163,13 @@ TEST_F(ProgramEnvTest, CreateProcessTrailingSlash) {
std::string my_exe =
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
- const char *argv[] = {
- my_exe.c_str(),
- "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
- "-program-test-string-arg1", "has\\\\ trailing\\",
- "-program-test-string-arg2", "has\\\\ trailing\\",
- nullptr
- };
+ StringRef argv[] = {
+ my_exe,
+ "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
+ "-program-test-string-arg1",
+ "has\\\\ trailing\\",
+ "-program-test-string-arg2",
+ "has\\\\ trailing\\"};
// Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
addEnvVar("LLVM_PROGRAM_TEST_CHILD=1");
@@ -186,7 +177,7 @@ TEST_F(ProgramEnvTest, CreateProcessTrailingSlash) {
std::string error;
bool ExecutionFailed;
// Redirect stdout and stdin to NUL, but let stderr through.
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
StringRef nul("NUL");
#else
StringRef nul("/dev/null");
@@ -209,11 +200,8 @@ TEST_F(ProgramEnvTest, TestExecuteNoWait) {
std::string Executable =
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
- const char *argv[] = {
- Executable.c_str(),
- "--gtest_filter=ProgramEnvTest.TestExecuteNoWait",
- nullptr
- };
+ StringRef argv[] = {Executable,
+ "--gtest_filter=ProgramEnvTest.TestExecuteNoWait"};
// Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
@@ -267,11 +255,8 @@ TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
std::string Executable =
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
- const char *argv[] = {
- Executable.c_str(),
- "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout",
- nullptr
- };
+ StringRef argv[] = {
+ Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout"};
// Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
@@ -286,12 +271,12 @@ TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
TEST(ProgramTest, TestExecuteNegative) {
std::string Executable = "i_dont_exist";
- const char *argv[] = { Executable.c_str(), nullptr };
+ StringRef argv[] = {Executable};
{
std::string Error;
bool ExecutionFailed;
- int RetCode = ExecuteAndWait(Executable, argv, nullptr, {}, 0, 0, &Error,
+ int RetCode = ExecuteAndWait(Executable, argv, llvm::None, {}, 0, 0, &Error,
&ExecutionFailed);
ASSERT_TRUE(RetCode < 0) << "On error ExecuteAndWait should return 0 or "
"positive value indicating the result code";
@@ -302,7 +287,7 @@ TEST(ProgramTest, TestExecuteNegative) {
{
std::string Error;
bool ExecutionFailed;
- ProcessInfo PI = ExecuteNoWait(Executable, argv, nullptr, {}, 0, &Error,
+ ProcessInfo PI = ExecuteNoWait(Executable, argv, llvm::None, {}, 0, &Error,
&ExecutionFailed);
ASSERT_EQ(PI.Pid, ProcessInfo::InvalidPid)
<< "On error ExecuteNoWait should return an invalid ProcessInfo";
@@ -312,7 +297,7 @@ TEST(ProgramTest, TestExecuteNegative) {
}
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
const char utf16le_text[] =
"\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61\x00";
const char utf16be_text[] =
@@ -332,7 +317,7 @@ TEST(ProgramTest, TestWriteWithSystemEncoding) {
sys::WEM_UTF16));
int fd = 0;
ASSERT_NO_ERROR(fs::openFileForRead(file_pathname.c_str(), fd));
-#if defined(LLVM_ON_WIN32)
+#if defined(_WIN32)
char buf[18];
ASSERT_EQ(::read(fd, buf, 18), 18);
if (strncmp(buf, "\xfe\xff", 2) == 0) { // UTF16-BE