Skip to content

Replace file based extension tests with inline ones #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

stloyd
Copy link
Contributor

@stloyd stloyd commented Sep 12, 2023

This change simplifies testing of extensions against custom build PHP binaries, also inside the phar!

stloyd@MacBook-Pro-2 spc-test % ./spc build curl,dom --build-cli
     _        _   _                 _
 ___| |_ __ _| |_(_) ___      _ __ | |__  _ __
/ __| __/ _` | __| |/ __|____| '_ \| '_ \| '_ \
\__ \ || (_| | |_| | (_|_____| |_) | | | | |_) |
|___/\__\__,_|\__|_|\___|    | .__/|_| |_| .__/   v2.0-rc6
                             |_|         |_|
[17:28:32] [INFO] [EXEC] sysctl -n hw.ncpu
[17:28:32] [INFO] Build target: cli
[17:28:32] [INFO] Enabled extensions: curl, dom
[17:28:32] [INFO] Required libraries: zlib, openssl, curl, libiconv, libxml2
(...)
[17:31:01] [INFO] [EXEC] make -j10 EXTRA_CFLAGS='-g -Os' EXTRA_LIBS=' -liconv -framework CoreFoundation -framework SystemConfiguration  /Users/stloyd/Documents/spc-test/buildroot/lib/libxml2.a /Users/stloyd/Documents/spc-test/buildroot/lib/libiconv.a /Users/stloyd/Documents/spc-test/buildroot/lib/libcharset.a /Users/stloyd/Documents/spc-test/buildroot/lib/libcurl.a /Users/stloyd/Documents/spc-test/buildroot/lib/libssl.a /Users/stloyd/Documents/spc-test/buildroot/lib/libcrypto.a /Users/stloyd/Documents/spc-test/buildroot/lib/libz.a -lresolv' cli
[17:33:29] [INFO] [EXEC] dsymutil -f sapi/cli/php
[17:33:30] [INFO] [EXEC] strip sapi/cli/php
[17:33:30] [INFO] Deploying cli file
[17:33:30] [INFO] [EXEC] cp '/Users/stloyd/Documents/spc-test/source/php-src/sapi/cli/php' '/Users/stloyd/Documents/spc-test/buildroot/bin/'
[17:33:30] [INFO] running cli sanity check
[17:33:30] [INFO] [EXEC] /Users/stloyd/Documents/spc-test/buildroot/bin/php -r "echo \"hello\";"
[17:33:31] [INFO] [EXEC] /Users/stloyd/Documents/spc-test/buildroot/bin/php -r "assert(function_exists('curl_init'));"
[17:33:32] [INFO] [EXEC] /Users/stloyd/Documents/spc-test/buildroot/bin/php -r "assert(class_exists('\DOMDocument'));$doc = new DOMDocument();$doc->loadHtml('<html><head><meta charset="UTF-8"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body id='app'>Hello</body></html>');assert($doc->getElementById('app')->nodeValue === 'Hello');"
[17:33:32] [INFO] Build complete, used 299.775 s !
[17:33:32] [INFO] Static php binary path: /Users/stloyd/Documents/spc-test/buildroot/bin/php
[17:33:32] [INFO] License path: /Users/stloyd/Documents/spc-test/buildroot/license/

@stloyd
Copy link
Contributor Author

stloyd commented Sep 12, 2023

This (along with #175) should allow to fix the #124

Tested locally, logs in description were generated by custom-built PHP binary with SPC phar combined!

@crazywhalecc crazywhalecc added need test This PR has not been tested yet, cannot merge now kind/framework Issues related to CLI app framework and removed need test This PR has not been tested yet, cannot merge now labels Sep 13, 2023
@crazywhalecc
Copy link
Owner

crazywhalecc commented Sep 13, 2023

I feel that including code in json is not a good solution. Maybe directly reading the existing xxx.php and use -r is an option, or putting these codes in the Extension.php file and using an abstract method.

@stloyd
Copy link
Contributor Author

stloyd commented Sep 13, 2023

I may agree that it looks confusing to have php-alike code in json file. I will play with other options today.

In the worst case, are you open to replacing that json file with another approach? (like php files or array?)

@crazywhalecc
Copy link
Owner

crazywhalecc commented Sep 13, 2023

I have previously considered using PHP to replace the current JSON format configuration file, for example lib.json becomes lib.php, but it will look a bit ambiguous with today's Library class. The first local version I had was even a version that did not use configuration files at all (all configurations were in the corresponding library and extension classes).

Now in the end I decided to use json and completely separate the PHP code for a simple purpose: everything seems to be low coupling, these configuration files are more like metadata that does not change, and the compiled code and parameters are like functions part.

If we use PHP as a configuration file, it is inevitable that dynamically executed code will be included in the configuration file. Maybe writing all configuration items directly as classes and deleting the configuration files is a more reasonable choice (just like the solution I originally abandoned).

@crazywhalecc
Copy link
Owner

crazywhalecc commented Sep 13, 2023

Now it is indeed a troublesome question: should the test code of these extensions be part of the logic code or a static configuration? Honestly I have no idea. This is why I planned to put these test files into a separate tests directory.

@simonhamp
Copy link
Contributor

I vote for separation of test code from configuration metadata. The tests MUST be PHP (if you're using PHP to run them), the other can be anything (e.g. JSON)

Why not simply include a relative path reference in the JSON metadata to any test file? e.g.

    "bcmath": {
        "type": "builtin"
        "type": "builtin",
        "tests": "./tests/bcmath.php"
    },

This is explicit, can easily be resolved from whichever context is executing the build process, and acts as a pointer for any developer looking to see how things are wired up without having to dive into code.

@crazywhalecc
Copy link
Owner

crazywhalecc commented Sep 13, 2023

@simonhamp This seems to be possible and a compromise. Anyway, I think we finally use -r to run the extension test code. So if we want to use a file, spc needs to read the file and remove the <?php tag.

@simonhamp
Copy link
Contributor

@crazywhalecc the -r flag only seems to have been added in this PR. The "original" way (at least from the perspective of the changes here) was pointing to a PHP path - I don't see that needs to change

I guess I'm struggling to see how changing to using -r has made the tests pass - it looks like the failure is due to a path resolution issue when the test files are inside a .phar... isn't the fix here to find that and resolve it rather than moving all of the testing code from one execution context to another?

It feels super brittle to be interpolating PHP from a file (any file, JSON or PHP) into a shell string.

@crazywhalecc
Copy link
Owner

crazywhalecc commented Sep 13, 2023

It feels super brittle to be interpolating PHP from a file (any file, JSON or PHP) into a shell string.

@simonhamp This is true. But in general, these test codes are theoretically controllable, because they are only in the src directory within the project, and everyone can check their security. What may need to be done is to escape special characters. I also mentioned three options I can think of in issue #124, the first two seem riskier than this one.

In addition, I also thought that on Unix platform, it can be executed through pipes:

  1. Create a command that outputs the extension test code, such as bin/spc dev:ext-check bcmath, and then output the code of tests/bcmath.php.
  2. Use pipes to pass to the compiled php-cli: bin/spc dev:ext-check xxx | buildroot/bin/php

But this solution also has problems: it is not possible to obtain its own executable path now after packaging Phar and phpmicro executable.

@simonhamp
Copy link
Contributor

@crazywhalecc I agree with you, there's minimal concern from a security perspective.

I meant 'brittle' in the sense that they could be the source of various gotchas with interpolation depending on how convoluted the test needs to be, something that even escaping may not help with.

It still feels like a path resolution issue to me...

@crazywhalecc
Copy link
Owner

It still feels like a path resolution issue to me...

I have convinced myself not to trapped in this Phar path issue, because after using phar and phpmicro sfx, everything related to the path became anxious, whether reading an internal file from inside Phar or outside.

I once thought that the ULTIMATE is to manually generate a PHP file containing the test code for each extension, and then call it on demand: buildroot/bin/php sanity_check.php bcmath.

@crazywhalecc crazywhalecc added the help wanted Extra attention is needed label Sep 13, 2023
@stloyd
Copy link
Contributor Author

stloyd commented Sep 13, 2023

For me the best for now (till someone finds "better" way) is to inline the test files with trim as suggested by @crazywhalecc, provided an alternative PR: #182

Closing this one as no-go.

@stloyd stloyd closed this Sep 13, 2023
@stloyd stloyd deleted the ext-tests branch September 13, 2023 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed kind/framework Issues related to CLI app framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants