Skip to content

Commit 853dbdb

Browse files
committed
better modify n show tests
1 parent 0b86f72 commit 853dbdb

5 files changed

Lines changed: 383 additions & 72 deletions

File tree

src/am.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,9 @@ function am.modify_file(mode, file, path, value)
239239

240240
-- try parse value as json/hjson
241241
if type(value) == "string" then
242-
local parsed_value, _ = hjson.parse(value)
243-
if parsed_value ~= nil then
244-
value = parsed_value
245-
end
242+
local parsed_value, err = hjson.parse(value)
243+
ami_assert(err == nil, "failed to parse value: " .. tostring(err), EXIT_MODIFY_ERROR)
244+
value = parsed_value
246245
end
247246

248247
local ok, err = ami_util.modify_file(mode, file, path_parts, value)

src/ami/internals/util.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ local function modify_add(current_value, new_value)
167167
end
168168

169169
local function modify_remove(current_value, value_to_remove)
170+
local equals = require"eli.util".equals
170171
if type(current_value) == "table" and table.is_array(current_value) then
171172
for i, v in ipairs(current_value) do
172-
if v == value_to_remove then
173+
if equals(v, value_to_remove, true) then
173174
table.remove(current_value, i)
174175
return current_value
175176
end
@@ -240,7 +241,10 @@ function util.modify_file(mode, file, path, value)
240241
local new_value, err = modify_handlers[mode](current_value, value)
241242
if not new_value and err then return nil, "modification failed: " .. tostring(err) end
242243

243-
local result = table.set(content, path, new_value)
244+
local result, err = table.set(content, path, new_value)
245+
if err == "cannot set nested value on a non-table object" then
246+
return nil, "cannot set nested value on a non-table value at path: " .. table.concat(path, ".")
247+
end
244248
if not result then return nil, "failed to set new value in configuration" end
245249

246250
local new_raw_content, err = hjson.stringify(result, { indent = "\t", sort_keys = true })

tests/all.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require"tests.test.am-app"
1414
require"tests.test.cache"
1515
require"tests.test.ami"
1616
require"tests.test.interfaces"
17+
require"tests.test.modify_n_show"
1718

1819
local ntests, nfailed = test.result()
1920
test.summary()

tests/test/ami.lua

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -45,72 +45,6 @@ local function init_ami_test(testDir, configPath, options)
4545
error_called = false
4646
end
4747

48-
local function add_modify_and_show_test(test, mode, file, set_path, value)
49-
test["modify and show: ".. tostring(mode or "--set") ..", ".. tostring(file) ..", ".. tostring(set_path) ..", ".. tostring(value)] = function()
50-
local test_dir = "tests/tmp/ami_test_modify"
51-
init_ami_test(test_dir, "tests/app/configs/ami_test_app@latest.hjson", { cleanupTestDir = true })
52-
53-
os.chdir(default_cwd)
54-
if type(file) == "string" then
55-
local file = path.combine(test_dir, file)
56-
if not fs.exists(file) then
57-
fs.write_file(file, "{}")
58-
end
59-
end
60-
61-
local cwd = os.cwd() or "."
62-
local args = { "--log-level=error", "--path="..test_dir, "modify", set_path, value }
63-
if type(file) == "string" then
64-
table.insert(args, 4, "--file="..file)
65-
end
66-
if type(mode) == "string" then
67-
table.insert(args, 4, "--"..mode)
68-
end
69-
ami(table.unpack(args))
70-
os.chdir(cwd) -- restore cwd
71-
local original_print = print
72-
local printed = ""
73-
print = function(...)
74-
for _, v in ipairs({...}) do
75-
printed = printed .. tostring(v) .. "\t"
76-
end
77-
printed = printed .. "\n"
78-
end
79-
80-
local show_args = { "--log-level=error", "--path="..test_dir, "show"}
81-
if type(file) == "string" then
82-
table.insert(show_args, 4, "--file="..file)
83-
end
84-
ami(table.unpack(show_args))
85-
print = original_print
86-
os.chdir(default_cwd)
87-
test.assert(not error_called)
88-
89-
local value_as_string = type(value) == "string" and value or hjson.stringify(value, { indent = false, sort_keys = true, invalid_objects_as_type = true })
90-
if mode == "unset" or mode == "remove" then
91-
test.assert(printed:match("null") or printed:match(value_as_string) == nil, "Unexpected value '" .. tostring(value) .. "' found in output: " .. printed)
92-
else
93-
test.assert(printed:match(value_as_string) ~= nil, "Expected value '" .. tostring(value) .. "' not found in output: " .. printed)
94-
end
95-
end
96-
end
97-
98-
add_modify_and_show_test(test, "set", nil, "test", "123")
99-
add_modify_and_show_test(test, nil, nil, "test.new_value", "123")
100-
add_modify_and_show_test(test, "unset", nil, "test.new_value", nil)
101-
add_modify_and_show_test(test, "add", nil, "test", "item3")
102-
add_modify_and_show_test(test, "add", nil, "test.list", "item4")
103-
add_modify_and_show_test(test, "remove", nil, "test.list", "item4")
104-
add_modify_and_show_test(test, "remove", nil, "configuration.TEST_CONFIGURATION", "bool2")
105-
106-
add_modify_and_show_test(test, "set", "test.hjson", "test", "123")
107-
add_modify_and_show_test(test, nil, "test.hjson", "test.new_value", "123")
108-
add_modify_and_show_test(test, "unset", "test.hjson", "test.new_value", nil)
109-
add_modify_and_show_test(test, "add", "test.hjson", "test", "item3")
110-
add_modify_and_show_test(test, "add", "test.hjson", "test.list", "item4")
111-
add_modify_and_show_test(test, "remove", "test.hjson", "test.list", "item4")
112-
add_modify_and_show_test(test, "remove", "test.hjson", "configuration.TEST_CONFIGURATION", "bool2")
113-
11448
test["shallow"] = function()
11549
local test_dir = "tests/tmp/ami_test_shallow"
11650
init_ami_test(test_dir, "tests/app/configs/ami_test_app@latest.hjson", { cleanupTestDir = true })

0 commit comments

Comments
 (0)