• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nightconcept / almandine / 14757041512

30 Apr 2025 02:28PM UTC coverage: 79.95% (-0.9%) from 80.835%
14757041512

push

github

nightconcept
fix: add and install installing wrong file

61 of 81 new or added lines in 4 files covered. (75.31%)

11 existing lines in 2 files now uncovered.

1615 of 2020 relevant lines covered (79.95%)

2.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.29
/src/utils/lockfile.lua
1
--[[
2
  Lockfile Utility Module
3

4
  Centralizes all lockfile operations for Almandine. Provides functions to generate, serialize, write,
5
  and modify the lockfile (almd-lock.lua) in a consistent, reusable way.
6
]]
7
--
8

9
--- Lockfile schema version (increment if schema changes)
10
local API_VERSION = "1"
2✔
11

12
local lockfile = {}
2✔
13
local io = io
2✔
14
local tostring = tostring
2✔
15

16
---
17
-- Generates a lockfile table from resolved dependencies.
18
-- @param resolved_deps table Table of resolved dependencies. Each key is a package name, value is a table with fields:
19
--                          - version (string, optional)
20
--                          - hash (string, required)
21
--                          - source (string, optional)
22
-- @return table Lockfile table matching the schema
23
function lockfile.generate_lockfile_table(resolved_deps)
2✔
24
  assert(type(resolved_deps) == "table", "resolved_deps must be a table")
4✔
25
  local pkgs = {}
3✔
26
  for name, dep in pairs(resolved_deps) do
5✔
27
    assert(type(dep) == "table", "Dependency entry must be a table")
4✔
28
    assert(dep.hash, "Dependency '" .. name .. "' must have a hash")
3✔
29
    local entry = { hash = dep.hash }
2✔
30
    if dep.version then
2✔
31
      entry.version = dep.version
1✔
32
    end
33
    if dep.source then
2✔
34
      entry.source = dep.source
1✔
35
    end
36
    pkgs[name] = entry
2✔
37
  end
38
  return {
1✔
39
    api_version = API_VERSION,
1✔
40
    package = pkgs,
1✔
41
  }
1✔
42
end
43

44
---
45
-- Serializes a lockfile table to a string (Lua syntax).
46
-- @param lockfile_table table Lockfile table
47
-- @return string Lua code as string
48
function lockfile.serialize_lockfile(lockfile_table)
2✔
49
  assert(type(lockfile_table) == "table", "lockfile_table must be a table")
4✔
50
  local function serialize(tbl, indent)
51
    indent = indent or 0
9✔
52
    local pad = string.rep("  ", indent)
9✔
53
    local lines = { "{" }
9✔
54
    for k, v in pairs(tbl) do
21✔
55
      local key = (type(k) == "string" and string.format("%s = ", k)) or ("[" .. tostring(k) .. "] = ")
12✔
56
      if type(v) == "table" then
12✔
57
        local serialized = serialize(v, indent + 1)
6✔
58
        table.insert(lines, pad .. "  " .. key .. serialized .. ",")
6✔
59
      elseif type(v) == "string" then
6✔
60
        table.insert(lines, pad .. "  " .. key .. string.format('"%s"', v) .. ",")
6✔
61
      else
62
        table.insert(lines, pad .. "  " .. key .. tostring(v) .. ",")
×
63
      end
64
    end
65
    table.insert(lines, pad .. "}")
9✔
66
    return table.concat(lines, "\n")
9✔
67
  end
68
  local result = "return " .. serialize(lockfile_table, 0) .. "\n"
3✔
69
  return result
3✔
70
end
71

72
---
73
-- Writes the lockfile to disk as `almd-lock.lua`.
74
-- @param lockfile_table table Lockfile table
75
-- @param path string (optional) Path to write to (default: "almd-lock.lua" in project root)
76
-- @return boolean, string True and path if successful, false and error message otherwise
77
function lockfile.write_lockfile(lockfile_table, path)
2✔
78
  path = path or "almd-lock.lua"
2✔
79
  local content = lockfile.serialize_lockfile(lockfile_table)
2✔
80
  local file, err = io.open(path, "w")
2✔
81
  if not file then
2✔
82
    return false, err
1✔
83
  end
84
  file:write(content)
1✔
85
  file:close()
1✔
86
  return true, path
1✔
87
end
88

89
---
90
-- Removes a dependency from the lockfile on disk.
91
-- @param dep_name string Name of dependency to remove
92
-- @param path string (optional) Path to lockfile (default: "almd-lock.lua")
93
-- @return boolean, string True if successful, false and error message otherwise
94
function lockfile.remove_dep_from_lockfile(dep_name, path)
2✔
95
  path = path or "almd-lock.lua"
4✔
96
  local chunk = loadfile(path)
4✔
97
  if not chunk then
4✔
98
    return false, "Lockfile not found"
4✔
99
  end
100
  local ok, lock = pcall(chunk)
×
101
  if not ok or type(lock) ~= "table" or type(lock.package) ~= "table" then
×
102
    return false, "Malformed lockfile"
×
103
  end
104
  if lock.package[dep_name] then
×
105
    lock.package[dep_name] = nil
×
106
    return lockfile.write_lockfile(lock, path)
×
107
  end
108
  return true, path -- No-op if dep not present
×
109
end
110

111
---
112
-- Updates the lockfile from a manifest loader function.
113
-- @param load_manifest function Function to load the manifest
114
-- @return boolean, string True if successful, false and error message otherwise
115
function lockfile.update_lockfile_from_manifest(load_manifest)
2✔
UNCOV
116
  local manifest, err = load_manifest()
×
UNCOV
117
  if not manifest then
×
UNCOV
118
    return false, err or "Could not load manifest"
×
119
  end
UNCOV
120
  local resolved_deps = {}
×
UNCOV
121
  for name, dep in pairs(manifest.dependencies or {}) do
×
UNCOV
122
    local dep_entry = type(dep) == "table" and dep or { url = dep }
×
123
    -- Compute hash (placeholder: use URL as hash; replace with real hash logic if available)
UNCOV
124
    local hash = dep_entry.url or tostring(dep)
×
UNCOV
125
    resolved_deps[name] = { hash = hash, source = dep_entry.url or tostring(dep) }
×
126
  end
UNCOV
127
  local lockfile_table = lockfile.generate_lockfile_table(resolved_deps)
×
UNCOV
128
  return lockfile.write_lockfile(lockfile_table)
×
129
end
130

131
return lockfile
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc