2014-07-20 12:13:25 +02:00
|
|
|
/*
|
|
|
|
* tests.cpp - run WeeChat tests
|
|
|
|
*
|
2021-01-02 21:34:16 +01:00
|
|
|
* Copyright (C) 2014-2021 Sébastien Helleu <flashcode@flashtux.org>
|
2014-07-20 12:13:25 +02:00
|
|
|
*
|
|
|
|
* This file is part of WeeChat, the extensible chat client.
|
|
|
|
*
|
|
|
|
* WeeChat is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* WeeChat is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-11-29 23:16:07 +01:00
|
|
|
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
|
2014-07-20 12:13:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2019-01-11 22:26:16 +01:00
|
|
|
#include <dlfcn.h>
|
2014-08-14 19:15:25 +02:00
|
|
|
#include <string.h>
|
2014-07-20 12:13:25 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2014-07-21 21:25:55 +02:00
|
|
|
#ifndef HAVE_CONFIG_H
|
2014-07-20 12:13:25 +02:00
|
|
|
#define HAVE_CONFIG_H
|
2014-07-21 21:25:55 +02:00
|
|
|
#endif
|
2014-08-25 07:38:38 +02:00
|
|
|
#include "src/core/weechat.h"
|
2021-04-18 21:23:14 +02:00
|
|
|
#include "src/core/wee-dir.h"
|
2014-08-25 07:38:38 +02:00
|
|
|
#include "src/core/wee-hook.h"
|
|
|
|
#include "src/core/wee-input.h"
|
|
|
|
#include "src/core/wee-string.h"
|
|
|
|
#include "src/plugins/plugin.h"
|
|
|
|
#include "src/gui/gui-main.h"
|
|
|
|
#include "src/gui/gui-buffer.h"
|
2017-07-05 21:33:19 +02:00
|
|
|
#include "src/gui/gui-chat.h"
|
2014-08-02 11:38:58 +02:00
|
|
|
|
|
|
|
extern void gui_main_init ();
|
|
|
|
extern void gui_main_loop ();
|
2014-07-20 12:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "CppUTest/CommandLineTestRunner.h"
|
|
|
|
|
2017-07-23 15:12:40 +02:00
|
|
|
#define LOCALE_TESTS "en_US.UTF-8"
|
|
|
|
|
2017-10-07 16:51:25 +02:00
|
|
|
#define WEECHAT_TESTS_HOME "./tmp_weechat_test"
|
|
|
|
|
2019-01-11 22:26:16 +01:00
|
|
|
/* lib with tests on plugins when autotools is used to compile */
|
|
|
|
#define WEECHAT_TESTS_PLUGINS_LIB_DEFAULT \
|
|
|
|
"./tests/.libs/lib_weechat_unit_tests_plugins.so.0.0.0"
|
|
|
|
|
2014-07-20 12:13:25 +02:00
|
|
|
/* import tests from libs */
|
2018-08-07 21:50:04 +02:00
|
|
|
/* core */
|
|
|
|
IMPORT_TEST_GROUP(CoreArraylist);
|
2019-09-20 21:37:01 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreCalc);
|
2021-10-01 22:55:38 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreConfigFile);
|
2020-03-01 18:02:39 +01:00
|
|
|
IMPORT_TEST_GROUP(CoreCrypto);
|
2021-04-18 21:23:14 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreDir);
|
2018-08-07 21:50:04 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreEval);
|
|
|
|
IMPORT_TEST_GROUP(CoreHashtable);
|
|
|
|
IMPORT_TEST_GROUP(CoreHdata);
|
2018-08-16 18:27:04 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreHook);
|
2018-08-07 21:50:04 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreInfolist);
|
|
|
|
IMPORT_TEST_GROUP(CoreList);
|
2021-05-16 14:52:11 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreNetwork);
|
2018-10-27 11:03:03 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreSecure);
|
2021-03-16 18:47:31 +01:00
|
|
|
IMPORT_TEST_GROUP(CoreSignal);
|
2018-08-07 21:50:04 +02:00
|
|
|
IMPORT_TEST_GROUP(CoreString);
|
|
|
|
IMPORT_TEST_GROUP(CoreUrl);
|
|
|
|
IMPORT_TEST_GROUP(CoreUtf8);
|
|
|
|
IMPORT_TEST_GROUP(CoreUtil);
|
|
|
|
/* GUI */
|
2019-09-29 13:52:58 +02:00
|
|
|
IMPORT_TEST_GROUP(GuiColor);
|
2018-08-07 21:50:04 +02:00
|
|
|
IMPORT_TEST_GROUP(GuiLine);
|
2019-08-19 23:27:11 +02:00
|
|
|
IMPORT_TEST_GROUP(GuiNick);
|
2018-08-07 21:50:04 +02:00
|
|
|
/* scripts */
|
2017-10-07 16:51:25 +02:00
|
|
|
IMPORT_TEST_GROUP(Scripts);
|
|
|
|
|
|
|
|
struct t_gui_buffer *ptr_core_buffer = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback for exec_on_files (to remove all files in WeeChat home directory).
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
exec_on_files_cb (void *data, const char *filename)
|
|
|
|
{
|
|
|
|
/* make C++ compiler happy */
|
|
|
|
(void) data;
|
2014-07-20 12:13:25 +02:00
|
|
|
|
2017-10-07 16:51:25 +02:00
|
|
|
unlink (filename);
|
|
|
|
}
|
2014-07-20 12:13:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback for any message displayed by WeeChat or a plugin.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2016-03-21 18:11:21 +01:00
|
|
|
test_print_cb (const void *pointer, void *data, struct t_gui_buffer *buffer,
|
2017-10-07 16:51:25 +02:00
|
|
|
time_t date, int tags_count, const char **tags, int displayed,
|
|
|
|
int highlight, const char *prefix, const char *message)
|
2014-07-20 12:13:25 +02:00
|
|
|
{
|
2016-03-21 18:11:21 +01:00
|
|
|
/* make C++ compiler happy */
|
|
|
|
(void) pointer;
|
2014-07-20 12:13:25 +02:00
|
|
|
(void) data;
|
|
|
|
(void) buffer;
|
|
|
|
(void) date;
|
|
|
|
(void) tags_count;
|
|
|
|
(void) tags;
|
|
|
|
(void) displayed;
|
|
|
|
(void) highlight;
|
|
|
|
|
2018-08-16 18:27:04 +02:00
|
|
|
/* keep only messages displayed on core buffer */
|
2019-01-11 22:26:16 +01:00
|
|
|
if (strcmp (gui_buffer_get_string (buffer, "full_name"),
|
|
|
|
"core.weechat") != 0)
|
|
|
|
{
|
2018-08-16 18:27:04 +02:00
|
|
|
return WEECHAT_RC_OK;
|
2019-01-11 22:26:16 +01:00
|
|
|
}
|
2018-08-16 18:27:04 +02:00
|
|
|
|
2014-07-20 12:13:25 +02:00
|
|
|
printf ("%s%s%s\n", /* with color: "\33[34m%s%s%s\33[0m\n" */
|
|
|
|
(prefix && prefix[0]) ? prefix : "",
|
|
|
|
(prefix && prefix[0]) ? " " : "",
|
|
|
|
(message && message[0]) ? message : "");
|
|
|
|
|
|
|
|
return WEECHAT_RC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes GUI for tests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
test_gui_init ()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Catch all messages to display them directly on stdout
|
|
|
|
* (Curses library is not used for tests).
|
|
|
|
*/
|
|
|
|
hook_print (NULL, /* plugin */
|
|
|
|
NULL, /* buffer */
|
|
|
|
NULL, /* tags */
|
|
|
|
NULL, /* message */
|
|
|
|
1, /* strip colors */
|
|
|
|
&test_print_cb,
|
2016-03-21 18:11:21 +01:00
|
|
|
NULL,
|
2014-07-20 12:13:25 +02:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the function "gui_main_init" from Curses sources (all Curses
|
|
|
|
* calls are made with the fake ncurses library).
|
|
|
|
*/
|
|
|
|
gui_main_init ();
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:51:25 +02:00
|
|
|
/*
|
|
|
|
* Displays and runs a command on a buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
run_cmd (const char *command)
|
|
|
|
{
|
|
|
|
printf (">>> Running command: %s\n", command);
|
2019-03-28 18:45:22 +01:00
|
|
|
input_data (ptr_core_buffer, command, NULL);
|
2017-10-07 16:51:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-20 12:13:25 +02:00
|
|
|
/*
|
|
|
|
* Runs tests in WeeChat environment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2014-08-14 19:15:25 +02:00
|
|
|
int rc, length, weechat_argc;
|
2019-01-11 22:26:16 +01:00
|
|
|
char *weechat_tests_args, *args, **weechat_argv, *tests_plugins_lib;
|
|
|
|
const char *tests_plugins_lib_default = WEECHAT_TESTS_PLUGINS_LIB_DEFAULT;
|
|
|
|
const char *ptr_path;
|
|
|
|
void *handle;
|
2014-07-20 12:13:25 +02:00
|
|
|
|
2017-03-27 21:14:51 +02:00
|
|
|
/* setup environment: English language, no specific timezone */
|
2017-07-23 15:12:40 +02:00
|
|
|
setenv ("LC_ALL", LOCALE_TESTS, 1);
|
2014-07-20 12:13:25 +02:00
|
|
|
setenv ("TZ", "", 1);
|
|
|
|
|
2017-07-23 15:12:40 +02:00
|
|
|
/* check if locale exists */
|
|
|
|
if (!setlocale (LC_ALL, ""))
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"ERROR: the locale %s must be installed to run WeeChat "
|
|
|
|
"tests.\n",
|
|
|
|
LOCALE_TESTS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:51:25 +02:00
|
|
|
/* clean WeeChat home */
|
2021-04-18 21:23:14 +02:00
|
|
|
dir_exec_on_files (WEECHAT_TESTS_HOME, 1, 1, &exec_on_files_cb, NULL);
|
2017-10-07 16:51:25 +02:00
|
|
|
|
2014-08-14 19:15:25 +02:00
|
|
|
/* build arguments for WeeChat */
|
|
|
|
weechat_tests_args = getenv ("WEECHAT_TESTS_ARGS");
|
|
|
|
length = strlen (argv[0]) +
|
2017-07-05 21:33:19 +02:00
|
|
|
64 + /* --dir ... */
|
2014-08-14 19:15:25 +02:00
|
|
|
((weechat_tests_args) ? 1 + strlen (weechat_tests_args) : 0) +
|
|
|
|
1;
|
|
|
|
args = (char *)malloc (length);
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Memory error\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
snprintf (args, length,
|
2017-10-07 16:51:25 +02:00
|
|
|
"%s --dir %s%s%s",
|
2014-08-14 19:15:25 +02:00
|
|
|
argv[0],
|
2017-10-07 16:51:25 +02:00
|
|
|
WEECHAT_TESTS_HOME,
|
2014-08-14 19:15:25 +02:00
|
|
|
(weechat_tests_args) ? " " : "",
|
|
|
|
(weechat_tests_args) ? weechat_tests_args : "");
|
|
|
|
weechat_argv = string_split_shell (args, &weechat_argc);
|
|
|
|
printf ("WeeChat arguments: \"%s\"\n", args);
|
2014-07-20 12:13:25 +02:00
|
|
|
|
|
|
|
/* init WeeChat */
|
2018-03-11 09:59:39 +01:00
|
|
|
weechat_init_gettext ();
|
2014-08-14 19:15:25 +02:00
|
|
|
weechat_init (weechat_argc, weechat_argv, &test_gui_init);
|
2014-08-15 15:08:31 +02:00
|
|
|
if (weechat_argv)
|
|
|
|
string_free_split (weechat_argv);
|
|
|
|
free (args);
|
2014-07-20 12:13:25 +02:00
|
|
|
|
2017-07-05 21:33:19 +02:00
|
|
|
ptr_core_buffer = gui_buffer_search_main ();
|
2014-07-20 12:13:25 +02:00
|
|
|
|
2017-07-05 21:33:19 +02:00
|
|
|
/* auto-load plugins from WEECHAT_EXTRA_LIBDIR if no plugin were loaded */
|
|
|
|
if (!weechat_plugins)
|
|
|
|
{
|
|
|
|
gui_chat_printf (NULL,
|
|
|
|
"Auto-loading plugins from path in "
|
|
|
|
"environment variable WEECHAT_EXTRA_LIBDIR (\"%s\")",
|
|
|
|
getenv ("WEECHAT_EXTRA_LIBDIR"));
|
2018-08-17 19:44:41 +02:00
|
|
|
plugin_auto_load (NULL, 0, 1, 0, 0, NULL);
|
2017-07-05 21:33:19 +02:00
|
|
|
}
|
2017-07-04 22:11:14 +02:00
|
|
|
|
2019-01-11 22:26:16 +01:00
|
|
|
/* load plugins tests */
|
|
|
|
tests_plugins_lib = getenv ("WEECHAT_TESTS_PLUGINS_LIB");
|
|
|
|
ptr_path = (tests_plugins_lib && tests_plugins_lib[0]) ?
|
|
|
|
tests_plugins_lib : tests_plugins_lib_default;
|
|
|
|
printf ("Loading tests on plugins: \"%s\"\n", ptr_path);
|
|
|
|
handle = dlopen (ptr_path, RTLD_GLOBAL | RTLD_NOW);
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "ERROR: unable to load tests on plugins: %s\n",
|
|
|
|
dlerror ());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-01 08:00:39 +02:00
|
|
|
/* display WeeChat version and directories */
|
2017-10-07 16:51:25 +02:00
|
|
|
run_cmd ("/command core version");
|
|
|
|
run_cmd ("/debug dirs");
|
|
|
|
run_cmd ("/debug libs");
|
2017-10-01 08:00:39 +02:00
|
|
|
|
2014-07-20 12:13:25 +02:00
|
|
|
/* run all tests */
|
|
|
|
printf ("\n");
|
|
|
|
rc = CommandLineTestRunner::RunAllTests (argc, argv);
|
|
|
|
|
|
|
|
/* end WeeChat */
|
2019-03-23 20:08:17 +01:00
|
|
|
gui_chat_mute = GUI_CHAT_MUTE_ALL_BUFFERS;
|
2014-07-20 12:13:25 +02:00
|
|
|
weechat_end (&gui_main_end);
|
|
|
|
|
2019-01-11 22:26:16 +01:00
|
|
|
dlclose (handle);
|
|
|
|
|
2014-07-20 12:13:25 +02:00
|
|
|
return rc;
|
|
|
|
}
|