tests: Add test for release_is_new()

Also refactor the function so we pass it the current version
and don't rely on the define inside of it.

This function only allows version format in the style of "x.y.z".
This commit is contained in:
Michael Vetter
2026-02-25 12:07:46 +01:00
parent ea7a59808a
commit 549f28fa0d
6 changed files with 46 additions and 4 deletions

View File

@@ -471,11 +471,15 @@ release_get_latest(void)
}
gboolean
release_is_new(char* found_version)
release_is_new(const char* const curr_version, const char* const found_version)
{
if (!curr_version || !found_version) {
return FALSE;
}
int curr_maj, curr_min, curr_patch, found_maj, found_min, found_patch;
int parse_curr = sscanf(PACKAGE_VERSION, "%d.%d.%d", &curr_maj, &curr_min,
int parse_curr = sscanf(curr_version, "%d.%d.%d", &curr_maj, &curr_min,
&curr_patch);
int parse_found = sscanf(found_version, "%d.%d.%d", &found_maj, &found_min,
&found_patch);

View File

@@ -193,7 +193,7 @@ gboolean string_matches_one_of(const char* what, const char* is, gboolean is_can
gboolean valid_tls_policy_option(const char* is);
char* release_get_latest(void);
gboolean release_is_new(char* found_version);
gboolean release_is_new(const char* const curr_version, const char* const found_version);
char* strip_arg_quotes(const char* const input);
gboolean is_notify_enabled(void);

View File

@@ -395,7 +395,7 @@ cons_check_version(gboolean not_available_msg)
gboolean relase_valid = g_regex_match_simple("^\\d+\\.\\d+\\.\\d+$", latest_release, 0, 0);
if (relase_valid) {
if (release_is_new(latest_release)) {
if (release_is_new(PACKAGE_VERSION, latest_release)) {
win_println(console, THEME_DEFAULT, "-", "A new version of Profanity is available: %s", latest_release);
win_println(console, THEME_DEFAULT, "-", "Check <https://profanity-im.github.io> for details.");
win_println(console, THEME_DEFAULT, "-", "");

View File

@@ -936,6 +936,42 @@ get_mentions_tests(void** state)
g_slist_free(expected); expected = NULL;
}
void
release_is_new_tests(void** state)
{
// Higher major version
assert_true(release_is_new("0.16.0", "1.0.0"));
// Higher minor version
assert_true(release_is_new("0.16.0", "0.17.0"));
// Higher patch version
assert_true(release_is_new("0.16.0", "0.16.1"));
// Same version
assert_false(release_is_new("0.16.0", "0.16.0"));
// Lower major version
assert_false(release_is_new("0.16.0", "0.15.9"));
// Lower minor version
assert_false(release_is_new("0.16.0", "0.15.0"));
// Lower patch version
assert_false(release_is_new("0.16.1", "0.16.0"));
// Higher version but with different current versions
assert_true(release_is_new("1.2.3", "1.2.4"));
assert_true(release_is_new("1.2.3", "1.3.0"));
assert_true(release_is_new("1.2.3", "2.0.0"));
// Malformed version strings
assert_false(release_is_new("0.16.0", "0.16")); // Missing patch in found
assert_false(release_is_new("0.16", "0.16.0")); // Missing patch in curr
assert_false(release_is_new("0.16.0", "0.16.0.1")); // Extra part
assert_false(release_is_new("0.16.0", "abc.def.ghi"));
assert_false(release_is_new("0.16.0", ""));
assert_false(release_is_new(NULL, "1.0.0"));
assert_false(release_is_new("1.0.0", NULL));
}
void
prof_whole_occurrences_tests(void** state)
{

View File

@@ -44,3 +44,4 @@ void test_strtoi_range_null_err_msg(void** state);
void test_string_matches_one_of_edge_cases(void** state);
void test_valid_tls_policy_option(void** state);
void get_mentions_tests(void** state);
void release_is_new_tests(void** state);

View File

@@ -635,6 +635,7 @@ main(int argc, char* argv[])
cmocka_unit_test(prof_whole_occurrences_tests),
cmocka_unit_test(prof_occurrences_of_large_message_tests),
cmocka_unit_test(get_mentions_tests),
cmocka_unit_test(release_is_new_tests),
cmocka_unit_test_setup_teardown(returns_no_commands,
load_preferences,