Commit e808fb42 authored by Matthias Clasen's avatar Matthias Clasen

testsuite: Stop requiring diff

Unify the many copies of diff_with_file in one source
file, and patch it to detect diff at runtime and fall
back to a simple strcmp if we don't have it. Make all
tests use this new testutils.c, and stop requiring
diff for building the tests.

This should let us allow to build on Windows with the
default value for -Dbuild-tests.
parent 6c54abec
Pipeline #249239 passed with stages
in 16 minutes and 20 seconds
......@@ -13,6 +13,7 @@ testdatadir = join_paths(installed_test_datadir, 'css')
test_change = executable(
'test-css-change',
'test-css-change.c',
'../../testutils.c',
c_args: common_cflags,
dependencies: libgtk_dep,
install: get_option('install-tests'),
......
......@@ -21,6 +21,7 @@
#include <string.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "testsuite/testutils.h"
#ifdef G_OS_WIN32
# include <io.h>
......@@ -50,54 +51,6 @@ test_get_other_file (const char *ui_file, const char *extension)
return g_string_free (file, FALSE);
}
static char *
diff_with_file (const char *file1,
char *text,
gssize len,
GError **error)
{
const char *command[] = { "diff", "-u", file1, NULL, NULL };
char *diff, *tmpfile;
int fd;
diff = NULL;
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;
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;
/* run diff command */
g_spawn_sync (NULL,
(char **) command,
NULL,
G_SPAWN_SEARCH_PATH,
NULL, NULL,
&diff,
NULL, NULL,
error);
done:
g_unlink (tmpfile);
g_free (tmpfile);
return diff;
}
static void
style_context_changed (GtkWidget *window, const char **output)
{
......
......@@ -10,7 +10,7 @@ nodetest_env.set('G_ENABLE_DIAGNOSTIC', '0')
testexecdir = join_paths(installed_test_bindir, 'css', 'nodes')
testdatadir = join_paths(installed_test_datadir, 'css')
test_nodes = executable('test-css-nodes', 'test-css-nodes.c',
test_nodes = executable('test-css-nodes', 'test-css-nodes.c', '../../testutils.c',
c_args: common_cflags,
install: get_option('install-tests'),
install_dir: testexecdir,
......
......@@ -21,6 +21,7 @@
#include <string.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "testsuite/testutils.h"
#ifdef G_OS_WIN32
# include <io.h>
......@@ -48,54 +49,6 @@ test_get_reference_file (const char *ui_file)
return g_string_free (file, FALSE);
}
static char *
diff_with_file (const char *file1,
char *text,
gssize len,
GError **error)
{
const char *command[] = { "diff", "-u", file1, NULL, NULL };
char *diff, *tmpfile;
int fd;
diff = NULL;
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;
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;
/* run diff command */
g_spawn_sync (NULL,
(char **) command,
NULL,
G_SPAWN_SEARCH_PATH,
NULL, NULL,
&diff,
NULL, NULL,
error);
done:
g_unlink (tmpfile);
g_free (tmpfile);
return diff;
}
static void
style_context_changed (GtkWidget *window, const char **output)
{
......
testexecdir = join_paths(installed_test_bindir, 'css', 'parser')
testdatadir = join_paths(installed_test_datadir, 'css')
test_parser = executable('test-css-parser', 'test-css-parser.c',
test_parser = executable('test-css-parser', 'test-css-parser.c', '../../testutils.c',
c_args: common_cflags,
install: get_option('install-tests'),
install_dir: testexecdir,
......
......@@ -25,6 +25,7 @@
#include <string.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "testsuite/testutils.h"
#ifdef G_OS_WIN32
# include <io.h>
......@@ -72,51 +73,6 @@ test_get_errors_file (const char *css_file)
return g_string_free (file, FALSE);
}
static GBytes *
diff_with_file (const char *file1,
char *text,
gssize len,
GError **error)
{
GSubprocess *process;
GBytes *input, *output;
process = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE,
error,
"diff", "-u", file1, "-", NULL);
if (process == NULL)
return NULL;
input = g_bytes_new_static (text, len >= 0 ? len : strlen (text));
if (!g_subprocess_communicate (process,
input,
NULL,
&output,
NULL,
error))
{
g_object_unref (process);
g_bytes_unref (input);
return NULL;
}
if (!g_subprocess_get_successful (process) &&
/* this is the condition when the files differ */
!(g_subprocess_get_if_exited (process) && g_subprocess_get_exit_status (process) == 1))
{
g_clear_pointer (&output, g_bytes_unref);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"The `diff' process exited with error status %d",
g_subprocess_get_exit_status (process));
}
g_object_unref (process);
g_bytes_unref (input);
return output;
}
static void
append_error_value (GString *string,
GType enum_type,
......@@ -167,7 +123,7 @@ parse_css_file (GFile *file, gboolean generate)
GtkCssProvider *provider;
char *css, *css_file, *reference_file, *errors_file;
GString *errors;
GBytes *diff;
char *diff;
GError *error = NULL;
css_file = g_file_get_path (file);
......@@ -193,14 +149,13 @@ parse_css_file (GFile *file, gboolean generate)
diff = diff_with_file (reference_file, css, -1, &error);
g_assert_no_error (error);
if (diff && g_bytes_get_size (diff) > 0)
if (diff && diff[0])
{
g_test_message ("Resulting CSS doesn't match reference:\n%s",
(const char *) g_bytes_get_data (diff, NULL));
g_test_message ("Resulting CSS doesn't match reference:\n%s", diff);
g_test_fail ();
}
g_free (reference_file);
g_clear_pointer (&diff, g_bytes_unref);
g_free (diff);
errors_file = test_get_errors_file (css_file);
......@@ -209,13 +164,12 @@ parse_css_file (GFile *file, gboolean generate)
diff = diff_with_file (errors_file, errors->str, errors->len, &error);
g_assert_no_error (error);
if (diff && g_bytes_get_size (diff) > 0)
if (diff && diff[0])
{
g_test_message ("Errors don't match expected errors:\n%s",
(const char *) g_bytes_get_data (diff, NULL));
g_test_message ("Errors don't match expected errors:\n%s", diff);
g_test_fail ();
}
g_clear_pointer (&diff, g_bytes_unref);
g_free (diff);
}
else if (errors->str[0])
{
......
......@@ -19,6 +19,7 @@ cssresources = gnome.compile_resources(
test_style = executable(
'test-css-style',
'test-css-style.c',
'../../testutils.c',
cssresources,
c_args: common_cflags,
dependencies: libgtk_dep,
......
......@@ -21,6 +21,7 @@
#include <string.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "testsuite/testutils.h"
#ifdef G_OS_WIN32
# include <io.h>
......@@ -50,51 +51,6 @@ test_get_other_file (const char *ui_file, const char *extension)
return g_string_free (file, FALSE);
}
static GBytes *
diff_with_file (const char *file1,
char *text,
gssize len,
GError **error)
{
GSubprocess *process;
GBytes *input, *output;
process = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE,
error,
"diff", "-u", file1, "-", NULL);
if (process == NULL)
return NULL;
input = g_bytes_new_static (text, len >= 0 ? len : strlen (text));
if (!g_subprocess_communicate (process,
input,
NULL,
&output,
NULL,
error))
{
g_object_unref (process);
g_bytes_unref (input);
return NULL;
}
if (!g_subprocess_get_successful (process) &&
/* this is the condition when the files differ */
!(g_subprocess_get_if_exited (process) && g_subprocess_get_exit_status (process) == 1))
{
g_clear_pointer (&output, g_bytes_unref);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"The `diff' process exited with error status %d",
g_subprocess_get_exit_status (process));
}
g_object_unref (process);
g_bytes_unref (input);
return output;
}
static char *
fixup_style_differences (const char *str)
{
......@@ -133,7 +89,7 @@ load_ui_file (GFile *file, gboolean generate)
GtkBuilder *builder;
GtkWidget *window;
char *output;
GBytes *diff;
char *diff;
char *ui_file, *css_file, *reference_file;
GtkCssProvider *provider;
GError *error = NULL;
......@@ -174,13 +130,13 @@ load_ui_file (GFile *file, gboolean generate)
diff = diff_with_file (reference_file, output, -1, &error);
g_assert_no_error (error);
if (diff && g_bytes_get_size (diff) > 0)
if (diff && diff[0])
{
g_test_message ("Resulting output doesn't match reference:\n%s", (const char *) g_bytes_get_data (diff, NULL));
g_test_message ("Resulting output doesn't match reference:\n%s", diff);
g_test_fail ();
}
g_free (reference_file);
g_clear_pointer (&diff, g_bytes_unref);
g_free (diff);
out:
gtk_style_context_remove_provider_for_display (gdk_display_get_default (),
......
......@@ -250,7 +250,7 @@ focus_chain_tests = [
focus_chain = executable(
'test-focus-chain',
sources: ['test-focus-chain.c'],
sources: ['test-focus-chain.c', '../testutils.c'],
dependencies: libgtk_dep,
c_args: common_cflags,
install: get_option('install-tests'),
......
......@@ -21,6 +21,7 @@
#include <string.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "testsuite/testutils.h"
#ifdef G_OS_WIN32
# include <io.h>
......@@ -38,54 +39,6 @@ struct {
{ GTK_DIR_RIGHT, "right" }
};
static char *
diff_with_file (const char *file1,
char *text,
gssize len,
GError **error)
{
const char *command[] = { "diff", "-u", file1, NULL, NULL };
char *diff, *tmpfile;
int fd;
diff = NULL;
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;
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;
/* run diff command */
g_spawn_sync (NULL,
(char **) command,
NULL,
G_SPAWN_SEARCH_PATH,
NULL, NULL,
&diff,
NULL, NULL,
error);
done:
g_unlink (tmpfile);
g_free (tmpfile);
return diff;
}
static void
check_focus_states (GtkWidget *focus_widget)
{
......
......@@ -2,10 +2,6 @@ gtk_libexecdir = join_paths(gtk_prefix, get_option('libexecdir'))
installed_test_bindir = join_paths(gtk_libexecdir, 'installed-tests', 'gtk-4.0')
installed_test_datadir = join_paths(gtk_datadir, 'installed-tests', 'gtk-4.0')
# We call diff in various tests, so we need to check it's available,
# otherwise we're going to have failures down the line
diff = find_program('diff', required: true)
common_env = [
'GIO_USE_VOLUME_MONITOR=unix',
'GSETTINGS_BACKEND=memory',
......
/*
* Copyright © 2021 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Matthias Clasen <mclasen@redhat.com>
*/
#include <glib.h>
#include <glib/gstdio.h>
#include <unistd.h>
#include "testsuite/testutils.h"
char *
diff_with_file (const char *file1,
char *text,
gssize len,
GError **error)
{
const char *command[] = { "diff", "-u", file1, NULL, NULL };
char *diff, *tmpfile;
int fd;
diff = NULL;
if (g_find_program_in_path ("diff"))
{
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;
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;
/* run diff command */
g_spawn_sync (NULL,
(char **) command,
NULL,
G_SPAWN_SEARCH_PATH,
NULL, NULL,
&diff,
NULL, NULL,
error);
done:
g_unlink (tmpfile);
g_free (tmpfile);
}
else
{
char *buf1;
gsize len1;
if (!g_file_get_contents (file1, &buf1, &len1, error))
return NULL;
if ((len != -1 && len != len1) ||
strncmp (text, buf1, len1) != 0)
diff = g_strdup ("Files differ.\n");
g_free (buf1);
}
return diff;
}
#pragma once
char * diff_with_file (const char *file1,
char *text,
gssize len,
GError **error);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment