Skip to content
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

fix(test): exit code of lime test #301

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

alquerci
Copy link
Contributor

@alquerci alquerci commented Jan 1, 2024

Lime test should exit with non-zero code on failure.

Before this patch, it is not the case.

@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from f4f102e to 46a8d60 Compare January 1, 2024 09:41
@alquerci alquerci changed the title [WIP] fix(test): exit code of lime test fix(test): exit code of lime test Jan 1, 2024
@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from 46a8d60 to a60ff02 Compare January 5, 2024 20:38
@thePanz thePanz force-pushed the fixtest-exit-code-of-lime-test branch from a60ff02 to 05ac333 Compare January 17, 2024 23:42
@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from 05ac333 to 154bc18 Compare January 19, 2024 18:36
@alquerci
Copy link
Contributor Author

Rebased on master branch

Copy link
Collaborator

@thirsch thirsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch is also included in #302, isn't it?

@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from 154bc18 to 136b3c1 Compare March 4, 2024 19:39
@alquerci
Copy link
Contributor Author

alquerci commented Mar 4, 2024

This patch is also included in #302, isn't it?

Yes, but, I suggest merging this one before #302.

@@ -155,6 +161,31 @@ public function __destruct()
}

flush();

self::$allExitCode |= $this->getExitCode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a binary operation, done on an integer value, we should cast it to a bool before, or rename getExitCode() to isTestFailed()

WDYT?

@@ -24,9 +24,13 @@ class lime_test
protected $options = array();

static protected $all_results = array();
static private $instanceCount = 0;
static private $allExitCode = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about $overallSuccessful = true?

$plan = $this->results['stats']['plan'];
$failed = count($this->results['stats']['failed']);
$total = $this->results['stats']['total'];
is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rewrite this as an if statement?

Comment on lines 179 to 188
if ($failed)
{
return 1;
}
else if ($total == $plan)
{
return 0;
}

return 1;
Copy link
Member

@thePanz thePanz Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ($failed)
{
return 1;
}
else if ($total == $plan)
{
return 0;
}
return 1;
if (0 !== $failed) {
return 1;
}
return $total == $plan ? 0 : 1;

@alquerci
Copy link
Contributor Author

@thePanz thanks for your review.

Mainly, your feedbacks are only about having better code structure.

So I will proceed with fixing code structure.
Then I will ping you when done.

@alquerci
Copy link
Contributor Author

After rebase on master branch, I got this error.

$ test/bin/test 
+ docker-compose build
ERROR: The Compose file is invalid because:
Service php81 has neither an image nor a build context specified. At least one must be provided.

I know how to fix it.
But I am curious, if there are new prerequisites for testing.

https://github.com/FriendsOfSymfony1/symfony1/blob/master/README.md?plain=1#L61-L64

@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from 136b3c1 to dd6a51c Compare March 25, 2024 20:06
self::$allExitCode |= $this->getExitCode();

if (0 === self::$instanceCount) {
exit(self::$allExitCode);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exit(self::$allExitCode);
exit(self::$allExitCode);

$plan = $this->results['stats']['plan'];
$failed = count($this->results['stats']['failed']);
$total = $this->results['stats']['total'];
is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please improve the readability of this. This line makes me think of perl or bash code before php.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is it a valuation that is a mistake? 🤔

Comment on lines 21 to 20
const STATE_PASS = 0;
const STATE_FAIL = 1;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add visibility.

test/bin/test Outdated
#
dependencyPreferences='highest'
skipPHPVersions='php8'
main ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to keep the context of the PR.

@@ -50,6 +50,7 @@ services:
php81:
<<: *services_php74
build:
context: .docker
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to keep the context of the PR.

@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch 2 times, most recently from fa11b69 to f25c0cd Compare March 27, 2024 00:45
@alquerci alquerci marked this pull request as draft March 27, 2024 00:46
@connorhu
Copy link
Collaborator

Return types for private methods! Nice! That's what I was thinking, that I could do it everywhere in the framework. Thank you!

@@ -952,8 +1015,7 @@ function lime_shutdown()
);

ob_start();
// see http://trac.symfony-project.org/ticket/5437 for the explanation on the weird "cd" thing
Copy link
Collaborator

@connorhu connorhu Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm copying the content of the trac ticket here to keep what it was:

[PATCH] lime.php not working on windows when there are spaces in php interpreter path

lime.php, line 541

passthru(sprintf('%s "%s" 2>&1', $this->php_cli, $file), $return);

the command fails if the path to php interpreter contains spaces, this obviously prevent all test to run.

In order to work with executables in directories with spaces it should be

passthru(sprintf('"%s" "%s" 2>&1', $this->php_cli, $file), $return);

but this fails too on windows: system/exec/passthru all fail when more than two arguments are quoted (it seems a php bug but I couldn't find any reference apart from some comments on the system function page)

In order to work on windows it should be written as

passthru(sprintf('""%s" "%s"" 2>&1', $this->php_cli, $file), $return);

this works on windows but still fails on linux (note the double "double quotes": I need to quote the entire command not just the executable and the argument)

The only workaround I could find was something I found on the comments of the system function that is adding a "nop" command call before the quoted command

passthru(sprintf('cd & "%s" "%s" 2>&1', $this->php_cli, $file), $return);

@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from f25c0cd to d094b34 Compare March 27, 2024 20:15
@connorhu
Copy link
Collaborator

Apart from the fact that it's red now, I'm happy with the state of it! Well done!

@alquerci
Copy link
Contributor Author

It was not only about code structure, but the behavior has been exposed with the proof that work as expected (tests).

The current tests that failed introduce the fact that it should treat PHP errors/warnings/... as test failure with failed status code.

This failed test is like a marker to tell me what's next, for the next coding session.


Now I wonder if that is it useful to expose the behavior when an Exception or an Error is thrown.

As the migration to PHPUnit will make this obsolete.
Anyway, that's a good training for improving legacy code structure, like a kata.

@connorhu
Copy link
Collaborator

Now I wonder if that is it useful to expose the behavior when an Exception or an Error is thrown.

If hidden errors occur, yes. If valuable information about the state of the code is obtained, yes. If it just generates noise, no. The effects of such a change are only visible to you at the moment.

As the migration to PHPUnit will make this obsolete.

What you did was not useless, because it will take a long time to migrate all the tests under phpunit. Looking at the current tempo, an optimistic estimate is that one tenth of the tests will be moved to phpunit this year.

@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from d094b34 to c8ff5bc Compare March 29, 2024 22:53
@alquerci alquerci force-pushed the fixtest-exit-code-of-lime-test branch from 7e19aea to d943f48 Compare March 31, 2024 21:21
@alquerci
Copy link
Contributor Author

@thePanz
Copy link
Member

thePanz commented Apr 3, 2024

There was a point when PR was doing perfectly what you set out to do. But the changes that have been made since then have made it impossible to follow what PR is actually doing. In my language, there is a saying that "fall off the other side of the horse".

@alquerci I agree here: the class extraction is a good thing to have, but makes this PR quite hard to follow. yes, maybe this is a "too much refactoring". WDYT?

@alquerci
Copy link
Contributor Author

alquerci commented Apr 3, 2024

@thePanz I agree too.

What do you suggest?
Does it needs to extract the class extraction on another PR, or it is enough to have it on a dedicated commit like now?

Related to #356

@connorhu
Copy link
Collaborator

connorhu commented Apr 4, 2024

I propose a separate PR for cleaning, cs-fix and separation.

@alquerci
Copy link
Contributor Author

alquerci commented Apr 4, 2024

I propose a separate PR for cleaning, cs-fix and separation.

To rephrase

  • One PR: make it work
    • with clean lime tests
    • with minimum code to make tests passes
    • with all commit history kept that show each baby steps
  • One PR: make it clean
    • with extract methods
    • with move classes from big file to dedicated files
      • New files comes with CS fix
    • with all commit history kept that show each baby steps

That's a nice kata, I like it.

@connorhu Should I do it?

How I will do it?

First PR
  1. Delete all new production code, but keep the tests
  2. Commit
  3. Change tests expectations to make them passes
  4. Change expectations one by one to reach new behaviour
  5. One by one make them all passes
  6. All of that steps without any cleaning

The code will be hard to read, but tests will pass.

Second PR
  1. Create a PR based on first PR
  2. This will prevent any modification to first PR, to avoid extra work when rebase.
  3. Clean the code around diff of the first PR

OR

  1. Wait for the first PR to be merged
  2. Start cleaning

@connorhu
Copy link
Collaborator

connorhu commented Apr 5, 2024

If clean code is a prerequisite for successful test execution, the PR order is: cleanup, exit code. If clean code is not a prerequisite for successful test execution: exit code, cleanup.
Am I missing something? Maybe I'm missing something.

@alquerci
Copy link
Contributor Author

alquerci commented Apr 5, 2024

@connorhu It is something between.

To be able to clean the code (having a better code structure).
=> The prerequisite is to have a test suite that you trust, if it pass you can ship it.

After that a trusted test suite is in place.
=> You can clean it, but it will be useless to touch code that does not need to be changed for fixing the exit code.

After that you make one small test pass
And if you do not clean the test then the production code.
=> It will be even harder to clean them all at the end.
=> It will be harder to make all tests pass in short term, with code hard to modify.

Instead, start to make one small step to the new behaviour (as steps I used on this PR).

  1. Change one expectation
  2. Make it pass with the minimal code change
  3. Make test code clean
  4. Make production code clean
  5. Change second expectation
  6. Make it pass with the minimal code change
  7. Make test code clean
  8. Make production code clean
    N. ...

So here, I see no problem to restart the work on this.
This time I will keep all commit history.
But without cleaning the code progressively it will be a challenge that I accept.

What do you think?

@connorhu
Copy link
Collaborator

connorhu commented Apr 5, 2024

You can clean it, but it will be useless to touch code that does not need to be changed for fixing the exit code.

Here's what we think differently about. There are many PRs that are created because we see something in the code as a cleaning, tidying up process. The fact that lime classes are placed in a file is incorrect in the current practice of symfony core. The fact that indentation is 2 spaces is actually incorrect according to the current code base of symfony core.

So we need one (or two) organizer PRs that will eliminate this inconsistency. There is no need for these PRs to be created because of the "exit code" issue. If I previously notice lime using 2 space indentation I will have already cleaned it up (or added the rule to editorconfig).

If you want to fix the "exit code" problem, you must do it in a way that the fix is 100% understandable and transparent (so you must do the cleanup first or last).

Regardless of whether we're talking about 2-3 PRs, the end result of PRs in a row is that you have a more reliable test system, so you've achieved your goal.

To me, a measure of the reliability of a change is a) the code logic is unchanged (processes that are done by programs of proven quality, e.g. rector, cs-fixer) b) the change is verified by tests.

@alquerci
Copy link
Contributor Author

alquerci commented Apr 5, 2024

I am closing this PR.

In order to provide a fix 100% understandable and transparent.

  1. I will create a new PR with the proof of the current behaviour.
    a. fix(test): exit code of lime test -- add proof of current behaviour #371
  2. Then another PR, with the change on the behaviour.
  3. Then another PR, with the local cleaning.
  4. Then let an organizer to make a PR, with the structural cleaning.

@connorhu @thePanz does it's steps are ok for you?

@connorhu
Copy link
Collaborator

connorhu commented Apr 9, 2024

@alquerci I don't think you should close this PR. If I'm right, this is step 2 of 4. The 4 steps sketched out seem logical to me.

@alquerci
Copy link
Contributor Author

alquerci commented Apr 9, 2024

Okay, @connorhu thanks for your feedback.

Let's go, I will keep open this PR with force push when step 1 is done.

@alquerci
Copy link
Contributor Author

alquerci commented Apr 9, 2024

1st step done

#371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants