SPC_store.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. // mock global functions
  4. namespace SPC\store;
  5. use SPC\exception\RuntimeException;
  6. function f_exec(string $command, mixed &$output, mixed &$result_code): bool
  7. {
  8. $result_code = 0;
  9. if (str_contains($command, 'https://api.github.com/repos/AOMediaCodec/libavif/releases')) {
  10. $output = explode("\n", gzdecode(file_get_contents(__DIR__ . '/../assets/github_api_AOMediaCodec_libavif_releases.json.gz')));
  11. return true;
  12. }
  13. if (str_contains($command, 'https://api.github.com/repos/AOMediaCodec/libavif/tarball/v1.1.1')) {
  14. $output = explode("\n", "HTTP/1.1 200 OK\r\nContent-Disposition: attachment; filename=AOMediaCodec-libavif-v1.1.1-0-gbb24db0.tar.gz\r\n\r\n");
  15. return true;
  16. }
  17. if (str_contains($command, 'https://api.bitbucket.org/2.0/repositories/')) {
  18. $output = explode("\n", json_encode(['values' => [['name' => '1.0.0']], 'tag_name' => '1.0.0']));
  19. return true;
  20. }
  21. if (str_contains($command, 'https://bitbucket.org/')) {
  22. $output = explode("\n", str_contains($command, 'MATCHED') ? "HTTP/2 200 OK\r\ncontent-disposition: attachment; filename=abc.tar.gz\r\n\r\n" : "HTTP/2 200 OK\r\n\r\n");
  23. return true;
  24. }
  25. if (str_contains($command, 'ghreltest/ghrel')) {
  26. $output = explode("\n", json_encode([[
  27. 'prerelease' => false,
  28. 'assets' => [
  29. [
  30. 'name' => 'ghreltest.tar.gz',
  31. 'browser_download_url' => 'https://fakecmd.com/ghreltest.tar.gz',
  32. ],
  33. ],
  34. ]]));
  35. return true;
  36. }
  37. if (str_contains($command, 'filelist')) {
  38. $output = explode("\n", gzdecode(file_get_contents(__DIR__ . '/../assets/filelist.gz')));
  39. return true;
  40. }
  41. $result_code = -2;
  42. $output = null;
  43. return false;
  44. }
  45. function f_passthru(string $cmd): bool
  46. {
  47. if (str_starts_with($cmd, 'git')) {
  48. if (str_contains($cmd, '--branch "SIGINT"')) {
  49. throw new RuntimeException('Interrupt', 2);
  50. }
  51. return true;
  52. }
  53. if (str_contains($cmd, 'https://fakecmd.com/curlDown')) {
  54. if (str_contains($cmd, 'SIGINT')) {
  55. throw new RuntimeException('Interrupt', 2);
  56. }
  57. return true;
  58. }
  59. // allowed commands
  60. $allowed = ['cp', 'copy', 'xcopy'];
  61. foreach ($allowed as $a) {
  62. if (str_starts_with($cmd, $a)) {
  63. \f_passthru($cmd);
  64. return true;
  65. }
  66. }
  67. throw new RuntimeException('Invalid tests');
  68. }