Commit 178fda03 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

pkg: Formatting of package status

Functions to format the status block for a single package.
parent 27168a64
......@@ -95,6 +95,69 @@ function block_parse(block)
return result
end
--[[
Format single block of data.
The block shall be passed as an array, each item an object with header and value strings.
The object may be an empty table. In that case, no output is generated for the given
object.
]]
function block_dump_ordered(block)
return table.concat(utils.map(block, function (i, line)
if line.header then
local space = ' '
if line.value:match("^%s") then
space = ''
end
return i, line.header .. ":" .. space .. line.value .. "\n"
else
return i, ''
end
end))
end
--[[
Dump status of a single package.
]]
function pkg_status_dump(status)
local function line(name, conversion)
if status[name] then
return {header = name, value = conversion(status[name])}
else
return {}
end
end
local function raw(name)
return line(name, function (v) return v end)
end
return block_dump_ordered({
raw "Package",
raw "Version",
line("Depends", function (deps)
-- Join the dependencies together, separated by commas
return table.concat(deps, ', ')
end),
raw "Conflicts",
line("Status", function (status)
-- Join status flags together, separated by spaces
return table.concat(utils.set2arr(status), ' ')
end),
raw "Architecture",
line("Conffiles", function (confs)
local i = 0
--[[
For each dep, place it into an array instead of map and format the line.
Then connect these lines together with newlines.
]]
return "\n" .. table.concat(utils.map(confs, function (filename, hash)
i = i + 1
return i, " " .. filename .. " " .. hash
end), "\n")
end),
raw "Installed-Time",
raw "Auto-Installed"
})
end
--[[
Split text into blocks separated by at least one empty line.
Returns an iterator.
......
......@@ -423,6 +423,97 @@ function test_collisions()
}, rem)
end
function test_block_dump_ordered()
-- Empty block should produce empty output
assert_equal('', B.block_dump_ordered({}))
-- An ordinary block, nothing special
assert_equal(
[[
Header: value
Header2: value2
]], B.block_dump_ordered({
{ header = "Header", value = "value" },
{ header = "Header2", value = "value2" }
}))
-- Repeated headers. Not that we would actually need that in practice.
assert_equal(
[[
Header: value
Header: value
]], B.block_dump_ordered({
{ header = "Header", value = "value" },
{ header = "Header", value = "value" }
}))
-- An empty object generates nothing
assert_equal(
[[
Header: value
Header: value
]], B.block_dump_ordered({
{ header = "Header", value = "value" },
{},
{ header = "Header", value = "value" }
}))
-- A multi-line value
assert_equal(
[[
Header:
value
another line
]], B.block_dump_ordered({
{ header = "Header", value =
-- Since lua eats the first newline directly after [[, we need to provide two.
[[
value
another line]]}}))
end
function test_pkg_status_dump()
-- Simple package with just one-line headers
assert_equal(
[[
Package: pkg-name
Version: 1
Installed-Time: 1
]], B.pkg_status_dump({
Package = "pkg-name",
Version = "1",
["Installed-Time"] = "1"
}))
-- Package with some extra (unused) headers
assert_equal(
[[
Package: pkg-name
Version: 1
Installed-Time: 1
]], B.pkg_status_dump({
Package = "pkg-name",
Version = "1",
["Installed-Time"] = "1",
Extra = "xxxx"
}))
-- Package with more complex headers
assert_equal(
[[
Package: pkg-name
Version: 1
Depends: dep1, dep2
Status: flag
Conffiles:
file 1234567890123456
Installed-Time: 1
]], B.pkg_status_dump({
Package = "pkg-name",
Version = "1",
["Installed-Time"] = "1",
Extra = "xxxx",
Depends = { "dep1", "dep2" },
Status = { flag = true },
Conffiles = { ["file"] = "1234567890123456" }
}))
end
function setup()
local sdir = os.getenv("S") or "."
-- Use a shortened version of a real status file for tests
......
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