[cairo] [PATCH v4 4/5] test: Ensure output dirs exist, falling back to current dir if needed

Bryce W. Harrington b.harrington at samsung.com
Sun Sep 8 13:10:03 PDT 2013


This change makes several tests behave more like ps-eps.c, et al by
making them attempt to mkdir "output", and in case of trouble use "."
instead.  filenames are now allocated at runtime due to this change, so
ensure the corresponding free()'s are in place as well.

This should facilitate running the test suite with a relative path
outside cairo's source tree, such as when employing the CAIRO_REF_DIR
environment variable.

Signed-off-by: Bryce Harrington <b.harrington at samsung.com>
---
 test/create-for-stream.c   |   14 +++++++++++---
 test/fallback-resolution.c |   20 +-------------------
 test/multi-page.c          |    9 ++++++---
 test/pdf-features.c        |    6 +++++-
 test/pdf-mime-data.c       |    6 +++++-
 test/pdf-surface-source.c  |    6 +++++-
 test/png.c                 |    8 +++++++-
 test/ps-eps.c              |   20 +-------------------
 test/ps-features.c         |    8 +++++---
 test/ps-surface-source.c   |    6 +++++-
 test/svg-clip.c            |    6 +++++-
 test/svg-surface-source.c  |    6 +++++-
 test/svg-surface.c         |    6 +++++-
 13 files changed, 66 insertions(+), 55 deletions(-)

diff --git a/test/create-for-stream.c b/test/create-for-stream.c
index e999795..af1632f 100644
--- a/test/create-for-stream.c
+++ b/test/create-for-stream.c
@@ -239,6 +239,8 @@ preamble (cairo_test_context_t *ctx)
 {
     cairo_test_status_t status = CAIRO_TEST_UNTESTED;
     cairo_test_status_t test_status;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
 #if CAIRO_HAS_PS_SURFACE
     if (cairo_test_is_target_enabled (ctx, "ps2") ||
@@ -247,7 +249,8 @@ preamble (cairo_test_context_t *ctx)
 	if (status == CAIRO_TEST_UNTESTED)
 	    status = CAIRO_TEST_SUCCESS;
 
-	test_status = test_surface (ctx, "ps",  CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".ps",
+	xasprintf (&filename, "%s/%s", path, BASENAME ".ps");
+	test_status = test_surface (ctx, "ps", filename,
 				    cairo_ps_surface_create,
 				    cairo_ps_surface_create_for_stream);
 	cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
@@ -255,6 +258,7 @@ preamble (cairo_test_context_t *ctx)
 			test_status ? "FAIL" : "PASS");
 	if (status == CAIRO_TEST_SUCCESS)
 	    status = test_status;
+	free (filename);
     }
 #endif
 
@@ -263,7 +267,8 @@ preamble (cairo_test_context_t *ctx)
 	if (status == CAIRO_TEST_UNTESTED)
 	    status = CAIRO_TEST_SUCCESS;
 
-	test_status = test_surface (ctx, "pdf", CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".pdf",
+	xasprintf (&filename, "%s/%s", path, BASENAME ".pdf");
+	test_status = test_surface (ctx, "pdf", filename,
 				    cairo_pdf_surface_create,
 				    cairo_pdf_surface_create_for_stream);
 	cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
@@ -271,6 +276,7 @@ preamble (cairo_test_context_t *ctx)
 			test_status ? "FAIL" : "PASS");
 	if (status == CAIRO_TEST_SUCCESS)
 	    status = test_status;
+	free (filename);
     }
 #endif
 
@@ -281,7 +287,8 @@ preamble (cairo_test_context_t *ctx)
 	if (status == CAIRO_TEST_UNTESTED)
 	    status = CAIRO_TEST_SUCCESS;
 
-	test_status = test_surface (ctx, "svg", CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".svg",
+	xasprintf (&filename, "%s/%s", path, BASENAME ".svg");
+	test_status = test_surface (ctx, "svg", filename,
 				    cairo_svg_surface_create,
 				    cairo_svg_surface_create_for_stream);
 	cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
@@ -289,6 +296,7 @@ preamble (cairo_test_context_t *ctx)
 			test_status ? "FAIL" : "PASS");
 	if (status == CAIRO_TEST_SUCCESS)
 	    status = test_status;
+	free (filename);
     }
 #endif
 
diff --git a/test/fallback-resolution.c b/test/fallback-resolution.c
index bf83e31..fa1729b 100644
--- a/test/fallback-resolution.c
+++ b/test/fallback-resolution.c
@@ -320,24 +320,6 @@ generate_reference (double ppi_x, double ppi_y, const char *filename)
 }
 #endif
 
-static cairo_bool_t
-_cairo_test_mkdir (const char *path)
-{
-#if ! HAVE_MKDIR
-    return FALSE;
-#elif HAVE_MKDIR == 1
-    if (mkdir (path) == 0)
-	return TRUE;
-#elif HAVE_MKDIR == 2
-    if (mkdir (path, 0770) == 0)
-	return TRUE;
-#else
-#error Bad value for HAVE_MKDIR
-#endif
-
-    return errno == EEXIST;
-}
-
 static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
@@ -362,7 +344,7 @@ preamble (cairo_test_context_t *ctx)
     };
     unsigned int i;
     int n, num_ppi;
-    const char *path = _cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     num_ppi = ARRAY_LENGTH (ppi);
 
diff --git a/test/multi-page.c b/test/multi-page.c
index dea3b42..e5b3dd2 100644
--- a/test/multi-page.c
+++ b/test/multi-page.c
@@ -133,8 +133,9 @@ preamble (cairo_test_context_t *ctx)
 {
     cairo_surface_t *surface;
     cairo_status_t status;
-    const char *filename;
+    char *filename;
     cairo_test_status_t result = CAIRO_TEST_UNTESTED;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
 #if CAIRO_HAS_PS_SURFACE
     if (cairo_test_is_target_enabled (ctx, "ps2") ||
@@ -143,7 +144,7 @@ preamble (cairo_test_context_t *ctx)
 	if (result == CAIRO_TEST_UNTESTED)
 	    result = CAIRO_TEST_SUCCESS;
 
-	filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".ps";
+	xasprintf (&filename, "%s/%s", path, BASENAME ".ps");
 	surface = cairo_ps_surface_create (filename,
 					   WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
 	status = cairo_surface_status (surface);
@@ -156,6 +157,7 @@ preamble (cairo_test_context_t *ctx)
 	draw_some_pages (surface);
 
 	cairo_surface_destroy (surface);
+	free (filename);
 
 	printf ("multi-page: Please check %s to ensure it looks happy.\n", filename);
     }
@@ -166,7 +168,7 @@ preamble (cairo_test_context_t *ctx)
 	if (result == CAIRO_TEST_UNTESTED)
 	    result = CAIRO_TEST_SUCCESS;
 
-	filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".pdf";
+	xasprintf (&filename, "%s/%s", path, BASENAME ".pdf");
 	surface = cairo_pdf_surface_create (filename,
 					    WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
 	status = cairo_surface_status (surface);
@@ -179,6 +181,7 @@ preamble (cairo_test_context_t *ctx)
 	draw_some_pages (surface);
 
 	cairo_surface_destroy (surface);
+	free (filename);
 
 	printf ("multi-page: Please check %s to ensure it looks happy.\n", filename);
     }
diff --git a/test/pdf-features.c b/test/pdf-features.c
index c5f6bea..0cc50ed 100644
--- a/test/pdf-features.c
+++ b/test/pdf-features.c
@@ -89,11 +89,14 @@ preamble (cairo_test_context_t *ctx)
     cairo_t *cr;
     cairo_status_t status;
     size_t i;
-    const char *filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".pdf";
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     if (! cairo_test_is_target_enabled (ctx, "pdf"))
 	return CAIRO_TEST_UNTESTED;
 
+    xasprintf (&filename, "%s/%s.pdf", path, BASENAME);
+
     /* The initial size passed here is the default size that will be
      * inheritable by each page. That is, any page for which this
      * initial size applies will not have its own /MediaBox entry in
@@ -125,6 +128,7 @@ preamble (cairo_test_context_t *ctx)
 
     cairo_destroy (cr);
     cairo_surface_destroy (surface);
+    free (filename);
 
     if (status) {
 	cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
diff --git a/test/pdf-mime-data.c b/test/pdf-mime-data.c
index e4fbaa2..c575c4a 100644
--- a/test/pdf-mime-data.c
+++ b/test/pdf-mime-data.c
@@ -84,7 +84,6 @@ read_file (const cairo_test_context_t *ctx,
 static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
-    const char *filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".pdf";
     cairo_surface_t *image;
     cairo_surface_t *surface;
     cairo_t *cr;
@@ -96,6 +95,8 @@ preamble (cairo_test_context_t *ctx)
     unsigned int len, out_len;
     char command[4096];
     int exit_status;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     if (! cairo_test_is_target_enabled (ctx, "pdf"))
 	return CAIRO_TEST_UNTESTED;
@@ -113,6 +114,7 @@ preamble (cairo_test_context_t *ctx)
     width = cairo_image_surface_get_width (image);
     height = cairo_image_surface_get_height (image);
 
+    xasprintf (&filename, "%s/%s.pdf", path, BASENAME);
     surface = cairo_pdf_surface_create (filename, width + 20, height + 20);
     cr = cairo_create (surface);
     cairo_translate (cr, 10, 10);
@@ -128,6 +130,7 @@ preamble (cairo_test_context_t *ctx)
     cairo_surface_destroy (image);
 
     if (status) {
+        free (filename);
 	cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
 			filename, cairo_status_to_string (status));
 	return CAIRO_TEST_FAILURE;
@@ -137,6 +140,7 @@ preamble (cairo_test_context_t *ctx)
 
     sprintf (command, "pdfimages -j %s %s", filename, CAIRO_TEST_OUTPUT_DIR "/" BASENAME);
     exit_status = system (command);
+    free (filename);
     if (exit_status) {
 	cairo_test_log (ctx, "pdfimages failed with exit status %d\n", exit_status);
 	return CAIRO_TEST_FAILURE;
diff --git a/test/pdf-surface-source.c b/test/pdf-surface-source.c
index 6307271..354a725 100644
--- a/test/pdf-surface-source.c
+++ b/test/pdf-surface-source.c
@@ -34,9 +34,13 @@ static cairo_surface_t *
 create_source_surface (int size)
 {
     cairo_surface_t *surface;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
-    surface = cairo_pdf_surface_create (CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".pdf", size, size);
+    xasprintf (&filename, "%s/%s.pdf", path, BASENAME);
+    surface = cairo_pdf_surface_create (filename, size, size);
     cairo_surface_set_fallback_resolution (surface, 72., 72.);
+    free (filename);
 
     return surface;
 }
diff --git a/test/png.c b/test/png.c
index 1dfc793..c01fbd7 100644
--- a/test/png.c
+++ b/test/png.c
@@ -78,11 +78,13 @@ print_surface (const cairo_test_context_t *ctx, cairo_surface_t *surface)
 static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
-    const char *filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".png";
     cairo_surface_t *surface0, *surface1;
     cairo_status_t status;
     uint32_t argb32 = 0xdeadbede;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
+    xasprintf (&filename, "%s/%s.png", path, BASENAME);
     surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
 						    CAIRO_FORMAT_ARGB32,
 						    1, 1, 4);
@@ -92,6 +94,7 @@ preamble (cairo_test_context_t *ctx)
 			filename, cairo_status_to_string (status));
 
 	cairo_surface_destroy (surface0);
+	free (filename);
 	return cairo_test_status_from_status (ctx, status);
     }
     surface1 = cairo_image_surface_create_from_png (filename);
@@ -102,6 +105,7 @@ preamble (cairo_test_context_t *ctx)
 
 	cairo_surface_destroy (surface1);
 	cairo_surface_destroy (surface0);
+	free (filename);
 	return cairo_test_status_from_status (ctx, status);
     }
 
@@ -112,6 +116,7 @@ preamble (cairo_test_context_t *ctx)
 
 	cairo_surface_destroy (surface0);
 	cairo_surface_destroy (surface1);
+	free (filename);
 	return CAIRO_TEST_FAILURE;
     }
     assert (*(uint32_t *) cairo_image_surface_get_data (surface1) == argb32);
@@ -131,6 +136,7 @@ preamble (cairo_test_context_t *ctx)
     }
     surface1 = cairo_image_surface_create_from_png (filename);
     status = cairo_surface_status (surface1);
+    free (filename);
     if (status) {
 	cairo_test_log (ctx, "Error reading '%s': %s\n",
 			filename, cairo_status_to_string (status));
diff --git a/test/ps-eps.c b/test/ps-eps.c
index 1961463..66224e2 100644
--- a/test/ps-eps.c
+++ b/test/ps-eps.c
@@ -239,31 +239,13 @@ check_bbox (cairo_test_context_t *ctx,
     return TRUE;
 }
 
-static cairo_bool_t
-_cairo_test_mkdir (const char *path)
-{
-#if ! HAVE_MKDIR
-    return FALSE;
-#elif HAVE_MKDIR == 1
-    if (mkdir (path) == 0)
-	return TRUE;
-#elif HAVE_MKDIR == 2
-    if (mkdir (path, 0770) == 0)
-	return TRUE;
-#else
-#error Bad value for HAVE_MKDIR
-#endif
-
-    return errno == EEXIST;
-}
-
 static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
     cairo_t *cr;
     cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
-    const char *path = _cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
     unsigned int i;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     for (i = 0; i < ctx->num_targets; i++) {
 	const cairo_boilerplate_target_t *target = ctx->targets_to_test[i];
diff --git a/test/ps-features.c b/test/ps-features.c
index 2eb9ace..fe5de42 100644
--- a/test/ps-features.c
+++ b/test/ps-features.c
@@ -91,9 +91,10 @@ preamble (cairo_test_context_t *ctx)
     cairo_surface_t *surface;
     cairo_t *cr;
     cairo_status_t status;
-    const char *filename;
     size_t i;
     char dsc[255];
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     if (! (cairo_test_is_target_enabled (ctx, "ps2") ||
 	   cairo_test_is_target_enabled (ctx, "ps3")))
@@ -101,8 +102,7 @@ preamble (cairo_test_context_t *ctx)
 	return CAIRO_TEST_UNTESTED;
     }
 
-    filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".ps";
-
+    xasprintf (&filename, "%s/%s.ps", path, BASENAME);
     /* We demonstrate that the initial size doesn't matter (we're
      * passing 0,0), if we use cairo_ps_surface_set_size on the first
      * page. */
@@ -149,10 +149,12 @@ preamble (cairo_test_context_t *ctx)
     if (status) {
 	cairo_test_log (ctx, "Failed to create ps surface for file %s: %s\n",
 			filename, cairo_status_to_string (status));
+	free (filename);
 	return CAIRO_TEST_FAILURE;
     }
 
     printf ("ps-features: Please check %s to ensure it looks/prints correctly.\n", filename);
+    free (filename);
     return CAIRO_TEST_SUCCESS;
 }
 
diff --git a/test/ps-surface-source.c b/test/ps-surface-source.c
index 3573cb2..16c6776 100644
--- a/test/ps-surface-source.c
+++ b/test/ps-surface-source.c
@@ -34,9 +34,13 @@ static cairo_surface_t *
 create_source_surface (int size)
 {
     cairo_surface_t *surface;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
-    surface = cairo_ps_surface_create (CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".ps", size, size);
+    xasprintf (&filename, "%s/%s.ps", path, BASENAME);
+    surface = cairo_ps_surface_create (filename, size, size);
     cairo_surface_set_fallback_resolution (surface, 72., 72.);
+    free (filename);
 
     return surface;
 }
diff --git a/test/svg-clip.c b/test/svg-clip.c
index 149033b..b39e29f 100644
--- a/test/svg-clip.c
+++ b/test/svg-clip.c
@@ -108,8 +108,9 @@ static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
     cairo_t *cr;
-    const char *filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".svg";
     cairo_surface_t *surface;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     if (! cairo_test_is_target_enabled (ctx, "svg11") &&
 	! cairo_test_is_target_enabled (ctx, "svg12"))
@@ -117,12 +118,14 @@ preamble (cairo_test_context_t *ctx)
 	return CAIRO_TEST_UNTESTED;
     }
 
+    xasprintf (&filename, "%s/%s.svg", path, BASENAME);
     surface = cairo_svg_surface_create (filename,
 					WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
     if (cairo_surface_status (surface)) {
 	cairo_test_log (ctx,
 			"Failed to create svg surface for file %s: %s\n",
 			filename, cairo_status_to_string (cairo_surface_status (surface)));
+	free (filename);
 	return CAIRO_TEST_FAILURE;
     }
 
@@ -136,6 +139,7 @@ preamble (cairo_test_context_t *ctx)
 
     printf ("svg-clip: Please check %s to make sure it looks happy.\n",
 	    filename);
+    free (filename);
     return CAIRO_TEST_SUCCESS;
 }
 
diff --git a/test/svg-surface-source.c b/test/svg-surface-source.c
index 2c33980..3c7730f 100644
--- a/test/svg-surface-source.c
+++ b/test/svg-surface-source.c
@@ -34,10 +34,14 @@ static cairo_surface_t *
 create_source_surface (int size)
 {
     cairo_surface_t *surface;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
-    surface = cairo_svg_surface_create (CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".svg",
+    xasprintf (&filename, "%s/%s.svg", path, BASENAME);
+    surface = cairo_svg_surface_create (filename,
 					size, size);
     cairo_surface_set_fallback_resolution (surface, 72., 72.);
+    free (filename);
 
     return surface;
 }
diff --git a/test/svg-surface.c b/test/svg-surface.c
index 30cf994..c0cdc16 100644
--- a/test/svg-surface.c
+++ b/test/svg-surface.c
@@ -91,8 +91,9 @@ static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
     cairo_t *cr;
-    const char *filename = CAIRO_TEST_OUTPUT_DIR "/" BASENAME ".svg";
     cairo_surface_t *surface;
+    char *filename;
+    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
     if (! cairo_test_is_target_enabled (ctx, "svg11") &&
 	! cairo_test_is_target_enabled (ctx, "svg12"))
@@ -100,6 +101,7 @@ preamble (cairo_test_context_t *ctx)
 	return CAIRO_TEST_UNTESTED;
     }
 
+    xasprintf (&filename, "%s/%s.svg", path, BASENAME);
     surface = cairo_svg_surface_create (filename,
 					WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
     if (cairo_surface_status (surface)) {
@@ -107,6 +109,7 @@ preamble (cairo_test_context_t *ctx)
 			"Failed to create svg surface for file %s: %s\n",
 			filename,
 			cairo_status_to_string (cairo_surface_status (surface)));
+	free (filename);
 	return CAIRO_TEST_FAILURE;
     }
 
@@ -120,6 +123,7 @@ preamble (cairo_test_context_t *ctx)
     cairo_surface_destroy (surface);
 
     printf ("svg-surface: Please check svg-surface.svg to make sure it looks happy.\n");
+    free (filename);
     return CAIRO_TEST_SUCCESS;
 }
 
-- 
1.7.9.5


More information about the cairo mailing list