2 +----------------------------------------------------------------------+
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"
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>
31 ///////////////////////////////////////////////////////////////////////////////
32 static const StaticString
36 bool TestLogger::initializeRun() {
37 static const StaticString
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()) {
69 bool TestLogger::finishRun() {
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()) {
85 bool TestLogger::logTest(Array test
) {
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()) {
102 bool TestLogger::doLog() {
103 return log_url
!= nullptr;
106 Array
TestLogger::postData(Array arr
) {
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)
124 std::string
TestLogger::getRepoRoot() {
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)
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) {
148 std::string
TestLogger::getGitRevision() {
151 if (getOutput("git rev-list -1 HEAD", commit
) != -1)
157 std::string
TestLogger::getSVNRevision() {
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) {
173 if (fread(b
, sizeof(char), sizeof(b
), output
) <= 0) {
181 if (pclose(output
)) {
185 size_t pos
= buf
.find("\n");
186 if (pos
< buf
.length())