Skip to content

Commit 88b9196

Browse files
Feat: Add Basic Lua Test Suite (#115)
This MR adds a Lua test suite to the project, run via busted, and introduces tests for a number of the utility functions. Subsequent work will have to be done to test functions that use the `vim.api` scope and external packages to the plugin.
1 parent 1abc33d commit 88b9196

File tree

12 files changed

+401
-180
lines changed

12 files changed

+401
-180
lines changed

.busted

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
return {
2+
_all = {
3+
pattern = "_spec",
4+
lpath = "lua/?.lua;lua/?/init.lua",
5+
ROOT = {"lua/gitlab"},
6+
},
7+
default = {
8+
verbose = true
9+
},
10+
tests = {
11+
verbose = true,
12+
},
13+
}

.github/workflows/go.yaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@ on:
44
branches:
55
- main
66
jobs:
7-
build:
7+
go_lint:
8+
name: Lint Go 💅
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-go@v4
13+
with:
14+
go-version: '1.19'
15+
cache: false
16+
- name: golangci-lint
17+
uses: golangci/golangci-lint-action@v3
18+
with:
19+
version: v1.54
20+
only-new-issues: true
21+
go_test:
22+
name: Test Go 🧪
23+
needs: [go_lint]
824
runs-on: ubuntu-latest
925
steps:
1026
- uses: actions/checkout@v3

.github/workflows/lua-tests.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Lua Tests
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v3
12+
- name: Run Busted
13+
uses: lunarmodules/[email protected]
14+
# with:
15+
# args: --run=api
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
name: Linting and Formatting
1+
name: Lua
22
on:
33
pull_request:
44
branches:
55
- main
66
jobs:
7-
luacheck:
7+
lua_lint:
8+
name: Lint Lua 💅
89
runs-on: ubuntu-latest
910
steps:
1011
- name: Checkout
@@ -13,7 +14,8 @@ jobs:
1314
uses: lunarmodules/luacheck@v1
1415
with:
1516
args: --globals vim --no-max-line-length -- .
16-
stylua:
17+
lua_format:
18+
name: Formatting Lua 💅
1719
runs-on: ubuntu-latest
1820
steps:
1921
- name: Checkout
@@ -24,16 +26,12 @@ jobs:
2426
token: ${{ secrets.GITHUB_TOKEN }}
2527
version: latest
2628
args: --check .
27-
golangci:
29+
lua_test:
30+
name: Test Lua 🧪
31+
needs: [lua_format,lua_lint]
2832
runs-on: ubuntu-latest
2933
steps:
30-
- uses: actions/checkout@v3
31-
- uses: actions/setup-go@v4
32-
with:
33-
go-version: '1.19'
34-
cache: false
35-
- name: golangci-lint
36-
uses: golangci/golangci-lint-action@v3
37-
with:
38-
version: v1.54
39-
only-new-issues: true
34+
- name: Checkout
35+
uses: actions/checkout@v3
36+
- name: Run Busted
37+
uses: lunarmodules/[email protected]

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
bin
22
.luarc.json
3+
/luarocks
4+
/lua_modules
5+
/.luarocks

logs

Lines changed: 0 additions & 1 deletion
This file was deleted.

lua/gitlab/actions/discussions.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ end
892892
---@param note Note
893893
---@return string
894894
M.build_note_header = function(note)
895-
return "@" .. note.author.username .. " " .. u.format_date(note.created_at)
895+
return "@" .. note.author.username .. " " .. u.time_since(note.created_at)
896896
end
897897

898898
M.build_note_body = function(note, resolve_info)
@@ -977,7 +977,7 @@ M.add_discussions_to_table = function(items)
977977
end
978978

979979
-- Creates the first node in the discussion, and attaches children
980-
local body = u.join_tables(root_text_nodes, discussion_children)
980+
local body = u.spread(root_text_nodes, discussion_children)
981981
local root_node = NuiTree.Node({
982982
text = root_text,
983983
is_note = true,

lua/gitlab/actions/pipeline.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ M.open = function()
4646
u.switch_can_edit_buf(bufnr, true)
4747
table.insert(lines, string.format("Status: %s (%s)", state.settings.pipeline[pipeline.status], pipeline.status))
4848
table.insert(lines, "")
49-
table.insert(lines, string.format("Last Run: %s", u.format_date(pipeline.created_at)))
49+
table.insert(lines, string.format("Last Run: %s", u.time_since(pipeline.created_at)))
5050
table.insert(lines, string.format("Url: %s", pipeline.web_url))
5151
table.insert(lines, string.format("Triggered By: %s", pipeline.source))
5252

@@ -99,7 +99,7 @@ M.see_logs = function()
9999
local bufnr = vim.api.nvim_get_current_buf()
100100
local linnr = vim.api.nvim_win_get_cursor(0)[1]
101101
local text = u.get_line_content(bufnr, linnr)
102-
local last_word = u.get_last_chunk(text)
102+
local last_word = u.get_last_word(text)
103103
if last_word == nil then
104104
u.notify("Cannot find job name", vim.log.levels.ERROR)
105105
return

lua/gitlab/spec/util_spec.lua

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
describe("utils/init.lua", function()
2+
it("Loads package", function()
3+
local utils_ok, _ = pcall(require, "gitlab.utils")
4+
assert._is_true(utils_ok)
5+
end)
6+
7+
local _, u = pcall(require, "gitlab.utils")
8+
describe("extract", function()
9+
it("Extracts a single value", function()
10+
local t = { { one = 1, two = 2 }, { three = 3, four = 4 } }
11+
local got = u.extract(t, "one")
12+
local want = { 1 }
13+
assert.are.same(want, got)
14+
end)
15+
it("Returns nothing with empty table", function()
16+
local t = {}
17+
local got = u.extract(t, "one")
18+
local want = {}
19+
assert.are.same(want, got)
20+
end)
21+
end)
22+
23+
describe("get_last_word", function()
24+
it("Returns the last word in a sentence", function()
25+
local sentence = "Hello world!"
26+
local got = u.get_last_word(sentence)
27+
local want = "world!"
28+
assert.are.same(want, got)
29+
end)
30+
it("Returns an empty string without text", function()
31+
local sentence = ""
32+
local got = u.get_last_word(sentence)
33+
local want = ""
34+
assert.are.same(want, got)
35+
end)
36+
it("Returns whole string w/out divider", function()
37+
local sentence = "Thisdoesnothavebreaks"
38+
local got = u.get_last_word(sentence)
39+
assert.are.same(sentence, got)
40+
end)
41+
it("Returns correct word w/ different divider", function()
42+
local sentence = "this|uses|a|different|divider"
43+
local got = u.get_last_word(sentence, "|")
44+
local want = "divider"
45+
assert.are.same(want, got)
46+
end)
47+
end)
48+
49+
describe("format_date", function()
50+
local current_date = {
51+
day = 19,
52+
hour = 22,
53+
isdst = false,
54+
min = 0,
55+
month = 11,
56+
sec = 44,
57+
wday = 1,
58+
yday = 323,
59+
year = 2023,
60+
}
61+
it("Returns days since a valid UTC timestamp", function()
62+
local stamp = "2023-11-16T19:52:36.946Z"
63+
local got = u.time_since(stamp, current_date)
64+
local want = "3 days ago"
65+
assert.are.same(want, got)
66+
end)
67+
68+
it("Returns hours since a valid UTC timestamp", function()
69+
local stamp = "2023-11-19T19:52:36.946Z"
70+
local got = u.time_since(stamp, current_date)
71+
local want = "2 hours ago"
72+
assert.are.same(want, got)
73+
end)
74+
it("Returns readable time if > 1 year", function()
75+
local stamp = "2011-11-19T19:52:36.946Z"
76+
local got = u.time_since(stamp, current_date)
77+
local want = "November 19, 2011"
78+
assert.are.same(want, got)
79+
end)
80+
end)
81+
82+
describe("remove_first_value", function()
83+
it("Removes the first value correctly", function()
84+
local got = u.remove_first_value({ 1, 2 })
85+
local want = { 2 }
86+
assert.are.same(want, got)
87+
end)
88+
it("Handles a one-length list", function()
89+
local got = u.remove_first_value({ 1 })
90+
local want = {}
91+
assert.are.same(want, got)
92+
end)
93+
it("Handles a zero-length list", function()
94+
local got = u.remove_first_value({})
95+
local want = {}
96+
assert.are.same(want, got)
97+
end)
98+
end)
99+
100+
describe("table_size", function()
101+
it("Works for associative tables", function()
102+
local got = u.remove_first_value({ 1, 2 })
103+
local want = { 2 }
104+
assert.are.same(want, got)
105+
end)
106+
it("Handles a one-length list", function()
107+
local got = u.remove_first_value({ 1 })
108+
local want = {}
109+
assert.are.same(want, got)
110+
end)
111+
it("Handles a zero-length list", function()
112+
local got = u.remove_first_value({})
113+
local want = {}
114+
assert.are.same(want, got)
115+
end)
116+
end)
117+
118+
describe("contains", function()
119+
it("Finds a value in a list", function()
120+
local got = u.contains({ 1, 2 }, 1)
121+
assert._is_true(got)
122+
end)
123+
it("Handles missing values", function()
124+
local got = u.contains({ 1, 3, 4 }, 2)
125+
assert._is_false(got)
126+
end)
127+
it("Handles empty lists", function()
128+
local got = u.contains({}, 1)
129+
assert._is_false(got)
130+
end)
131+
end)
132+
133+
describe("reverse", function()
134+
it("Reverses the values in a list", function()
135+
local got = u.reverse({ 1, 2, 3, 4 })
136+
local want = { 4, 3, 2, 1 }
137+
assert.are.same(got, want)
138+
end)
139+
it("Handles single value", function()
140+
local got = u.reverse({ 1 })
141+
local want = { 1 }
142+
assert.are.same(got, want)
143+
end)
144+
it("Handles empty list", function()
145+
local got = u.reverse({})
146+
local want = {}
147+
assert.are.same(got, want)
148+
end)
149+
end)
150+
151+
describe("spread", function()
152+
it("Spreads the values", function()
153+
local t1 = { 1, 2, 3 }
154+
local t2 = { 4, 5, 6 }
155+
local got = u.spread(t1, t2)
156+
local want = { 1, 2, 3, 4, 5, 6 }
157+
assert.are.same(got, want)
158+
end)
159+
it("Handles an empty t1 table", function()
160+
local t1 = {}
161+
local t2 = { 4, 5, 6 }
162+
local got = u.spread(t1, t2)
163+
local want = { 4, 5, 6 }
164+
assert.are.same(got, want)
165+
end)
166+
it("Handles an empty t2 table", function()
167+
local t1 = { 1, 2, 3 }
168+
local t2 = {}
169+
local got = u.spread(t1, t2)
170+
local want = { 1, 2, 3 }
171+
assert.are.same(got, want)
172+
end)
173+
it("Handles both empty tables", function()
174+
local t1 = {}
175+
local t2 = {}
176+
local got = u.spread(t1, t2)
177+
local want = {}
178+
assert.are.same(got, want)
179+
end)
180+
end)
181+
end)

lua/gitlab/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ M.merge_settings = function(args)
122122
end
123123

124124
M.print_settings = function()
125-
u.P(M.settings)
125+
vim.print(M.settings)
126126
end
127127

128128
-- First reads environment variables into the settings module,

0 commit comments

Comments
 (0)