Skip to content

Commit 7451d62

Browse files
Add assertions for version in package-lock.json
Co-authored-by: Peter Wilson <[email protected]>
1 parent 377a929 commit 7451d62

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/phpunit/tests/theme.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,30 @@ public function test_version_consistency_in_package_json( string $theme ) {
364364
$this->assertSame( 1, preg_match( '/^Version: (\d+\.\d+(?:\.\d+)?)$/m', file_get_contents( $path_to_style_css ), $matches ), 'Expected Version in style.css' );
365365
$style_version = $matches[1];
366366

367+
// Version in style.css does not use patch versions, so supply one of 0.
368+
$version_with_patch = $matches[1];
369+
if ( 1 === substr_count( $version_with_patch, '.' ) ) {
370+
$version_with_patch .= '.0';
371+
}
372+
367373
$package_files = array( 'package.json', 'package-lock.json' );
368374
$present_package_files = array();
369375
foreach ( $package_files as $package_file ) {
370376
$path_to_package_json = $wp_theme->get_theme_root() . '/' . $wp_theme->get_stylesheet() . '/' . $package_file;
371377
if ( file_exists( $path_to_package_json ) ) {
372378
$present_package_files[] = $package_file;
373-
$package_json = json_decode( file_get_contents( $path_to_package_json ), true );
374-
$this->assertIsArray( $package_json, "Expected $package_file to be an array." );
375-
$this->assertArrayHasKey( 'name', $package_json, "Expected name key in $package_file." );
376-
$this->assertSame( $theme, $package_json['name'], "Expected name field to be the theme slug in $package_file." );
377-
$this->assertArrayHasKey( 'version', $package_json, "Expected version key in $package_file." );
378-
$this->assertSame( $style_version . '.0', $package_json['version'], "Expected version from style.css to match version in $package_file." );
379+
380+
$data = json_decode( file_get_contents( $path_to_package_json ), true );
381+
$this->assertIsArray( $data, "Expected $package_file to be an array." );
382+
$this->assertArrayHasKey( 'name', $data, "Expected name key in $package_file." );
383+
$this->assertSame( $theme, $data['name'], "Expected name field to be the theme slug in $package_file." );
384+
$this->assertArrayHasKey( 'version', $data, "Expected version key in $package_file." );
385+
$this->assertSame( $version_with_patch, $data['version'], "Expected version from style.css to match version in $package_file." );
386+
387+
if ( 'package-lock.json' === $package_file && isset( $data['packages'][''] ) ) {
388+
$this->assertArrayHasKey( 'version', $data['packages'][''], "Expected version key in packages[''] in package-lock.json." );
389+
$this->assertSame( $version_with_patch, $data['packages']['']['version'], "Expected version from style.css to match packages[''].version in package-lock.json." );
390+
}
379391
}
380392
}
381393
if ( count( $present_package_files ) > 0 ) {

0 commit comments

Comments
 (0)