Break out LocalChanges into a separate functor.
[hiphop-php.git] / hphp / test / ext / test_logger.cpp
blob3160bec28a00fdad52147fcbede34cb9dcb4dc7b
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/test/ext/test_logger.h"
18 #include <pwd.h>
19 #include <sys/param.h>
20 #include "hphp/runtime/base/array-init.h"
21 #include "hphp/runtime/base/http-client.h"
22 #include "hphp/runtime/ext/json/ext_json.h"
23 #include "hphp/runtime/ext/mbstring/ext_mbstring.h"
24 #include "hphp/runtime/ext/std/ext_std_file.h"
25 #include "hphp/runtime/ext/url/ext_url.h"
27 #include <folly/portability/Unistd.h>
29 using namespace HPHP;
31 ///////////////////////////////////////////////////////////////////////////////
32 static const StaticString
33 s_result("result"),
34 s_runId("runId");
36 bool TestLogger::initializeRun() {
37 static const StaticString
38 s_result("result"),
39 s_runId("runId");
40 if (!doLog())
41 return true;
43 char buf[100];
44 gethostname(buf, sizeof(buf));
45 buf[sizeof(buf) - 1] = '\0';
46 std::string hostname = buf;
48 ArrayInit data(8, ArrayInit::Map{});
49 data.set(String("startedTime"), time(nullptr));
50 data.set(String("stillRunning"), true);
51 data.set(String("hostname"), hostname);
52 data.set(String("username"), getpwuid(getuid())->pw_name);
53 data.set(String("repository"), getRepoRoot());
54 data.set(String("svnRevision"), getSVNRevision());
55 data.set(String("gitRevision"), getGitRevision());
56 data.set(String("tags"), make_packed_array("hphp", "c++"));
58 auto dataArr = data.toArray();
60 Array response = postData(make_map_array("runData", dataArr));
62 if (!response[s_result].toBoolean()) {
63 return false;
66 return true;
69 bool TestLogger::finishRun() {
70 if (!doLog())
71 return true;
72 if (run_id <= 0)
73 return false;
75 Array data = make_map_array("runId", run_id,
76 "runData", make_map_array("stillRunning", false));
78 Array response = postData(data);
79 if (response[s_result].toBoolean()) {
80 return true;
82 return false;
85 bool TestLogger::logTest(Array test) {
86 if (!doLog())
87 return true;
88 if (run_id <= 0)
89 return false;
91 Array data = make_map_array("runId", run_id,
92 "runData", make_map_array("stillRunning", true),
93 "tests", make_packed_array(test));
95 Array response = postData(data);
96 if (response[s_result].toBoolean()) {
97 return true;
99 return false;
102 bool TestLogger::doLog() {
103 return log_url != nullptr;
106 Array TestLogger::postData(Array arr) {
107 HttpClient client;
108 StringBuffer response;
110 Array data = make_map_array(
111 "method", "recordTestResults", "args",
112 Variant::attach(HHVM_FN(json_encode)(make_packed_array(arr)))
115 auto const str = HHVM_FN(http_build_query)(data, "", "").toString();
117 client.post(log_url, str.c_str(), str.length(), response);
119 return Variant::attach(
120 HHVM_FN(json_decode)(response.detach(), true)
121 ).toArray();
124 std::string TestLogger::getRepoRoot() {
125 std::string out;
127 if (getOutput("git rev-parse --show-toplevel", out) != -1) {
128 // Not all git versions we use know --show-toplevel, if it was just
129 // echoed back (this version doesn't know it), fall back to another method
130 if (strcmp(out.c_str(), "--show-toplevel") != 0)
131 return out;
133 // Need to normalize of this commands succeeds
134 if (getOutput("git rev-parse --show-cdup", out) != -1)
135 return HHVM_FN(realpath)(String(out)).toString().data();
138 // Fall back to our current directory
139 char buf[MAXPATHLEN];
140 if (getcwd(buf, sizeof(buf)) == nullptr) {
141 out.assign(buf);
142 } else {
143 out.assign("/");
145 return out;
148 std::string TestLogger::getGitRevision() {
149 std::string commit;
151 if (getOutput("git rev-list -1 HEAD", commit) != -1)
152 return commit;
154 return "";
157 std::string TestLogger::getSVNRevision() {
158 return "";
162 * Runs a command and gets the first line of output
164 int TestLogger::getOutput(const char* cmd, std::string &buf) {
165 FILE *output = popen(cmd, "r");
166 if (output == nullptr) {
167 buf = "";
168 pclose(output);
169 return -1;
172 char b[1024];
173 if (fread(b, sizeof(char), sizeof(b), output) <= 0) {
174 buf = "";
175 pclose(output);
176 return -1;
179 buf = b;
181 if (pclose(output)) {
182 return -1;
185 size_t pos = buf.find("\n");
186 if (pos < buf.length())
187 buf.erase(pos);
189 if (buf == "")
190 return -1;
191 return 1;