Rage-cleanup.

While trying to get the unit tests working again I stumbled over all those
things that I thought could be better^TM.

Now we also know "TODO: why does this make the test fail?" - because
the unit tests are brittle AF ... and we have to init the subsystems
we use in the test, otherwise the cleanup will fail...

BTW. you can now also only run a single test ... or a pattern or so ...
you'd have to read how `cmocka_set_test_filter()` works exactly.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2025-03-07 19:03:10 +01:00
parent 72b99ceb6d
commit c0da36c48d
26 changed files with 139 additions and 110 deletions

View File

@@ -11,6 +11,9 @@
#include "helpers.h"
#include "config/preferences.h"
#include "xmpp/chat_session.h"
#include "command/cmd_defs.h"
void prof_shutdown(void);
void
create_config_dir(void** state)
@@ -51,18 +54,24 @@ load_preferences(void** state)
FILE* f = fopen("./tests/files/xdg_config_home/profanity/profrc", "ab+");
if (f) {
prefs_load(NULL);
} else {
return 1;
}
fclose(f);
cmd_init();
chat_sessions_init();
return 0;
}
int
close_preferences(void** state)
{
chat_sessions_clear();
prefs_close();
remove("./tests/files/xdg_config_home/profanity/profrc");
remove_config_dir(state);
rmdir("./tests/files");
prof_shutdown();
return 0;
}
@@ -79,6 +88,7 @@ close_chat_sessions(void** state)
{
chat_sessions_clear();
close_preferences(NULL);
prof_shutdown();
return 0;
}

View File

@@ -16,9 +16,6 @@ returns_no_commands(void** state)
GList* commands = plugins_get_command_names();
assert_true(commands == NULL);
callbacks_close();
g_list_free(commands);
}
void
@@ -26,15 +23,15 @@ returns_commands(void** state)
{
callbacks_init();
PluginCommand* command1 = malloc(sizeof(PluginCommand));
PluginCommand* command1 = calloc(1, sizeof(PluginCommand));
command1->command_name = strdup("command1");
callbacks_add_command("plugin1", command1);
PluginCommand* command2 = malloc(sizeof(PluginCommand));
PluginCommand* command2 = calloc(1, sizeof(PluginCommand));
command2->command_name = strdup("command2");
callbacks_add_command("plugin1", command2);
PluginCommand* command3 = malloc(sizeof(PluginCommand));
PluginCommand* command3 = calloc(1, sizeof(PluginCommand));
command3->command_name = strdup("command3");
callbacks_add_command("plugin2", command3);
@@ -61,6 +58,4 @@ returns_commands(void** state)
assert_true(foundCommand1 && foundCommand2 && foundCommand3);
g_list_free(names);
// TODO: why does this make the test fail?
// callbacks_close();
}

View File

@@ -84,7 +84,6 @@ cmd_alias_add_shows_message_when_exists(void** state)
{
gchar* args[] = { "add", "hc", "/help commands", NULL };
cmd_init();
prefs_add_alias("hc", "/help commands");
cmd_ac_add("/hc");

View File

@@ -216,8 +216,6 @@ cmd_bookmark_uses_roomjid_in_room(void** state)
gboolean result = cmd_bookmark(&muc_win.window, CMD_BOOKMARK, args);
assert_true(result);
muc_close();
}
void
@@ -243,8 +241,6 @@ cmd_bookmark_add_uses_roomjid_in_room(void** state)
gboolean result = cmd_bookmark(&muc_win.window, CMD_BOOKMARK, args);
assert_true(result);
muc_close();
}
void
@@ -271,8 +267,6 @@ cmd_bookmark_add_uses_supplied_jid_in_room(void** state)
gboolean result = cmd_bookmark(&muc_win.window, CMD_BOOKMARK, args);
assert_true(result);
muc_close();
}
void
@@ -401,8 +395,6 @@ cmd_bookmark_remove_uses_roomjid_in_room(void** state)
gboolean result = cmd_bookmark(&muc_win.window, CMD_BOOKMARK, args);
assert_true(result);
muc_close();
}
void
@@ -426,6 +418,4 @@ cmd_bookmark_remove_uses_supplied_jid_in_room(void** state)
gboolean result = cmd_bookmark(&muc_win.window, CMD_BOOKMARK, args);
assert_true(result);
muc_close();
}

View File

@@ -87,8 +87,6 @@ cmd_join_uses_account_mucservice_when_no_service_specified(void** state)
gboolean result = cmd_join(NULL, CMD_JOIN, args);
assert_true(result);
muc_close();
}
void
@@ -115,8 +113,6 @@ cmd_join_uses_supplied_nick(void** state)
gboolean result = cmd_join(NULL, CMD_JOIN, args);
assert_true(result);
muc_close();
}
void
@@ -143,8 +139,6 @@ cmd_join_uses_account_nick_when_not_supplied(void** state)
gboolean result = cmd_join(NULL, CMD_JOIN, args);
assert_true(result);
muc_close();
}
void
@@ -174,6 +168,4 @@ cmd_join_uses_password_when_supplied(void** state)
gboolean result = cmd_join(NULL, CMD_JOIN, args);
assert_true(result);
muc_close();
}

View File

@@ -16,7 +16,6 @@ muc_before_test(void** state)
int
muc_after_test(void** state)
{
muc_close();
return 0;
}

View File

@@ -17,9 +17,6 @@
#include "plugins/plugins.h"
#include "ui/window_list.h"
void prof_shutdown(void);
void
console_shows_online_presence_when_set_online(void** state)
{
@@ -38,7 +35,6 @@ console_shows_online_presence_when_set_online(void** state)
sv_ev_contact_online(barejid, resource, NULL, NULL);
roster_destroy();
prof_shutdown();
}
void
@@ -59,7 +55,6 @@ console_shows_online_presence_when_set_all(void** state)
sv_ev_contact_online(barejid, resource, NULL, NULL);
roster_destroy();
prof_shutdown();
}
void
@@ -80,7 +75,6 @@ console_shows_dnd_presence_when_set_all(void** state)
sv_ev_contact_online(barejid, resource, NULL, NULL);
roster_destroy();
prof_shutdown();
}
void
@@ -96,7 +90,7 @@ handle_offline_removes_chat_session(void** state)
Resource* resourcep = resource_new(resource, RESOURCE_ONLINE, NULL, 10);
roster_update_presence(barejid, resourcep, NULL);
chat_session_recipient_active(barejid, resource, FALSE);
ProfConsoleWin* console = malloc(sizeof(ProfConsoleWin));
ProfConsoleWin* console = calloc(1, sizeof(ProfConsoleWin));
will_return(win_create_console, &console->window);
wins_init();
sv_ev_contact_offline(barejid, resource, NULL);
@@ -106,7 +100,6 @@ handle_offline_removes_chat_session(void** state)
roster_destroy();
chat_sessions_clear();
prof_shutdown();
}
void

View File

@@ -57,6 +57,9 @@ main(int argc, char* argv[])
printf(" MB_CUR_MAX: %d\n", (int)MB_CUR_MAX);
printf(" MB_LEN_MAX: %d\n", (int)MB_LEN_MAX);
if (argc > 1)
cmocka_set_test_filter(argv[1]);
const struct CMUnitTest all_tests[] = {
cmocka_unit_test(replace_one_substr),
@@ -460,7 +463,9 @@ main(int argc, char* argv[])
cmocka_unit_test_setup_teardown(handle_offline_removes_chat_session,
load_preferences,
close_preferences),
cmocka_unit_test(lost_connection_clears_chat_sessions),
cmocka_unit_test_setup_teardown(lost_connection_clears_chat_sessions,
load_preferences,
close_preferences),
cmocka_unit_test(cmd_alias_add_shows_usage_when_no_args),
cmocka_unit_test(cmd_alias_add_shows_usage_when_no_value),
@@ -623,8 +628,12 @@ main(int argc, char* argv[])
cmocka_unit_test(prof_whole_occurrences_tests),
cmocka_unit_test(prof_occurrences_of_large_message_tests),
cmocka_unit_test(returns_no_commands),
cmocka_unit_test(returns_commands),
cmocka_unit_test_setup_teardown(returns_no_commands,
load_preferences,
close_preferences),
cmocka_unit_test_setup_teardown(returns_commands,
load_preferences,
close_preferences),
cmocka_unit_test(returns_empty_list_when_none),
cmocka_unit_test(returns_added_feature),
@@ -634,6 +643,5 @@ main(int argc, char* argv[])
cmocka_unit_test(removes_plugin_features),
cmocka_unit_test(does_not_remove_feature_when_more_than_one_reference),
};
return cmocka_run_group_tests(all_tests, NULL, NULL);
}