flags: Allow storing and accessing them from scripts

parent 4238679a
......@@ -31,6 +31,7 @@ local tostring = tostring
local table = table
local utils = require "utils"
local uri = require "uri"
local backend = require "backend"
local DBG = DBG
local WARN = WARN
......@@ -326,4 +327,10 @@ function script(result, context, name, script_uri, extra)
result.uri = script_uri
end
function store_flags(result, context, ...)
DBG("Storing flags ", ...)
backend.flags_mark(context.full_name, ...)
backend.flags_write(false)
end
return _M
......@@ -257,6 +257,10 @@ local funcs = {
Script = {
mode = "morpher",
value = requests.script
},
StoreFlags = {
mode = "morpher",
value = requests.store_flags
}
}
}
......
......@@ -855,6 +855,16 @@ function test_flags_mark()
a = "123"
}
})
check_stored_flags(true, {
["/path"] = {
a = "5678",
x = "123",
y = "2345"
},
["/another"] = {
a = "123"
}
})
end
function setup()
......
......@@ -27,6 +27,8 @@ local backend = require "backend"
module("requests-tests", package.seeall, lunit.testcase)
local tmp_dirs = {}
local function run_sandbox_fun(func_code, level)
local chunk = "result = " .. func_code
local env
......@@ -222,6 +224,30 @@ function test_script_err_propagate()
assert_equal("error", err.tp)
end
function test_store_flags()
local test_root = mkdtemp()
table.insert(tmp_dirs, test_root)
backend.flags_storage = test_root .. "/flags"
local result = sandbox.run_sandboxed([[
flags[""].x = "hello"
flags[""].y = "hi"
StoreFlags "x"
]], "", "Local")
assert_equal("context", result.tp, result.msg)
assert_table_equal({
[""] = {
values = {
x = "hello"
},
provided = {
x = "hello",
y = "hi"
},
proxy = {}
}
}, backend.stored_flags)
end
function setup()
-- Don't download stuff now
mock_gen("uri.new", function (context, u) return {u = u} end, true)
......@@ -233,4 +259,6 @@ function teardown()
requests.content_requests = {}
backend.stored_flags = {}
mocks_reset()
utils.cleanup_dirs(tmp_dirs)
tmp_dirs = {}
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment