LCOV - code coverage report
Current view: top level - egg - test-file-tracker.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 112 112
Test Date: 2024-12-15 20:37:51 Functions: 100.0 % 10 10

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-file-tracker.c: File tracker tests
       3              : 
       4              :    Copyright (C) 2007 Stefan Walter
       5              : 
       6              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       7              :    modify it under the terms of the GNU Library General Public License as
       8              :    published by the Free Software Foundation; either version 2 of the
       9              :    License, or (at your option) any later version.
      10              : 
      11              :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      12              :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :    Library General Public License for more details.
      15              : 
      16              :    You should have received a copy of the GNU Library General Public
      17              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      18              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include <stdlib.h>
      26              : #include <stdio.h>
      27              : #include <string.h>
      28              : #include <unistd.h>
      29              : 
      30              : #include "egg/egg-file-tracker.h"
      31              : 
      32              : #include <glib/gstdio.h>
      33              : 
      34              : #define DATA "test-data"
      35              : #define SUBDIR "test-subdir"
      36              : #define WILDCARD "*.woo?"
      37              : 
      38              : typedef struct {
      39              :         EggFileTracker *the_tracker;
      40              :         gchar *test_dir;
      41              :         gchar *test_file;
      42              :         guint n_files_added;
      43              :         gchar* last_file_added;
      44              :         guint n_files_changed;
      45              :         gchar* last_file_changed;
      46              :         guint n_files_removed;
      47              :         gchar* last_file_removed;
      48              : } Test;
      49              : 
      50              : static void
      51            1 : file_added (EggFileTracker *tracker,
      52              :             const gchar *path,
      53              :             gpointer user_data)
      54              : {
      55            1 :         Test *test = user_data;
      56              : 
      57            1 :         g_assert ("should be a non-null path" && path != NULL);
      58              : 
      59            1 :         ++test->n_files_added;
      60            1 :         g_free (test->last_file_added);
      61            1 :         test->last_file_added = g_strdup (path);
      62            1 : }
      63              : 
      64              : static void
      65            2 : file_changed (EggFileTracker *tracker,
      66              :               const gchar *path,
      67              :               gpointer user_data)
      68              : {
      69            2 :         Test *test = user_data;
      70              : 
      71            2 :         g_assert ("should be a non-null path" && path != NULL);
      72              : 
      73            2 :         ++test->n_files_changed;
      74            2 :         g_free (test->last_file_changed);
      75            2 :         test->last_file_changed = g_strdup (path);
      76            2 : }
      77              : 
      78              : static void
      79            1 : file_removed (EggFileTracker *tracker,
      80              :               const gchar *path,
      81              :               gpointer user_data)
      82              : {
      83            1 :         Test *test = user_data;
      84              : 
      85            1 :         g_assert ("should be a non-null path" && path != NULL);
      86              : 
      87            1 :         ++test->n_files_removed;
      88            1 :         g_free (test->last_file_removed);
      89            1 :         test->last_file_removed = g_strdup (path);
      90            1 : }
      91              : 
      92              : static void
      93            7 : file_reset_stats (Test *test)
      94              : {
      95            7 :         g_free (test->last_file_removed);
      96            7 :         g_free (test->last_file_added);
      97            7 :         g_free (test->last_file_changed);
      98            7 :         test->last_file_removed = test->last_file_added = test->last_file_changed = NULL;
      99            7 :         test->n_files_added = test->n_files_changed = test->n_files_removed = 0;
     100            7 : }
     101              : 
     102              : static void
     103            3 : setup (Test *test, gconstpointer unused)
     104              : {
     105              :         /* Make a test directory */
     106            3 :         test->test_dir = g_build_filename ("/tmp", SUBDIR, NULL);
     107              : 
     108            3 :         test->the_tracker = egg_file_tracker_new (test->test_dir, WILDCARD, NULL);
     109            3 :         g_signal_connect (test->the_tracker, "file-added", G_CALLBACK (file_added), test);
     110            3 :         g_signal_connect (test->the_tracker, "file-removed", G_CALLBACK (file_removed), test);
     111            3 :         g_signal_connect (test->the_tracker, "file-changed", G_CALLBACK (file_changed), test);
     112              : 
     113              :         /* Mtime must change so wait between tests */
     114            3 :         sleep (1);
     115              : 
     116            3 :         test->test_file = g_build_filename (test->test_dir, "my-file.woof", NULL);
     117            3 :         g_unlink (test->test_file);
     118            3 : }
     119              : 
     120              : static void
     121            3 : teardown (Test *test, gconstpointer unused)
     122              : {
     123            3 :         file_reset_stats (test);
     124            3 :         g_object_unref (test->the_tracker);
     125            3 :         g_free (test->test_dir);
     126            3 :         g_free (test->test_file);
     127            3 : }
     128              : 
     129              : static void
     130            1 : test_file_watch (Test *test, gconstpointer unused)
     131              : {
     132              :         /* A watch for an non-existant directory, should have no responses */
     133            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     134              : 
     135            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     136            1 :         g_assert_cmpint (0, ==, test->n_files_changed);
     137            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     138              : 
     139            1 :         g_mkdir_with_parents (test->test_dir, 0700);
     140              : 
     141              :         /* Should still have no responses even though it exists */
     142            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     143              : 
     144            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     145            1 :         g_assert_cmpint (0, ==, test->n_files_changed);
     146            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     147            1 : }
     148              : 
     149              : static void
     150            1 : test_watch_file (Test *test, gconstpointer unused)
     151              : {
     152              :         gboolean ret;
     153              : 
     154              :         /* Make sure things are clean */
     155            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     156              : 
     157            1 :         test->n_files_added = test->n_files_changed = test->n_files_removed = 0;
     158            1 :         test->last_file_added = test->last_file_changed = test->last_file_removed = 0;
     159              : 
     160            1 :         ret = g_file_set_contents (test->test_file, DATA, strlen (DATA), NULL);
     161            1 :         g_assert (ret);
     162              : 
     163              :         /* Now make sure that file is located */
     164            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     165              : 
     166            1 :         g_assert_cmpint (1, ==, test->n_files_added);
     167            1 :         g_assert_cmpint (0, ==, test->n_files_changed);
     168            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     169              : 
     170              :         /* The added one should match our file */
     171            1 :         g_assert_cmpstr (test->last_file_added, ==, test->test_file);
     172              : 
     173            1 :         file_reset_stats (test);
     174            1 :         sleep (1);
     175              : 
     176              :         /* Shouldn't find the file again */
     177            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     178            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     179            1 :         g_assert_cmpint (0, ==, test->n_files_changed);
     180            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     181              : 
     182              :         /* But we should find the file if forced to */
     183            1 :         egg_file_tracker_refresh (test->the_tracker, TRUE);
     184            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     185            1 :         g_assert_cmpint (1, ==, test->n_files_changed);
     186            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     187            1 :         g_assert_cmpstr (test->last_file_changed, ==, test->test_file);
     188              : 
     189            1 :         file_reset_stats (test);
     190              : 
     191            1 :         ret = g_file_set_contents (test->test_file, DATA, strlen (DATA), NULL);
     192            1 :         g_assert (ret);
     193              : 
     194              :         /* File was updated */
     195            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     196            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     197            1 :         g_assert_cmpint (1, ==, test->n_files_changed);
     198            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     199            1 :         g_assert_cmpstr (test->last_file_changed, ==, test->test_file);
     200              : 
     201            1 :         file_reset_stats (test);
     202            1 :         g_unlink (test->test_file);
     203              : 
     204              :         /* Now file should be removed */
     205            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     206              : 
     207            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     208            1 :         g_assert_cmpint (0, ==, test->n_files_changed);
     209            1 :         g_assert_cmpint (1, ==, test->n_files_removed);
     210            1 :         g_assert_cmpstr (test->last_file_removed, ==, test->test_file);
     211            1 : }
     212              : 
     213              : static void
     214            1 : test_nomatch (Test *test, gconstpointer unused)
     215              : {
     216            1 :         gchar *file = g_build_filename (test->test_dir, "my-file.toot", NULL);
     217              :         gboolean ret;
     218              : 
     219              :         /* Mtime must change so wait between tests */
     220            1 :         sleep (1);
     221              : 
     222            1 :         ret = g_file_set_contents (file, DATA, strlen (DATA), NULL);
     223            1 :         g_assert (ret);
     224              : 
     225            1 :         file_reset_stats (test);
     226              : 
     227              :         /* Now make sure that file is not located */
     228            1 :         egg_file_tracker_refresh (test->the_tracker, FALSE);
     229              : 
     230            1 :         g_assert_cmpint (0, ==, test->n_files_added);
     231            1 :         g_assert_cmpint (0, ==, test->n_files_changed);
     232            1 :         g_assert_cmpint (0, ==, test->n_files_removed);
     233              : 
     234            1 :         g_unlink (file);
     235            1 :         g_free (file);
     236            1 : }
     237              : 
     238              : int
     239            1 : main (int argc, char **argv)
     240              : {
     241              : #if !GLIB_CHECK_VERSION(2,35,0)
     242              :         g_type_init ();
     243              : #endif
     244            1 :         g_test_init (&argc, &argv, NULL);
     245              : 
     246            1 :         g_test_add ("/egg/file-tracker/file_watch", Test, NULL, setup, test_file_watch, teardown);
     247            1 :         g_test_add ("/egg/file-tracker/watch_file", Test, NULL, setup, test_watch_file, teardown);
     248            1 :         g_test_add ("/egg/file-tracker/nomatch", Test, NULL, setup, test_nomatch, teardown);
     249              : 
     250            1 :         return g_test_run ();
     251              : }
        

Generated by: LCOV version 2.0-1