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
This commit is contained in:
Sergey Biryukov 2020-07-14 01:28:17 +00:00
parent 0ded98ff2c
commit 022ebde54d
10 changed files with 29 additions and 23 deletions

View File

@ -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' );
}

View File

@ -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 );
}

View File

@ -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 = '"\'[]*&?$';
}

View File

@ -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'] );
}
}
}

View File

@ -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 );
}

View File

@ -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',

View File

@ -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 );

View File

@ -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(

View File

@ -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(

View File

@ -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(