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

nightconcept / almandine / 14846535458

05 May 2025 09:22PM UTC coverage: 32.421% (-35.5%) from 67.965%
14846535458

push

github

web-flow
fix: Change init module to be e2e testable (#17)

91 of 420 new or added lines in 11 files covered. (21.67%)

462 of 1425 relevant lines covered (32.42%)

1.34 hits per line

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

11.32
/src/modules/run.lua
1
--[[
2
  Run Command Module
3

4
  Provides logic for executing scripts defined in the `scripts` table of project.lua.
5
  Used by the main CLI entrypoint for the `run` command and for direct script invocation if unambiguous.
6
]]
7
--
8

9
---@class RunDeps
10
---@field manifest_loader fun()|table Function or module to load the manifest.
11
---@field executor fun(cmd: string): boolean, string?, number? Optional function for command execution.
12
---@field printer table Printer utility with stdout/stderr methods.
13

14
---TODO: remove this once we have a pass over this file
15
-- luacheck: ignore
16
---
17
-- Executes a script by name from the project manifest using provided dependencies.
18
-- @param script_name string The key of the script in the scripts table.
19
-- @param deps RunDeps Dependencies: { manifest_loader, executor?, printer }.
20
-- @return boolean success True if successful, false otherwise.
21
-- @return string|nil output_message Message for stdout.
22
-- @return string|nil error_message Message for stderr.
23
local function run_script(script_name, deps)
24
  local manifest_loader = deps.manifest_loader
×
25
  local executor = deps.executor or os.execute
×
NEW
26
  local printer = deps.printer
×
27

NEW
28
  local output_messages = {}
×
NEW
29
  local error_messages = {}
×
30

31
  -- Handle manifest_loader being either a function or a module
32
  local load_manifest = type(manifest_loader) == "function" and manifest_loader
×
33
    or manifest_loader.safe_load_project_manifest
×
34

35
  local manifest = load_manifest("project.lua")
×
36
  if not manifest then
×
NEW
37
    table.insert(error_messages, "Failed to load project manifest.")
×
NEW
38
    return false, nil, table.concat(error_messages, "\n")
×
39
  end
40
  local scripts = manifest.scripts or {}
×
41
  if not scripts or not scripts[script_name] then
×
NEW
42
    local err_msg = string.format("Script '%s' not found in project.lua.", script_name)
×
NEW
43
    table.insert(error_messages, err_msg)
×
NEW
44
    return false, nil, table.concat(error_messages, "\n")
×
45
  end
46
  local script = scripts[script_name]
×
47
  local cmd = script.cmd or script
×
48
  local args = script.args or {}
×
49
  local command = cmd
×
50
  if #args > 0 then
×
51
    command = cmd .. " " .. table.concat(args, " ")
×
52
  end
NEW
53
  table.insert(output_messages, string.format("Running script '%s': %s", script_name, command))
×
54

55
  -- Capture command output (stdout/stderr) if possible
56
  -- This basic os.execute doesn't capture, but a more robust executor could.
57
  -- For now, we just report success/failure.
58
  local ok, exit_reason, code = executor(command)
×
59

60
  if ok then
×
NEW
61
    table.insert(output_messages, string.format("Script '%s' completed successfully.", script_name))
×
NEW
62
    return true, table.concat(output_messages, "\n"), nil
×
63
  else
NEW
64
    local fail_msg = string.format(
×
65
      "Script '%s' failed (reason: %s, code: %s)",
66
      script_name,
NEW
67
      tostring(exit_reason),
×
NEW
68
      tostring(code)
×
69
    )
NEW
70
    table.insert(error_messages, fail_msg)
×
71
    return false,
×
NEW
72
      table.concat(output_messages, "\n"),
×
NEW
73
      table.concat(error_messages, "\n")
×
74
  end
75
end
76

77
---
78
-- Determines if a string is a reserved command name.
79
-- @param name string
80
-- @return boolean
81
local function is_reserved_command(name)
82
  local reserved = {
×
83
    ["init"] = true,
84
    ["add"] = true,
85
    ["i"] = true,
86
    ["install"] = true,
87
    ["in"] = true,
88
    ["ins"] = true,
89
    ["remove"] = true,
90
    ["rm"] = true,
91
    ["uninstall"] = true,
92
    ["un"] = true,
93
    ["update"] = true,
94
    ["up"] = true,
95
    ["upgrade"] = true,
96
    ["run"] = true,
97
    ["list"] = true,
98
  }
99
  return reserved[name] == true
×
100
end
101

102
---
103
-- Finds a matching script if the name is unambiguous using provided dependencies.
104
-- @param name string The candidate script name.
105
-- @param deps table Dependencies: { manifest_loader }.
106
--               `manifest_loader` is a function or module with safe_load_project_manifest.
107
-- @return string|nil The script name if unambiguous, or nil.
108
local function get_unambiguous_script(name, deps)
109
  local manifest_loader = deps.manifest_loader
×
110
  -- Handle manifest_loader being either a function or a module
111
  local load_manifest = type(manifest_loader) == "function" and manifest_loader
×
112
    or manifest_loader.safe_load_project_manifest
×
113

114
  local manifest = load_manifest("project.lua")
×
115
  if not manifest or not manifest.scripts then
×
116
    return nil
×
117
  end
118
  if manifest.scripts[name] then
×
119
    return name
×
120
  end
121
  return nil
×
122
end
123

124
---
125
-- Prints usage/help information for the `run` command.
126
-- Usage: almd run <script_name>
127
-- Executes a script defined in project.lua.
128
local function help_info()
NEW
129
  return [[
×
130
Usage: almd run <script_name>
131

132
Executes a script defined in the `scripts` table of project.lua.
133
Example:
134
  almd run test
NEW
135
]]
×
136
end
137

138
return {
2✔
139
  run_script = run_script,
2✔
140
  is_reserved_command = is_reserved_command,
2✔
141
  get_unambiguous_script = get_unambiguous_script,
2✔
142
  help_info = help_info,
2✔
143
}
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