From 549f28fa0d2b366b791077998edffc167977e8cb Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 25 Feb 2026 12:07:46 +0100 Subject: [PATCH] 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". --- src/common.c | 8 ++++++-- src/common.h | 2 +- src/ui/console.c | 2 +- tests/unittests/test_common.c | 36 +++++++++++++++++++++++++++++++++++ tests/unittests/test_common.h | 1 + tests/unittests/unittests.c | 1 + 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/common.c b/src/common.c index 96f5cac6..52aa0ee6 100644 --- a/src/common.c +++ b/src/common.c @@ -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); diff --git a/src/common.h b/src/common.h index d7c7baa9..6d4c0007 100644 --- a/src/common.h +++ b/src/common.h @@ -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); diff --git a/src/ui/console.c b/src/ui/console.c index 9302b9d3..bea715ac 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -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 for details."); win_println(console, THEME_DEFAULT, "-", ""); diff --git a/tests/unittests/test_common.c b/tests/unittests/test_common.c index 40912679..8926e57f 100644 --- a/tests/unittests/test_common.c +++ b/tests/unittests/test_common.c @@ -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) { diff --git a/tests/unittests/test_common.h b/tests/unittests/test_common.h index 04c9453c..6229d54a 100644 --- a/tests/unittests/test_common.h +++ b/tests/unittests/test_common.h @@ -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); diff --git a/tests/unittests/unittests.c b/tests/unittests/unittests.c index 1b054eeb..8bf25b9b 100644 --- a/tests/unittests/unittests.c +++ b/tests/unittests/unittests.c @@ -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,