Skip to content

Commit 308ca09

Browse files
authored
Merge pull request #96 from TPath123/luacheck
[luacheck] Run luacheck on all files and fix warnings
2 parents 26d7780 + bc94e24 commit 308ca09

15 files changed

Lines changed: 203 additions & 195 deletions

.luacheckrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ignore = {"213/_.*"} -- unused Loop Variables beginning with underscore
2+
files["openwisp-config/tests"] = {
3+
ignore = {"11./Test.*"} -- globals relating to defining unittests
4+
}

openwisp-config/files/lib/openwisp/net.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local net = {}
44

55
function net.get_interface(name, family)
66
local uci_cursor = uci.cursor()
7-
local family = family or 'inet'
7+
local ip_family = family or 'inet'
88
-- if UCI network name is a bridge, the ifname won't be the name of the bridge
99
local is_bridge = uci_cursor:get('network', name, 'type') == 'bridge'
1010
local ifname
@@ -17,8 +17,8 @@ function net.get_interface(name, family)
1717
end
1818
-- get list of interfaces and loop until found
1919
local interfaces = nixio.getifaddrs()
20-
for i, interface in pairs(interfaces) do
21-
if interface.name == ifname and interface.family == family then
20+
for _, interface in pairs(interfaces) do
21+
if interface.name == ifname and interface.family == ip_family then
2222
return interface
2323
end
2424
end

openwisp-config/files/lib/openwisp/utils.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ end
3030

3131
function utils.dirname(path)
3232
local parts = utils.split(path, '/')
33-
local path = '/'
33+
local returnPath = '/'
3434
local length = table.getn(parts)
3535
for i, part in ipairs(parts) do
3636
if i < length then
37-
path = path..part..'/'
37+
returnPath = returnPath..part..'/'
3838
end
3939
end
40-
return path
40+
return returnPath
4141
end
4242

4343
function utils.add_values_to_set(set, values)
44-
for i, el in pairs(values) do
44+
for _, el in pairs(values) do
4545
set[el] = true
4646
end
4747
return set
@@ -142,10 +142,10 @@ end
142142

143143
-- Code by David Kastrup
144144
-- http://lua-users.org/wiki/DirTreeIterator
145-
function utils.dirtree(dir)
146-
assert(dir and dir ~= '', 'directory parameter is missing or empty')
147-
if string.sub(dir, -1) == '/' then
148-
local dir = string.sub(dir, 1, -2)
145+
function utils.dirtree(dirParam)
146+
assert(dirParam and dirParam ~= '', 'directory parameter is missing or empty')
147+
if string.sub(dirParam, -1) == '/' then
148+
dirParam = string.sub(dirParam, 1, -2)
149149
end
150150
local function yieldtree(dir)
151151
for entry in lfs.dir(dir) do
@@ -159,7 +159,7 @@ function utils.dirtree(dir)
159159
end
160160
end
161161
end
162-
return coroutine.wrap(function() yieldtree(dir) end)
162+
return coroutine.wrap(function() yieldtree(dirParam) end)
163163
end
164164

165165
function utils.file_exists(path)

openwisp-config/files/sbin/openwisp-remove-default-wifi.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env lua
22
-- removes OpenWrt/LEDE default wifi-iface settings, if present
33

4-
require('uci')
4+
local uci = require('uci')
55

66
-- parse arguments
7+
local test
78
local arg={...}
8-
for key, value in pairs(arg) do
9+
for _, value in pairs(arg) do
910
-- test argument
1011
if value == '--test=1' then test = true; end
1112
end
@@ -16,7 +17,7 @@ local standard_path = standard_prefix .. 'config/'
1617
local standard = uci.cursor(standard_path)
1718
local changed = false
1819

19-
function is_default_wifi(section)
20+
local function is_default_wifi(section)
2021
if section.encryption == 'none' and
2122
section.mode == 'ap' and
2223
section.network == 'lan' and

openwisp-config/files/sbin/openwisp-restore-unmanaged.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env lua
22
-- restores unmanaged configurations
33

4-
require('uci')
4+
local uci = require('uci')
5+
local lfs = require('lfs')
56
local utils = require('openwisp.utils')
67

78
-- parse arguments
9+
local test = false
810
local arg={...}
911
for key, value in pairs(arg) do
1012
-- test argument

openwisp-config/files/sbin/openwisp-store-unmanaged.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env lua
22
-- stores unmanaged configurations
33

4-
require('os')
5-
require('io')
6-
require('uci')
4+
local os = require('os')
5+
local io = require('io')
6+
local uci = require('uci')
77
local utils = require('openwisp.utils')
88
local sections
99
local arg={...}
1010

1111
-- parse arguments
12+
local test = false
1213
for key, value in pairs(arg) do
1314
-- test argument
1415
if value == '--test=1' then test = true; end
@@ -25,7 +26,7 @@ local standard_path = standard_prefix .. 'config/'
2526
local unmanaged_path = unmanaged_prefix .. 'unmanaged/'
2627
local uci_tmp_path = '/tmp/openwisp/.uci'
2728

28-
function empty_file(path)
29+
local function empty_file(path)
2930
local file = io.open(path, 'w')
3031
file:write('')
3132
file:close()
@@ -46,7 +47,7 @@ end
4647
-- }
4748
local unmanaged_map = {}
4849
local section_list = utils.split(sections)
49-
for i, section in pairs(section_list) do
50+
for _, section in pairs(section_list) do
5051
local parts = utils.split(section, '.')
5152
-- skip unrecognized strings
5253
if parts[1] and parts[2] then
@@ -64,7 +65,7 @@ for i, section in pairs(section_list) do
6465
if not unmanaged_map[config] then
6566
unmanaged_map[config] = {}
6667
end
67-
el = {name=section_name, type=section_type}
68+
local el = {name=section_name, type=section_type}
6869
table.insert(unmanaged_map[config], el)
6970
end
7071
end

openwisp-config/files/sbin/openwisp-update-config.lua

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
#!/usr/bin/env lua
22
-- openwisp-config configuration updater
33

4-
require('os')
5-
require('lfs')
6-
require('uci')
4+
local os = require('os')
5+
local lfs = require('lfs')
6+
local uci = require('uci')
77
local utils = require('openwisp.utils')
88
local arg = {...}
99

1010
-- parse arguments
11-
MERGE = true
12-
TEST = false
13-
for key, value in pairs(arg) do
11+
local MERGE = true
12+
local TEST = false
13+
for _, value in pairs(arg) do
1414
-- test argument
1515
if value == '--merge=0' then MERGE = false; end
1616
if value == '--test=1' then TEST = true; end
1717
end
1818

19-
working_dir = lfs.currentdir()
20-
tmp_dir = not TEST and '/tmp/openwisp' or working_dir
21-
check_dir = tmp_dir .. '/check'
22-
check_config_dir = check_dir..'/etc/config'
23-
downloaded_conf = tmp_dir .. '/configuration.tar.gz'
24-
openwisp_dir = not TEST and '/etc/openwisp' or working_dir .. '/openwisp'
25-
standard_config_dir = not TEST and '/etc/config' or working_dir .. '/update-test/etc/config'
26-
test_root_dir = working_dir .. '/update-test'
27-
remote_dir = openwisp_dir .. '/remote'
28-
remote_config_dir = remote_dir .. '/etc/config'
29-
stored_dir = openwisp_dir .. '/stored'
30-
added_file = openwisp_dir .. '/added.list'
31-
modified_file = openwisp_dir .. '/modified.list'
32-
get_standard = function() return uci.cursor(standard_config_dir) end
33-
get_remote = function() return uci.cursor(remote_config_dir, '/tmp/openwisp/.uci') end
34-
get_check = function() return uci.cursor(check_config_dir, '/tmp/openwisp/.uci') end
19+
local working_dir = lfs.currentdir()
20+
local tmp_dir = not TEST and '/tmp/openwisp' or working_dir
21+
local check_dir = tmp_dir .. '/check'
22+
local check_config_dir = check_dir..'/etc/config'
23+
local downloaded_conf = tmp_dir .. '/configuration.tar.gz'
24+
local openwisp_dir = not TEST and '/etc/openwisp' or working_dir .. '/openwisp'
25+
local standard_config_dir = not TEST and '/etc/config' or working_dir .. '/update-test/etc/config'
26+
local test_root_dir = working_dir .. '/update-test'
27+
local remote_dir = openwisp_dir .. '/remote'
28+
local remote_config_dir = remote_dir .. '/etc/config'
29+
local stored_dir = openwisp_dir .. '/stored'
30+
local added_file = openwisp_dir .. '/added.list'
31+
local modified_file = openwisp_dir .. '/modified.list'
32+
local get_standard = function() return uci.cursor(standard_config_dir) end
33+
local get_remote = function() return uci.cursor(remote_config_dir, '/tmp/openwisp/.uci') end
34+
local get_check = function() return uci.cursor(check_config_dir, '/tmp/openwisp/.uci') end
3535

3636
-- uci cursors
37-
standard = get_standard()
38-
remote = get_remote()
37+
local standard = get_standard()
38+
local remote = get_remote()
3939

4040
-- check downloaded UCI files before proceeding
4141
os.execute('mkdir -p '..check_dir)
4242
os.execute('tar -zxf '..downloaded_conf..' -C '..check_dir)
43-
check = get_check()
43+
local check = get_check()
4444

4545
if lfs.attributes(check_config_dir, 'mode') == 'directory' then
4646
for file in lfs.dir(check_config_dir) do
47-
path = check_config_dir..'/'..file
47+
local path = check_config_dir..'/'..file
4848
if lfs.attributes(path, 'mode') == 'file' then
4949
if check:get_all(file) == nil then
5050
print('ERROR: invalid UCI configuration file: '..file)
@@ -67,7 +67,7 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
6767
for file in lfs.dir(remote_config_dir) do
6868
local standard_path = standard_config_dir .. '/' .. file
6969
if lfs.attributes(standard_path, 'mode') == 'file' then
70-
for key, section in pairs(remote:get_all(file)) do
70+
for _, section in pairs(remote:get_all(file)) do
7171
-- search section in the downloaded configuration
7272
local section_check = check:get(file, section['.name'])
7373
-- remove section from current configuration if not in downloaded configuration
@@ -92,8 +92,8 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
9292
end
9393
standard:commit(file)
9494
-- remove uci file if empty
95-
local uci = standard:get_all(file)
96-
if uci and utils.is_table_empty(uci) then
95+
local uciFile = standard:get_all(file)
96+
if uciFile and utils.is_table_empty(uciFile) then
9797
os.remove(standard_path)
9898
end
9999
end
@@ -120,7 +120,7 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
120120
if MERGE then
121121
-- avoid "file does not exist" error
122122
os.execute('touch ' .. standard_path)
123-
for key, section in pairs(remote:get_all(file)) do
123+
for _, section in pairs(remote:get_all(file)) do
124124
utils.write_uci_section(standard, file, section)
125125
end
126126
standard:commit(file)
@@ -136,14 +136,14 @@ end
136136
os.execute('touch ' .. added_file)
137137
os.execute('touch ' .. modified_file)
138138
-- convert lists to lua sets
139-
added = utils.file_to_set(added_file)
140-
modified = utils.file_to_set(modified_file)
141-
added_changed = false
142-
modified_changed = false
139+
local added = utils.file_to_set(added_file)
140+
local modified = utils.file_to_set(modified_file)
141+
local added_changed = false
142+
local modified_changed = false
143143

144144
-- loop each file except directories and standard UCI config files
145-
ignored_path = remote_config_dir .. '/'
146-
function is_ignored(path)
145+
local ignored_path = remote_config_dir .. '/'
146+
local function is_ignored(path)
147147
return utils.starts_with(path, ignored_path)
148148
end
149149

openwisp-config/tests/test_remove_default_wifi.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require('os')
22
require('io')
3-
luaunit = require('luaunit')
4-
remove_default_wifi = assert(loadfile("../files/sbin/openwisp-remove-default-wifi.lua"))
3+
local luaunit = require('luaunit')
4+
local remove_default_wifi = assert(loadfile("../files/sbin/openwisp-remove-default-wifi.lua"))
55

66
TestRemoveDefaultWifi = {
77
setUp = function()

openwisp-config/tests/test_restore_unmanaged.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
package.path = package.path .. ';../files/lib/?.lua'
33
require('os')
44
require('io')
5-
luaunit = require('luaunit')
6-
restore_unmanaged = assert(loadfile("../files/sbin/openwisp-restore-unmanaged.lua"))
7-
write_dir = './unmanaged/'
5+
local luaunit = require('luaunit')
6+
local restore_unmanaged = assert(loadfile("../files/sbin/openwisp-restore-unmanaged.lua"))
7+
local write_dir = './unmanaged/'
88

99
TestRestoreUnmanaged = {
1010
setUp = function()

openwisp-config/tests/test_store_unmanaged.lua

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
package.path = package.path .. ';../files/lib/?.lua'
33
require('os')
44
require('io')
5-
luaunit = require('luaunit')
6-
store_unmanaged = assert(loadfile("../files/sbin/openwisp-store-unmanaged.lua"))
7-
default_blocks = "system.ntp " ..
5+
local luaunit = require('luaunit')
6+
local store_unmanaged = assert(loadfile("../files/sbin/openwisp-store-unmanaged.lua"))
7+
local default_blocks = "system.ntp " ..
88
"system.@led " ..
99
"network.loopback " ..
1010
"network.@globals " ..
1111
"network.lan " ..
1212
"network.wan " ..
1313
"network.@switch " ..
1414
"network.@switch_vlan"
15-
write_dir = './unmanaged/'
16-
assertNotNil = luaunit.assertNotNil
17-
assertNil = luaunit.assertNil
18-
assertEquals = luaunit.assertEquals
15+
local write_dir = './unmanaged/'
16+
local assertNotNil = luaunit.assertNotNil
17+
local assertNil = luaunit.assertNil
18+
local assertEquals = luaunit.assertEquals
1919

2020
local function _clean()
2121
os.remove(write_dir .. 'network')
@@ -37,27 +37,27 @@ end
3737
function TestStoreUnmanaged.test_default()
3838
store_unmanaged('--test=1', '-o=' .. default_blocks)
3939
-- ensure network config file has been created correctly
40-
local file = io.open(write_dir .. 'network')
41-
assertNotNil(file)
42-
local contents = file:read('*all')
43-
assertNotNil(string.find(contents, "config interface 'loopback'"))
44-
assertNotNil(string.find(contents, "option ipaddr '127.0.0.1'"))
45-
assertNotNil(string.find(contents, "option ula_prefix 'fd8e:f40a:6701::/48'"))
46-
assertNotNil(string.find(contents, "config interface 'wan'"))
47-
assertNotNil(string.find(contents, "config switch"))
48-
assertNotNil(string.find(contents, "option vlan '2'"))
49-
assertNotNil(string.find(contents, "option vlan '1'"))
50-
assertNil(string.find(contents, "config interface 'wlan0'"))
51-
assertNil(string.find(contents, "config interface 'wlan1'"))
40+
local networkFile = io.open(write_dir .. 'network')
41+
assertNotNil(networkFile)
42+
local networkContents = networkFile:read('*all')
43+
assertNotNil(string.find(networkContents, "config interface 'loopback'"))
44+
assertNotNil(string.find(networkContents, "option ipaddr '127.0.0.1'"))
45+
assertNotNil(string.find(networkContents, "option ula_prefix 'fd8e:f40a:6701::/48'"))
46+
assertNotNil(string.find(networkContents, "config interface 'wan'"))
47+
assertNotNil(string.find(networkContents, "config switch"))
48+
assertNotNil(string.find(networkContents, "option vlan '2'"))
49+
assertNotNil(string.find(networkContents, "option vlan '1'"))
50+
assertNil(string.find(networkContents, "config interface 'wlan0'"))
51+
assertNil(string.find(networkContents, "config interface 'wlan1'"))
5252
-- ensure system config file exists
53-
local file = io.open(write_dir .. 'system')
54-
assertNotNil(file)
55-
local contents = file:read('*all')
56-
assertNotNil(string.find(contents, "list server '1.openwrt.pool.ntp.org'"))
57-
assertNotNil(string.find(contents, "config led 'led_usb1'"))
58-
assertNotNil(string.find(contents, "config led 'led_usb2'"))
59-
assertNotNil(string.find(contents, "config led 'led_wlan2g'"))
60-
assertNil(string.find(contents, "option hostname 'OpenWrt'"))
53+
local systemFile = io.open(write_dir .. 'system')
54+
assertNotNil(systemFile)
55+
local systemContents = systemFile:read('*all')
56+
assertNotNil(string.find(systemContents, "list server '1.openwrt.pool.ntp.org'"))
57+
assertNotNil(string.find(systemContents, "config led 'led_usb1'"))
58+
assertNotNil(string.find(systemContents, "config led 'led_usb2'"))
59+
assertNotNil(string.find(systemContents, "config led 'led_wlan2g'"))
60+
assertNil(string.find(systemContents, "option hostname 'OpenWrt'"))
6161
end
6262

6363
function TestStoreUnmanaged.test_specific_name()

0 commit comments

Comments
 (0)