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

nightconcept / almandine / 14789063338

02 May 2025 05:06AM UTC coverage: 67.965% (-32.0%) from 100.0%
14789063338

push

github

web-flow
fix: Add with e2e tests (#16)

311 of 448 new or added lines in 3 files covered. (69.42%)

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
-- End-to-End Tests for the `add` command
2

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

11
  -- Variables to hold sandbox info between tests
12
  local sandbox_path
13
  local cleanup_func
14
  local initial_project_data -- Optional: Can be customized per test if needed
15

16
  --luacheck: ignore
17
  local LOCKSOURCE_GITHUB_RAW = "https://raw.githubusercontent.com/Oval-Tutu/shove/81f7f879a812e4479493a88e646831d0f0409560/shove.lua"
1✔
18

19
  before_each(function()
2✔
20
    local path, cleaner, err = scaffold.create_sandbox_project()
6✔
21
    assert.is_not_nil(path, "Failed to create sandbox: " .. tostring(err))
6✔
22
    sandbox_path = path
6✔
23
    cleanup_func = cleaner
6✔
24

25
    -- Initialize a basic project.lua file
26
    initial_project_data = { name = "e2e-add-test", version = "0.1.0", dependencies = {} }
6✔
27
    local success, init_err = scaffold.init_project_file(sandbox_path, initial_project_data)
6✔
28
    assert.is_true(success, "Failed to initialize project.lua: " .. tostring(init_err))
6✔
29
  end)
30

31
  -- Teardown: Clean up the sandbox after each test
32
  after_each(function()
2✔
33
    if cleanup_func then
6✔
34
      cleanup_func()
6✔
35
      sandbox_path = nil
6✔
36
      cleanup_func = nil
6✔
37
    else
NEW
38
      print("Warning: No cleanup function available for sandbox: " .. tostring(sandbox_path))
×
39
    end
40
  end)
41

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

51
  -- Helper function for verifying successful add cases
52
  local function _verify_add(params)
53
    -- Construct command arguments
54
    local cmd_args = { "add", params.url }
5✔
55
    if params.extra_args then
5✔
56
      for _, arg in ipairs(params.extra_args) do
11✔
57
        table.insert(cmd_args, arg)
6✔
58
      end
59
    end
60

61
    -- Default expect_success to true if not provided
62
    local expect_success = params.expect_success == nil or params.expect_success == true
5✔
63

64
    -- Capture initial state if expecting failure, to compare later
65
    local initial_dependencies
66
    if not expect_success then
5✔
67
      local initial_project_data_before, proj_err_before = scaffold.read_project_lua(sandbox_path)
1✔
68
      assert.is_not_nil(
2✔
69
        initial_project_data_before,
1✔
70
        string.format("Test '%s': Failed to read initial project.lua: %s", params.description, tostring(proj_err_before))
1✔
71
      )
72
      initial_dependencies = initial_project_data_before.dependencies or {}
1✔
73
    end
74

75
    -- Run the add command
76
    local success, output = scaffold.run_almd(sandbox_path, cmd_args)
5✔
77

78
    if expect_success then
5✔
79
      -- Assertions for successful add
80
      assert.is_true(
8✔
81
        success,
4✔
82
        string.format("Test '%s': almd add command failed. Output:\n%s", params.description, output)
4✔
83
      )
84

85
      -- Verify file downloaded
86
      local expected_file_path_absolute = sandbox_path .. "/" .. params.expected_file_path_relative
4✔
87
      local file_exists = scaffold.file_exists(expected_file_path_absolute)
4✔
88
      assert.is_true(
8✔
89
        file_exists,
4✔
90
        string.format(
8✔
91
          "Test '%s': Expected file %s was not found.",
4✔
92
          params.description,
4✔
93
          params.expected_file_path_relative
94
        )
4✔
95
      )
96

97
      -- Verify project.lua content
98
      local project_data, proj_err = scaffold.read_project_lua(sandbox_path)
4✔
99
      assert.is_not_nil(
8✔
100
        project_data,
4✔
101
        string.format("Test '%s': Failed to read project.lua: %s", params.description, tostring(proj_err))
4✔
102
      )
103
      assert.is_not_nil(
8✔
104
        project_data.dependencies,
4✔
105
        string.format("Test '%s': Dependencies table missing in project.lua", params.description)
4✔
106
      )
107
      local actual_proj_dep_entry = project_data.dependencies and project_data.dependencies[params.expected_dep_name]
4✔
108
      assert.is_table(
8✔
109
        actual_proj_dep_entry,
4✔
110
        string.format("Test '%s': Project dependency entry should be a table.", params.description)
4✔
111
      )
112

113
      local expected_source_identifier, id_err = url_utils.create_github_source_identifier(params.url)
4✔
114
      assert.is_not_nil(
8✔
115
        expected_source_identifier,
4✔
116
        string.format("Test '%s': Failed to create expected source identifier: %s", params.description, tostring(id_err))
4✔
117
      )
118
      assert.are.equal(
8✔
119
        expected_source_identifier,
4✔
120
        actual_proj_dep_entry.source,
4✔
121
        string.format("Test '%s': Dependency source identifier mismatch in project.lua", params.description)
4✔
122
      )
123
      assert.are.equal(
8✔
124
        params.expected_file_path_relative,
4✔
125
        actual_proj_dep_entry.path,
4✔
126
        string.format("Test '%s': Dependency path mismatch in project.lua", params.description)
4✔
127
      )
128

129
      -- Verify almd-lock.lua content
130
      local lock_data, lock_err = scaffold.read_lock_lua(sandbox_path)
4✔
131
      assert.is_not_nil(
8✔
132
        lock_data,
4✔
133
        string.format("Test '%s': Failed to read almd-lock.lua: %s", params.description, tostring(lock_err))
4✔
134
      )
135
      assert.is_not_nil(
8✔
136
        lock_data.package,
4✔
137
        string.format("Test '%s': Package table missing in almd-lock.lua", params.description)
4✔
138
      )
139
      local dep_lock_info = lock_data.package and lock_data.package[params.expected_dep_name]
4✔
140
      assert.is_not_nil(
8✔
141
        dep_lock_info,
4✔
142
        string.format(
8✔
143
          "Test '%s': Dependency entry missing in almd-lock.lua for %s",
4✔
144
          params.description,
4✔
145
          params.expected_dep_name
146
        )
4✔
147
      )
148

149
      assert.are.equal(
8✔
150
        params.expected_file_path_relative,
4✔
151
        dep_lock_info.path,
4✔
152
        string.format("Test '%s': Lockfile path mismatch", params.description)
4✔
153
      )
154
      assert.are.equal(
8✔
155
        params.expected_lock_source,
4✔
156
        dep_lock_info.source,
4✔
157
        string.format("Test '%s': Lockfile source mismatch", params.description)
4✔
158
      )
159

160
      local expected_hash = params.expected_hash_type .. ":" .. params.expected_hash_value
4✔
161
      assert.are.equal(
8✔
162
        expected_hash,
4✔
163
        dep_lock_info.hash,
4✔
164
        string.format("Test '%s': Lockfile hash mismatch", params.description)
4✔
165
      )
4✔
166
    else
167
      -- Assertions for failed add
168
      assert.is_false(
2✔
169
        success,
1✔
170
        string.format("Test '%s': almd add command should fail (exit code non-zero). Output:\n%s", params.description, output)
1✔
171
      )
172

173
      -- Verify file was NOT downloaded (check default location based on expected name)
174
      -- Note: assumes failure means no custom path/name was processed to create file.
175
      local default_file_path = sandbox_path .. "/src/lib/" .. params.expected_dep_name .. ".lua"
1✔
176
      local file_exists = scaffold.file_exists(default_file_path)
1✔
177
      assert.is_false(
2✔
178
        file_exists,
1✔
179
        string.format("Test '%s': Dependency file should NOT have been downloaded to %s", params.description, default_file_path)
1✔
180
      )
181

182
      -- Verify project.lua content remains unchanged
183
      local project_data_after, proj_err_after = scaffold.read_project_lua(sandbox_path)
1✔
184
      assert.is_not_nil(
2✔
185
        project_data_after,
1✔
186
        string.format("Test '%s': Failed to read project.lua after command: %s", params.description, tostring(proj_err_after))
1✔
187
      )
188
      assert.are.same(
2✔
189
        initial_dependencies,
1✔
190
        project_data_after.dependencies or {},
1✔
191
        string.format("Test '%s': project.lua dependencies table should remain unchanged.", params.description)
1✔
192
      )
193

194
      -- Verify almd-lock.lua was not created or remains unchanged
195
      local lock_file_path = sandbox_path .. "/almd-lock.lua"
1✔
196
      local lock_file_exists = scaffold.file_exists(lock_file_path)
1✔
197
      assert.is_false(
2✔
198
        lock_file_exists,
1✔
199
        string.format("Test '%s': almd-lock.lua should not have been created.", params.description)
1✔
200
      )
201
    end
202
  end
203

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

271
  -- Iterate over cases and run tests
272
  for _, test_case in ipairs(test_cases) do
6✔
273
    it(test_case.description, function()
10✔
274
      _verify_add(test_case)
5✔
275
    end)
276
  end
277
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