2011-04-07 00:15:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Benjamin Otte <otte@redhat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-04-07 00:15:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2014-08-29 20:33:37 +00:00
|
|
|
#undef GTK_DISABLE_DEPRECATED
|
|
|
|
|
2011-04-07 00:15:42 +00:00
|
|
|
#include <string.h>
|
2011-04-07 22:50:32 +00:00
|
|
|
#include <glib/gstdio.h>
|
2011-04-07 00:15:42 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static char *
|
|
|
|
test_get_reference_file (const char *css_file)
|
|
|
|
{
|
|
|
|
GString *file = g_string_new (NULL);
|
|
|
|
|
|
|
|
if (g_str_has_suffix (css_file, ".css"))
|
|
|
|
g_string_append_len (file, css_file, strlen (css_file) - 4);
|
|
|
|
else
|
|
|
|
g_string_append (file, css_file);
|
|
|
|
|
|
|
|
g_string_append (file, ".ref.css");
|
|
|
|
|
|
|
|
if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
g_string_free (file, TRUE);
|
|
|
|
return g_strdup (css_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (file, FALSE);
|
|
|
|
}
|
|
|
|
|
2011-04-07 23:32:13 +00:00
|
|
|
static char *
|
|
|
|
test_get_errors_file (const char *css_file)
|
|
|
|
{
|
|
|
|
GString *file = g_string_new (NULL);
|
|
|
|
|
|
|
|
if (g_str_has_suffix (css_file, ".css"))
|
|
|
|
g_string_append_len (file, css_file, strlen (css_file) - 4);
|
|
|
|
else
|
|
|
|
g_string_append (file, css_file);
|
|
|
|
|
|
|
|
g_string_append (file, ".errors");
|
|
|
|
|
|
|
|
if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
g_string_free (file, TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (file, FALSE);
|
|
|
|
}
|
|
|
|
|
2011-04-07 00:15:42 +00:00
|
|
|
static char *
|
2011-04-07 22:50:32 +00:00
|
|
|
diff_with_file (const char *file1,
|
|
|
|
char *text,
|
|
|
|
gssize len,
|
|
|
|
GError **error)
|
2011-04-07 00:15:42 +00:00
|
|
|
{
|
2011-04-07 22:50:32 +00:00
|
|
|
const char *command[] = { "diff", "-u", file1, NULL, NULL };
|
|
|
|
char *diff, *tmpfile;
|
|
|
|
int fd;
|
2011-04-07 00:15:42 +00:00
|
|
|
|
2011-04-07 22:50:32 +00:00
|
|
|
diff = NULL;
|
2011-04-07 00:15:42 +00:00
|
|
|
|
2011-04-07 22:50:32 +00:00
|
|
|
if (len < 0)
|
|
|
|
len = strlen (text);
|
|
|
|
|
|
|
|
/* write the text buffer to a temporary file */
|
|
|
|
fd = g_file_open_tmp (NULL, &tmpfile, error);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
2011-04-07 00:15:42 +00:00
|
|
|
|
2011-04-07 22:50:32 +00:00
|
|
|
if (write (fd, text, len) != (int) len)
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"Could not write data to temporary file '%s'", tmpfile);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
close (fd);
|
|
|
|
command[3] = tmpfile;
|
2011-04-07 00:15:42 +00:00
|
|
|
|
|
|
|
/* run diff command */
|
2011-04-07 22:50:32 +00:00
|
|
|
g_spawn_sync (NULL,
|
|
|
|
(char **) command,
|
|
|
|
NULL,
|
|
|
|
G_SPAWN_SEARCH_PATH,
|
|
|
|
NULL, NULL,
|
|
|
|
&diff,
|
|
|
|
NULL, NULL,
|
|
|
|
error);
|
|
|
|
|
|
|
|
done:
|
|
|
|
g_unlink (tmpfile);
|
|
|
|
g_free (tmpfile);
|
2011-04-07 00:15:42 +00:00
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2011-04-08 14:59:17 +00:00
|
|
|
static void
|
|
|
|
append_error_value (GString *string,
|
|
|
|
GType enum_type,
|
|
|
|
guint value)
|
|
|
|
{
|
|
|
|
GEnumClass *enum_class;
|
|
|
|
GEnumValue *enum_value;
|
|
|
|
|
|
|
|
enum_class = g_type_class_ref (enum_type);
|
|
|
|
enum_value = g_enum_get_value (enum_class, value);
|
|
|
|
|
|
|
|
g_string_append (string, enum_value->value_name);
|
|
|
|
|
|
|
|
g_type_class_unref (enum_class);
|
|
|
|
}
|
|
|
|
|
2011-04-07 23:32:13 +00:00
|
|
|
static void
|
|
|
|
parsing_error_cb (GtkCssProvider *provider,
|
2011-06-17 05:39:11 +00:00
|
|
|
GtkCssSection *section,
|
|
|
|
const GError *error,
|
|
|
|
GString *errors)
|
2011-04-07 23:32:13 +00:00
|
|
|
{
|
|
|
|
char *basename;
|
|
|
|
|
2011-06-17 05:39:11 +00:00
|
|
|
basename = g_file_get_basename (gtk_css_section_get_file (section));
|
2011-04-07 23:32:13 +00:00
|
|
|
g_string_append_printf (errors,
|
2011-04-08 14:59:17 +00:00
|
|
|
"%s:%u: error: ",
|
2011-06-17 05:39:11 +00:00
|
|
|
basename, gtk_css_section_get_end_line (section) + 1);
|
2011-04-07 23:32:13 +00:00
|
|
|
g_free (basename);
|
2015-12-27 22:21:50 +00:00
|
|
|
|
2011-04-08 14:59:17 +00:00
|
|
|
if (error->domain == GTK_CSS_PROVIDER_ERROR)
|
|
|
|
append_error_value (errors, GTK_TYPE_CSS_PROVIDER_ERROR, error->code);
|
|
|
|
else
|
|
|
|
g_string_append_printf (errors,
|
|
|
|
"%s %u\n",
|
|
|
|
g_quark_to_string (error->domain),
|
|
|
|
error->code);
|
|
|
|
|
|
|
|
g_string_append_c (errors, '\n');
|
2011-04-07 23:32:13 +00:00
|
|
|
}
|
|
|
|
|
2011-04-07 00:15:42 +00:00
|
|
|
static void
|
2015-12-27 22:21:50 +00:00
|
|
|
parse_css_file (GFile *file, gboolean generate)
|
2011-04-07 00:15:42 +00:00
|
|
|
{
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
char *css, *diff;
|
2011-04-07 23:32:13 +00:00
|
|
|
char *css_file, *reference_file, *errors_file;
|
|
|
|
GString *errors;
|
2011-04-07 00:15:42 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
css_file = g_file_get_path (file);
|
2011-04-07 23:32:13 +00:00
|
|
|
errors = g_string_new ("");
|
2011-04-07 00:15:42 +00:00
|
|
|
|
|
|
|
provider = gtk_css_provider_new ();
|
2011-04-07 23:32:13 +00:00
|
|
|
g_signal_connect (provider,
|
|
|
|
"parsing-error",
|
|
|
|
G_CALLBACK (parsing_error_cb),
|
|
|
|
errors);
|
2016-10-17 15:41:03 +00:00
|
|
|
gtk_css_provider_load_from_path (provider, css_file);
|
2011-04-07 00:15:42 +00:00
|
|
|
|
|
|
|
css = gtk_css_provider_to_string (provider);
|
|
|
|
|
2015-12-27 22:21:50 +00:00
|
|
|
if (generate)
|
|
|
|
{
|
|
|
|
g_print ("%s", css);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-04-07 00:15:42 +00:00
|
|
|
reference_file = test_get_reference_file (css_file);
|
|
|
|
|
2011-04-07 22:50:32 +00:00
|
|
|
diff = diff_with_file (reference_file, css, -1, &error);
|
2011-04-07 00:15:42 +00:00
|
|
|
g_assert_no_error (error);
|
|
|
|
|
2011-04-07 23:32:13 +00:00
|
|
|
if (diff && diff[0])
|
|
|
|
{
|
2011-04-15 14:21:25 +00:00
|
|
|
g_test_message ("Resulting CSS doesn't match reference:\n%s", diff);
|
2011-04-14 11:48:06 +00:00
|
|
|
g_test_fail ();
|
2011-04-07 23:32:13 +00:00
|
|
|
}
|
2011-04-07 00:15:42 +00:00
|
|
|
g_free (reference_file);
|
|
|
|
|
2011-04-07 23:32:13 +00:00
|
|
|
errors_file = test_get_errors_file (css_file);
|
|
|
|
|
|
|
|
if (errors_file)
|
2011-04-07 00:15:42 +00:00
|
|
|
{
|
2011-04-07 23:32:13 +00:00
|
|
|
diff = diff_with_file (errors_file, errors->str, errors->len, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
if (diff && diff[0])
|
|
|
|
{
|
2011-04-15 14:21:25 +00:00
|
|
|
g_test_message ("Errors don't match expected errors:\n%s", diff);
|
2011-04-14 11:48:06 +00:00
|
|
|
g_test_fail ();
|
2011-04-07 23:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (errors->str[0])
|
|
|
|
{
|
|
|
|
g_test_message ("Unexpected errors:\n%s", errors->str);
|
2011-04-14 11:48:06 +00:00
|
|
|
g_test_fail ();
|
2011-04-07 00:15:42 +00:00
|
|
|
}
|
|
|
|
|
2011-04-07 23:32:13 +00:00
|
|
|
g_free (errors_file);
|
|
|
|
g_string_free (errors, TRUE);
|
|
|
|
|
2011-04-07 00:15:42 +00:00
|
|
|
g_free (diff);
|
2015-12-27 22:21:50 +00:00
|
|
|
|
|
|
|
out:
|
2011-04-07 00:15:42 +00:00
|
|
|
g_free (css_file);
|
2015-12-27 22:21:50 +00:00
|
|
|
g_free (css);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_css_file (GFile *file)
|
|
|
|
{
|
|
|
|
parse_css_file (file, FALSE);
|
2011-04-07 00:15:42 +00:00
|
|
|
}
|
|
|
|
|
2011-04-09 23:53:43 +00:00
|
|
|
static void
|
|
|
|
add_test_for_file (GFile *file)
|
|
|
|
{
|
2014-02-23 14:40:09 +00:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = g_file_get_path (file);
|
|
|
|
|
|
|
|
g_test_add_vtable (path,
|
2011-04-09 23:53:43 +00:00
|
|
|
0,
|
|
|
|
g_object_ref (file),
|
|
|
|
NULL,
|
|
|
|
(GTestFixtureFunc) test_css_file,
|
|
|
|
(GTestFixtureFunc) g_object_unref);
|
2014-02-23 14:40:09 +00:00
|
|
|
|
|
|
|
g_free (path);
|
2011-04-09 23:53:43 +00:00
|
|
|
}
|
|
|
|
|
2011-04-11 15:57:50 +00:00
|
|
|
static int
|
|
|
|
compare_files (gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
GFile *file1 = G_FILE (a);
|
|
|
|
GFile *file2 = G_FILE (b);
|
|
|
|
char *path1, *path2;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
path1 = g_file_get_path (file1);
|
|
|
|
path2 = g_file_get_path (file2);
|
|
|
|
|
|
|
|
result = strcmp (path1, path2);
|
|
|
|
|
|
|
|
g_free (path1);
|
|
|
|
g_free (path2);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-09 23:53:43 +00:00
|
|
|
static void
|
|
|
|
add_tests_for_files_in_directory (GFile *dir)
|
2011-04-07 00:15:42 +00:00
|
|
|
{
|
|
|
|
GFileEnumerator *enumerator;
|
|
|
|
GFileInfo *info;
|
2011-04-11 15:57:50 +00:00
|
|
|
GList *files;
|
2011-04-09 23:53:43 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
|
|
|
|
g_assert_no_error (error);
|
2011-04-11 15:57:50 +00:00
|
|
|
files = NULL;
|
2011-04-09 23:53:43 +00:00
|
|
|
|
|
|
|
while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
|
|
|
|
{
|
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
filename = g_file_info_get_name (info);
|
|
|
|
|
|
|
|
if (!g_str_has_suffix (filename, ".css") ||
|
|
|
|
g_str_has_suffix (filename, ".out.css") ||
|
|
|
|
g_str_has_suffix (filename, ".ref.css"))
|
|
|
|
{
|
|
|
|
g_object_unref (info);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-04-11 15:57:50 +00:00
|
|
|
files = g_list_prepend (files, g_file_get_child (dir, filename));
|
2011-04-09 23:53:43 +00:00
|
|
|
|
|
|
|
g_object_unref (info);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
g_object_unref (enumerator);
|
2011-04-11 15:57:50 +00:00
|
|
|
|
|
|
|
files = g_list_sort (files, compare_files);
|
|
|
|
g_list_foreach (files, (GFunc) add_test_for_file, NULL);
|
|
|
|
g_list_free_full (files, g_object_unref);
|
2011-04-09 23:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2011-04-07 00:15:42 +00:00
|
|
|
gtk_test_init (&argc, &argv);
|
|
|
|
|
2011-04-09 23:53:43 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
const char *basedir;
|
|
|
|
GFile *dir;
|
|
|
|
|
2013-05-30 01:37:05 +00:00
|
|
|
basedir = g_test_get_dir (G_TEST_DIST);
|
2011-04-09 23:53:43 +00:00
|
|
|
dir = g_file_new_for_path (basedir);
|
|
|
|
add_tests_for_files_in_directory (dir);
|
|
|
|
|
|
|
|
g_object_unref (dir);
|
|
|
|
}
|
2015-12-27 22:21:50 +00:00
|
|
|
else if (strcmp (argv[1], "--generate") == 0)
|
|
|
|
{
|
|
|
|
if (argc >= 3)
|
|
|
|
{
|
|
|
|
GFile *file = g_file_new_for_commandline_arg (argv[2]);
|
|
|
|
|
|
|
|
parse_css_file (file, TRUE);
|
|
|
|
|
|
|
|
g_object_unref (file);
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 00:15:42 +00:00
|
|
|
else
|
|
|
|
{
|
2011-04-09 23:53:43 +00:00
|
|
|
guint i;
|
2011-04-07 00:15:42 +00:00
|
|
|
|
2011-04-09 23:53:43 +00:00
|
|
|
for (i = 1; i < argc; i++)
|
2011-04-07 00:15:42 +00:00
|
|
|
{
|
2011-04-09 23:53:43 +00:00
|
|
|
GFile *file = g_file_new_for_commandline_arg (argv[i]);
|
2011-04-07 00:15:42 +00:00
|
|
|
|
2011-04-09 23:53:43 +00:00
|
|
|
add_test_for_file (file);
|
2011-04-07 00:15:42 +00:00
|
|
|
|
2011-04-09 23:53:43 +00:00
|
|
|
g_object_unref (file);
|
|
|
|
}
|
2011-04-07 00:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|
|
|
|
|