From 022ebde54d6f06556382fa34f6e8d5fb0ee24d03 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 14 Jul 2020 01:28:17 +0000 Subject: [PATCH] Tests: Replace hardcoded `/tmp/` references with `get_temp_dir()`. This allows more tests to pass on Windows. Props danielhuesken, DJPaul, christophherr, joemcgill, netweb, davidbaumwald, SergeyBiryukov. Fixes #40856, #39975. git-svn-id: https://develop.svn.wordpress.org/trunk@48464 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 6 ++++- tests/phpunit/tests/customize/manager.php | 4 ++-- tests/phpunit/tests/file.php | 4 +--- tests/phpunit/tests/image/functions.php | 24 +++++++++++-------- .../rest-api/rest-attachments-controller.php | 4 ++-- .../tests/rest-api/rest-schema-setup.php | 2 +- tests/phpunit/tests/upload.php | 2 +- .../tests/widgets/media-gallery-widget.php | 2 +- .../tests/widgets/media-image-widget.php | 2 +- tests/phpunit/tests/widgets/media-widget.php | 2 +- 10 files changed, 29 insertions(+), 23 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 6cc2f5f022..9895bcf691 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -913,16 +913,20 @@ abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { public function temp_filename() { $tmp_dir = ''; $dirs = array( 'TMP', 'TMPDIR', 'TEMP' ); + foreach ( $dirs as $dir ) { if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) { $tmp_dir = $dir; break; } } + if ( empty( $tmp_dir ) ) { - $tmp_dir = '/tmp'; + $tmp_dir = get_temp_dir(); } + $tmp_dir = realpath( $tmp_dir ); + return tempnam( $tmp_dir, 'wpunit' ); } diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index 00dcd333c6..4a9d5caead 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -60,10 +60,10 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->undefined = new stdClass(); $orig_file = DIR_TESTDATA . '/images/canola.jpg'; - $this->test_file = '/tmp/canola.jpg'; + $this->test_file = get_temp_dir() . 'canola.jpg'; copy( $orig_file, $this->test_file ); $orig_file2 = DIR_TESTDATA . '/images/waffles.jpg'; - $this->test_file2 = '/tmp/waffles.jpg'; + $this->test_file2 = get_temp_dir() . 'waffles.jpg'; copy( $orig_file2, $this->test_file2 ); } diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php index 9a2c63ecbf..934bb751e4 100644 --- a/tests/phpunit/tests/file.php +++ b/tests/phpunit/tests/file.php @@ -8,9 +8,7 @@ class Tests_File extends WP_UnitTestCase { function setUp() { parent::setUp(); - $file = tempnam( '/tmp', 'foo' ); - $this->dir = dirname( $file ); - unlink( $file ); + $this->dir = untrailingslashit( get_temp_dir() ); $this->badchars = '"\'[]*&?$'; } diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 838f14c5a1..cfffd71922 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -20,7 +20,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { require_once DIR_TESTDATA . '/../includes/mock-image-editor.php'; // Ensure no legacy / failed tests detritus. - $folder = '/tmp/wordpress-gsoc-flyer*.{jpg,pdf}'; + $folder = get_temp_dir() . 'wordpress-gsoc-flyer*.{jpg,pdf}'; foreach ( glob( $folder, GLOB_BRACE ) as $file ) { unlink( $file ); @@ -425,7 +425,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { } $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; - $test_file = '/tmp/wordpress-gsoc-flyer.pdf'; + $test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf'; copy( $orig_file, $test_file ); $editor = wp_get_image_editor( $test_file ); @@ -476,8 +476,9 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->assertSame( $expected, $metadata ); unlink( $test_file ); + $temp_dir = get_temp_dir(); foreach ( $metadata['sizes'] as $size ) { - unlink( '/tmp/' . $size['file'] ); + unlink( $temp_dir . $size['file'] ); } } @@ -560,7 +561,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { } $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; - $test_file = '/tmp/wordpress-gsoc-flyer.pdf'; + $test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf'; copy( $orig_file, $test_file ); $editor = wp_get_image_editor( $test_file ); @@ -596,8 +597,9 @@ class Tests_Image_Functions extends WP_UnitTestCase { remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 ); unlink( $test_file ); + $temp_dir = get_temp_dir(); foreach ( $metadata['sizes'] as $size ) { - unlink( '/tmp/' . $size['file'] ); + unlink( $temp_dir . $size['file'] ); } } @@ -618,14 +620,16 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' ); } + $temp_dir = get_temp_dir(); + // Dummy JPEGs. - $jpg1_path = '/tmp/test.jpg'; // Straight. + $jpg1_path = $temp_dir . 'test.jpg'; // Straight. file_put_contents( $jpg1_path, 'asdf' ); - $jpg2_path = '/tmp/test-pdf.jpg'; // With PDF marker. + $jpg2_path = $temp_dir . 'test-pdf.jpg'; // With PDF marker. file_put_contents( $jpg2_path, 'fdsa' ); // PDF with same name as JPEG. - $pdf_path = '/tmp/test.pdf'; + $pdf_path = $temp_dir . 'test.pdf'; copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path ); $editor = wp_get_image_editor( $pdf_path ); @@ -642,7 +646,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { ); $metadata = wp_generate_attachment_metadata( $attachment_id, $pdf_path ); - $preview_path = '/tmp/' . $metadata['sizes']['full']['file']; + $preview_path = $temp_dir . $metadata['sizes']['full']['file']; // PDF preview didn't overwrite PDF. $this->assertNotEquals( $pdf_path, $preview_path ); @@ -658,7 +662,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { unlink( $jpg2_path ); unlink( $pdf_path ); foreach ( $metadata['sizes'] as $size ) { - unlink( '/tmp/' . $size['file'] ); + unlink( $temp_dir . $size['file'] ); } } } diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index 761d91683f..0b52d4c651 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -80,10 +80,10 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control $role->add_cap( 'level_0' ); $orig_file = DIR_TESTDATA . '/images/canola.jpg'; - $this->test_file = '/tmp/canola.jpg'; + $this->test_file = get_temp_dir() . 'canola.jpg'; copy( $orig_file, $this->test_file ); $orig_file2 = DIR_TESTDATA . '/images/codeispoetry.png'; - $this->test_file2 = '/tmp/codeispoetry.png'; + $this->test_file2 = get_temp_dir() . 'codeispoetry.png'; copy( $orig_file2, $this->test_file2 ); } diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php index eb70f9d88c..92551704c7 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-setup.php +++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php @@ -226,7 +226,7 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase { ); $media_id = $this->factory->attachment->create_object( - '/tmp/canola.jpg', + get_temp_dir() . 'canola.jpg', 0, array( 'post_mime_type' => 'image/jpeg', diff --git a/tests/phpunit/tests/upload.php b/tests/phpunit/tests/upload.php index dbf927059e..719a756403 100644 --- a/tests/phpunit/tests/upload.php +++ b/tests/phpunit/tests/upload.php @@ -46,7 +46,7 @@ class Tests_Upload extends WP_UnitTestCase { * @ticket 5953 */ function test_upload_dir_absolute() { - $path = '/tmp/wp-unit-test'; + $path = get_temp_dir() . 'wp-unit-test'; // wp_upload_dir() with an absolute upload path. update_option( 'upload_path', $path ); diff --git a/tests/phpunit/tests/widgets/media-gallery-widget.php b/tests/phpunit/tests/widgets/media-gallery-widget.php index 401a788e5d..509504f3d8 100644 --- a/tests/phpunit/tests/widgets/media-gallery-widget.php +++ b/tests/phpunit/tests/widgets/media-gallery-widget.php @@ -58,7 +58,7 @@ class Test_WP_Widget_Media_Gallery extends WP_UnitTestCase { $attachments = array(); foreach ( array( 'canola.jpg', 'waffles.jpg' ) as $filename ) { - $test_image = '/tmp/' . $filename; + $test_image = get_temp_dir() . $filename; copy( DIR_TESTDATA . '/images/canola.jpg', $test_image ); $attachment_id = self::factory()->attachment->create_object( array( diff --git a/tests/phpunit/tests/widgets/media-image-widget.php b/tests/phpunit/tests/widgets/media-image-widget.php index aa4e65f267..d6b4b57863 100644 --- a/tests/phpunit/tests/widgets/media-image-widget.php +++ b/tests/phpunit/tests/widgets/media-image-widget.php @@ -412,7 +412,7 @@ class Test_WP_Widget_Media_Image extends WP_UnitTestCase { function test_render_media() { $widget = new WP_Widget_Media_Image(); - $test_image = '/tmp/canola.jpg'; + $test_image = get_temp_dir() . 'canola.jpg'; copy( DIR_TESTDATA . '/images/canola.jpg', $test_image ); $attachment_id = self::factory()->attachment->create_object( array( diff --git a/tests/phpunit/tests/widgets/media-widget.php b/tests/phpunit/tests/widgets/media-widget.php index 6b9dc926ce..27a4d5af60 100644 --- a/tests/phpunit/tests/widgets/media-widget.php +++ b/tests/phpunit/tests/widgets/media-widget.php @@ -138,7 +138,7 @@ class Test_WP_Widget_Media extends WP_UnitTestCase { */ function test_is_attachment_with_mime_type() { - $test_image = '/tmp/canola.jpg'; + $test_image = get_temp_dir() . 'canola.jpg'; copy( DIR_TESTDATA . '/images/canola.jpg', $test_image ); $attachment_id = self::factory()->attachment->create_object( array(