Skip to content

Commit a5a989d

Browse files
committed
Port tests
1 parent 1b50fb8 commit a5a989d

File tree

11 files changed

+294
-18
lines changed

11 files changed

+294
-18
lines changed

__tests__/data/exit.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
os.exit()

__tests__/data/loadfile.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
local f, error = loadfile("test.lua")
2+
print(f, error)

__tests__/data/notable.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return "Not a table"

__tests__/data/print.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
print("Hello World!")
2+
3+
return {}

__tests__/data/testspec.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
return {
2+
Name = "ValeLS",
3+
Version = "1.0.0",
4+
CompatVersion = "1.0.0",
5+
Vendor = "The Qt Company",
6+
Copyright = "(C) The Qt Company 2024",
7+
License = "GPL",
8+
Category = "Language Server",
9+
Description = "Provides an integration of the Vale language server",
10+
Url = "https://www.qt.io",
11+
Experimental = true,
12+
DisabledByDefault = false,
13+
Dependencies = {
14+
{ Name = "Lua", Version = "14.0.82" },
15+
},
16+
languages = {"en", "de"},
17+
setup = function()
18+
require 'init'.setup()
19+
end
20+
} --[[@as QtcPlugin]]

__tests__/data/utf8spec.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Copyright (C) 2024 The Qt Company Ltd.
2+
-- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3+
return {
4+
Id = "utf8test",
5+
Name = "UTF8 Test",
6+
Version = "1.0",
7+
CompatVersion = "1.0",
8+
VendorId = "theqtcompany",
9+
Vendor = "The Qt Company",
10+
Category = "Test",
11+
Description = "Utf8 Test",
12+
Experimental = true,
13+
DisabledByDefault = false,
14+
TermsAndConditions = {
15+
version = 2,
16+
text = [[
17+
These utf8 codepoints may be a problem: “Customer”
18+
]]
19+
},
20+
LongDescription = [[UTF8 IS LONG!]],
21+
Dependencies = {
22+
{ Id = "lua", Version = "15.0.0" },
23+
{ Id = "lualanguageclient", Version = "15.0.0" }
24+
}
25+
} --[[@as QtcPlugin]]

__tests__/main.test.ts

Lines changed: 226 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import path from 'path'
1616
const runMock = jest.spyOn(main, 'run')
1717

1818
// Mock the GitHub Actions core library
19-
// let debugMock: jest.SpiedFunction<typeof core.debug>
19+
let debugMock: jest.SpiedFunction<typeof core.debug>
2020
let errorMock: jest.SpiedFunction<typeof core.error>
2121
let getInputMock: jest.SpiedFunction<typeof core.getInput>
2222
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>
@@ -29,7 +29,7 @@ describe('action', () => {
2929
jest.clearAllMocks()
3030
fetchMock.resetMocks()
3131

32-
// debugMock = jest.spyOn(core, 'debug').mockImplementation()
32+
debugMock = jest.spyOn(core, 'debug').mockImplementation()
3333
errorMock = jest.spyOn(core, 'error').mockImplementation()
3434
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
3535
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
@@ -82,7 +82,9 @@ describe('action', () => {
8282
expect(runMock).toHaveReturned()
8383
expect(errorMock).not.toHaveBeenCalled()
8484
expect(setFailedMock).toHaveBeenCalledWith(
85-
'Error: HTTP Error: 400, Bad Request, {"message":"Something went wrong"}'
85+
expect.stringContaining(
86+
'Error: HTTP Error: 400, Bad Request, {"message":"Something went wrong"}'
87+
)
8688
)
8789

8890
expect(fetchMock).toHaveBeenNthCalledWith(
@@ -99,4 +101,225 @@ describe('action', () => {
99101
})
100102
)
101103
})
104+
it('can read lua spec', async () => {
105+
// Set the action's inputs as return values from core.getInput()
106+
getInputMock.mockImplementation(name => {
107+
switch (name) {
108+
case 'test':
109+
return 'true'
110+
case 'spec':
111+
return path.join(__dirname, 'data', 'testspec.lua')
112+
case 'api':
113+
return 'https://example.com/'
114+
default:
115+
return ''
116+
}
117+
})
118+
119+
await main.run()
120+
expect(runMock).toHaveReturned()
121+
122+
expect(errorMock).not.toHaveBeenCalled()
123+
expect(setFailedMock).not.toHaveBeenCalled()
124+
})
125+
it('fails if it tries to exit', async () => {
126+
// Set the action's inputs as return values from core.getInput()
127+
getInputMock.mockImplementation(name => {
128+
switch (name) {
129+
case 'test':
130+
return 'true'
131+
case 'spec':
132+
return path.join(__dirname, 'data', 'exit.lua')
133+
default:
134+
return ''
135+
}
136+
})
137+
138+
await main.run()
139+
expect(runMock).toHaveReturned()
140+
141+
expect(errorMock).not.toHaveBeenCalled()
142+
expect(setFailedMock).toHaveBeenNthCalledWith(
143+
1,
144+
expect.stringContaining('Error: osExit not allowed')
145+
)
146+
})
147+
it('fails if it tries to load a file', async () => {
148+
// Set the action's inputs as return values from core.getInput()
149+
getInputMock.mockImplementation(name => {
150+
switch (name) {
151+
case 'test':
152+
return 'true'
153+
case 'spec':
154+
return path.join(__dirname, 'data', 'loadfile.lua')
155+
default:
156+
return ''
157+
}
158+
})
159+
160+
await main.run()
161+
expect(runMock).toHaveReturned()
162+
163+
expect(errorMock).not.toHaveBeenCalled()
164+
expect(setFailedMock).toHaveBeenNthCalledWith(
165+
1,
166+
expect.stringContaining('Error: loadFile not allowed')
167+
)
168+
})
169+
it('can print to debug', async () => {
170+
// Set the action's inputs as return values from core.getInput()
171+
getInputMock.mockImplementation(name => {
172+
switch (name) {
173+
case 'test':
174+
return 'true'
175+
case 'spec':
176+
return path.join(__dirname, 'data', 'print.lua')
177+
default:
178+
return ''
179+
}
180+
})
181+
182+
await main.run()
183+
expect(runMock).toHaveReturned()
184+
expect(debugMock).toHaveBeenNthCalledWith(1, 'Hello World!')
185+
186+
expect(errorMock).not.toHaveBeenCalled()
187+
expect(setFailedMock).not.toHaveBeenCalled()
188+
})
189+
it('spec must be a table', async () => {
190+
// Set the action's inputs as return values from core.getInput()
191+
getInputMock.mockImplementation(name => {
192+
switch (name) {
193+
case 'test':
194+
return 'true'
195+
case 'spec':
196+
return path.join(__dirname, 'data', 'notable.lua')
197+
default:
198+
return ''
199+
}
200+
})
201+
202+
await main.run()
203+
expect(runMock).toHaveReturned()
204+
205+
expect(errorMock).not.toHaveBeenCalled()
206+
expect(setFailedMock).toHaveBeenNthCalledWith(
207+
1,
208+
expect.stringContaining('Error: Spec must be a table')
209+
)
210+
})
211+
it('spec can contain utf-8 characters', async () => {
212+
// Set the action's inputs as return values from core.getInput()
213+
getInputMock.mockImplementation(name => {
214+
switch (name) {
215+
case 'test':
216+
return 'true'
217+
case 'spec':
218+
return path.join(__dirname, 'data', 'utf8spec.lua')
219+
default:
220+
return ''
221+
}
222+
})
223+
224+
await main.run()
225+
expect(runMock).toHaveReturned()
226+
227+
expect(errorMock).not.toHaveBeenCalled()
228+
expect(setFailedMock).not.toHaveBeenCalled()
229+
})
230+
it('should fail if invalid response', async () => {
231+
getInputMock.mockImplementation(name => {
232+
switch (name) {
233+
case 'test':
234+
return 'false'
235+
case 'spec':
236+
return path.join(__dirname, 'data', 'testspec.lua')
237+
case 'download-url':
238+
return 'https://example.com/test.zip'
239+
case 'api':
240+
return 'https://example.com/'
241+
case 'token':
242+
return 'token'
243+
default:
244+
return ''
245+
}
246+
})
247+
248+
fetchMock.mockResponseOnce(
249+
JSON.stringify({ message: 'Something went wrong' }),
250+
{
251+
status: 400,
252+
statusText: 'Bad Request'
253+
}
254+
)
255+
await main.run()
256+
expect(setFailedMock).toHaveBeenCalledWith(
257+
expect.stringContaining(
258+
'HTTP Error: 400, Bad Request, {"message":"Something went wrong"}'
259+
)
260+
)
261+
})
262+
it('should handle a 500 error', async () => {
263+
getInputMock.mockImplementation(name => {
264+
switch (name) {
265+
case 'test':
266+
return 'false'
267+
case 'spec':
268+
return path.join(__dirname, 'data', 'testspec.lua')
269+
case 'download-url':
270+
return 'https://example.com/test.zip'
271+
case 'api':
272+
return 'https://example.com/'
273+
case 'token':
274+
return 'token'
275+
default:
276+
return ''
277+
}
278+
})
279+
280+
fetchMock.mockResponseOnce('', {
281+
status: 500,
282+
statusText: 'Internal Server Error'
283+
})
284+
await main.run()
285+
expect(setFailedMock).toHaveBeenCalledWith(
286+
expect.stringContaining('HTTP Error: 500, Internal Server Error')
287+
)
288+
})
289+
it('Should create a new plugin if not found', async () => {
290+
getInputMock.mockImplementation(name => {
291+
switch (name) {
292+
case 'test':
293+
return 'false'
294+
case 'spec':
295+
return path.join(__dirname, 'data', 'testspec.lua')
296+
case 'download-url':
297+
return 'https://example.com/test.zip'
298+
case 'api':
299+
return 'https://example.com/'
300+
case 'token':
301+
return '__token__'
302+
default:
303+
return ''
304+
}
305+
})
306+
307+
fetchMock.mockResponseOnce(JSON.stringify({ items: [] }), { status: 200 })
308+
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 200 })
309+
await main.run()
310+
expect(fetchMock).toHaveBeenNthCalledWith(
311+
1,
312+
'https://example.com/api/v1/management/plugins',
313+
expect.objectContaining({
314+
body: expect.anything(),
315+
method: 'POST',
316+
headers: expect.objectContaining({
317+
Authorization: 'Bearer __token__',
318+
'Content-Type': 'application/json',
319+
accept: 'application/json'
320+
})
321+
})
322+
)
323+
expect(setFailedMock).not.toHaveBeenCalled()
324+
})
102325
})

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)