Cleanup and updates based on feedback

This commit is contained in:
Michael Withagen 2025-04-25 20:36:03 +02:00
parent d47e867adc
commit 952f493d72
No known key found for this signature in database
GPG key ID: 44BB2237B942C729
7 changed files with 7 additions and 150 deletions

View file

@ -985,7 +985,7 @@ Feature: Split PO files into JSON files.
And the return code should be 0
And the foo-theme/my-custom-domain-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
Scenario: Should only proces js/min.js extensions by default
Scenario: Should only process js/min.js extensions by default
Given an empty foo-theme directory
And a foo-theme/de_DE.po file:
"""
@ -1010,7 +1010,7 @@ Feature: Split PO files into JSON files.
msgid "Foo Theme"
msgstr "Foo Theme"
#: bar-minified.min.js:15
#: bar.min.minified.min.js:15
msgid "Foo Theme"
msgstr "Foo Theme"
@ -1030,7 +1030,7 @@ Feature: Split PO files into JSON files.
"""
And the return code should be 0
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
And the foo-theme/foo-theme-de_DE-da8cc4943ac0f9e5cc173c7de3f7299d.json file should exist
And the foo-theme/foo-theme-de_DE-a9c6627f5fe96185c0a0d0ddd8fa0216.json file should exist
Scenario: Allows processing custom file extensions
Given an empty foo-theme directory

Binary file not shown.

Binary file not shown.

View file

@ -263,11 +263,12 @@ class MakeJsonCommand extends WP_CLI_Command {
// Find all unique sources this translation originates from.
$sources = array_map(
static function ( $reference ) use ( $extensions ) {
$file = $reference[0];
$file = $reference[0];
$extension = pathinfo( $file, PATHINFO_EXTENSION );
return in_array( $extension, $extensions, true ) ? str_replace( '.min.', '.', $file ) : null;
return in_array( $extension, $extensions, true )
? preg_replace( "/.min.{$extension}$/", ".{$extension}", $file )
: null;
},
$this->reference_map( $translation->getReferences(), $map )
);

View file

@ -1,101 +0,0 @@
<?php
namespace WP_CLI\I18n\Tests;
use ReflectionClass;
use ReflectionMethod;
use WP_CLI\I18n\MakeJsonCommand;
use WP_CLI\Tests\TestCase;
use WP_CLI\Utils;
class MakeJsonTest extends TestCase {
/** @var string A path files are located */
private static $base;
/** @var \PHPUnit\Framework\MockObject\MockObject MakeJsonCommand partial mock */
private static $mock = null;
/** @var \ReflectionMethod make_json reflection (private) */
private static $make_json = null;
public function set_up() {
parent::set_up();
/**
* PHP5.4 cannot set property with __DIR__ constant.
* Shamelessly stolen from @see IterableCodeExtractorTest.php
*/
self::$base = Utils\normalize_path( __DIR__ ) . '/data/make_json/';
self::$mock = $this->createPartialMock( MakeJsonCommand::class, [ 'build_json_files' ] );
$reflection = new ReflectionClass( self::$mock );
self::$make_json = $reflection->getMethod( 'make_json' );
self::$make_json->setAccessible( true );
}
public function test_should_pass_array_of_extensions() {
$mock = $this->createPartialMock( MakeJsonCommand::class, [ 'make_json' ] );
$mock
->expects( $this->once() )
->method( 'make_json' )
->with(
$this->isType( 'string' ),
'foo',
null,
'',
[ 'ts', 'tsx' ]
)
->willReturn( [] );
$mock->__invoke(
[
self::$base . 'empty.po',
'foo',
],
[
'extensions' => '.ts, .tsx',
]
);
}
public function test_no_custom_extensions() {
self::$mock
->expects( $this->once() )
->method( 'build_json_files' )
->with(
$this->callback(
function ( $mapping ) {
$this->assertEquals( array_keys( $mapping ), [ 'baz.js', 'qux.js' ] );
return true;
}
),
$this->isType( 'string' ),
$this->isType( 'string' )
)
->willReturn( [] );
self::$make_json->invoke( self::$mock, self::$base . 'translations.po', 'bar', null, '', [] );
}
public function test_with_custom_extensions() {
self::$mock
->expects( $this->once() )
->method( 'build_json_files' )
->with(
$this->callback(
function ( $mapping ) {
$this->assertEquals( array_keys( $mapping ), [ 'foo.ts', 'bar.tag', 'baz.js', 'qux.js' ] );
return true;
}
),
$this->isType( 'string' ),
$this->isType( 'string' )
)
->willReturn( [] );
self::$make_json->invoke( self::$mock, self::$base . 'translations.po', 'bar', null, '', [ 'tag', 'ts' ] );
}
}

View file

@ -1,14 +0,0 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2025-04-25T10:38:48+02:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Language: \n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Domain: ts\n"

View file

@ -1,29 +0,0 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2025-04-25T10:38:48+02:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Domain: ts\n"
#: foo.ts:1
msgid "Hello TS"
msgstr ""
#: bar.tag:2
msgid "Hello TAG"
msgstr ""
#: baz.js:3
msgid "Hello JS"
msgstr ""
#: qux.min.js:4
msgid "Hello Minified JS"
msgstr ""