Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Plugin Checker to only check root directory readme files #241

Merged
merged 5 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,41 @@ public function get_categories() {
* @param array $files Array of plugin files.
*/
protected function check_files( Check_Result $result, array $files ) {

$plugin_relative_path = $result->plugin()->path();

// Find the readme file.
$readme = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' );
$readme_list = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' );

// Filter the readme files that located at rootx.
$potential_readme_files = array_filter(
$readme_list,
function ( $file ) use ( $plugin_relative_path ) {
$file = str_replace( $plugin_relative_path, '', $file );
if ( ! strpos( $file, '/' ) ) {
return true;
}
}
);

// Find the .txt versions of the readme files.
$readme_txt = array_filter(
$potential_readme_files,
function ( $file ) {
return preg_match( '/^readme\.txt$/i', basename( $file ) );
}
);

// Find the .md versions of the readme files.
$readme_md = array_filter(
$potential_readme_files,
function ( $file ) {
return preg_match( '/^readme\.md$/i', basename( $file ) );
}
);

// If there's a .txt version, ignore .md versions.
$readme = ( ! empty( $readme_txt ) ) ? $readme_txt : $readme_md;

// If the readme file does not exist, add a warning and skip other tests.
if ( empty( $readme ) ) {
Expand Down Expand Up @@ -84,24 +117,20 @@ private function check_default_text( Check_Result $result, array $files ) {
'Donate link: http://example.com/',
);

$file = '';
foreach ( $default_text_patterns as $pattern ) {
$file = self::file_str_contains( $files, $pattern );
if ( $file ) {
$result->add_message(
false,
__( 'The readme appears to contain default text.', 'plugin-check' ),
array(
'code' => 'default_readme_text',
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
break;
}
}

if ( $file ) {
$result->add_message(
false,
__( 'The readme appears to contain default text.', 'plugin-check' ),
array(
'code' => 'default_readme_text',
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,20 @@ public function test_run_md_with_errors() {
$this->assertArrayHasKey( 'code', $warnings['readme.md'][0][0][3] );
$this->assertEquals( 'stable_tag_mismatch', $warnings['readme.md'][0][0][3]['code'] );
}

public function test_run_root_readme_file_without_errors() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-root-readme-without-errors/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertEmpty( $errors );
$this->assertEmpty( $warnings );
$this->assertEquals( 0, $check_result->get_error_count() );
$this->assertEquals( 0, $check_result->get_warning_count() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

=== Plugin Check ===

Contributors: wordpressdotorg
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: 5.6
Stable tag: trunk
License: Oculus VR Inc. Software Development Kit License
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: performance, testing, security

Here is a short description of the plugin.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Plugin Name: Test Plugin Readme check (no errors)
* Plugin URI: https://github.com/wordpress/plugin-check
* Description: Test plugin for the Readme check.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check-errors
*
* @package test-plugin-check-errors
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

=== Test Plugin with readme ===

Contributors: plugin-check
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: 5.6
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: testing, security

A simple readme file for testing.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

=== Test Plugin with readme ===

Contributors: plugin-check
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: 5.6
Stable tag: n.e.x.t
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Donate URL: https://example.com
Tags: tag1, testing, security

Here is a short description of the plugin.