Skip to content

Commit 259d3f9

Browse files
authored
feat: establish eunit test suite with 29 tests (#24)
* feat: establish eunit test suite with 29 tests Covers the core logic modules: - rebar3_dependency_submission_rebar3: lock parsing, version extraction, consult - rebar3_dependency_submission_snapshot: PURL conversion, dependency resolution - rebar3_dependency_submission_options: CLI argument parsing, env var fallback - rebar3_dependency_submission_common: to_binary, format_markdown Includes test fixtures for hex-only, mixed (hex+git+git_subdir), and empty lock files. Ref #13 * chore: add REUSE license files for test fixtures
1 parent 8148a1a commit 259d3f9

11 files changed

+423
-2
lines changed

src/rebar3_dependency_submission_rebar3.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
-export(?API).
1313
-ignore_xref(?API).
1414

15+
-ifdef(TEST).
16+
-export([version/1, consult/1]).
17+
-endif.
18+
1519
-export_type([
1620
t/0
1721
]).

test/fixtures/empty.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[].

test/fixtures/empty.lock.license

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%% SPDX-License-Identifier: Apache-2.0
2+
%% SPDX-FileCopyrightText: 2026 Kivra AB

test/fixtures/hex_only.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{"1.2.0",
2+
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.12.0">>},0},
3+
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.13.0">>},1},
4+
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.8.0">>},1}]}.
5+
[
6+
{pkg_hash,[
7+
{<<"cowboy">>, <<"AA68E5B4CDB4AAFCF2B442D41D621639B6DA4F3B">>},
8+
{<<"cowlib">>, <<"DB768DB367F5A3DC1F97E8B0A37F6C445414BE7E">>},
9+
{<<"ranch">>, <<"49FBCFD3682FAB1F5D109351B61257676DA1A2EF">>}]},
10+
{pkg_hash_ext,[
11+
{<<"cowboy">>, <<"EE83A7E606A7EC0B78C99CFFFC08ACA3B1ADBE32">>},
12+
{<<"cowlib">>, <<"EBF28A6E308F8A0B5A2683A5F5D4E8BCAE1F2709">>},
13+
{<<"ranch">>, <<"0B440C854C84E2AB3A42D8FEDBE015ABDE8D4F7A">>}]}
14+
].
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%% SPDX-License-Identifier: Apache-2.0
2+
%% SPDX-FileCopyrightText: 2026 Kivra AB

test/fixtures/mixed.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{"1.2.0",
2+
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.12.0">>},0},
3+
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.13.0">>},1},
4+
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.8.0">>},1},
5+
{<<"purl">>,
6+
{git,"git@github.com:erlef/purl.git",
7+
{ref,"d64773d59dfd1d6b8920209f219e3c796c974a3a"}},
8+
0},
9+
{<<"rebar">>,
10+
{git_subdir,"git@github.com:erlang/rebar3.git",
11+
{ref,"049f80c4fb5990b682b23b1fde643103ec1e6dea"},
12+
"apps/rebar"},
13+
0}]}.
14+
[
15+
{pkg_hash,[
16+
{<<"cowboy">>, <<"AA68E5B4CDB4AAFCF2B442D41D621639B6DA4F3B">>},
17+
{<<"cowlib">>, <<"DB768DB367F5A3DC1F97E8B0A37F6C445414BE7E">>},
18+
{<<"ranch">>, <<"49FBCFD3682FAB1F5D109351B61257676DA1A2EF">>}]},
19+
{pkg_hash_ext,[
20+
{<<"cowboy">>, <<"EE83A7E606A7EC0B78C99CFFFC08ACA3B1ADBE32">>},
21+
{<<"cowlib">>, <<"EBF28A6E308F8A0B5A2683A5F5D4E8BCAE1F2709">>},
22+
{<<"ranch">>, <<"0B440C854C84E2AB3A42D8FEDBE015ABDE8D4F7A">>}]}
23+
].

test/fixtures/mixed.lock.license

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%% SPDX-License-Identifier: Apache-2.0
2+
%% SPDX-FileCopyrightText: 2026 Kivra AB
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
%% SPDX-License-Identifier: Apache-2.0
2+
%% SPDX-FileCopyrightText: 2026 Kivra AB
3+
-module(rebar3_dependency_submission_common_tests).
4+
5+
-include_lib("eunit/include/eunit.hrl").
6+
7+
%%--------------------------------------------------------------------
8+
%% to_binary
9+
%%--------------------------------------------------------------------
10+
11+
to_binary_from_binary_test() ->
12+
?assertEqual(
13+
~"hello", rebar3_dependency_submission_common:to_binary(~"hello")
14+
).
15+
16+
to_binary_from_list_test() ->
17+
?assertEqual(
18+
~"hello", rebar3_dependency_submission_common:to_binary("hello")
19+
).
20+
21+
to_binary_from_iolist_test() ->
22+
?assertEqual(
23+
~"hello world",
24+
rebar3_dependency_submission_common:to_binary([~"hello", $\s, "world"])
25+
).
26+
27+
to_binary_invalid_utf8_test() ->
28+
?assertError(
29+
badarg, rebar3_dependency_submission_common:to_binary(<<255, 254>>)
30+
).
31+
32+
%%--------------------------------------------------------------------
33+
%% format_markdown
34+
%%--------------------------------------------------------------------
35+
36+
format_markdown_no_backticks_test() ->
37+
Result = rebar3_dependency_submission_common:format_markdown("hello ~s", [
38+
"world"
39+
]),
40+
?assertEqual("hello world", lists:flatten(Result)).
41+
42+
format_markdown_with_backticks_test() ->
43+
Result = rebar3_dependency_submission_common:format_markdown(
44+
"`foo` bar", []
45+
),
46+
Flat = unicode:characters_to_list(iolist_to_binary(Result)),
47+
?assertNotEqual(nomatch, string:find(Flat, "foo")),
48+
%% Backticks should be replaced with ANSI bold
49+
?assertEqual(nomatch, string:find(Flat, "`")).
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
%% SPDX-License-Identifier: Apache-2.0
2+
%% SPDX-FileCopyrightText: 2026 Kivra AB
3+
-module(rebar3_dependency_submission_options_tests).
4+
5+
-include_lib("eunit/include/eunit.hrl").
6+
7+
%%--------------------------------------------------------------------
8+
%% parse
9+
%%--------------------------------------------------------------------
10+
11+
parse_help_test() ->
12+
?assertEqual(help, rebar3_dependency_submission_options:parse(["--help"])).
13+
14+
parse_all_flags_test() ->
15+
Args = [
16+
"--token",
17+
"ghp_test123",
18+
"--sha",
19+
"abc123def456",
20+
"--ref",
21+
"refs/heads/main",
22+
"--repo",
23+
"erlef/rebar3-dependency-submission",
24+
"--run-id",
25+
"12345",
26+
"--correlator",
27+
"my_workflow_my_job",
28+
"--api-url",
29+
"https://api.github.com",
30+
"--attempt",
31+
"1"
32+
],
33+
{ok, Options, []} = rebar3_dependency_submission_options:parse(Args),
34+
?assertEqual(~"ghp_test123", maps:get(token, Options)),
35+
?assertEqual(~"abc123def456", maps:get(sha, Options)),
36+
?assertEqual(~"refs/heads/main", maps:get(ref, Options)),
37+
?assertEqual(
38+
~"erlef/rebar3-dependency-submission", maps:get(repo, Options)
39+
),
40+
?assertEqual(~"12345", maps:get(run_id, Options)),
41+
?assertEqual(~"my_workflow_my_job", maps:get(correlator, Options)).
42+
43+
parse_missing_required_test() ->
44+
{error, missing} = rebar3_dependency_submission_options:parse([]).
45+
46+
parse_env_fallback_test() ->
47+
Envs = [
48+
{"GITHUB_TOKEN", "env_token"},
49+
{"GITHUB_SHA", "env_sha"},
50+
{"GITHUB_REF", "refs/heads/main"},
51+
{"GITHUB_REPOSITORY", "owner/repo"},
52+
{"GITHUB_RUN_ID", "999"},
53+
{"GITHUB_WORKFLOW", "ci"},
54+
{"GITHUB_JOB", "test"},
55+
{"GITHUB_RUN_ATTEMPT", "1"}
56+
],
57+
try
58+
[os:putenv(K, V) || {K, V} <- Envs],
59+
{ok, Options, []} = rebar3_dependency_submission_options:parse([]),
60+
?assertEqual(~"env_token", maps:get(token, Options)),
61+
?assertEqual(~"env_sha", maps:get(sha, Options)),
62+
?assertEqual(~"ci_test", maps:get(correlator, Options))
63+
after
64+
[os:unsetenv(K) || {K, _} <- Envs]
65+
end.
66+
67+
parse_cli_overrides_env_test() ->
68+
os:putenv("GITHUB_TOKEN", "env_token"),
69+
try
70+
Envs = [
71+
{"GITHUB_SHA", "env_sha"},
72+
{"GITHUB_REF", "refs/heads/main"},
73+
{"GITHUB_REPOSITORY", "owner/repo"},
74+
{"GITHUB_RUN_ID", "999"},
75+
{"GITHUB_WORKFLOW", "ci"},
76+
{"GITHUB_JOB", "test"},
77+
{"GITHUB_RUN_ATTEMPT", "1"}
78+
],
79+
[os:putenv(K, V) || {K, V} <- Envs],
80+
{ok, Options, []} = rebar3_dependency_submission_options:parse([
81+
"--token", "cli_token"
82+
]),
83+
?assertEqual(~"cli_token", maps:get(token, Options))
84+
after
85+
os:unsetenv("GITHUB_TOKEN"),
86+
[
87+
os:unsetenv(K)
88+
|| {K, _} <- [
89+
{"GITHUB_SHA", ""},
90+
{"GITHUB_REF", ""},
91+
{"GITHUB_REPOSITORY", ""},
92+
{"GITHUB_RUN_ID", ""},
93+
{"GITHUB_WORKFLOW", ""},
94+
{"GITHUB_JOB", ""},
95+
{"GITHUB_RUN_ATTEMPT", ""}
96+
]
97+
]
98+
end.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
%% SPDX-License-Identifier: Apache-2.0
2+
%% SPDX-FileCopyrightText: 2026 Kivra AB
3+
-module(rebar3_dependency_submission_rebar3_tests).
4+
5+
-include_lib("eunit/include/eunit.hrl").
6+
-include("rebar3_dependency_submission_records.hrl").
7+
8+
fixture(Name) ->
9+
filename:join([
10+
code:lib_dir(rebar3_dependency_submission),
11+
"test",
12+
"fixtures",
13+
Name
14+
]).
15+
16+
%%--------------------------------------------------------------------
17+
%% lock parsing
18+
%%--------------------------------------------------------------------
19+
20+
from_hex_only_test() ->
21+
State = rebar3_dependency_submission_rebar3:from(fixture("hex_only.lock")),
22+
#{
23+
lock_version := LockVersion,
24+
packages := Packages,
25+
pkg_hash := PkgHash,
26+
pkg_hash_ext := PkgHashExt
27+
} = State,
28+
?assertEqual("1.2.0", LockVersion),
29+
?assertEqual(3, map_size(Packages)),
30+
?assertMatch(
31+
{#pkg{name = ~"cowboy", version = ~"2.12.0"}, 0},
32+
maps:get(cowboy, Packages)
33+
),
34+
?assertMatch(
35+
{#pkg{name = ~"cowlib", version = ~"2.13.0"}, 1},
36+
maps:get(cowlib, Packages)
37+
),
38+
?assertMatch(
39+
{#pkg{name = ~"ranch", version = ~"1.8.0"}, 1},
40+
maps:get(ranch, Packages)
41+
),
42+
?assertEqual(3, map_size(PkgHash)),
43+
?assertEqual(3, map_size(PkgHashExt)).
44+
45+
from_mixed_test() ->
46+
State = rebar3_dependency_submission_rebar3:from(fixture("mixed.lock")),
47+
#{packages := Packages} = State,
48+
?assertEqual(5, map_size(Packages)),
49+
?assertMatch(
50+
{#git{repo = "git@github.com:erlef/purl.git", ref = {ref, _}}, 0},
51+
maps:get(purl, Packages)
52+
),
53+
?assertMatch(
54+
{
55+
#git_subdir{
56+
repo = "git@github.com:erlang/rebar3.git",
57+
ref = {ref, _},
58+
subdir = "apps/rebar"
59+
},
60+
0
61+
},
62+
maps:get(rebar, Packages)
63+
).
64+
65+
from_empty_lock_test() ->
66+
State = rebar3_dependency_submission_rebar3:from(fixture("empty.lock")),
67+
#{packages := Packages, lock_version := LockVersion} = State,
68+
?assertEqual(0, map_size(Packages)),
69+
?assertEqual(~"", LockVersion).
70+
71+
from_nonexistent_file_test() ->
72+
?assertError(
73+
enoent,
74+
rebar3_dependency_submission_rebar3:from("/nonexistent/rebar.lock")
75+
).
76+
77+
%%--------------------------------------------------------------------
78+
%% version extraction
79+
%%--------------------------------------------------------------------
80+
81+
version_pkg_test() ->
82+
?assertEqual(
83+
~"2.12.0",
84+
rebar3_dependency_submission_rebar3:version(
85+
#pkg{name = ~"cowboy", version = ~"2.12.0"}
86+
)
87+
).
88+
89+
version_git_tag_test() ->
90+
?assertEqual(
91+
~"v1.0.0",
92+
rebar3_dependency_submission_rebar3:version(
93+
#git{repo = "repo", ref = {tag, ~"v1.0.0"}}
94+
)
95+
).
96+
97+
version_git_branch_test() ->
98+
?assertEqual(
99+
~"main",
100+
rebar3_dependency_submission_rebar3:version(
101+
#git{repo = "repo", ref = {branch, ~"main"}}
102+
)
103+
).
104+
105+
version_git_ref_test() ->
106+
?assertEqual(
107+
~"abc123",
108+
rebar3_dependency_submission_rebar3:version(
109+
#git{repo = "repo", ref = {ref, ~"abc123"}}
110+
)
111+
).
112+
113+
version_git_subdir_test() ->
114+
?assertEqual(
115+
~"v2.0.0",
116+
rebar3_dependency_submission_rebar3:version(
117+
#git_subdir{
118+
repo = "repo", ref = {tag, ~"v2.0.0"}, subdir = "apps/foo"
119+
}
120+
)
121+
).
122+
123+
%%--------------------------------------------------------------------
124+
%% consult
125+
%%--------------------------------------------------------------------
126+
127+
consult_nonexistent_test() ->
128+
?assertError(
129+
enoent,
130+
rebar3_dependency_submission_rebar3:consult("/does/not/exist.config")
131+
).
132+
133+
consult_valid_file_test() ->
134+
Result = rebar3_dependency_submission_rebar3:consult(
135+
fixture("hex_only.lock")
136+
),
137+
?assertMatch([{"1.2.0", _} | _], Result).

0 commit comments

Comments
 (0)