[PHP-WEBMASTER] [web-downloads] main: Fix parsing deps files in winlibs command

Author: Shivam Mathur (shivammathur)
Date: 2025-03-15T18:28:58+05:30

Commit: Fix parsing deps files in winlibs command · php/web-downloads@126c0e4 · GitHub
Raw diff: https://github.com/php/web-downloads/commit/126c0e410ff7d61cc46cf5cb3d3062858228ad3a.diff

Fix parsing deps files in winlibs command

Changed paths:
  M src/Console/Command/WinlibsCommand.php
  M tests/Console/Command/WinlibsCommandTest.php

Diff:

diff --git a/src/Console/Command/WinlibsCommand.php b/src/Console/Command/WinlibsCommand.php
index 3aeb89b..78b91ca 100644
--- a/src/Console/Command/WinlibsCommand.php
+++ b/src/Console/Command/WinlibsCommand.php
@@ -60,21 +60,20 @@ public function handle(): int
         }
     }

- private function parseFiles(array $files): array
+ public function parseFiles(array $files): array
     {
         $data = ;
         foreach ($files as $file) {
             $fileName = basename($file);
- $fileNameParts = explode('.', $fileName);
- $parsedFileNameParts = explode('-', $fileName);
- $archParts = explode('.', $parsedFileNameParts[3]);
+ $pattern = '/^(?P<artifact>.+?)-(?P<version>\d.*)-(?P<vs>vs\d+)-(?P<arch>[^.]+)\.zip$/';
+ preg_match($pattern, $fileName, $matches);
             $data = [
- 'file_path' => $file,
- 'file_name' => $fileName,
- 'extension' => $fileNameParts[count($fileNameParts)-1],
- 'artifact_name' => $parsedFileNameParts[0],
- 'vs_version' => $parsedFileNameParts[2],
- 'arch' => $archParts[0],
+ 'file_path' => $file,
+ 'file_name' => $fileName,
+ 'extension' => 'zip',
+ 'artifact_name' => $matches['artifact'],
+ 'vs_version' => $matches['vs'],
+ 'arch' => $matches['arch'],
             ];
         }
         return $data;
diff --git a/tests/Console/Command/WinlibsCommandTest.php b/tests/Console/Command/WinlibsCommandTest.php
index b966752..31154b4 100644
--- a/tests/Console/Command/WinlibsCommandTest.php
+++ b/tests/Console/Command/WinlibsCommandTest.php
@@ -173,4 +173,67 @@ public function testHandlesCorruptDataFile(): void
         $this->assertStringContainsString('Syntax error', $output);
         $this->assertEquals(1, $result);
     }
+
+
+ #[DataProvider('fileProvider')]
+ public function testParseFiles($file, $expected): void
+ {
+ $command = new WinlibsCommand();
+ $result = $command->parseFiles([$file]);
+ $this->assertEquals($expected, $result[0]);
+ }
+
+ public static function fileProvider(): array
+ {
+ return [
+ ['/tmp/net-snmp-5.7.3-1-vs16-x86.zip', [
+ 'file_path' => '/tmp/net-snmp-5.7.3-1-vs16-x86.zip',
+ 'file_name' => 'net-snmp-5.7.3-1-vs16-x86.zip',
+ 'extension' => 'zip',
+ 'artifact_name' => 'net-snmp',
+ 'vs_version' => 'vs16',
+ 'arch' => 'x86',
+ ]],
+ ['/tmp/libxml2-2.9.14-1-vs16-x86.zip', [
+ 'file_path' => '/tmp/libxml2-2.9.14-1-vs16-x86.zip',
+ 'file_name' => 'libxml2-2.9.14-1-vs16-x86.zip',
+ 'extension' => 'zip',
+ 'artifact_name' => 'libxml2',
+ 'vs_version' => 'vs16',
+ 'arch' => 'x86',
+ ]],
+ ['/tmp/c-client-2007f-1-vs16-x86.zip', [
+ 'file_path' => '/tmp/c-client-2007f-1-vs16-x86.zip',
+ 'file_name' => 'c-client-2007f-1-vs16-x86.zip',
+ 'extension' => 'zip',
+ 'artifact_name' => 'c-client',
+ 'vs_version' => 'vs16',
+ 'arch' => 'x86',
+ ]],
+ ['/tmp/nghttp2-1.57.0-vs16-x86.zip', [
+ 'file_path' => '/tmp/nghttp2-1.57.0-vs16-x86.zip',
+ 'file_name' => 'nghttp2-1.57.0-vs16-x86.zip',
+ 'extension' => 'zip',
+ 'artifact_name' => 'nghttp2',
+ 'vs_version' => 'vs16',
+ 'arch' => 'x86',
+ ]],
+ ['/tmp/openssl-1.1.1w.pl1-vs16-x86.zip', [
+ 'file_path' => '/tmp/openssl-1.1.1w.pl1-vs16-x86.zip',
+ 'file_name' => 'openssl-1.1.1w.pl1-vs16-x86.zip',
+ 'extension' => 'zip',
+ 'artifact_name' => 'openssl',
+ 'vs_version' => 'vs16',
+ 'arch' => 'x86',
+ ]],
+ ['/tmp/zlib-1.2.12-vs16-x86.zip', [
+ 'file_path' => '/tmp/zlib-1.2.12-vs16-x86.zip',
+ 'file_name' => 'zlib-1.2.12-vs16-x86.zip',
+ 'extension' => 'zip',
+ 'artifact_name' => 'zlib',
+ 'vs_version' => 'vs16',
+ 'arch' => 'x86',
+ ]],
+ ];
+ }
}