diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2014-01-12 14:56:39 -0200 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2014-01-12 14:56:39 -0200 |
| commit | b78aa96d3858554c49d3a6521f1d458fb80c3105 (patch) | |
| tree | c8e4e70243ebfe655fb915eb104b6dbffbfa83ae | |
| parent | 77f68c79217cea4e5244d3403fc089e3d8dabd82 (diff) | |
| download | luarocks-b78aa96d3858554c49d3a6521f1d458fb80c3105.tar.gz luarocks-b78aa96d3858554c49d3a6521f1d458fb80c3105.tar.bz2 luarocks-b78aa96d3858554c49d3a6521f1d458fb80c3105.zip | |
Catch undeclared globals in rockspecs
| -rw-r--r-- | src/luarocks/fetch.lua | 3 | ||||
| -rw-r--r-- | src/luarocks/manif_core.lua | 3 | ||||
| -rw-r--r-- | src/luarocks/persist.lua | 12 | ||||
| -rw-r--r-- | src/luarocks/type_check.lua | 29 |
4 files changed, 39 insertions, 8 deletions
diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 83ab6fa9..7960acb4 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua | |||
| @@ -148,10 +148,11 @@ function load_local_rockspec(filename, quick) | |||
| 148 | if not rockspec then | 148 | if not rockspec then |
| 149 | return nil, "Could not load rockspec file "..filename.." ("..err..")" | 149 | return nil, "Could not load rockspec file "..filename.." ("..err..")" |
| 150 | end | 150 | end |
| 151 | local globals = err | ||
| 151 | 152 | ||
| 152 | local ok, err = true, nil | 153 | local ok, err = true, nil |
| 153 | if not quick then | 154 | if not quick then |
| 154 | ok, err = type_check.type_check_rockspec(rockspec) | 155 | ok, err = type_check.type_check_rockspec(rockspec, globals) |
| 155 | if not ok then | 156 | if not ok then |
| 156 | return nil, filename..": "..err | 157 | return nil, filename..": "..err |
| 157 | end | 158 | end |
diff --git a/src/luarocks/manif_core.lua b/src/luarocks/manif_core.lua index 6424a1e8..b9fe8ab9 100644 --- a/src/luarocks/manif_core.lua +++ b/src/luarocks/manif_core.lua | |||
| @@ -22,8 +22,9 @@ function manifest_loader(file, repo_url, quick) | |||
| 22 | if not manifest then | 22 | if not manifest then |
| 23 | return nil, "Failed loading manifest for "..repo_url..": "..err | 23 | return nil, "Failed loading manifest for "..repo_url..": "..err |
| 24 | end | 24 | end |
| 25 | local globals = err | ||
| 25 | if not quick then | 26 | if not quick then |
| 26 | local ok, err = type_check.type_check_manifest(manifest) | 27 | local ok, err = type_check.type_check_manifest(manifest, globals) |
| 27 | if not ok then | 28 | if not ok then |
| 28 | return nil, "Error checking manifest: "..err | 29 | return nil, "Error checking manifest: "..err |
| 29 | end | 30 | end |
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 5b92f9cb..8f07da75 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua | |||
| @@ -20,6 +20,15 @@ function load_into_table(filename, tbl) | |||
| 20 | 20 | ||
| 21 | local result, chunk, ran, err | 21 | local result, chunk, ran, err |
| 22 | local result = tbl or {} | 22 | local result = tbl or {} |
| 23 | local globals = {} | ||
| 24 | local globals_mt = { | ||
| 25 | __index = function(t, n) | ||
| 26 | globals[n] = true | ||
| 27 | return rawget(t, n) | ||
| 28 | end | ||
| 29 | } | ||
| 30 | local save_mt = getmetatable(result) | ||
| 31 | setmetatable(result, globals_mt) | ||
| 23 | if _VERSION == "Lua 5.1" then -- Lua 5.1 | 32 | if _VERSION == "Lua 5.1" then -- Lua 5.1 |
| 24 | chunk, err = loadfile(filename) | 33 | chunk, err = loadfile(filename) |
| 25 | if chunk then | 34 | if chunk then |
| @@ -32,6 +41,7 @@ function load_into_table(filename, tbl) | |||
| 32 | ran, err = pcall(chunk) | 41 | ran, err = pcall(chunk) |
| 33 | end | 42 | end |
| 34 | end | 43 | end |
| 44 | setmetatable(result, save_mt) | ||
| 35 | 45 | ||
| 36 | if not chunk then | 46 | if not chunk then |
| 37 | if err:sub(1,5) ~= filename:sub(1,5) then | 47 | if err:sub(1,5) ~= filename:sub(1,5) then |
| @@ -42,7 +52,7 @@ function load_into_table(filename, tbl) | |||
| 42 | if not ran then | 52 | if not ran then |
| 43 | return nil, "Error running file: "..err | 53 | return nil, "Error running file: "..err |
| 44 | end | 54 | end |
| 45 | return result | 55 | return result, globals |
| 46 | end | 56 | end |
| 47 | 57 | ||
| 48 | local write_table | 58 | local write_table |
diff --git a/src/luarocks/type_check.lua b/src/luarocks/type_check.lua index a3d41efc..bf063bf9 100644 --- a/src/luarocks/type_check.lua +++ b/src/luarocks/type_check.lua | |||
| @@ -5,9 +5,9 @@ module("luarocks.type_check", package.seeall) | |||
| 5 | 5 | ||
| 6 | local cfg = require("luarocks.cfg") | 6 | local cfg = require("luarocks.cfg") |
| 7 | 7 | ||
| 8 | rockspec_format = "1.0" | 8 | local rockspec_format = "1.0" |
| 9 | 9 | ||
| 10 | rockspec_types = { | 10 | local rockspec_types = { |
| 11 | rockspec_format = "string", | 11 | rockspec_format = "string", |
| 12 | MUST_package = "string", | 12 | MUST_package = "string", |
| 13 | MUST_version = "[%w.]+-[%d]+", | 13 | MUST_version = "[%w.]+-[%d]+", |
| @@ -97,7 +97,7 @@ rockspec_types.external_dependencies.platforms.ANY = rockspec_types.external_dep | |||
| 97 | rockspec_types.MUST_source.platforms.ANY = rockspec_types.MUST_source | 97 | rockspec_types.MUST_source.platforms.ANY = rockspec_types.MUST_source |
| 98 | rockspec_types.hooks.platforms.ANY = rockspec_types.hooks | 98 | rockspec_types.hooks.platforms.ANY = rockspec_types.hooks |
| 99 | 99 | ||
| 100 | manifest_types = { | 100 | local manifest_types = { |
| 101 | MUST_repository = { | 101 | MUST_repository = { |
| 102 | -- packages | 102 | -- packages |
| 103 | ANY = { | 103 | ANY = { |
| @@ -248,18 +248,35 @@ type_check_table = function(tbl, types, context) | |||
| 248 | return true | 248 | return true |
| 249 | end | 249 | end |
| 250 | 250 | ||
| 251 | local function check_undeclared_globals(globals, types) | ||
| 252 | local undeclared = {} | ||
| 253 | for glob, _ in pairs(globals) do | ||
| 254 | if not (types[glob] or types["MUST_"..glob]) then | ||
| 255 | table.insert(undeclared, glob) | ||
| 256 | end | ||
| 257 | end | ||
| 258 | if #undeclared == 1 then | ||
| 259 | return nil, "Unknown variable: "..undeclared[1] | ||
| 260 | elseif #undeclared > 1 then | ||
| 261 | return nil, "Unknown variables: "..table.concat(undeclared, ", ") | ||
| 262 | end | ||
| 263 | return true | ||
| 264 | end | ||
| 265 | |||
| 251 | --- Type check a rockspec table. | 266 | --- Type check a rockspec table. |
| 252 | -- Verify the correctness of elements from a | 267 | -- Verify the correctness of elements from a |
| 253 | -- rockspec table, reporting on unknown fields and type | 268 | -- rockspec table, reporting on unknown fields and type |
| 254 | -- mismatches. | 269 | -- mismatches. |
| 255 | -- @return boolean or (nil, string): true if type checking | 270 | -- @return boolean or (nil, string): true if type checking |
| 256 | -- succeeded, or nil and an error message if it failed. | 271 | -- succeeded, or nil and an error message if it failed. |
| 257 | function type_check_rockspec(rockspec) | 272 | function type_check_rockspec(rockspec, globals) |
| 258 | assert(type(rockspec) == "table") | 273 | assert(type(rockspec) == "table") |
| 259 | if rockspec.rockspec_format then | 274 | if rockspec.rockspec_format then |
| 260 | -- relies on global state | 275 | -- relies on global state |
| 261 | load_extensions() | 276 | load_extensions() |
| 262 | end | 277 | end |
| 278 | local ok, err = check_undeclared_globals(globals, rockspec_types) | ||
| 279 | if not ok then return nil, err end | ||
| 263 | return type_check_table(rockspec, rockspec_types, "") | 280 | return type_check_table(rockspec, rockspec_types, "") |
| 264 | end | 281 | end |
| 265 | 282 | ||
| @@ -269,7 +286,9 @@ end | |||
| 269 | -- mismatches. | 286 | -- mismatches. |
| 270 | -- @return boolean or (nil, string): true if type checking | 287 | -- @return boolean or (nil, string): true if type checking |
| 271 | -- succeeded, or nil and an error message if it failed. | 288 | -- succeeded, or nil and an error message if it failed. |
| 272 | function type_check_manifest(manifest) | 289 | function type_check_manifest(manifest, globals) |
| 273 | assert(type(manifest) == "table") | 290 | assert(type(manifest) == "table") |
| 291 | local ok, err = check_undeclared_globals(globals, manifest_types) | ||
| 292 | if not ok then return nil, err end | ||
| 274 | return type_check_table(manifest, manifest_types, "") | 293 | return type_check_table(manifest, manifest_types, "") |
| 275 | end | 294 | end |
