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

nightconcept / almandine / 14837152382

05 May 2025 01:06PM UTC coverage: 67.965%. Remained the same
14837152382

push

github

nightconcept
chore: cleanup add and spec comments

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

314 of 462 relevant lines covered (67.97%)

4.39 hits per line

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

99.41
/src/spec/e2e/modules/add_spec.lua
1
--[[
2
  add_spec
3
  @module add_spec
4

5
  End-to-End Tests for the `add` command
6
]]
7

8
describe("E2E: `almd add` command", function()
2✔
9
  -- Require the scaffold helper
10
  -- Note the relative path from this spec file to the helper
11
  -- Use dot notation assuming busted runs relative to src/
12
  local scaffold = require("spec.e2e.helpers.scaffold")
1✔
13
  local assert = require("luassert")
1✔
14
  local url_utils = require("utils.url")
1✔
15

16
  -- Variables to hold sandbox info between tests
17
  local sandbox_path
18
  local cleanup_func
19
  local initial_project_data -- Optional: Can be customized per test if needed. TODO: Move to base scaffold functions
20

21
  -- TODO: Remove entire luacheck ignore
22
  --luacheck: ignore
23
  local LOCKSOURCE_GITHUB_RAW = "https://raw.githubusercontent.com/Oval-Tutu/shove/81f7f879a812e4479493a88e646831d0f0409560/shove.lua"
1✔
24

25
  before_each(function()
2✔
26
    local path, cleaner, err = scaffold.create_sandbox_project()
6✔
27
    assert.is_not_nil(path, "Failed to create sandbox: " .. tostring(err))
6✔
28
    sandbox_path = path
6✔
29
    cleanup_func = cleaner
6✔
30

31
    -- Initialize a basic project.lua file
32
    initial_project_data = { name = "e2e-add-test", version = "0.1.0", dependencies = {} }
6✔
33
    local success, init_err = scaffold.init_project_file(sandbox_path, initial_project_data)
6✔
34
    assert.is_true(success, "Failed to initialize project.lua: " .. tostring(init_err))
6✔
35
  end)
36

37
  -- Teardown: Clean up the sandbox after each test
38
  after_each(function()
2✔
39
    if cleanup_func then
6✔
40
      cleanup_func()
6✔
41
      sandbox_path = nil
6✔
42
      cleanup_func = nil
6✔
43
    else
UNCOV
44
      print("Warning: No cleanup function available for sandbox: " .. tostring(sandbox_path))
×
45
    end
46
  end)
47

48
  -- Ensure the spec file is valid and setup is done
49
  it("should run setup and teardown without errors", function()
2✔
50
    -- This test primarily verifies that before_each and after_each work
51
    assert.is_not_nil(sandbox_path)
1✔
52
    assert.is_function(cleanup_func)
1✔
53
    local exists = scaffold.file_exists(sandbox_path .. "/project.lua")
1✔
54
    assert.is_true(exists, "project.lua should exist after setup")
1✔
55
  end)
56

57
  -- Helper function for verifying successful add cases
58
  -- TODO: Add params definitions
59
  local function _verify_add(params)
60
    -- Construct command arguments
61
    local cmd_args = { "add", params.url }
5✔
62
    if params.extra_args then
5✔
63
      for _, arg in ipairs(params.extra_args) do
11✔
64
        table.insert(cmd_args, arg)
6✔
65
      end
66
    end
67

68
    -- Default expect_success to true if not provided
69
    local expect_success = params.expect_success == nil or params.expect_success == true
5✔
70

71
    -- Capture initial state if expecting failure, to compare later
72
    local initial_dependencies
73
    if not expect_success then
5✔
74
      local initial_project_data_before, proj_err_before = scaffold.read_project_lua(sandbox_path)
1✔
75
      assert.is_not_nil(
2✔
76
        initial_project_data_before,
1✔
77
        string.format("Test '%s': Failed to read initial project.lua: %s", params.description, tostring(proj_err_before))
1✔
78
      )
79
      initial_dependencies = initial_project_data_before.dependencies or {}
1✔
80
    end
81

82
    -- TODO: Also run for coverage rather than success. Have run_almd also call the main path.
83
    -- Run the add command
84
    local success, output = scaffold.run_almd(sandbox_path, cmd_args)
5✔
85

86
    if expect_success then
5✔
87
      -- Assertions for successful add
88
      assert.is_true(
8✔
89
        success,
4✔
90
        string.format("Test '%s': almd add command failed. Output:\n%s", params.description, output)
4✔
91
      )
92

93
      -- Verify file downloaded
94
      local expected_file_path_absolute = sandbox_path .. "/" .. params.expected_file_path_relative
4✔
95
      local file_exists = scaffold.file_exists(expected_file_path_absolute)
4✔
96
      assert.is_true(
8✔
97
        file_exists,
4✔
98
        string.format(
8✔
99
          "Test '%s': Expected file %s was not found.",
4✔
100
          params.description,
4✔
101
          params.expected_file_path_relative
102
        )
4✔
103
      )
104

105
      -- Verify project.lua content
106
      local project_data, proj_err = scaffold.read_project_lua(sandbox_path)
4✔
107
      assert.is_not_nil(
8✔
108
        project_data,
4✔
109
        string.format("Test '%s': Failed to read project.lua: %s", params.description, tostring(proj_err))
4✔
110
      )
111
      assert.is_not_nil(
8✔
112
        project_data.dependencies,
4✔
113
        string.format("Test '%s': Dependencies table missing in project.lua", params.description)
4✔
114
      )
115
      local actual_proj_dep_entry = project_data.dependencies and project_data.dependencies[params.expected_dep_name]
4✔
116
      assert.is_table(
8✔
117
        actual_proj_dep_entry,
4✔
118
        string.format("Test '%s': Project dependency entry should be a table.", params.description)
4✔
119
      )
120

121
      local expected_source_identifier, id_err = url_utils.create_github_source_identifier(params.url)
4✔
122
      assert.is_not_nil(
8✔
123
        expected_source_identifier,
4✔
124
        string.format("Test '%s': Failed to create expected source identifier: %s", params.description, tostring(id_err))
4✔
125
      )
126
      assert.are.equal(
8✔
127
        expected_source_identifier,
4✔
128
        actual_proj_dep_entry.source,
4✔
129
        string.format("Test '%s': Dependency source identifier mismatch in project.lua", params.description)
4✔
130
      )
131
      assert.are.equal(
8✔
132
        params.expected_file_path_relative,
4✔
133
        actual_proj_dep_entry.path,
4✔
134
        string.format("Test '%s': Dependency path mismatch in project.lua", params.description)
4✔
135
      )
136

137
      -- Verify almd-lock.lua content
138
      local lock_data, lock_err = scaffold.read_lock_lua(sandbox_path)
4✔
139
      assert.is_not_nil(
8✔
140
        lock_data,
4✔
141
        string.format("Test '%s': Failed to read almd-lock.lua: %s", params.description, tostring(lock_err))
4✔
142
      )
143
      assert.is_not_nil(
8✔
144
        lock_data.package,
4✔
145
        string.format("Test '%s': Package table missing in almd-lock.lua", params.description)
4✔
146
      )
147
      local dep_lock_info = lock_data.package and lock_data.package[params.expected_dep_name]
4✔
148
      assert.is_not_nil(
8✔
149
        dep_lock_info,
4✔
150
        string.format(
8✔
151
          "Test '%s': Dependency entry missing in almd-lock.lua for %s",
4✔
152
          params.description,
4✔
153
          params.expected_dep_name
154
        )
4✔
155
      )
156

157
      assert.are.equal(
8✔
158
        params.expected_file_path_relative,
4✔
159
        dep_lock_info.path,
4✔
160
        string.format("Test '%s': Lockfile path mismatch", params.description)
4✔
161
      )
162
      assert.are.equal(
8✔
163
        params.expected_lock_source,
4✔
164
        dep_lock_info.source,
4✔
165
        string.format("Test '%s': Lockfile source mismatch", params.description)
4✔
166
      )
167

168
      local expected_hash = params.expected_hash_type .. ":" .. params.expected_hash_value
4✔
169
      assert.are.equal(
8✔
170
        expected_hash,
4✔
171
        dep_lock_info.hash,
4✔
172
        string.format("Test '%s': Lockfile hash mismatch", params.description)
4✔
173
      )
4✔
174
    else
175
      -- Assertions for failed add
176
      assert.is_false(
2✔
177
        success,
1✔
178
        string.format("Test '%s': almd add command should fail (exit code non-zero). Output:\n%s", params.description, output)
1✔
179
      )
180

181
      -- Verify file was NOT downloaded (check default location based on expected name)
182
      -- Note: assumes failure means no custom path/name was processed to create file.
183
      local default_file_path = sandbox_path .. "/src/lib/" .. params.expected_dep_name .. ".lua"
1✔
184
      local file_exists = scaffold.file_exists(default_file_path)
1✔
185
      assert.is_false(
2✔
186
        file_exists,
1✔
187
        string.format("Test '%s': Dependency file should NOT have been downloaded to %s", params.description, default_file_path)
1✔
188
      )
189

190
      -- Verify project.lua content remains unchanged
191
      local project_data_after, proj_err_after = scaffold.read_project_lua(sandbox_path)
1✔
192
      assert.is_not_nil(
2✔
193
        project_data_after,
1✔
194
        string.format("Test '%s': Failed to read project.lua after command: %s", params.description, tostring(proj_err_after))
1✔
195
      )
196
      assert.are.same(
2✔
197
        initial_dependencies,
1✔
198
        project_data_after.dependencies or {},
1✔
199
        string.format("Test '%s': project.lua dependencies table should remain unchanged.", params.description)
1✔
200
      )
201

202
      -- Verify almd-lock.lua was not created or remains unchanged
203
      local lock_file_path = sandbox_path .. "/almd-lock.lua"
1✔
204
      local lock_file_exists = scaffold.file_exists(lock_file_path)
1✔
205
      assert.is_false(
2✔
206
        lock_file_exists,
1✔
207
        string.format("Test '%s': almd-lock.lua should not have been created.", params.description)
1✔
208
      )
209
    end
210
  end
211

212
  local test_cases = {
1✔
213
    -- Equivalent to:
214
    -- almd add https://github.com/Oval-Tutu/shove/blob/81f7f879a812e4479493a88e646831d0f0409560/shove.lua
215
    {
216
      description = "should add a dependency from a specific commit URL to the default path",
1✔
217
      url = "https://github.com/Oval-Tutu/shove/blob/81f7f879a812e4479493a88e646831d0f0409560/shove.lua",
1✔
218
      extra_args = {},
1✔
219
      expected_dep_name = "shove",
1✔
220
      expected_file_path_relative = "src/lib/shove.lua",
1✔
221
      expected_hash_type = "commit",
1✔
222
      expected_hash_value = "81f7f879a812e4479493a88e646831d0f0409560",
1✔
223
      expected_lock_source = LOCKSOURCE_GITHUB_RAW,
1✔
224
      expect_success = true, -- Explicitly true (though default)
1✔
225
    },
1✔
226
    -- Equivalent to:
227
    -- almd add https://github.com/Oval-Tutu/shove/blob/81f7f879a812e4479493a88e646831d0f0409560/shove.lua
228
    -- -d src/engine/lib/
229
    {
230
      description = "should add a dependency from a specific commit URL to a custom path",
1✔
231
      url = "https://github.com/Oval-Tutu/shove/blob/81f7f879a812e4479493a88e646831d0f0409560/shove.lua",
1✔
232
      extra_args = { "-d", "src/engine/lib/" },
1✔
233
      expected_dep_name = "shove",
1✔
234
      expected_file_path_relative = "src/engine/lib/shove.lua",
1✔
235
      expected_hash_type = "commit",
1✔
236
      expected_hash_value = "81f7f879a812e4479493a88e646831d0f0409560",
1✔
237
      expected_lock_source = LOCKSOURCE_GITHUB_RAW,
1✔
238
      expect_success = true,
1✔
239
    },
1✔
240
    -- Equivalent to:
241
    -- almd add https://github.com/Oval-Tutu/shove/blob/main/shove.lua
242
    {
243
      description = "should add a dependency from a specific branch URL to the default path", -- Changed path
1✔
244
      url = "https://github.com/Oval-Tutu/shove/blob/main/shove.lua",
1✔
245
      extra_args = {}, -- Removed custom path args
1✔
246
      expected_dep_name = "shove",
1✔
247
      expected_file_path_relative = "src/lib/shove.lua", -- Default path
1✔
248
      expected_hash_type = "sha256",
1✔
249
      expected_hash_value = "7126e9d1ee584dc1a19612d3347cbf6e778cbaa859f7416ea51d0b360bd2223c",
1✔
250
      expected_lock_source = "https://raw.githubusercontent.com/Oval-Tutu/shove/main/shove.lua",
1✔
251
      expect_success = true,
1✔
252
    },
1✔
253
    -- Equivalent to:
254
    -- almd add https://github.com/Oval-Tutu/shove/blob/81f7f879a812e4479493a88e646831d0f0409560/shove.lua
255
    -- -d src/engine/lib/ -n clove
256
    {
257
      description = "should add a dependency from a specific commit URL to a custom path with a custom file name",
1✔
258
      url = "https://github.com/Oval-Tutu/shove/blob/81f7f879a812e4479493a88e646831d0f0409560/shove.lua",
1✔
259
      extra_args = { "-d", "src/engine/lib/", "-n", "clove" },
1✔
260
      expected_dep_name = "clove",
1✔
261
      expected_file_path_relative = "src/engine/lib/clove.lua",
1✔
262
      expected_hash_type = "commit",
1✔
263
      expected_hash_value = "81f7f879a812e4479493a88e646831d0f0409560",
1✔
264
      expected_lock_source = LOCKSOURCE_GITHUB_RAW,
1✔
265
      expect_success = true,
1✔
266
    },
1✔
267
    -- Equivalent to:
268
    -- almd add https://github.com/Oval-Tutu/shove/blob/main/clove.lua (non-existent file)
269
    {
270
      description = "should fail to add a dependency from a URL pointing to a non-existent file",
1✔
271
      url = "https://github.com/Oval-Tutu/shove/blob/main/clove.lua", -- Assuming clove.lua does not exist
1✔
272
      extra_args = {},
1✔
273
      expected_dep_name = "clove", -- Based on the non-existent file name
1✔
274
      expect_success = false,
1✔
275
      -- Other expected_ fields are omitted as they are not checked on failure
276
    },
277
  }
1✔
278

279
  -- Iterate over cases and run tests
280
  for _, test_case in ipairs(test_cases) do
6✔
281
    it(test_case.description, function()
10✔
282
      _verify_add(test_case)
5✔
283
    end)
284
  end
285
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