<commit_before>/*
 * Copyright (C) 2010, Nokia <<EMAIL>>
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <glib.h>
#include <glib/gstdio.h>

/* Special case, the monitor header is not normally exported */
#include <libtracker-miner/tracker-monitor.h>

#define ALARM_TIMEOUT 10 /* seconds */

static TrackerMonitor *monitor;
static GFile *file_for_monitor;
static GFile *file_for_tmp;
static GFile *file_for_events;
static GFile *file_for_move_in;
static GFile *file_for_move_out;
static gchar *path_for_monitor;
static gchar *path_for_events;
static gchar *path_for_move_in;
static gchar *path_for_move_out;
static GHashTable *events;
static GMainLoop *main_loop;

static void
signal_handler (int signo)
{
	if (g_strsignal (signo)) {
		g_print ("\n");
		g_print ("Received signal:%d->'%s'\n",
		         signo,
		         g_strsignal (signo));
	}

	g_print ("If we got this alarm, we likely didn't get the event we expected in time\n");
	g_assert_cmpint (signo, !=, SIGALRM);
}

static void
initialize_signal_handler (void)
{
#ifndef G_OS_WIN32
	struct sigaction act;
	sigset_t         empty_mask;

	sigemptyset (&empty_mask);
	act.sa_handler = signal_handler;
	act.sa_mask    = empty_mask;
	act.sa_flags   = 0;

	sigaction (SIGALRM, &act, NULL);
#endif /* G_OS_WIN32 */
}

static void
events_wait (void)
{
	/* Set alarm in case we don't get the event */
	alarm (ALARM_TIMEOUT);
	g_debug ("***** Setting ALARM");

	g_assert (main_loop == NULL);

	main_loop = g_main_loop_new (NULL, FALSE);
	g_assert (main_loop != NULL);

	g_main_loop_run (main_loop);
	g_main_loop_unref (main_loop);
	main_loop = NULL;
}

static void
events_received (void)
{
	/* Cancel alarm */
	alarm (0);
	g_debug ("***** Cancelled ALARM");

	g_assert (main_loop != NULL);
	g_main_loop_quit (main_loop);
}

static void
create_file (const gchar *filename,
             const gchar *contents)
{
	FILE *file;
	size_t length;

	g_assert (filename != NULL);
	g_assert (contents != NULL);

	file = g_fopen (filename, "wb");
	g_assert (file != NULL);

	length = strlen (contents);
	g_assert_cmpint (fwrite (contents, 1, length, file), >=, length);
	g_assert_cmpint (fflush (file), ==, 0);
	g_assert_cmpint (fclose (file), !=, EOF);
}

static void
test_monitor_enabled (void)
{
	/* Test general API with monitors enabled first */
	tracker_monitor_set_enabled (monitor, TRUE);
	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, TRUE);

	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE); /* Test double add on purpose */
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);

	tracker_monitor_add (monitor, file_for_monitor);
	tracker_monitor_add (monitor, file_for_tmp);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 2);
	g_assert_cmpint (tracker_monitor_remove_recursively (monitor, file_for_tmp), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);

	/* Test general API with monitors disabled first */
	tracker_monitor_set_enabled (monitor, FALSE);
	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, FALSE);

	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);

	tracker_monitor_add (monitor, file_for_monitor);
	tracker_monitor_add (monitor, file_for_tmp);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 2);
	g_assert_cmpint (tracker_monitor_remove_recursively (monitor, file_for_tmp), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
}

static void
test_monitor_file_events (void)
{
	/* Set up environment */
	g_assert_cmpint (g_hash_table_size (events), ==, 0);
	tracker_monitor_set_enabled (monitor, TRUE);
	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);

	/* Test CREATED */
	create_file (path_for_events, "foo");
	events_wait ();

	/* Test UPDATE */
	create_file (path_for_events, "bar");
	events_wait ();

	/* Test MOVE to (monitored dir) */
	g_assert_cmpint (g_rename (path_for_events, path_for_move_in), ==, 0);
	events_wait ();

	/* Test MOVE back (monitored dir) */
	g_assert_cmpint (g_rename (path_for_move_in, path_for_events), ==, 0);
	events_wait ();

	/* Test MOVE to (not monitored dir) */
	g_assert_cmpint (g_rename (path_for_events, path_for_move_out), ==, 0);
	events_wait ();

	/* Test MOVE back (not monitored dir) */
	g_assert_cmpint (g_rename (path_for_move_out, path_for_events), ==, 0);
	events_wait ();

	/* TODO: Add more complex MOVE operations */

	/* Test DELETED */
	g_assert_cmpint (g_unlink (path_for_events), ==, 0);
	events_wait ();

	/* Clean up */
	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
}<commit_msg>libtracker-miner tests: Added helper logs in monitor tester<commit_after>/*
 * Copyright (C) 2010, Nokia <<EMAIL>>
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <glib.h>
#include <glib/gstdio.h>

/* Special case, the monitor header is not normally exported */
#include <libtracker-miner/tracker-monitor.h>

#define ALARM_TIMEOUT 10 /* seconds */

static TrackerMonitor *monitor;
static GFile *file_for_monitor;
static GFile *file_for_tmp;
static GFile *file_for_events;
static GFile *file_for_move_in;
static GFile *file_for_move_out;
static gchar *path_for_monitor;
static gchar *path_for_events;
static gchar *path_for_move_in;
static gchar *path_for_move_out;
static GHashTable *events;
static GMainLoop *main_loop;

static void
signal_handler (int signo)
{
	if (g_strsignal (signo)) {
		g_print ("\n");
		g_print ("Received signal:%d->'%s'\n",
		         signo,
		         g_strsignal (signo));
	}

	g_print ("If we got this alarm, we likely didn't get the event we expected in time\n");
	g_assert_cmpint (signo, !=, SIGALRM);
}

static void
initialize_signal_handler (void)
{
#ifndef G_OS_WIN32
	struct sigaction act;
	sigset_t         empty_mask;

	sigemptyset (&empty_mask);
	act.sa_handler = signal_handler;
	act.sa_mask    = empty_mask;
	act.sa_flags   = 0;

	sigaction (SIGALRM, &act, NULL);
#endif /* G_OS_WIN32 */
}

static void
events_wait (void)
{
	/* Set alarm in case we don't get the event */
	alarm (ALARM_TIMEOUT);
	g_debug ("***** Setting ALARM");

	g_assert (main_loop == NULL);

	main_loop = g_main_loop_new (NULL, FALSE);
	g_assert (main_loop != NULL);

	g_main_loop_run (main_loop);
	g_main_loop_unref (main_loop);
	main_loop = NULL;
}

static void
events_received (void)
{
	/* Cancel alarm */
	alarm (0);
	g_debug ("***** Cancelled ALARM");

	g_assert (main_loop != NULL);
	g_main_loop_quit (main_loop);
}

static void
create_file (const gchar *filename,
             const gchar *contents)
{
	FILE *file;
	size_t length;

	g_assert (filename != NULL);
	g_assert (contents != NULL);

	file = g_fopen (filename, "wb");
	g_assert (file != NULL);

	length = strlen (contents);
	g_assert_cmpint (fwrite (contents, 1, length, file), >=, length);
	g_assert_cmpint (fflush (file), ==, 0);
	g_assert_cmpint (fclose (file), !=, EOF);
}

static void
test_monitor_enabled (void)
{
	/* Test general API with monitors enabled first */
	tracker_monitor_set_enabled (monitor, TRUE);
	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, TRUE);

	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE); /* Test double add on purpose */
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);

	tracker_monitor_add (monitor, file_for_monitor);
	tracker_monitor_add (monitor, file_for_tmp);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 2);
	g_assert_cmpint (tracker_monitor_remove_recursively (monitor, file_for_tmp), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);

	/* Test general API with monitors disabled first */
	tracker_monitor_set_enabled (monitor, FALSE);
	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, FALSE);

	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);

	tracker_monitor_add (monitor, file_for_monitor);
	tracker_monitor_add (monitor, file_for_tmp);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 2);
	g_assert_cmpint (tracker_monitor_remove_recursively (monitor, file_for_tmp), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
}

static void
test_monitor_file_events (void)
{
	/* Set up environment */
	g_assert_cmpint (g_hash_table_size (events), ==, 0);
	tracker_monitor_set_enabled (monitor, TRUE);
	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);

	/* Test CREATED */
	g_debug (">> Testing CREATE");
	create_file (path_for_events, "foo");
	events_wait ();

	/* Test UPDATE */
	g_debug (">> Testing UPDATE");
	create_file (path_for_events, "bar");
	events_wait ();

	/* Test MOVE to (monitored dir) */
	g_debug (">> Testing MOVE to monitored dir");
	g_assert_cmpint (g_rename (path_for_events, path_for_move_in), ==, 0);
	events_wait ();

	/* Test MOVE back (monitored dir) */
	g_debug (">> Testing MOVE from monitored dir");
	g_assert_cmpint (g_rename (path_for_move_in, path_for_events), ==, 0);
	events_wait ();

	/* Test MOVE to (not monitored dir) */
	g_debug (">> Testing MOVE to NOT monitored dir");
	g_assert_cmpint (g_rename (path_for_events, path_for_move_out), ==, 0);
	events_wait ();

	/* Test MOVE back (not monitored dir) */
	g_debug (">> Testing MOVE from NOT monitored dir");
	g_assert_cmpint (g_rename (path_for_move_out, path_for_events), ==, 0);
	events_wait ();

	/* TODO: Add more complex MOVE operations */

	/* Test DELETED */
	g_assert_cmpint (g_unlink (path_for_events), ==, 0);
	events_wait ();

	/* Clean up */
	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
}
{'fraction_non_alphanumeric': 0.12089810017271158, 'fraction_numerical': 0.006188831318364997, 'mean_word_length': 5.749392909179213, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5003219', 'n_tokens_mistral': 5064, 'n_tokens_neox': 4871, 'n_words': 1245}