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

nightconcept / almandine / 14717913225

28 Apr 2025 09:08PM UTC coverage: 97.573% (+1.6%) from 95.996%
14717913225

push

github

nightconcept
test: Increase coverage for update command

106 of 107 new or added lines in 2 files covered. (99.07%)

3 existing lines in 2 files now uncovered.

1005 of 1030 relevant lines covered (97.57%)

2.15 hits per line

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

98.52
/src/spec/modules/remove_spec.lua
1
--[[
2
  Remove Command Specification
3

4
  Busted test suite for the remove command in src/modules/remove.lua.
5
  - Verifies dependencies are removed from project.lua and files are deleted.
6
  - Handles both existing and non-existent dependencies.
7
]]
8
--
9

10
-- luacheck: globals describe it after_each assert
11

12
--- Remove module specification for Busted.
13
-- @module remove_spec
14

15
local remove_module = require("modules.remove")
1✔
16
local manifest_loader = require("utils.manifest")
1✔
17

18
local TEST_DEP_NAME = "testdep"
1✔
19
local TEST_DEP_PATH = "src/lib/testdep.lua"
1✔
20
local MANIFEST_FILE = "project.lua"
1✔
21

22
-- Helper to remove 'nul' file (Windows test artifact)
23
local function cleanup_nul()
24
  os.remove("nul")
2✔
25
end
26

27
describe("remove_module.remove_dependency", function()
2✔
28
  local function write_manifest(deps)
29
    local file = assert(io.open(MANIFEST_FILE, "w"))
10✔
30
    file:write("return {\n")
10✔
31
    file:write('  name = "testproj",\n')
10✔
32
    file:write('  type = "application",\n')
10✔
33
    file:write('  version = "0.1.0",\n')
10✔
34
    file:write('  license = "MIT",\n')
10✔
35
    file:write('  description = "Test manifest",\n')
10✔
36
    file:write("  scripts = {},\n")
10✔
37
    file:write("  dependencies = {\n")
10✔
38
    for k, v in pairs(deps) do
15✔
39
      if type(v) == "table" then
5✔
40
        file:write(string.format("    [%q] = {", k))
1✔
41
        for tkey, tval in pairs(v) do
3✔
42
          file:write(string.format(" %s = %q,", tkey, tval))
2✔
43
        end
44
        file:write(" },\n")
1✔
45
      else
46
        file:write(string.format("    [%q] = %q,\n", k, v))
4✔
47
      end
48
    end
49
    file:write("  }\n")
10✔
50
    file:write("}\n")
10✔
51
    file:close()
10✔
52
  end
53

54
  local function file_exists(path)
55
    local f = io.open(path, "r")
5✔
56
    if f then
5✔
57
      f:close()
2✔
58
      return true
2✔
59
    end
60
    return false
3✔
61
  end
62

63
  local function cleanup()
64
    os.remove(MANIFEST_FILE)
8✔
65
    os.remove(TEST_DEP_PATH)
8✔
66
  end
67

68
  setup(cleanup_nul)
1✔
69
  after_each(cleanup)
1✔
70
  teardown(cleanup_nul)
1✔
71

72
  it("removes existing dependency and deletes file", function()
2✔
73
    write_manifest({ [TEST_DEP_NAME] = "dummy_source" })
1✔
74
    os.execute("mkdir -p src/lib >nul 2>&1 || true")
1✔
75
    local f = assert(io.open(TEST_DEP_PATH, "w"))
1✔
76
    f:write("dummy content")
1✔
77
    f:close()
1✔
78
    assert.is_true(file_exists(TEST_DEP_PATH))
1✔
79
    local function save_manifest(m)
80
      m.name = "testproj"
1✔
81
      m.type = "application"
1✔
82
      m.version = "0.1.0"
1✔
83
      m.license = "MIT"
1✔
84
      m.description = "Test manifest"
1✔
85
      m.scripts = m.scripts or {}
1✔
86
      local deps = m.dependencies or {}
1✔
87
      write_manifest(deps)
1✔
88
      return true, nil
1✔
89
    end
90
    remove_module.remove_dependency(TEST_DEP_NAME, manifest_loader.safe_load_project_manifest, save_manifest)
1✔
91
    local manifest = manifest_loader.safe_load_project_manifest()
1✔
92
    -- TODO: Check for nil value before using result
93
    assert.is_not_nil(manifest)
1✔
94
    assert.is_nil((manifest.dependencies or {})[TEST_DEP_NAME])
1✔
95
    assert.is_false(file_exists(TEST_DEP_PATH))
1✔
96
  end)
97

98
  it("does not error when removing nonexistent dependency", function()
2✔
99
    write_manifest({})
1✔
100
    assert.has_no.errors(function()
2✔
101
      remove_module.remove_dependency("doesnotexist", manifest_loader.safe_load_project_manifest, function(m)
2✔
UNCOV
102
        return write_manifest(m.dependencies or {})
×
103
      end)
104
    end)
105
  end)
106

107
  it("prints error when manifest fails to load", function()
2✔
108
    local printed = {}
1✔
109
    local stub = require("luassert.stub")
1✔
110
    local print_stub = stub(_G, "print", function(msg)
2✔
111
      table.insert(printed, tostring(msg))
1✔
112
    end)
113
    local function load_fail()
114
      return nil, "load error!"
1✔
115
    end
116
    local save_manifest = function()
UNCOV
117
      error("should not be called")
×
118
    end
119
    remove_module.remove_dependency(TEST_DEP_NAME, load_fail, save_manifest)
1✔
120
    print_stub:revert()
1✔
121
    assert.is_not_nil(table.concat(printed, "\n"):match("load error!"))
1✔
122
  end)
123
  it("prints error when save_manifest fails", function()
2✔
124
    write_manifest({ [TEST_DEP_NAME] = "dummy_source" })
1✔
125
    os.execute("mkdir -p src/lib >nul 2>&1 || true")
1✔
126
    local f = assert(io.open(TEST_DEP_PATH, "w"))
1✔
127
    f:write("dummy content")
1✔
128
    f:close()
1✔
129
    local printed = {}
1✔
130
    local stub = require("luassert.stub")
1✔
131
    local print_stub = stub(_G, "print", function(msg)
2✔
132
      table.insert(printed, tostring(msg))
1✔
133
    end)
134
    local function save_fail()
135
      return false, "save failed!"
1✔
136
    end
137
    remove_module.remove_dependency(TEST_DEP_NAME, manifest_loader.safe_load_project_manifest, save_fail)
1✔
138
    print_stub:revert()
1✔
139
    assert.is_not_nil(table.concat(printed, "\n"):match("save failed!"))
1✔
140
    assert.is_true(file_exists(TEST_DEP_PATH)) -- file should not be deleted
1✔
141
  end)
142
  it("removes dependency with custom .path", function()
2✔
143
    local dep_path = "src/lib/customdep.lua"
1✔
144
    write_manifest({ customdep = { url = "url", path = dep_path } })
1✔
145
    os.execute("mkdir -p src/lib >nul 2>&1 || true")
1✔
146
    local f = assert(io.open(dep_path, "w"))
1✔
147
    f:write("dummy content")
1✔
148
    f:close()
1✔
149
    local function save_manifest(m)
150
      write_manifest(m.dependencies or {})
1✔
151
      return true, nil
1✔
152
    end
153
    remove_module.remove_dependency("customdep", manifest_loader.safe_load_project_manifest, save_manifest)
1✔
154
    assert.is_false(file_exists(dep_path))
1✔
155
    local manifest = manifest_loader.safe_load_project_manifest()
1✔
156
    assert.is_nil((manifest.dependencies or {}).customdep)
1✔
157
  end)
158
  it("removes dependency using _G.dependency_add_test_paths", function()
2✔
159
    _G.dependency_add_test_paths = { foo = "src/lib/foo_testpath.lua" }
1✔
160
    write_manifest({ foo = "url" })
1✔
161
    os.execute("mkdir -p src/lib >nul 2>&1 || true")
1✔
162
    local f = assert(io.open("src/lib/foo_testpath.lua", "w"))
1✔
163
    f:write("dummy content")
1✔
164
    f:close()
1✔
165
    local function save_manifest(m)
166
      write_manifest(m.dependencies or {})
1✔
167
      return true, nil
1✔
168
    end
169
    remove_module.remove_dependency("foo", manifest_loader.safe_load_project_manifest, save_manifest)
1✔
170
    assert.is_false(file_exists("src/lib/foo_testpath.lua"))
1✔
171
    local manifest = manifest_loader.safe_load_project_manifest()
1✔
172
    assert.is_nil((manifest.dependencies or {}).foo)
1✔
173
    _G.dependency_add_test_paths = nil
1✔
174
  end)
175
  it("warns if file cannot be deleted", function()
2✔
176
    write_manifest({ bar = "url" })
1✔
177
    local printed = {}
1✔
178
    local stub = require("luassert.stub")
1✔
179
    local print_stub = stub(_G, "print", function(msg)
2✔
180
      table.insert(printed, tostring(msg))
2✔
181
    end)
182
    local function save_manifest(m)
183
      write_manifest(m.dependencies or {})
1✔
184
      return true, nil
1✔
185
    end
186
    -- do not create the file
187
    remove_module.remove_dependency("bar", manifest_loader.safe_load_project_manifest, save_manifest)
1✔
188
    print_stub:revert()
1✔
189
    local output = table.concat(printed, "\n")
1✔
190
    assert.is_not_nil(output:match("Warning: Could not delete file"))
1✔
191
  end)
192
  it("prints help_info output", function()
2✔
193
    local printed = {}
1✔
194
    local stub = require("luassert.stub")
1✔
195
    local print_stub = stub(_G, "print", function(msg)
2✔
196
      table.insert(printed, tostring(msg))
1✔
197
    end)
198
    assert.has_no.errors(function()
2✔
199
      remove_module.help_info()
1✔
200
    end)
201
    print_stub:revert()
1✔
202
    assert.is_not_nil(table.concat(printed, "\n"):match("Usage: almd remove"))
1✔
203
  end)
204
end)
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