forked from SimonKagstrom/kcov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoveralls-writer.cc
248 lines (196 loc) · 5 KB
/
coveralls-writer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Writer for the "coveralls.io" web service.
*/
#include <reporter.hh>
#include <file-parser.hh>
#include <configuration.hh>
#include <writer.hh>
#include <utils.hh>
#include <string>
#include <list>
#include <unordered_map>
#include <iostream>
#include <fstream>
#include <curl/curl.h>
#include <string.h>
#include "writer-base.hh"
using namespace kcov;
class CurlConnectionHandler
{
public:
CurlConnectionHandler() :
m_headerlist(NULL)
{
static const char buf[] = "Expect:";
// Workaround proxy problems
m_headerlist = curl_slist_append(m_headerlist, buf);
curl_global_init(CURL_GLOBAL_ALL);
m_curl = curl_easy_init();
// Setup curl to read from memory
curl_easy_setopt(m_curl, CURLOPT_URL, "https://coveralls.io/api/v1/jobs");
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curlWriteFuncStatic);
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (void *)this);
curl_easy_setopt(m_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, m_headerlist); // Proxy issues
}
~CurlConnectionHandler()
{
curl_slist_free_all(m_headerlist);
curl_easy_cleanup(m_curl);
curl_global_cleanup();
}
// Send fileName and data to the remote server
bool talk(const std::string &fileName)
{
m_writtenData = "";
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
CURLcode res;
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "json_file",
CURLFORM_FILE, fileName.c_str(),
CURLFORM_END);
curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(m_curl);
curl_formfree(formpost);
if (res != CURLE_OK)
return false;
if (m_writtenData.find("Job #") == std::string::npos)
{
warning("coveralls write failed: %s\n", m_writtenData.c_str());
return false;
}
return true;
}
private:
size_t curlWritefunc(void *ptr, size_t size, size_t nmemb)
{
const char *p = (char *)ptr;
m_writtenData.append(p, size * nmemb);
return size * nmemb;
}
static size_t curlWriteFuncStatic(void *ptr, size_t size, size_t nmemb, void *priv)
{
if (!priv)
return 0;
return ((CurlConnectionHandler *)priv)->curlWritefunc(ptr, size, nmemb);
}
CURL *m_curl;
std::string m_writtenData;
struct curl_slist *m_headerlist;
};
static CurlConnectionHandler *g_curl;
class CoverallsWriter : public WriterBase
{
public:
CoverallsWriter(IFileParser &parser, IReporter &reporter) :
WriterBase(parser, reporter),
m_doWrite(false)
{
}
void onStartup()
{
}
void onStop()
{
m_doWrite = true;
}
void write()
{
// Write once at the end only
if (!m_doWrite)
return;
IConfiguration &conf = IConfiguration::getInstance();
const std::string &id = conf.keyAsString("coveralls-id");
// No token? Skip output then
if (id == "")
return;
std::string outFile = conf.keyAsString("target-directory") + "/coveralls.out";
// Output file with coveralls json data
std::ofstream out(outFile);
// Output directory not writable?
if (!out.is_open())
return;
out << "{\n";
if (isRepoToken(id))
{
out << " \"repo_token\": \"" + id + "\",\n";
}
else
{
out << " \"service_name\": \"" + conf.keyAsString("coveralls-service-name") + "\",\n";
out << " \"service_job_id\": \"" + id + "\",\n";
}
out << " \"source_files\": [\n";
setupCommonPaths();
std::string strip_path = conf.keyAsString("strip-path");
if (strip_path.size() == 0)
{
setupCommonPaths();
strip_path = m_commonPath + "/";
}
unsigned int filesLeft = m_files.size();
for (FileMap_t::const_iterator it = m_files.begin();
it != m_files.end();
++it)
{
File *file = it->second;
std::string fileName;
// Strip away the specified path (unless this is the only file)
if (file->m_name.compare(0, strip_path.length(), strip_path) == 0)
fileName = file->m_name.substr(strip_path.size());
else
fileName = file->m_fileName;
out << " {\n";
out << " \"name\": \"" + escape_json(fileName) + "\",\n";
// Use hash as source file
out << fmt(" \"source_digest\": \"0x%08lx\",\n", (unsigned long)file->m_crc);
out << " \"coverage\": [";
// And coverage
for (unsigned int n = 1; n < file->m_lastLineNr; n++)
{
if (!m_reporter.lineIsCode(file->m_name, n))
{
out << "null";
}
else
{
IReporter::LineExecutionCount cnt =
m_reporter.getLineExecutionCount(file->m_name, n);
out << cnt.m_hits;
}
if (n != file->m_lastLineNr - 1)
out << ",";
}
out << "]\n";
// Add comma or not on the last run
filesLeft--;
if (filesLeft > 0)
out << " },\n";
else
out << " }\n";
}
out << " ]\n";
out << "}\n";
out.close();
// Create singleton
if (!g_curl)
g_curl = new CurlConnectionHandler();
if (id != "dry-run")
g_curl->talk(outFile);
}
private:
bool isRepoToken(const std::string &str)
{
return str.size() >= 32;
}
bool m_doWrite;
};
namespace kcov
{
IWriter &createCoverallsWriter(IFileParser &parser, IReporter &reporter)
{
return *new CoverallsWriter(parser, reporter);
}
}