-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest.js
154 lines (136 loc) · 4.54 KB
/
test.js
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
const test = require("ava");
const {
buildUrl,
jobUrl,
repo,
sha,
event,
commit_message,
pull_request_number,
branch,
ci,
platform
} = require("./index");
if (ci) {
console.log("values: ", {
repo,
sha,
event,
commit_message,
pull_request_number,
branch,
ci
});
test("ci is correctly set", t => {
if (process.env.TRAVIS) t.is(ci, "travis");
else if (process.env.CIRCLECI) t.is(ci, "circle");
else if (process.env.WERCKER) t.is(ci, "wercker");
else if (process.env.DRONE) t.is(ci, "drone");
else if (process.env.CI_NAME === "codeship") t.is(ci, "codeship");
else if (process.env.CI === "woodpecker") t.is(ci, "woodpecker");
else if (process.env.GITHUB_ACTION) t.is(ci, "github_actions");
else if (process.env.GITLAB_CI) t.is(ci, "gitlab");
else if (process.env.CF_BUILD_URL) t.is(ci, "codefresh");
else if (process.env.CF_PAGES) t.is(ci, "cloudflare");
});
test("repo is correctly set", t => {
if (process.env.GITLAB_CI) t.is(repo, process.env.CI_PROJECT_PATH);
else t.is(repo, "siddharthkp/ci-env");
});
test("sha is set", t => {
const real_sha =
process.env.TRAVIS_PULL_REQUEST_SHA ||
process.env.TRAVIS_COMMIT ||
process.env.CIRCLE_SHA1 ||
process.env.WERCKER_GIT_COMMIT ||
process.env.DRONE_COMMIT ||
process.env.GITHUB_SHA ||
process.env.CI_COMMIT_SHA || //gitlab & woodpecker
process.env.CF_REVISION ||
process.env.CF_PAGES_COMMIT_SHA;
t.is(sha, real_sha);
});
test("commit_message is set", t => {
const real_commit_message =
process.env.TRAVIS_COMMIT_MESSAGE ||
process.env.CI_COMMIT_MESSAGE ||
process.env.CI_MESSAGE ||
process.env.CF_COMMIT_MESSAGE ||
process.env.CI_COMMIT_MESSAGE || // woodpecker
"";
// Only travis and codeship set commit message
t.is(commit_message, real_commit_message);
});
test("pull_request_number is set", t => {
let pullRequestNumber;
if (process.env.CI_PULL_REQUEST)
pullRequestNumber = process.env.CI_PULL_REQUEST.split("/").pop();
if(process.env.GITHUB_ACTION && event === "pull_request")
pullRequestNumber = process.env.GITHUB_REF.split('/')[2];
const real_pull_request_number =
process.env.TRAVIS_PULL_REQUEST ||
process.env.DRONE_PULL_REQUEST ||
process.env.CI_MERGE_REQUEST_ID || // gitlab
process.env.CF_PULL_REQUEST_NUMBER ||
process.env.CI_COMMIT_PULL_REQUEST || // woodpecker
pullRequestNumber ||
""; // wercker does not expose pull request number
t.is(pull_request_number, real_pull_request_number);
});
test("jobUrl is set", t => {
let realJobUrl;
if (process.env.TRAVIS)
realJobUrl = `https://travis-ci.org/${repo}/jobs/${
process.env.TRAVIS_JOB_ID
}`;
else if (process.env.GITLAB_CI) realJobUrl = process.env.CI_JOB_URL;
const real_job_url =
process.env.CI_BUILD_LINK ||
realJobUrl;
t.is(jobUrl, real_job_url);
});
test("buildUrl is set", t => {
let realBuildUrl;
if (process.env.TRAVIS)
realBuildUrl = `https://travis-ci.org/${repo}/builds/${
process.env.TRAVIS_JOB_ID
}`;
else if (process.env.CF_BUILD_URL) realBuildUrl = process.env.CF_BUILD_URL;
const real_build_url =
process.env.CI_BUILD_LINK ||
realBuildUrl;
t.is(buildUrl, real_build_url);
});
test("event is correctly set", t => {
if (
(ci === "travis" && process.env.TRAVIS_EVENT_TYPE === "pull_request") ||
(ci === "github_actions" && process.env.GITHUB_EVENT_NAME === "pull_request") ||
(ci === "woodpecker" && process.env.CI_BUILD_EVENT === "pull_request")
)
t.is(event, "pull_request");
else t.is(event, "push");
});
test("branch is correctly set", t => {
if (event === "pull_request")
t.is(
branch,
process.env.TRAVIS_PULL_REQUEST_BRANCH || process.env.GITHUB_HEAD_REF || process.env.CI_COMMIT_TARGET_BRANCH
);
else {
let real_branch =
process.env.TRAVIS_BRANCH ||
process.env.CIRCLE_BRANCH ||
process.env.WERCKER_GIT_BRANCH ||
process.env.DRONE_BRANCH ||
process.env.CI_BRANCH || // codeship
process.env.CI_COMMIT_REF_NAME || // gitlab
process.env.CI_COMMIT_BRANCH // woodpecker
process.env.CF_BRANCH ||
process.env.CF_PAGES_BRANCH;
if (process.env.GITHUB_ACTION) real_branch = process.env.GITHUB_REF.split('/')[2]
t.is(branch, real_branch);
}
});
} else {
test.skip("These tests can only run in CI environments", t => t.pass());
}