1 //===- unittest/Support/ProgramTest.cpp -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 #include "llvm/Support/Program.h"
14 #include "gtest/gtest.h"
17 #if defined(__APPLE__)
18 # include <crt_externs.h>
19 #elif !defined(_MSC_VER)
20 // Forward declare environ in case it's not provided by stdlib.h.
21 extern char **environ;
24 #if defined(LLVM_ON_UNIX)
26 void sleep_for(unsigned int seconds) {
29 #elif defined(LLVM_ON_WIN32)
31 void sleep_for(unsigned int seconds) {
32 Sleep(seconds * 1000);
35 #error sleep_for is not implemented on your platform.
39 extern const char *TestMainArgv0;
46 static cl::opt<std::string>
47 ProgramTestStringArg1("program-test-string-arg1");
48 static cl::opt<std::string>
49 ProgramTestStringArg2("program-test-string-arg2");
51 static void CopyEnvironment(std::vector<const char *> &out) {
53 char **envp = *_NSGetEnviron();
55 // environ seems to work for Windows and most other Unices.
56 char **envp = environ;
64 TEST(ProgramTest, CreateProcessTrailingSlash) {
65 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
66 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
67 ProgramTestStringArg2 == "has\\\\ trailing\\") {
68 exit(0); // Success! The arguments were passed and parsed.
74 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
75 const char *argv[] = {
77 "--gtest_filter=ProgramTest.CreateProcessTrailingSlash",
78 "-program-test-string-arg1", "has\\\\ trailing\\",
79 "-program-test-string-arg2", "has\\\\ trailing\\",
83 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
84 std::vector<const char *> envp;
85 CopyEnvironment(envp);
86 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
91 // Redirect stdout and stdin to NUL, but let stderr through.
95 StringRef nul("/dev/null");
97 const StringRef *redirects[] = { &nul, &nul, 0 };
98 int rc = ExecuteAndWait(my_exe, argv, &envp[0], redirects,
99 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
101 EXPECT_FALSE(ExecutionFailed) << error;
105 TEST(ProgramTest, TestExecuteNoWait) {
106 using namespace llvm::sys;
108 if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT")) {
109 sleep_for(/*seconds*/ 1);
113 std::string Executable =
114 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
115 const char *argv[] = {
117 "--gtest_filter=ProgramTest.TestExecuteNoWait",
121 // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
122 std::vector<const char *> envp;
123 CopyEnvironment(envp);
124 envp.push_back("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
128 bool ExecutionFailed;
130 ExecuteNoWait(Executable, argv, &envp[0], 0, 0, &Error, &ExecutionFailed);
131 ASSERT_FALSE(ExecutionFailed) << Error;
132 ASSERT_NE(PI1.Pid, 0) << "Invalid process id";
134 unsigned LoopCount = 0;
136 // Test that Wait() with WaitUntilTerminates=true works. In this case,
137 // LoopCount should only be incremented once.
140 ProcessInfo WaitResult = Wait(PI1, 0, true, &Error);
141 ASSERT_TRUE(Error.empty());
142 if (WaitResult.Pid == PI1.Pid)
146 EXPECT_EQ(LoopCount, 1u) << "LoopCount should be 1";
149 ExecuteNoWait(Executable, argv, &envp[0], 0, 0, &Error, &ExecutionFailed);
150 ASSERT_FALSE(ExecutionFailed) << Error;
151 ASSERT_NE(PI2.Pid, 0) << "Invalid process id";
153 // Test that Wait() with SecondsToWait=0 performs a non-blocking wait. In this
154 // cse, LoopCount should be greater than 1 (more than one increment occurs).
157 ProcessInfo WaitResult = Wait(PI2, 0, false, &Error);
158 ASSERT_TRUE(Error.empty());
159 if (WaitResult.Pid == PI2.Pid)
163 ASSERT_GT(LoopCount, 1u) << "LoopCount should be >1";
166 TEST(ProgramTest, TestExecuteNegative) {
167 std::string Executable = "i_dont_exist";
168 const char *argv[] = { Executable.c_str(), 0 };
172 bool ExecutionFailed;
174 ExecuteAndWait(Executable, argv, 0, 0, 0, 0, &Error, &ExecutionFailed);
175 ASSERT_TRUE(RetCode < 0) << "On error ExecuteAndWait should return 0 or "
176 "positive value indicating the result code";
177 ASSERT_TRUE(ExecutionFailed);
178 ASSERT_FALSE(Error.empty());
183 bool ExecutionFailed;
185 ExecuteNoWait(Executable, argv, 0, 0, 0, &Error, &ExecutionFailed);
187 << "On error ExecuteNoWait should return an invalid ProcessInfo";
188 ASSERT_TRUE(ExecutionFailed);
189 ASSERT_FALSE(Error.empty());
194 } // end anonymous namespace