Skip to content

Commit b660d8e

Browse files
Run luacheck on CI, fix warnings
1 parent 1ee57f8 commit b660d8e

File tree

8 files changed

+57
-81
lines changed

8 files changed

+57
-81
lines changed

.luacheckrc

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
1-
redefined = false
21
include_files = {"**/*.lua", "*.rockspec", "*.luacheckrc"}
3-
exclude_files = {"lua_modules", ".luarocks", ".rocks", "luatest/luaunit.lua", "build"}
4-
new_read_globals = {
5-
'box',
6-
'_TARANTOOL',
7-
'tonumber64',
8-
os = {
9-
fields = {
10-
'environ',
11-
}
12-
},
13-
string = {
14-
fields = {
15-
'split',
16-
'startswith',
17-
},
18-
},
19-
table = {
20-
fields = {
21-
'maxn',
22-
'copy',
23-
'new',
24-
'clear',
25-
'move',
26-
'foreach',
27-
'sort',
28-
'remove',
29-
'foreachi',
30-
'deepcopy',
31-
'getn',
32-
'concat',
33-
'insert',
34-
},
35-
},
2+
exclude_files = {
3+
"lua_modules",
4+
".luarocks",
5+
".rocks",
6+
"build",
7+
"http/mime_types.lua",
368
}

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ _test: &test
2323
- sudo apt-get -y update
2424
- sudo apt-get install -y tarantool tarantool-dev
2525
- tarantoolctl rocks make
26+
- tarantoolctl rocks install luacheck 0.25.0
2627
- tarantoolctl rocks install luatest 0.2.2
27-
script: .rocks/bin/luatest
28+
script:
29+
- .rocks/bin/luacheck .
30+
- .rocks/bin/luatest
2831

2932
_deploy: &deploy
3033
provider: packagecloud

examples/middleware.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local tsgi = require('http.tsgi')
55
local json = require('json')
66
local log = require('log')
77

8-
box.cfg{} -- luacheck: ignore
8+
box.cfg{}
99

1010
local httpd = http_server.new('127.0.0.1', 12345, {
1111
log_requests = true,

http/router/fs.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ local function catfile(...)
2828
return
2929
end
3030

31-
for i, pe in pairs(sp) do
31+
for _, pe in pairs(sp) do
3232
if path == nil then
3333
path = pe
3434
elseif string.match(path, '.$') ~= '/' then

http/router/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ local function use_middleware(self, handler, opts)
175175
before = '?string|table',
176176
after = '?string|table',
177177
})
178-
local opts = table.deepcopy(opts)
178+
opts = table.deepcopy(opts)
179179
opts.handler = handler
180180

181181
local uuid = require('uuid')

http/router/request.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ end
8282

8383
local function post_param(self, name)
8484
local content_type = self:content_type()
85-
if self:content_type() == 'multipart/form-data' then
85+
if content_type == 'multipart/form-data' then
8686
-- TODO: do that!
8787
rawset(self, 'post_params', {})
88-
elseif self:content_type() == 'application/json' then
88+
elseif content_type == 'application/json' then
8989
local params = self:json()
9090
rawset(self, 'post_params', params)
91-
elseif self:content_type() == 'application/x-www-form-urlencoded' then
91+
elseif content_type == 'application/x-www-form-urlencoded' then
9292
local params = lib.params(self:read_cached())
9393
local pres = {}
9494
for k, v in pairs(params) do
@@ -136,8 +136,8 @@ local function cookie(self, cookiename)
136136
return nil
137137
end
138138

139-
local function iterate(self, gen, param, state)
140-
return setmetatable({ body = { gen = gen, param = param, state = state } },
139+
local function iterate(_, gen, gen_param, state)
140+
return setmetatable({ body = { gen = gen, param = gen_param, state = state } },
141141
response.metatable)
142142
end
143143

@@ -168,12 +168,12 @@ end
168168

169169
local function request_json(req)
170170
local data = req:read_cached()
171-
local s, json = pcall(json.decode, data)
171+
local s, json_data = pcall(json.decode, data)
172172
if s then
173-
return json
173+
return json_data
174174
else
175175
error(utils.sprintf("Can't decode json in request '%s': %s",
176-
data, tostring(json)))
176+
data, tostring(json_data)))
177177
return nil
178178
end
179179
end

http/server/init.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ local errno = require('errno')
1010

1111
local DETACHED = 101
1212

13-
local ok, VERSION = pcall(require, 'http.VERSION')
14-
if not ok then
15-
VERSION = 'unknown'
13+
local VERSION = 'unknown'
14+
if package.search('http.VERSION') then
15+
VERSION = require('http.VERSION')
1616
end
1717

1818
---------
@@ -239,7 +239,6 @@ local function process_client(self, s, peer)
239239
if not s:write(response) then
240240
break
241241
end
242-
response = nil -- luacheck: ignore 311
243242
-- Transfer-Encoding: chunked
244243
for _, part in gen, param, state do
245244
part = tostring(part)

test/http_test.lua

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
local t = require('luatest')
44
local g = t.group('http')
5-
local tap = require('tap')
65
local fio = require('fio')
76
local http_lib = require('http.lib')
87
local http_client = require('http.client')
@@ -57,8 +56,12 @@ g.test_split_uri = function()
5756
check('http://abc:123?', { scheme = 'http', host = 'abc', service = '123'})
5857
check('http://abc:123?query', { scheme = 'http', host = 'abc',
5958
service = '123', query = 'query'})
60-
check('http://domain.subdomain.com:service?query', { scheme = 'http',
61-
host = 'domain.subdomain.com', service = 'service', query = 'query'})
59+
check('http://domain.subdomain.com:service?query', {
60+
scheme = 'http',
61+
host = 'domain.subdomain.com',
62+
service = 'service',
63+
query = 'query',
64+
})
6265
check('google.com', { host = 'google.com'})
6366
check('google.com?query', { host = 'google.com', query = 'query'})
6467
check('google.com/abc?query', { host = 'google.com', path = '/abc',
@@ -99,12 +102,12 @@ g.test_template = function()
99102
tt[i] = string.rep('#', i)
100103
end
101104

102-
local rendered, code = http_lib.template(template, { t = tt })
105+
local rendered = http_lib.template(template, { t = tt })
103106
t.assertTrue(#rendered > 10000, "rendered size")
104107
t.assertEquals(rendered:sub(#rendered - 7, #rendered - 1), "</html>", "rendered eof")
105108
end
106109

107-
g.test_parse_request = function(test)
110+
g.test_parse_request = function()
108111

109112
t.assertEquals(http_lib._parse_request('abc'),
110113
{ error = 'Broken request line', headers = {} }, 'broken request')
@@ -138,7 +141,7 @@ g.test_parse_request = function(test)
138141
)
139142
end
140143

141-
g.test_params = function(test)
144+
g.test_params = function()
142145
t.assertEquals(http_lib.params(), {}, 'nil string')
143146
t.assertEquals(http_lib.params(''), {}, 'empty string')
144147
t.assertEquals(http_lib.params('a'), {a = ''}, 'separate literal')
@@ -197,15 +200,15 @@ local function cfgserv()
197200
:route({path = '/aba*def'}, function() end)
198201
:route({path = '/abb*def/cde', name = 'star'}, function() end)
199202
:route({path = '/banners/:token'})
200-
:helper('helper_title', function(self, a) return 'Hello, ' .. a end)
203+
:helper('helper_title', function(_, a) return 'Hello, ' .. a end)
201204
:route({path = '/helper', file = 'helper.html.el'})
202205
:route({ path = '/test', file = 'test.html.el' },
203206
function(cx) return cx:render({ title = 'title: 123' }) end)
204207
httpd:set_router(router)
205208
return httpd, router
206209
end
207210

208-
g.test_server_url_match = function(test)
211+
g.test_server_url_match = function()
209212
local httpd, router = cfgserv()
210213
t.assertIsTable(httpd, "httpd object")
211214
t.assertIsNil(router:match('GET', '/'))
@@ -241,7 +244,7 @@ end
241244

242245

243246
g.test_server_url_for = function()
244-
local httpd, router = cfgserv()
247+
local _, router = cfgserv()
245248
t.assertEquals(router:url_for('abcdef'), '/abcdef', '/abcdef')
246249
t.assertEquals(router:url_for('test'), '/abc//', '/abc//')
247250
t.assertEquals(router:url_for('test', { cde = 'cde_v', def = 'def_v' }),
@@ -264,17 +267,17 @@ g.test_server_requests = function()
264267
t.assertEquals(r.reason, 'Ok', '/test reason')
265268
t.assertEquals(string.match(r.body, 'title: 123'), 'title: 123', '/test body')
266269

267-
local r = http_client.get('http://127.0.0.1:12345/test404')
270+
r = http_client.get('http://127.0.0.1:12345/test404')
268271
t.assertEquals(r.status, 404, '/test404 code')
269272
-- broken in built-in tarantool/http
270273
--t.assertEquals(r.reason, 'Not found', '/test404 reason')
271274

272-
local r = http_client.get('http://127.0.0.1:12345/absent')
275+
r = http_client.get('http://127.0.0.1:12345/absent')
273276
t.assertEquals(r.status, 500, '/absent code')
274277
--t.assertEquals(r.reason, 'Internal server error', '/absent reason')
275278
t.assertEquals(string.match(r.body, 'load module'), 'load module', '/absent body')
276279

277-
local r = http_client.get('http://127.0.0.1:12345/ctxaction')
280+
r = http_client.get('http://127.0.0.1:12345/ctxaction')
278281
t.assertEquals(r.status, 200, '/ctxaction code')
279282
t.assertEquals(r.reason, 'Ok', '/ctxaction reason')
280283
t.assertEquals(string.match(r.body, 'Hello, Tarantool'), 'Hello, Tarantool',
@@ -284,49 +287,49 @@ g.test_server_requests = function()
284287
t.assertEquals(string.match(r.body, 'controller: module[.]controller'),
285288
'controller: module.controller', '/ctxaction body controller')
286289

287-
local r = http_client.get('http://127.0.0.1:12345/ctxaction.invalid')
290+
r = http_client.get('http://127.0.0.1:12345/ctxaction.invalid')
288291
t.assertEquals(r.status, 404, '/ctxaction.invalid code') -- WTF?
289292
--t.assertEquals(r.reason, 'Not found', '/ctxaction.invalid reason')
290293
--t.assertEquals(r.body, '', '/ctxaction.invalid body')
291294

292-
local r = http_client.get('http://127.0.0.1:12345/hello.html')
295+
r = http_client.get('http://127.0.0.1:12345/hello.html')
293296
t.assertEquals(r.status, 200, '/hello.html code')
294297
t.assertEquals(r.reason, 'Ok', '/hello.html reason')
295298
t.assertEquals(string.match(r.body, 'static html'), 'static html',
296299
'/hello.html body')
297300

298-
local r = http_client.get('http://127.0.0.1:12345/absentaction')
301+
r = http_client.get('http://127.0.0.1:12345/absentaction')
299302
t.assertEquals(r.status, 500, '/absentaction 500')
300303
--t.assertEquals(r.reason, 'Internal server error', '/absentaction reason')
301304
t.assertEquals(string.match(r.body, 'contain function'), 'contain function',
302305
'/absentaction body')
303306

304-
local r = http_client.get('http://127.0.0.1:12345/helper')
307+
r = http_client.get('http://127.0.0.1:12345/helper')
305308
t.assertEquals(r.status, 200, 'helper 200')
306309
t.assertEquals(r.reason, 'Ok', 'helper reason')
307310
t.assertEquals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body')
308311

309-
local r = http_client.get('http://127.0.0.1:12345/helper?abc')
312+
r = http_client.get('http://127.0.0.1:12345/helper?abc')
310313
t.assertEquals(r.status, 200, 'helper?abc 200')
311314
t.assertEquals(r.reason, 'Ok', 'helper?abc reason')
312315
t.assertEquals(string.match(r.body, 'Hello, world'), 'Hello, world', 'helper body')
313316

314317
router:route({path = '/die', file = 'helper.html.el'},
315318
function() error(123) end )
316319

317-
local r = http_client.get('http://127.0.0.1:12345/die')
320+
r = http_client.get('http://127.0.0.1:12345/die')
318321
t.assertEquals(r.status, 500, 'die 500')
319322
--t.assertEquals(r.reason, 'Internal server error', 'die reason')
320323

321324
router:route({ path = '/info' }, function(cx)
322325
return cx:render({ json = cx:peer() })
323326
end)
324327

325-
local r = json.decode(http_client.get('http://127.0.0.1:12345/info').body)
328+
r = json.decode(http_client.get('http://127.0.0.1:12345/info').body)
326329
t.assertEquals(r.host, '127.0.0.1', 'peer.host')
327330
t.assertIsNumber(r.port, 'peer.port')
328331

329-
local r = router:route({method = 'POST', path = '/dit', file = 'helper.html.el'},
332+
r = router:route({method = 'POST', path = '/dit', file = 'helper.html.el'},
330333
function(tx)
331334
return tx:render({text = 'POST = ' .. tx:read()})
332335
end)
@@ -376,7 +379,7 @@ g.test_server_requests = function()
376379
end)
377380

378381
-- http client currently doesn't support chunked encoding
379-
local r = http_client.get('http://127.0.0.1:12345/chunked')
382+
r = http_client.get('http://127.0.0.1:12345/chunked')
380383
t.assertEquals(r.status, 200, 'chunked 200')
381384
t.assertEquals(r.headers['transfer-encoding'], 'chunked', 'chunked headers')
382385
t.assertEquals(r.body, 'chunkedencodingt\r\nest', 'chunked body')
@@ -389,7 +392,7 @@ g.test_server_requests = function()
389392
text = ('foo=%s; baz=%s'):format(foo, baz)
390393
})
391394
end)
392-
local r = http_client.get('http://127.0.0.1:12345/receive_cookie', {
395+
r = http_client.get('http://127.0.0.1:12345/receive_cookie', {
393396
headers = {
394397
cookie = 'foo=bar; baz=feez',
395398
}
@@ -405,7 +408,7 @@ g.test_server_requests = function()
405408
resp:setcookie({ name = 'xxx', value = 'yyy' })
406409
return resp
407410
end)
408-
local r = http_client.get('http://127.0.0.1:12345/cookie')
411+
r = http_client.get('http://127.0.0.1:12345/cookie')
409412
t.assertEquals(r.status, 200, 'status')
410413
t.assertTrue(r.headers['set-cookie'] ~= nil, "header")
411414

@@ -425,7 +428,7 @@ g.test_server_requests = function()
425428
status = 200,
426429
}
427430
end)
428-
local r = http_client.get(
431+
r = http_client.get(
429432
'http://127.0.0.1:12345/check_req_properties?foo=1&bar=2', {
430433
headers = {
431434
['X-test-header'] = 'test-value'
@@ -475,7 +478,7 @@ g.test_server_requests = function()
475478
})
476479
t.assertEquals(r.status, 200, 'status')
477480

478-
local parsed_body = json.decode(r.body)
481+
parsed_body = json.decode(r.body)
479482
t.assertEquals(parsed_body.request_line, 'POST /check_req_methods_for_json HTTP/1.1', 'req.request_line')
480483
t.assertEquals(parsed_body.read_cached, '{"kind": "json"}', 'json req:read_cached()')
481484
t.assertEquals(parsed_body.json, {kind = "json"}, 'req:json()')
@@ -491,22 +494,21 @@ g.test_server_requests = function()
491494

492495
if is_builtin_test() then
493496
router:route({ path = '/post', method = 'POST'}, function(req)
494-
local t = {
497+
return req:render({json = {
495498
#req:read("\n");
496499
#req:read(10);
497500
#req:read({ size = 10, delimiter = "\n"});
498501
#req:read("\n");
499502
#req:read();
500503
#req:read();
501504
#req:read();
502-
}
503-
return req:render({json = t})
505+
}})
504506
end)
505507
local bodyf = os.getenv('LUA_SOURCE_DIR') or './'
506508
bodyf = io.open(fio.pathjoin(bodyf, 'test/public/lorem.txt'))
507509
local body = bodyf:read('*a')
508510
bodyf:close()
509-
local r = http_client.post('http://127.0.0.1:12345/post', body)
511+
r = http_client.post('http://127.0.0.1:12345/post', body)
510512
t.assertEquals(r.status, 200, 'status')
511513
t.assertEquals(json.decode(r.body), { 541,10,10,458,1375,0,0 },
512514
'req:read() results')
@@ -558,7 +560,7 @@ g.test_server_requests = function()
558560
body = 'GET *',
559561
}
560562
end)
561-
local r = http_client.get('http://127.0.0.1:12345/a/b/c')
563+
r = http_client.get('http://127.0.0.1:12345/a/b/c')
562564
t.assertEquals(r.status, 200, '/a/b/c request returns 200')
563565
t.assertEquals(r.body, 'GET *', 'GET * matches')
564566

@@ -568,7 +570,7 @@ g.test_server_requests = function()
568570
body = 'ANY /a/:foo/:bar',
569571
}
570572
end)
571-
local r = http_client.get('http://127.0.0.1:12345/a/b/c')
573+
r = http_client.get('http://127.0.0.1:12345/a/b/c')
572574
t.assertEquals(r.status, 200, '/a/b/c request returns 200')
573575
t.assertEquals(
574576
r.body,

0 commit comments

Comments
 (0)