This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
license.test.js
239 lines (221 loc) · 9.37 KB
/
license.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
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
/*
* Copyright 2021, 2022 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const nock = require('nock')
const {checkLicense} = require("./license");
const fs = require('fs')
const path = require('path')
const os = require('os')
const {readAndValidateConfigFile} = require("./file");
const core = require('@actions/core')
const github = require('@actions/github')
function setTestInput(name, value) {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`]=value;
}
describe('when checking license ', () => {
beforeEach(() => {
nock('https://api.github.com')
.get('/repos/test-org/test-repo/pulls/1')
.reply(200, {
"number":1,
"state":"closed",
"locked":false,
"title":"Update README.md",
"head": {
"label": "ZupIT:thallesfreitaszup-patch-1",
"ref": "thallesfreitaszup-patch-1",
"sha": "8e10d4b4d911000fcdbd3b852a35233b90dc7474",
},
"base":{
"label":"ZupIT:main",
"ref":"main",
"sha":"c80f870b21e9ba36bb35f11c77ac068a40ae5e45",
}
})
nock('https://api.github.com')
.get('/repos/test-org/test-repo/compare/c80f870b21e9ba36bb35f11c77ac068a40ae5e45...8e10d4b4d911000fcdbd3b852a35233b90dc7474')
.reply(200, {
"files": [
{
"sha": "a1a0173f1e58265b6a3a2237f09e197bb63c24f8",
"filename": "/tmp/test.txt",
"status": "modified",
"additions": 1,
"deletions": 1,
"changes": 2,
"blob_url": "https://github.com/ZupIT/header-license-checker/blob/8e10d4b4d911000fcdbd3b852a35233b90dc7474/README.md",
"raw_url": "https://github.com/ZupIT/header-license-checker/raw/8e10d4b4d911000fcdbd3b852a35233b90dc7474/README.md",
"contents_url": "https://api.github.com/repos/ZupIT/header-license-checker/contents/README.md?ref=8e10d4b4d911000fcdbd3b852a35233b90dc7474",
"patch": "@@ -1,4 +1,4 @@\n-# actions-license\n+# header-license-checker\n This action checks the license headers in the files of pull requests.\n Inspired on [Deno License Checker]\n "
}
]
})
})
afterEach( () => {
removeTestFile()
})
it("should return error when license date of file it's outdated", async () => {
const configJsonPath = createConfigJson()
const fileName = createTestFileWithLicenseDateOutdated()
const arrayFilesErrors = [fileName]
const config = readAndValidateConfigFile(configJsonPath)
setTestInput("token", "dummy-token")
github.context.payload = {
pull_request : {
number : 1
},
repository: {
owner : {
login :'test-org'
},
name: 'test-repo'
}
}
const errors = await checkLicense([fileName], {startDateLicense: config.startDateLicense, copyrightContent: config.copyright})
expect((errors.title)).toEqual('Quantity of files with copyright errors: 1')
expect((errors.details)).toEqual(`Files : [ '${fileName}' ]`)
})
it("should return error when file has a misspelling license header", async () => {
const configJsonPath = createConfigJson()
const fileName = createTestFileWithMisspellingLicense()
const config = readAndValidateConfigFile(configJsonPath)
setTestInput("token", "dummy-token")
github.context.payload = {
pull_request : {
number : 1
},
repository: {
owner : {
login :'test-org'
},
name: 'test-repo'
}
}
const errors = await checkLicense([fileName], {startDateLicense: config.startDateLicense, copyrightContent: config.copyright})
expect((errors.title)).toEqual('Quantity of files with copyright errors: 1')
expect((errors.details)).toEqual(`Files : [ '${fileName}' ]`)
})
it("should not return error when license date of files is updated", async () => {
const configJsonPath = createConfigJson()
const fileName = createTestFileWithLicenseDateUpdated()
const arrayFilesErrors = []
const config = readAndValidateConfigFile(configJsonPath)
setTestInput("token", "dummy-token")
github.context.payload = {
pull_request : {
number : 1
},
repository: {
owner : {
login :'test-org'
},
name: 'test-repo'
}
}
const errors = await checkLicense([fileName], {startDateLicense: config.startDateLicense, copyrightContent: config.copyright})
expect(errors).toBeUndefined()
})
})
function createTestFileWithLicenseDateOutdated() {
const dateFile = new Date().getFullYear()-1
const data = `
* Copyright ${dateFile} ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the \"License\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \"AS IS\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* `
const path = "/tmp/test.txt"
fs.writeFileSync(path, data)
return path;
}
function createTestFileWithMisspellingLicense() {
const dateFile = new Date().getFullYear()-1
const data = `
* Copiright ${dateFile} ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the \"License\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \"AS IS\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONSdada OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* `
const path = "/tmp/test.txt"
fs.writeFileSync(path, data)
return path;
}
function createTestFileWithLicenseDateUpdated() {
const dateFile = new Date().getFullYear()
const data = `
* Copyright ${dateFile} ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the \"License\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \"AS IS\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* `
const path = "/tmp/test.txt"
fs.writeFileSync(path, data)
return path;
}
function removeTestFile() {
fs.unlink('/tmp/test.txt', (err) => {
if (err) throw err;
});
}
function createConfigJson() {
const currentYear = new Date().getFullYear()
const path = "/tmp/config.json"
const data = `{
"copyright": [
"Copyright",
"ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA",
"Licensed under the Apache License, Version 2.0 (the \\"License\\");",
"you may not use this file except in compliance with the License.",
"You may obtain a copy of the License at",
"http://www.apache.org/licenses/LICENSE-2.0",
"Unless required by applicable law or agreed to in writing, software",
"distributed under the License is distributed on an \\"AS IS\\" BASIS,",
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.",
"See the License for the specific language governing permissions and",
"limitations under the License."
],
"ignore": [],
"startDateLicense": ${currentYear}
}`
fs.writeFileSync(path, data)
return path
}