Merge branch 'w32-tests' into 'main'

Various win32 test fixes

See merge request GNOME/glib!2952
This commit is contained in:
Philip Withnall 2022-10-17 09:53:44 +00:00
commit 292c117b98
6 changed files with 133 additions and 102 deletions

View File

@ -2441,6 +2441,26 @@ set_symlink (char *filename,
}
#endif
#if defined (HAVE_UTIMES) || defined (HAVE_UTIMENSAT) || defined(G_OS_WIN32)
static int
lazy_stat (const char *filename,
GStatBuf *statbuf,
gboolean *called_stat)
{
int res;
if (*called_stat)
return 0;
res = g_stat (filename, statbuf);
if (res == 0)
*called_stat = TRUE;
return res;
}
#endif
#if defined (G_OS_WIN32)
/* From
* https://support.microsoft.com/en-ca/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime
@ -2546,6 +2566,8 @@ set_mtime_atime (const char *filename,
FILETIME *p_mtime = NULL;
FILETIME *p_atime = NULL;
DWORD gle;
GStatBuf statbuf;
gboolean got_stat = FALSE;
/* ATIME */
if (atime_value)
@ -2554,30 +2576,44 @@ set_mtime_atime (const char *filename,
return FALSE;
val_usec = 0;
val_nsec = 0;
if (atime_usec_value &&
!get_uint32 (atime_usec_value, &val_usec, error))
return FALSE;
/* Convert to nanoseconds. Clamp the usec value if its going to overflow,
* as %G_MAXINT32 will trigger a too big error in
* _g_win32_unix_time_to_filetime() anyway. */
val_nsec = (val_usec > G_MAXINT32 / 1000) ? G_MAXINT32 : (val_usec * 1000);
if (atime_nsec_value &&
!get_uint32 (atime_nsec_value, &val_nsec, error))
return FALSE;
if (val_nsec > 0)
{
if (!_g_win32_unix_time_to_filetime (val, val_nsec, &atime, error))
return FALSE;
}
else
{
if (!_g_win32_unix_time_to_filetime (val, val_usec, &atime, error))
return FALSE;
}
p_atime = &atime;
}
else
{
if (lazy_stat (filename, &statbuf, &got_stat) == 0)
{
val = statbuf.st_atime;
#if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
val_nsec = statbuf.st_atimensec;
#elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
val_nsec = statbuf.st_atim.tv_nsec;
#endif
}
}
if (atime_usec_value &&
!get_uint32 (atime_usec_value, &val_usec, error))
return FALSE;
/* Convert to nanoseconds. Clamp the usec value if its going to overflow,
* as %G_MAXINT32 will trigger a too big error in
* _g_win32_unix_time_to_filetime() anyway. */
val_nsec = (val_usec > G_MAXINT32 / 1000) ? G_MAXINT32 : (val_usec * 1000);
if (atime_nsec_value &&
!get_uint32 (atime_nsec_value, &val_nsec, error))
return FALSE;
if (val_nsec > 0)
{
if (!_g_win32_unix_time_to_filetime (val, val_nsec, &atime, error))
return FALSE;
}
else
{
if (!_g_win32_unix_time_to_filetime (val, val_usec, &atime, error))
return FALSE;
}
p_atime = &atime;
/* MTIME */
if (mtime_value)
@ -2586,30 +2622,43 @@ set_mtime_atime (const char *filename,
return FALSE;
val_usec = 0;
val_nsec = 0;
if (mtime_usec_value &&
!get_uint32 (mtime_usec_value, &val_usec, error))
return FALSE;
/* Convert to nanoseconds. Clamp the usec value if its going to overflow,
* as %G_MAXINT32 will trigger a too big error in
* _g_win32_unix_time_to_filetime() anyway. */
val_nsec = (val_usec > G_MAXINT32 / 1000) ? G_MAXINT32 : (val_usec * 1000);
if (mtime_nsec_value &&
!get_uint32 (mtime_nsec_value, &val_nsec, error))
return FALSE;
if (val_nsec > 0)
{
if (!_g_win32_unix_time_to_filetime (val, val_nsec, &mtime, error))
return FALSE;
}
else
{
if (!_g_win32_unix_time_to_filetime (val, val_usec, &mtime, error))
return FALSE;
}
p_mtime = &mtime;
}
else
{
if (lazy_stat (filename, &statbuf, &got_stat) == 0)
{
val = statbuf.st_mtime;
#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
val_nsec = statbuf.st_mtimensec;
#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
val_nsec = statbuf.st_mtim.tv_nsec;
#endif
}
}
if (mtime_usec_value &&
!get_uint32 (mtime_usec_value, &val_usec, error))
return FALSE;
/* Convert to nanoseconds. Clamp the usec value if its going to overflow,
* as %G_MAXINT32 will trigger a too big error in
* _g_win32_unix_time_to_filetime() anyway. */
val_nsec = (val_usec > G_MAXINT32 / 1000) ? G_MAXINT32 : (val_usec * 1000);
if (mtime_nsec_value &&
!get_uint32 (mtime_nsec_value, &val_nsec, error))
return FALSE;
if (val_nsec > 0)
{
if (!_g_win32_unix_time_to_filetime (val, val_nsec, &mtime, error))
return FALSE;
}
else
{
if (!_g_win32_unix_time_to_filetime (val, val_usec, &mtime, error))
return FALSE;
}
p_mtime = &mtime;
filename_utf16 = g_utf8_to_utf16 (filename, -1, NULL, NULL, error);
@ -2654,24 +2703,6 @@ set_mtime_atime (const char *filename,
return res;
}
#elif defined (HAVE_UTIMES) || defined (HAVE_UTIMENSAT)
static int
lazy_stat (char *filename,
struct stat *statbuf,
gboolean *called_stat)
{
int res;
if (*called_stat)
return 0;
res = g_stat (filename, statbuf);
if (res == 0)
*called_stat = TRUE;
return res;
}
static gboolean
set_mtime_atime (char *filename,
const GFileAttributeValue *mtime_value,
@ -2684,7 +2715,7 @@ set_mtime_atime (char *filename,
{
int res;
guint64 val = 0;
struct stat statbuf;
GStatBuf statbuf;
gboolean got_stat = FALSE;
#ifdef HAVE_UTIMENSAT
struct timespec times_n[2] = { {0, 0}, {0, 0} };

View File

@ -252,10 +252,10 @@ test_icon (void)
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
#ifdef __APPLE__
g_assert_true (g_strv_contains (names, "text-*"));
#elif defined(G_OS_WIN32)
g_assert_cmpuint (g_strv_length ((GStrv) names), >, 0);
#else
#ifndef G_OS_WIN32
g_assert_true (g_strv_contains (names, "text-plain"));
#endif
g_assert_true (g_strv_contains (names, "text-x-generic"));
#endif
}

View File

@ -207,26 +207,25 @@ test_valid_thumbnail_size (gconstpointer data)
{
GFile *source;
GFile *thumbnail;
GFile *f;
GError *error = NULL;
GFileInfo *info;
const gchar *size = data;
char *thumbnail_path;
thumbnail = create_thumbnail_from_test_file ("valid.png", size, &source);
info = g_file_query_info (source, THUMBNAILS_ATTRIBS, G_FILE_QUERY_INFO_NONE,
NULL, &error);
g_assert_no_error (error);
thumbnail_path = g_file_get_path (thumbnail);
g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH));
g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAIL_IS_VALID));
g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED));
f = g_file_new_for_path (g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH));
g_assert_cmpstr (
g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH),
g_file_peek_path (f),
==,
thumbnail_path
g_file_peek_path (thumbnail)
);
/* TODO: We can't really test this without having a proper thumbnail created
@ -238,7 +237,7 @@ test_valid_thumbnail_size (gconstpointer data)
g_clear_object (&thumbnail);
g_clear_error (&error);
g_clear_object (&info);
g_free (thumbnail_path);
g_clear_object (&f);
}
static void
@ -310,7 +309,7 @@ test_thumbnails_size_priority (void)
for (i = 0; i < G_N_ELEMENTS (SIZES_NAMES); i++)
{
GFile *thumbnail = create_thumbnail (source, SIZES_NAMES[i]);
gchar *thumbnail_path = g_file_get_path (thumbnail);
GFile *f;
g_ptr_array_add (sized_thumbnails, thumbnail);
@ -322,14 +321,15 @@ test_thumbnails_size_priority (void)
g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAIL_IS_VALID));
g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED));
f = g_file_new_for_path (g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH));
g_assert_cmpstr (
g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH),
g_file_peek_path (f),
==,
thumbnail_path
g_file_peek_path (thumbnail)
);
g_free (thumbnail_path);
g_clear_object (&info);
g_clear_object (&f);
}
g_assert_cmpuint (sized_thumbnails->len, ==, G_N_ELEMENTS (SIZES_NAMES));
@ -339,7 +339,7 @@ test_thumbnails_size_priority (void)
{
GFile *thumbnail = g_ptr_array_index (sized_thumbnails, i - 1);
GFile *less_priority_thumbnail = g_ptr_array_index (sized_thumbnails, i - 2);
gchar *thumbnail_path = g_file_get_path (less_priority_thumbnail);
GFile *f;
g_file_delete (thumbnail, NULL, &error);
g_assert_no_error (error);
@ -352,14 +352,15 @@ test_thumbnails_size_priority (void)
g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAIL_IS_VALID));
g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED));
f = g_file_new_for_path (g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH));
g_assert_cmpstr (
g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH),
g_file_peek_path (f),
==,
thumbnail_path
g_file_peek_path (less_priority_thumbnail)
);
g_free (thumbnail_path);
g_clear_object (&info);
g_clear_object (&f);
}
/* And now let's remove the last valid one, so that failed should have priority */

View File

@ -59,22 +59,21 @@ class TestAssertMessage(unittest.TestCase):
self.__gdb = shutil.which("gdb")
self.timeout_seconds = 10 # seconds per test
ext = ""
if os.name == "nt":
ext = ".exe"
if "G_TEST_BUILDDIR" in os.environ:
self.__assert_msg_test = os.path.join(
os.environ["G_TEST_BUILDDIR"], "assert-msg-test"
os.environ["G_TEST_BUILDDIR"], "assert-msg-test" + ext
)
else:
self.__assert_msg_test = os.path.join(
os.path.dirname(__file__), "assert-msg-test"
os.path.dirname(__file__), "assert-msg-test" + ext
)
print("assert-msg-test:", self.__assert_msg_test)
def runAssertMessage(self, *args):
argv = [self.__assert_msg_test]
# shebang lines are not supported on native
# Windows consoles
if os.name == "nt":
argv.insert(0, sys.executable)
argv.extend(args)
print("Running:", argv)
@ -135,7 +134,10 @@ class TestAssertMessage(unittest.TestCase):
"""Test running g_assert() and fail the program."""
result = self.runAssertMessage()
self.assertEqual(result.info.returncode, -6)
if os.name == "nt":
self.assertEqual(result.info.returncode, 3)
else:
self.assertEqual(result.info.returncode, -6)
self.assertIn("assertion failed: (42 < 0)", result.out)
def test_gdb_gassert(self):
@ -144,12 +146,14 @@ class TestAssertMessage(unittest.TestCase):
self.skipTest("GDB is not installed, skipping this test!")
with tempfile.NamedTemporaryFile(
prefix="assert-msg-test-", suffix=".gdb", mode="w"
prefix="assert-msg-test-", suffix=".gdb", mode="w", delete=False
) as tmp:
tmp.write(GDB_SCRIPT)
tmp.flush()
result = self.runGdbAssertMessage("-x", tmp.name, self.__assert_msg_test)
try:
tmp.write(GDB_SCRIPT)
tmp.close()
result = self.runGdbAssertMessage("-x", tmp.name, self.__assert_msg_test)
finally:
os.unlink(tmp.name)
# Some CI environments disable ptrace (as theyre running in a
# container). If so, skip the test as theres nothing we can do.

View File

@ -274,7 +274,7 @@ test_thread_sort (gboolean sort)
if (sort) {
g_thread_pool_set_sort_function (pool,
test_thread_sort_compare_func,
GUINT_TO_POINTER (69));
NULL);
}
for (i = 0; i < limit; i++) {
@ -291,7 +291,7 @@ test_thread_sort (gboolean sort)
}
g_assert_cmpint (g_thread_pool_get_max_threads (pool), ==, (gint) max_threads);
g_assert_cmpuint (g_thread_pool_get_num_threads (pool), ==,
g_assert_cmpuint (g_thread_pool_get_num_threads (pool), <=,
(guint) g_thread_pool_get_max_threads (pool));
g_thread_pool_free (pool, TRUE, TRUE);
}

View File

@ -55,12 +55,6 @@ class TestGobjectQuery(unittest.TestCase):
def runGobjectQuery(self, *args):
argv = [self.__gobject_query]
# shebang lines are not supported on native
# Windows consoles
if os.name == "nt":
argv.insert(0, sys.executable)
argv.extend(args)
print("Running:", argv)
@ -75,7 +69,8 @@ class TestGobjectQuery(unittest.TestCase):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
universal_newlines=True,
text=True,
encoding='utf-8',
)
info.check_returncode()
out = info.stdout.strip()