Skip to content
Snippets Groups Projects
Commit 04c11c73 authored by Simon McVittie's avatar Simon McVittie
Browse files

Run the fdio test in its own temporary directory


The temporary directory will be deleted on success, but will remain
intact on failure.

Signed-off-by: default avatarSimon McVittie <smcv@collabora.com>
parent 5920f9f3
No related branches found
No related tags found
No related merge requests found
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright 2019 Collabora Ltd.
*
* 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 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "libglnx-testlib.h"
#include <errno.h>
#include "libglnx.h"
struct _GLnxTestAutoTempDir
{
gchar *old_cwd;
int old_cwd_fd;
GLnxTmpDir temp_dir;
};
_GLnxTestAutoTempDir *
_glnx_test_auto_temp_dir_enter (void)
{
GError *error = NULL;
_GLnxTestAutoTempDir *ret = g_new0 (_GLnxTestAutoTempDir, 1);
glnx_mkdtemp ("glnx-test-XXXXXX", 0700, &ret->temp_dir, &error);
g_assert_no_error (error);
/* just for better diagnostics */
ret->old_cwd = g_get_current_dir ();
glnx_opendirat (-1, ".", TRUE, &ret->old_cwd_fd, &error);
g_assert_no_error (error);
if (fchdir (ret->temp_dir.fd) != 0)
g_error ("fchdir(<fd for \"%s\">): %s", ret->temp_dir.path, g_strerror (errno));
return ret;
}
void
_glnx_test_auto_temp_dir_leave (_GLnxTestAutoTempDir *dir)
{
GError *error = NULL;
if (fchdir (dir->old_cwd_fd) != 0)
g_error ("fchdir(<fd for \"%s\">): %s", dir->old_cwd, g_strerror (errno));
glnx_tmpdir_delete (&dir->temp_dir, NULL, &error);
g_assert_no_error (error);
}
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
* *
* Copyright (C) 2017 Red Hat, Inc. * Copyright (C) 2017 Red Hat, Inc.
* Copyright 2019 Collabora Ltd.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -20,6 +21,10 @@ ...@@ -20,6 +21,10 @@
#pragma once #pragma once
#include <glib.h>
#include "glnx-backport-autoptr.h"
typedef GError _GLnxTestAutoError; typedef GError _GLnxTestAutoError;
static inline void static inline void
_glnx_test_auto_error_cleanup (_GLnxTestAutoError *autoerror) _glnx_test_auto_error_cleanup (_GLnxTestAutoError *autoerror)
...@@ -32,3 +37,12 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(_GLnxTestAutoError, _glnx_test_auto_error_cleanup) ...@@ -32,3 +37,12 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(_GLnxTestAutoError, _glnx_test_auto_error_cleanup)
#define _GLNX_TEST_DECLARE_ERROR(local_error, error) \ #define _GLNX_TEST_DECLARE_ERROR(local_error, error) \
g_autoptr(_GLnxTestAutoError) local_error = NULL; \ g_autoptr(_GLnxTestAutoError) local_error = NULL; \
GError **error = &local_error GError **error = &local_error
typedef struct _GLnxTestAutoTempDir _GLnxTestAutoTempDir;
_GLnxTestAutoTempDir *_glnx_test_auto_temp_dir_enter (void);
void _glnx_test_auto_temp_dir_leave (_GLnxTestAutoTempDir *dir);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(_GLnxTestAutoTempDir, _glnx_test_auto_temp_dir_leave);
#define _GLNX_TEST_SCOPED_TEMP_DIR \
g_autoptr(_GLnxTestAutoTempDir) temp_dir = _glnx_test_auto_temp_dir_enter ()
...@@ -9,7 +9,11 @@ test_names = [ ...@@ -9,7 +9,11 @@ test_names = [
foreach test_name : test_names foreach test_name : test_names
exe = executable(test_name, exe = executable(test_name,
['test-libglnx-' + test_name + '.c', 'libglnx-testlib.h'], [
'libglnx-testlib.c',
'libglnx-testlib.h',
'test-libglnx-' + test_name + '.c',
],
dependencies: [ dependencies: [
libglnx_dep, libglnx_dep,
libglnx_deps, libglnx_deps,
......
...@@ -237,6 +237,7 @@ test_filecopy (void) ...@@ -237,6 +237,7 @@ test_filecopy (void)
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
_GLNX_TEST_SCOPED_TEMP_DIR;
int ret; int ret;
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment