-
Notifications
You must be signed in to change notification settings - Fork 2
chore: change main()'s retval behavior #71
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
base: main
Are you sure you want to change the base?
Conversation
tests/test_cli.py
Outdated
| ret = main() | ||
|
|
||
| assert ret == 0 | ||
| assert ret == 26367 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come we know that 26367 means error X or Y? 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your question is absolutely valid.
First of all: during the review I found a bug. There was a duplicated check which used one more bit than it was necessary, so now the expected value is not 26367 but 9983.
9983 in binary format is 10 0110 1111 1111. The highest value is 2^(14-1).
Here you can see the listed error containers. As you can see there are 13 containers. If any container contains an error then its bit will turn on to 1: the first container (error_case_mistmatch) is 2^0 := 1, the second (error_action_order) is 2^1 := 2 and so on. The last (13th) one is the error_rule_hasnotest, its value can be 2^(13-1) := 4096.
There is one more possible error: the intendation error. This is a pre-check, it runs before the linter, so this "container" is not part of the linter class, and I didn't want to mix that. The most simple solution was to append at the end - this is why it uses len(c.error_vars).
Here is the full table:
| Error | Value |
|---|---|
| case mismatch error | 1 |
| action order error | 2 |
ctl:auditlogparts in wrong place |
4 |
undefined TX variable |
8 |
inconsistent PL tag |
16 |
inconsistent PL score |
32 |
duplicate id |
64 |
unknown tag |
128 |
| combined transformation and ignorecase | 256 |
no tag:CRS... |
512 |
no ver action or wrong value |
1024 |
rule uses tx:N without capture |
2048 |
| rule has no test | 4096 |
| indentation error | 8192 |
The last item is implicit an one, all others are explicit. This means if we extend the type of containers, the indentation error will have a new value.
The value 9983 = case mismatch error | action order error | ctl:auditlogparts in wrong place | undefined TX variable | inconsistent PL tag | inconsistent PL score | duplicate id | unknown tag | no tag:CRS... | no ver action or wrong value | indentation error.
Error flag "combined transformation and ignorecase", "rule uses tx:N without capture" and "rule has no test" flags aren't set. None the any rule has tests, but I filled the missing id's in linter's test so the last one is not set because of this.
I know this is impenetrable and hard to see but it's absolutely coherent and de-composable. I think this isn't necessary to understand, I just add this modification because here I offered a solution which was accepted by @theseion.
I can add some more code to make a map where we can check assertion's value not by an explicit integer, but a bit mask which compounded by flags, eg.
assert ret = linter.ERR_CASE_MISMATCH | linter.ERR_ACTION_ORDER | ...
Would that be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get the bitwise form of returning errors, just trying to understand this need. Because we didn't returned all the errors before, so now we need to interpret the results in a different way.
It is better to have a map at least to have it easy to read.
|
What do you think about this approach @theseion ? |
|
I'm not a fan of error codes, but this change is still an improvement. A way to look up the actual errors for tests would be great, yes. |
This PR changes the
main()function's behavior incli.py: now it returns with a non-zero value if any error occurred.This return value is a bit sequence and it's easy to trace the reason or easy to check the expected value in a test like we explained it in #57.
I also fixed
cli's test.