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
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 */