Skip to content

Commit

Permalink
fix: extract version fn for empty new lines (#54)
Browse files Browse the repository at this point in the history
* fix: extract version fn for empty new lines

* chore: add eunit test cases for fn extract_version
  • Loading branch information
VivekPipaliya23 authored Apr 11, 2024
1 parent 3607255 commit e10b5ad
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions apps/vmq_commons/src/vmq_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,59 @@ set_interval(Interval, Pid) ->
extract_version(File) ->
case file:read_file(File) of
{ok, BinaryData} when byte_size(BinaryData) > 0 ->
Line = hd(string:tokens(binary_to_list(BinaryData), "\n")),
case re:run(Line, "#\\s*(v\\d+\\.\\d+\\.\\d+)", [{capture, [1], list}]) of
{match, [Version]} -> Version;
nomatch -> nomatch
Lines = string:tokens(binary_to_list(BinaryData), "\n"),
case Lines of
% If Lines is empty, return nomatch
[] ->
nomatch;
[FirstLine | _Rest] ->
case re:run(FirstLine, "#\\s*(v\\d+\\.\\d+\\.\\d+)", [{capture, [1], list}]) of
{match, [Version]} ->
Version;
nomatch ->
nomatch
end
end;
{ok, _} ->
nomatch;
{error, Reason} ->
{error, Reason}
end.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-define(setup(F), {setup, fun setup/0, fun teardown/1, F}).

extract_version_test_() ->
[
{"Extract config version", ?setup(fun extract_version_test/1)}
].

setup() ->
%% Create a test file with some content
{ok, File} = file:open("test_file.acl", [write]),
file:write(File, "# v1.2.3\nSome text\n"),
file:close(File),
{ok, NoVersionFile} = file:open("no_version_file.acl", [write]),
file:write(NoVersionFile, "Some text\nRandom text\n"),
file:close(NoVersionFile),
{ok, EmptyFile} = file:open("empty_test_file.acl", [write]),
file:close(EmptyFile),
{ok, NewLineFile} = file:open("new_line_test_file.acl", [write]),
file:write(NewLineFile, "\n"),
file:close(NewLineFile).

teardown(_) ->
file:delete("test_file.acl"),
file:delete("no_version_file.acl"),
file:delete("empty_test_file.acl"),
file:delete("new_line_test_file.acl").

extract_version_test(_) ->
[
?_assertEqual("v1.2.3", extract_version("test_file.acl")),
?_assertEqual(nomatch, extract_version("no_version_file.acl")),
?_assertEqual(nomatch, extract_version("empty_test_file.acl")),
?_assertEqual(nomatch, extract_version("new_line_test_file.acl"))
].
-endif.

0 comments on commit e10b5ad

Please sign in to comment.