Skip to content

Commit

Permalink
Support for running against a list of files
Browse files Browse the repository at this point in the history
To run against a list of files, just pass in the list of files after `--`, like this: `wp-l10n-validator -- file-a.php file-b.php`

Fixes #33
  • Loading branch information
JDGrimes committed Dec 17, 2016
1 parent b17f374 commit c45d174
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ To see the basic usage and check that everything is working, type the command:
Usage
-----

`$ wp-l10n-validator -[1c] TEXTDOMAIN [CONFIG]`
`$ wp-l10n-validator -[1c] TEXTDOMAIN [CONFIG] [-- FILE ...]`

This validates all `.php` files in the current directory for proper gettexting.

Arguments:
* `TEXTDOMAIN` - The textdomain used in your project.
* `CONFIG` - Configuration to use. Corressponds to one of the directories in `/config` (`wordpress` by default).
* `FILE` - One or more files to validate. You must pass `--` before the list of files, like this: `wp-l10n-validator textdomain -- a.php b.php`

Flags:
* `1` - Parse only one file at a time.
Expand Down
9 changes: 9 additions & 0 deletions tests/data/no-config/other.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Another file.
*/

function do_some_things( $var ) {
return $var;
}
20 changes: 20 additions & 0 deletions tests/tests/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ public function test_ignores_cache_generation() {
$this->assertEquals( 0, $this->exit_code );
}

/**
* Test passing a list of files to check via the command line.
*
* @since 0.4.0
*/
public function test_files_passed() {

$output = $this->run_command( 'wp-l10n-validator -- other.php', '/no-config' );
$this->assertEquals( 0, strpos( $output, 'Usage:' ) );
$this->assertEquals( 1, $this->exit_code );

$output = $this->run_command( 'wp-l10n-validator textdomain -- other.php', '/no-config' );
$this->assertEquals( '', $output );
$this->assertEquals( 0, $this->exit_code );

$output = $this->run_command( 'wp-l10n-validator textdomain -- other.php no-config.php', '/no-config' );
$this->assertEquals( "/no-config.php#16: Non gettexted string 'Hello world'", $output );
$this->assertEquals( 1, $this->exit_code );
}

/**
* Run a command and return the output.
*
Expand Down
19 changes: 16 additions & 3 deletions wp-l10n-validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,11 @@ public static function cli() {

global $argv;

if ( ($key = array_search( '--', $argv )) ) {
$files = array_slice( $argv, $key + 1 );
$argv = array_slice( $argv, 0, $key );
}

$args = static::parse_cli_args( $argv );

$class = get_called_class();
Expand Down Expand Up @@ -2126,8 +2131,14 @@ public static function cli() {
}
}

// Parse the project.
$parser->parse();
if ( isset( $files ) ) {
foreach ( $files as $file ) {
$parser->parse_file( '/' . $file );
}
} else {
// Parse the project.
$parser->parse();
}

return $parser;

Expand Down Expand Up @@ -2236,12 +2247,14 @@ public static function cli_usage() {

fwrite(
STDERR,
"\nUsage: wp-l10n-validator -[1c] TEXTDOMAIN [CONFIG]\n\n"
"\nUsage: wp-l10n-validator -[1c] TEXTDOMAIN [CONFIG] [-- FILE ...]\n\n"
. "Validate all .php files in the current directory for proper gettexting.\n"
. "\nArguments:\n"
. "\tTEXTDOMAIN - The textdomain used in the project.\n"
. "\tCONFIG - Configuration to use. Corresponds to one of the directories\n"
. "\t\t in /config (wordpress by default).\n"
. "\tFILE - One or more files to validate. You must pass -- before the\n"
. "\t\tlist of files, like this: wp-l10n-validator -- a.php b.php\n"
. "\nFlags:\n"
. "\t1 - Parse only one file at a time.\n"
. "\tc - Generate a specific ignores cache.\n"
Expand Down

0 comments on commit c45d174

Please sign in to comment.