Skip to content

Commit a85d71d

Browse files
authored
Merge pull request #169 from MartinNowak/fix167
fix #167 - fix misleading error output during tests
2 parents c87961b + e152d97 commit a85d71d

File tree

15 files changed

+156
-82
lines changed

15 files changed

+156
-82
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dlang-bot
22
__test__unittest__
33
dlang-bot-test-unittest
4+
ut.d # unit-threaded
45

56
# Created by https://www.gitignore.io/api/d
67

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ d:
99
- ldc-beta
1010
- ldc
1111

12+
script:
13+
# Tests are neither random- nor parallelizable atm., so use a --single serial thread.
14+
- dub test --compiler=$DC -- --single --trace
15+
1216
addons:
1317
apt:
1418
packages:
@@ -19,8 +23,9 @@ matrix:
1923
- d: dmd-2.077.1 # the deployment compiler
2024
env: COVERAGE=true
2125
script:
22-
# https://issues.dlang.org/show_bug.cgi?id=13742
23-
- dub test --compiler=$DC --build=unittest-cov --build-mode=singleFile
26+
# Use --build-mode=singleFile to workaround https://issues.dlang.org/show_bug.cgi?id=13742
27+
# Tests are neither random- nor parallelizable atm., so use a --single serial thread.
28+
- dub test --compiler=$DC --build=unittest-cov --build-mode=singleFile -- --single --trace
2429
after_success:
2530
- bash <(curl -s https://codecov.io/bash)
2631
allow_failures:

dub.sdl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ copyright "Copyright © 2015, Martin Nowak"
44
authors "Martin Nowak"
55
dependency "vibe-d" version="~>0.8.0"
66
subConfiguration "vibe-d:core" "vibe-core"
7-
configuration "default" {
8-
versions "VibeCustomMain"
9-
targetType "executable"
7+
targetType "executable"
8+
9+
configuration "executable" {
1010
}
11+
1112
configuration "unittest" {
12-
versions "VibeCustomMain"
13-
sourcePaths "source" "test"
14-
importPaths "source" "test"
15-
stringImportPaths "views"
13+
dependency "unit-threaded" version="~>0.7.11"
14+
mainSourceFile "ut.d"
15+
preBuildCommands "dub run unit-threaded -c gen_ut_main -- -f ut.d"
16+
sourcePaths "source" "test"
17+
importPaths "source" "test"
1618
}

dub.selections.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"openssl": "1.1.6+1.0.1g",
1212
"stdx-allocator": "2.77.0",
1313
"taggedalgebraic": "0.10.8",
14+
"unit-threaded": "0.7.36",
1415
"vibe-core": "1.4.0-alpha.1",
1516
"vibe-d": "0.8.3-alpha.1"
1617
}

source/dlangbot/appveyor.d

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import vibe.core.log;
1313
void cancelBuild(string repoSlug, size_t buildId)
1414
{
1515
import std.format : format;
16-
import vibe.http.client : requestHTTP;
1716
import vibe.http.common : HTTPMethod;
1817
import vibe.stream.operations : readAllUTF8;
18+
import dlangbot.utils : request;
1919

2020
auto url = "%s/builds/%s/%s/cancel".format(appveyorAPIURL, repoSlug, buildId);
21-
requestHTTP(url, (scope req) {
21+
request(url, (scope req) {
2222
req.headers["Authorization"] = appveyorAuth;
2323
req.method = HTTPMethod.DELETE;
2424
}, (scope res) {
2525
if (res.statusCode / 100 == 2)
2626
logInfo("[appveyor/%s]: Canceled Build %s\n", repoSlug, buildId);
2727
else
28-
logWarn("[appveyor/%s]: POST %s failed; %s %s.\n%s", repoSlug, url, res.statusPhrase,
28+
logError("[appveyor/%s]: POST %s failed; %s %s.\n%s", repoSlug, url, res.statusPhrase,
2929
res.statusCode, res.bodyReader.readAllUTF8);
3030
});
3131
}
@@ -38,7 +38,7 @@ void dedupAppVeyorBuilds(string action, string repoSlug, uint pullRequestNumber)
3838
import std.format : format;
3939
import std.range : drop;
4040
import vibe.data.json : Json;
41-
import vibe.http.client : requestHTTP;
41+
import dlangbot.utils : request;
4242

4343
if (action != "synchronize" && action != "merged")
4444
return;
@@ -51,7 +51,7 @@ void dedupAppVeyorBuilds(string action, string repoSlug, uint pullRequestNumber)
5151
// GET /api/projects/{accountName}/{projectSlug}/history?recordsNumber={records-per-page}[&startBuildId={buildId}&branch={branch}]
5252

5353
auto url = "%s/projects/%s/history?recordsNumber=100".format(appveyorAPIURL, repoSlug);
54-
auto activeBuildsForPR = requestHTTP(url, (scope req) {
54+
auto activeBuildsForPR = request(url, (scope req) {
5555
req.headers["Authorization"] = appveyorAuth;
5656
})
5757
.readJson["builds"][]

source/dlangbot/bugzilla.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ struct Issue
6262
Issue[] getDescriptions(R)(R issueRefs)
6363
{
6464
import std.csv;
65-
import vibe.http.client : requestHTTP;
6665
import vibe.stream.operations : readAllUTF8;
66+
import dlangbot.utils : request;
6767

6868
if (issueRefs.empty)
6969
return null;
7070
return "%s/buglist.cgi?bug_id=%(%d,%)&ctype=csv&columnlist=short_desc,bug_status,resolution,bug_severity,priority"
7171
.format(bugzillaURL, issueRefs.map!(r => r.id))
72-
.requestHTTP
72+
.request
7373
.bodyReader.readAllUTF8
7474
.csvReader!Issue(null)
7575
.array

source/dlangbot/github_api.d

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ import std.typecons : Nullable;
1111

1212
import vibe.core.log;
1313
import vibe.data.json;
14-
import vibe.http.client : HTTPClientRequest, requestHTTP;
14+
import vibe.http.client : HTTPClientRequest;
1515
public import vibe.http.common : HTTPMethod;
1616
import vibe.stream.operations : readAllUTF8;
1717

18+
import dlangbot.utils : request;
19+
1820
auto ghGetRequest(string url)
1921
{
20-
return requestHTTP(url, (scope req) {
22+
return request(url, (scope req) {
2123
req.headers["Authorization"] = githubAuth;
2224
});
2325
}
2426

2527
auto ghGetRequest(scope void delegate(scope HTTPClientRequest req) userReq, string url)
2628
{
27-
return requestHTTP(url, (scope req) {
29+
return request(url, (scope req) {
2830
req.headers["Authorization"] = githubAuth;
2931
userReq(req);
3032
});
@@ -33,7 +35,7 @@ auto ghGetRequest(scope void delegate(scope HTTPClientRequest req) userReq, stri
3335
auto ghSendRequest(scope void delegate(scope HTTPClientRequest req) userReq, string url)
3436
{
3537
HTTPMethod method;
36-
requestHTTP(url, (scope req) {
38+
request(url, (scope req) {
3739
req.headers["Authorization"] = githubAuth;
3840
userReq(req);
3941
method = req.method;
@@ -43,9 +45,6 @@ auto ghSendRequest(scope void delegate(scope HTTPClientRequest req) userReq, str
4345
logInfo("%s %s, %s\n", method, url, res.statusPhrase);
4446
res.bodyReader.readAllUTF8;
4547
}
46-
else
47-
logWarn("%s %s failed; %s %s.\n%s", method, url,
48-
res.statusPhrase, res.statusCode, res.bodyReader.readAllUTF8);
4948
});
5049
}
5150

source/dlangbot/trello.d

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ string trelloAPIURL = "https://api.trello.com";
44
string trelloSecret, trelloAuth;
55

66
import dlangbot.bugzilla : Issue, IssueRef;
7+
import dlangbot.utils : request;
78
import std.algorithm, std.range;
89
import std.format : format;
910

1011
import vibe.core.log;
1112
import vibe.data.json;
12-
import vibe.http.client : requestHTTP;
1313
import vibe.http.common : HTTPMethod;
1414
import vibe.stream.operations : readAllUTF8;
1515

@@ -20,7 +20,7 @@ import vibe.stream.operations : readAllUTF8;
2020
void trelloSendRequest(T...)(HTTPMethod method, string url, T arg)
2121
if (T.length <= 1)
2222
{
23-
requestHTTP(url, (scope req) {
23+
request(url, (scope req) {
2424
req.method = method;
2525
static if (T.length)
2626
req.writeJsonBody(arg);
@@ -29,7 +29,7 @@ void trelloSendRequest(T...)(HTTPMethod method, string url, T arg)
2929
logInfo("%s %s: %s\n", method, url.replace(trelloAuth, "key=[hidden]&token=[hidden]")
3030
, res.statusPhrase);
3131
else
32-
logWarn("%s %s: %s %s.\n%s", method, url.replace(trelloAuth, "key=[hidden]&token=[hidden]"),
32+
logError("%s %s: %s %s.\n%s", method, url.replace(trelloAuth, "key=[hidden]&token=[hidden]"),
3333
res.statusPhrase, res.statusCode, res.bodyReader.readAllUTF8);
3434
});
3535
}
@@ -79,7 +79,7 @@ auto findTrelloCards(int issueID)
7979
{
8080

8181
return trelloAPI("/1/search?query=name:\"Issue %d\"", issueID)
82-
.requestHTTP
82+
.request
8383
.readJson["cards"][]
8484
.map!(c => TrelloCard(c["id"].get!string, issueID));
8585
}
@@ -89,7 +89,7 @@ struct Comment { string url, body_; }
8989
Comment getTrelloBotComment(string cardID)
9090
{
9191
auto res = trelloAPI("/1/cards/%s/actions?filter=commentCard", cardID)
92-
.requestHTTP
92+
.request
9393
.readJson[]
9494
.find!(c => c["memberCreator"]["username"] == "dlangbot");
9595
if (res.length)
@@ -102,10 +102,10 @@ Comment getTrelloBotComment(string cardID)
102102
void moveCardToList(string cardID, string listName)
103103
{
104104
auto card = trelloAPI("/1/cards/%s", cardID)
105-
.requestHTTP
105+
.request
106106
.readJson;
107107
auto lists = trelloAPI("/1/board/%s/lists", card["idBoard"].get!string)
108-
.requestHTTP
108+
.request
109109
.readJson[];
110110

111111
immutable curListName = lists.find!(c => c["id"].get!string == card["idList"].get!string)

source/dlangbot/utils.d

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,74 @@ module dlangbot.utils;
22

33
import dlangbot.app : runAsync;
44

5+
import vibe.http.client : HTTPClientRequest, HTTPClientResponse;
6+
import vibe.http.common : HTTPStatus;
7+
58
import std.datetime : Duration;
69

10+
version (unittest) int _expectedStatusCode;
11+
12+
bool expectedStatusCode(int statusCode) nothrow @safe @nogc
13+
{
14+
version (unittest)
15+
{
16+
if (_expectedStatusCode == statusCode)
17+
{
18+
_expectedStatusCode = 0;
19+
return true;
20+
}
21+
}
22+
return false;
23+
}
24+
25+
HTTPClientResponse request(
26+
string url,
27+
scope void delegate(scope HTTPClientRequest) requester = cast(void delegate(scope HTTPClientRequest req))null
28+
) @safe
29+
{
30+
import vibe.core.log : logError;
31+
import vibe.http.client : requestHTTP;
32+
import vibe.http.common : HTTPMethod;
33+
34+
HTTPMethod method;
35+
auto res = requestHTTP(
36+
url,
37+
(scope req) {
38+
if (requester !is null)
39+
requester(req);
40+
method = req.method;
41+
});
42+
if (res.statusCode / 100 != 2 && !expectedStatusCode(res.statusCode))
43+
logError("%s %s failed; %s %s.", method, url, res.statusPhrase, res.statusCode);
44+
return res;
45+
}
46+
47+
void request(
48+
string url,
49+
scope void delegate(scope HTTPClientRequest) requester,
50+
scope void delegate(scope HTTPClientResponse) responder
51+
) @safe
52+
{
53+
import vibe.core.log : logError;
54+
import vibe.http.client : requestHTTP;
55+
import vibe.http.common : HTTPMethod;
56+
57+
HTTPMethod method;
58+
requestHTTP(
59+
url,
60+
(scope req) {
61+
requester(req);
62+
method = req.method;
63+
},
64+
(scope res) {
65+
if (res.statusCode / 100 != 2 && !expectedStatusCode(res.statusCode))
66+
logError("%s %s failed; %s %s.", method, url, res.statusPhrase, res.statusCode);
67+
responder(res);
68+
}
69+
);
70+
71+
}
72+
773
auto runTaskHelper(Fun, Args...)(Fun fun, auto ref Args args)
874
{
975
import std.functional : toDelegate;

test/appveyor.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ unittest
2020
j["builds"] = j["builds"][0..2];
2121
},
2222
"/appveyor/builds/greenify/dmd/13074433/cancel",
23+
HTTPStatus.noContent,
2324
(scope HTTPServerRequest req, scope HTTPServerResponse res) {
2425
assert(req.method == HTTPMethod.DELETE);
25-
res.statusCode = 204;
2626
res.writeVoidBody;
2727
},
2828
);

0 commit comments

Comments
 (0)