From da83f5296a9e41bc55c8a5bd83fdc3bfd7bf5f54 Mon Sep 17 00:00:00 2001 From: Andrew Lewis Date: Mon, 14 Nov 2016 20:39:16 +0200 Subject: [PATCH] [Minor] Lint rspamadm & test suite scripts --- .luacheckrc | 16 ++++++++++------ src/rspamadm/confighelp.lua | 7 +++---- src/rspamadm/fuzzy_convert.lua | 30 ++++++++++++++++++++---------- src/rspamadm/fuzzy_stat.lua | 30 +++++++++++++++--------------- src/rspamadm/stat_convert.lua | 10 +++++----- test/functional/lua/deps.lua | 4 +--- test/functional/lua/flags.lua | 2 -- test/functional/lua/mapreload.lua | 2 +- test/functional/lua/simple.lua | 2 +- test/functional/lua/tlds.lua | 4 ++-- 10 files changed, 58 insertions(+), 49 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 2eb10ce1a..2f656c124 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -2,11 +2,16 @@ codes = true std = 'min' exclude_files = { + '/**/contrib/**', + '/**/test/lua/**', } globals = { + 'check_header_delimiter_empty', + 'check_header_delimiter_tab', 'classifiers', 'config', + 'kmail_msgid', 'rspamd_config', 'rspamd_count_metatokens', 'rspamd_gen_metatokens', @@ -21,13 +26,12 @@ globals = { ignore = { } -files['/**/rules/regexp/headers.lua'].globals = { - 'check_header_delimiter_empty', - 'check_header_delimiter_tab', - 'kmail_msgid', -} - files['/**/src/plugins/lua/spamassassin.lua'].globals = { 'ffi', 'jit', } + +files['/**/src/rspamadm/*'].globals = { + 'ansicolors', + 'getopt', +} diff --git a/src/rspamadm/confighelp.lua b/src/rspamadm/confighelp.lua index 3af3c5b1f..0d61bf394 100644 --- a/src/rspamadm/confighelp.lua +++ b/src/rspamadm/confighelp.lua @@ -1,4 +1,3 @@ -local util = require "rspamd_util" local opts = {} local known_attrs = { data = 1, @@ -20,7 +19,7 @@ local function maybe_print_color(key) end end -local function sort_values(tbl, opts) +local function sort_values(tbl) local res = {} for k, v in pairs(tbl) do table.insert(res, { key = k, value = v }) @@ -91,7 +90,7 @@ local function print_help(key, value, tabs) end local sorted = sort_values(value) - for i, v in ipairs(sorted) do + for _, v in ipairs(sorted) do if not known_attrs[v['key']] then -- We need to go deeper print_help(v['key'], v['value'], tabs .. '\t') @@ -104,7 +103,7 @@ return function(args, res) local sorted = sort_values(res) - for i,v in ipairs(sorted) do + for _,v in ipairs(sorted) do print_help(v['key'], v['value'], '') print('') end diff --git a/src/rspamadm/fuzzy_convert.lua b/src/rspamadm/fuzzy_convert.lua index 5db3c925b..a7ee666b9 100644 --- a/src/rspamadm/fuzzy_convert.lua +++ b/src/rspamadm/fuzzy_convert.lua @@ -41,10 +41,13 @@ end local function send_digests(digests, redis_host, redis_password, redis_db) local conn, err = connect_redis(redis_host, redis_password, redis_db) - if not conn then return false end - local _, v + if err then + print(err) + return false + end + local ret for _, v in ipairs(digests) do - local ret, err = conn:add_cmd('HMSET', { + ret, err = conn:add_cmd('HMSET', { 'fuzzy' .. v[1], 'F', v[2], 'V', v[3], @@ -72,10 +75,13 @@ end local function send_shingles(shingles, redis_host, redis_password, redis_db) local conn, err = connect_redis(redis_host, redis_password, redis_db) - if not conn then return false end - local _, v + if err then + print("Redis error: " .. err) + return false + end + local ret for _, v in ipairs(shingles) do - local ret, err = conn:add_cmd('SET', { + ret, err = conn:add_cmd('SET', { 'fuzzy_' .. v[2] .. '_' .. v[1], v[4], }) @@ -102,8 +108,12 @@ end local function update_counters(total, redis_host, redis_password, redis_db) local conn, err = connect_redis(redis_host, redis_password, redis_db) - if not conn then return false end - local ret, err = conn:add_cmd('SET', { + if err then + print(err) + return false + end + local ret + ret, err = conn:add_cmd('SET', { 'fuzzylocal', total, }) @@ -111,7 +121,7 @@ local function update_counters(total, redis_host, redis_password, redis_db) print('Cannot batch SET command: ' .. err) return false end - local ret, err = conn:add_cmd('SET', { + ret, err = conn:add_cmd('SET', { 'fuzzy_count', total, }) @@ -127,7 +137,7 @@ local function update_counters(total, redis_host, redis_password, redis_db) return true end -return function (args, res) +return function (_, res) local db = sqlite3.open(res['source_db']) local shingles = {} local digests = {} diff --git a/src/rspamadm/fuzzy_stat.lua b/src/rspamadm/fuzzy_stat.lua index 864e149a0..401b297d1 100644 --- a/src/rspamadm/fuzzy_stat.lua +++ b/src/rspamadm/fuzzy_stat.lua @@ -47,7 +47,7 @@ local function print_stat(st, tabs) end -- Sort by checked -local function sort_ips(tbl, opts) +local function sort_ips(tbl, _opts) local res = {} for k,v in pairs(tbl) do table.insert(res, {ip = k, data = v}) @@ -55,23 +55,23 @@ local function sort_ips(tbl, opts) local function sort_order(elt) local key = 'checked' - local res = 0 + local _res = 0 - if opts['sort'] then - if opts['sort'] == 'matched' then + if _opts['sort'] then + if _opts['sort'] == 'matched' then key = 'matched' - elseif opts['sort'] == 'errors' then + elseif _opts['sort'] == 'errors' then key = 'errors' - elseif opts['sort'] == 'ip' then + elseif _opts['sort'] == 'ip' then return elt['ip'] end end if elt['data'][key] then - res = elt['data'][key] + _res = elt['data'][key] end - return res + return _res end table.sort(res, function(a, b) @@ -150,7 +150,7 @@ return function(args, res) opts = getopt(args, '') if wrk then - for i,pr in pairs(wrk) do + for _,pr in pairs(wrk) do -- processes cycle if pr['data'] then local id = pr['id'] @@ -227,16 +227,16 @@ return function(args, res) local res_keys = st['keys'] if res_keys and not opts['no-keys'] and not opts['short'] then print('Keys statistics:') - for k,st in pairs(res_keys) do + for k,_st in pairs(res_keys) do print(string.format('Key id: %s', k)) - print_stat(st, '\t') + print_stat(_st, '\t') - if st['ips'] and not opts['no-ips'] then + if _st['ips'] and not opts['no-ips'] then print('') print('\tIPs stat:') - local sorted_ips = sort_ips(st['ips'], opts) + local sorted_ips = sort_ips(_st['ips'], opts) - for i,v in ipairs(sorted_ips) do + for _,v in ipairs(sorted_ips) do print(string.format('\t%s', v['ip'])) print_stat(v['data'], '\t\t') print('') @@ -264,7 +264,7 @@ return function(args, res) print('IPs statistics:') local sorted_ips = sort_ips(res_ips, opts) - for i, v in ipairs(sorted_ips) do + for _, v in ipairs(sorted_ips) do print(string.format('%s', v['ip'])) print_stat(v['data'], '\t') print('') diff --git a/src/rspamadm/stat_convert.lua b/src/rspamadm/stat_convert.lua index bd3e21bd0..5c880d7c5 100644 --- a/src/rspamadm/stat_convert.lua +++ b/src/rspamadm/stat_convert.lua @@ -41,6 +41,7 @@ local function convert_learned(cache, server, password, redis_db) local converted = 0 local db = sqlite3.open(cache) local ret = true + local err_str if not db then print('Cannot open cache database: ' .. cache) @@ -84,7 +85,6 @@ local function convert_learned(cache, server, password, redis_db) end db:sql('COMMIT;') - local err_str if ret then ret,err_str = conn:exec() end @@ -99,7 +99,7 @@ local function convert_learned(cache, server, password, redis_db) return ret end -return function (args, res) +return function (_, res) local db = sqlite3.open(res['source_db']) local tokens = {} local num = 0 @@ -110,8 +110,8 @@ return function (args, res) local learns = {} local redis_password = res['redis_password'] local redis_db = nil - local ret = false local cmd = 'HINCRBY' + local ret, err_str if res['redis_db'] then redis_db = tostring(res['redis_db']) @@ -166,7 +166,7 @@ return function (args, res) num = num + 1 total = total + 1 if num > lim then - local ret,err_str = send_redis(res['redis_host'], res['symbol'], + ret,err_str = send_redis(res['redis_host'], res['symbol'], tokens, redis_password, redis_db, cmd) if not ret then print('Cannot send tokens to the redis server: ' .. err_str) @@ -178,7 +178,7 @@ return function (args, res) end end if #tokens > 0 then - local ret, err_str = send_redis(res['redis_host'], res['symbol'], tokens, + ret, err_str = send_redis(res['redis_host'], res['symbol'], tokens, redis_password, redis_db, cmd) if not ret then diff --git a/test/functional/lua/deps.lua b/test/functional/lua/deps.lua index 581825400..55897091e 100644 --- a/test/functional/lua/deps.lua +++ b/test/functional/lua/deps.lua @@ -1,5 +1,3 @@ -local logger = require "rspamd_logger" - local cb = function(task) task:insert_result('TOP', 1.0) end @@ -29,4 +27,4 @@ rspamd_config:register_dependency('DEP1', 'TOP') for i = 2,10 do rspamd_config:register_symbol('DEP' .. tostring(i), 1.0, cb_gen(i - 1)) rspamd_config:register_dependency('DEP' .. tostring(i), 'DEP' .. tostring(i - 1)) -end \ No newline at end of file +end diff --git a/test/functional/lua/flags.lua b/test/functional/lua/flags.lua index 61f135f1c..fa99d367f 100644 --- a/test/functional/lua/flags.lua +++ b/test/functional/lua/flags.lua @@ -24,8 +24,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ]]-- -local logger = require "rspamd_logger" - rspamd_config:register_post_filter(function(task) task:set_flag('no_stat') task:set_flag('no_log', false) diff --git a/test/functional/lua/mapreload.lua b/test/functional/lua/mapreload.lua index 73c5d0729..aa1de9392 100644 --- a/test/functional/lua/mapreload.lua +++ b/test/functional/lua/mapreload.lua @@ -6,7 +6,7 @@ local test_map = rspamd_config:add_map ({ rspamd_config:register_symbol({ name = 'MAP_SET_HIT_AND_MISS', score = 1.0, - callback = function(task) + callback = function() if (test_map:get_key('example.com') and not test_map:get_key('rspamd.com')) then return true, 'example.com' elseif (test_map:get_key('rspamd.com') and not test_map:get_key('example.com')) then diff --git a/test/functional/lua/simple.lua b/test/functional/lua/simple.lua index 51a1f8b27..22ecde802 100644 --- a/test/functional/lua/simple.lua +++ b/test/functional/lua/simple.lua @@ -1,7 +1,7 @@ rspamd_config:register_symbol({ name = 'SIMPLE_TEST', score = 1.0, - callback = function(task) + callback = function() return true, 'Fires always' end }) diff --git a/test/functional/lua/tlds.lua b/test/functional/lua/tlds.lua index bc655dc9e..efaf01af5 100644 --- a/test/functional/lua/tlds.lua +++ b/test/functional/lua/tlds.lua @@ -1,7 +1,7 @@ rspamd_config:register_symbol({ name = 'TEST_TLD', score = 1.0, - callback = function(task) + callback = function() local prefixes = { '', 'example.' @@ -38,7 +38,7 @@ rspamd_config:register_symbol({ return end local u = rspamd_url.create(pool, p .. d) - local test = u:get_tld() + test = u:get_tld() if (test ~= d) then table.insert(worry, 'url.get_tld:' .. p .. d .. ':' .. test) return -- 2.39.5