test: Add util_ArgParsing test #17390

pull ryanofsky wants to merge 1 commits into bitcoin:master from ryanofsky:pr/argparse-test changing 1 files +54 −0
  1. ryanofsky commented at 11:53 PM on November 5, 2019: member

    ArgsManager test coverage for parsing of integer and boolean values is currently very poor and doesn't give us a way of knowing whether changes to ArgsManager may unintentionally break backwards compatibility, so this adds a new test to catch regressions.

  2. Add util_ArgParsing test
    ArgsManager test coverage for parsing of integer and boolean values is
    currently very poor and doesn't give us a way of knowing whether changes to
    ArgsManager may unintentionally break backwards compatibility, so this adds a
    new test to catch regressions.
    286f197704
  3. ryanofsky force-pushed on Nov 6, 2019
  4. fanquake added the label Tests on Nov 6, 2019
  5. in src/test/util_tests.cpp:280 in 9c1b60bc07 outdated
     268 | +    TestParse("5.5", true, 5);
     269 | +    TestParse("x", false, 0);
     270 | +    TestParse("x0", false, 0);
     271 | +    TestParse("x5", false, 0);
     272 | +    TestParse("0x", false, 0);
     273 | +    TestParse("5x", true, 5);
    


    laanwj commented at 9:08 AM on November 6, 2019:

    I really don't like setting these in stone. Let's test desired behavior. Instead of adding tests for the current insane behavior (which relies on implementation details of atoi), I think we need to work on getting errors to the user for these.


    MarcoFalke commented at 12:24 PM on November 6, 2019:

    I think it is easier to review "fixes" when they are also documented with test changes.


    laanwj commented at 12:30 PM on November 6, 2019:

    I'm not sure about that. It could just as well bite anyone that dares to change this parsing in the future. This behavior was never documented so no one should be relying on it. (and a test is essentially "this should work" documentation)


    ryanofsky commented at 1:01 PM on November 6, 2019:

    I really don't like setting these in stone.

    Like Marco is saying the goal of the test is to make it easier not harder to fix these confusing parsings in the future. Easier as a developer because you can test the effects of your change with a simple make check, easier as a reviewer to underand the change. Easier as a developer because after implementing the desired behavior you don't have to write a new test to gain coverage, but can just update a line in the existing test.

    I agree with laanwj's objection broadly that it would be bad to add tests which indirectly rely on parsing behaviors, because they would be fragile and difficult to update when parsing is cleaned up. But this is a simple, direct test added specifically so we can be confident that when we intend to make parsing changes, they work as expected, and that when we intend to make other changes, we don't unknowingly break parsing.

    I'm still planning on reviewing #17385 today, too. Vast majority of changes there look very good and can go a new pr even if #17385 won't be reopened.


    laanwj commented at 1:08 PM on November 6, 2019:

    OK, fair enough. To be clear I agree with adding this test conceptually, and some of the additional cases like "spaces around the argument should be ignored" seem decent but say, we probably don't want a test for "what atoi regards as whitespace depends on your locale" :cry: anyhow, if this is with the understanding that these aren't supposed to be guaranteed to be supported for the infinite future, I'm ok with this list


    MarcoFalke commented at 1:17 PM on November 6, 2019:

    Maybe a comment that says the tests are fragile and meant to be removed could help?


    ryanofsky commented at 1:27 PM on November 6, 2019:

    Maybe a comment that says the tests are fragile and meant to be removed could help?

    There is a general comment already that "Some of these cases could be ambiguous or surprising to users, and might be worth triggering errors or warning in the future", but I could also note something like "// Confusing parsing, should be changed or disallowed in the future" on specific cases if desired.

    but say, we probably don't want a test for "what atoi regards as whitespace depends on your locale"

    I didn't think of that but definitely agree.


    laanwj commented at 2:12 PM on November 6, 2019:

    "// Confusing parsing, should be changed or disallowed in the future" on specific cases if desired.

    I think that's a good idea, to split the list into "normal" and "expected" cases and "parser details leaking through please don't do this" cases.


    ryanofsky commented at 2:56 PM on November 6, 2019:

    "// Confusing parsing, should be changed or disallowed in the future" on specific cases if desired.

    I think that's a good idea, to split the list into "normal" and "expected" cases and "parser details leaking through please don't do this" cases.

    I started to do this, but wasn't actually sure which specific cases people may want to change in the future, and figured it would be best to leave decisions to future PRs actually implementing fixes, rather than add my opinions or speculation here. For example I think leading and trailing spaces should probably just be ignored and not made into errors, and probably 5.0 and 5.5 should be errors but maybe 5. should be allowed. Unclear whether numbers with extra leading 0's should be allowed because maybe they could be mistaken for octal. Personally I think it's strange to interpret empty string "" as true and space " " as false (especially when after #15934 and #16545 we could begin to treat -flag and -flag= distinctly), and I also think we could support true false yes no for boolean flags in the future, but who knows what people are doing with existing bools today. Basically people could have opinions about every single case here except maybe the 0 case. Even the 5 case could arguably trigger an error or warning in the future if specified as the value to a boolean flag.


    laanwj commented at 3:13 PM on November 6, 2019:

    I also think we could support true false yes no for boolean flags in the future

    Sure, but even in that case, they probably shouldn't all evaluate as 0/false. 5. and even 5.0 such at least do what could be remotely expected (truncating decimal). Accepting faux hexadecimal is pretty bad though (which is why I initially commented on this line specifically)

    But I agree this might not be the place for this discussion. I'm sorry for starting this in the first place. I honestly didn't know anyone had such strong opinions about this, and would have let this sleeping dog lie if I knew.


    ryanofsky commented at 3:48 PM on November 6, 2019:

    I honestly didn't know anyone had such strong opinions about this, and would have let this sleeping dog lie if I knew.

    Heh, I will talk to you all day about settings. But thanks for starting this. Previously, I assumed we actually had pretty good test coverage for parsing settings strings. I think filling the gap can prevent confusing and potentially dangerous bugs like #14523 (comment) in the future.

    I also think we could support true false yes no for boolean flags in the future

    Sure, but even in that case, they probably shouldn't all evaluate as 0/false.

    Of course yes. By "support" I meant interpret true as true instead of false

  6. MarcoFalke renamed this:
    Add util_ArgParsing test
    test: Add util_ArgParsing test
    on Nov 6, 2019
  7. MarcoFalke commented at 1:40 PM on November 6, 2019: member

    ACK 9c1b60bc0793207295a8355df83ffcf094aa77c8

  8. ryanofsky force-pushed on Nov 6, 2019
  9. ryanofsky commented at 2:15 PM on November 6, 2019: member

    Updated 9c1b60bc0793207295a8355df83ffcf094aa77c8 -> fc76edec6140be51cfc1b44900aaf0139fdc4950 (pr/argparse-test.2 -> pr/argparse-test.3, compare) to make comment a little clearer and avoid 99999 special case for int parsing Updated fc76edec6140be51cfc1b44900aaf0139fdc4950 -> 286f197704e82045c762d332aba5d1ac52e0212d (pr/argparse-test.3 -> pr/argparse-test.4, compare) with a handful more test cases

  10. in src/test/util_tests.cpp:242 in fc76edec61 outdated
     237 | +    test.SetupArgs({{"-value", ArgsManager::ALLOW_ANY}});
     238 | +    std::string arg = "-value=" + str;
     239 | +    const char* argv[] = {"ignored", arg.c_str()};
     240 | +    std::string error;
     241 | +    BOOST_CHECK(test.ParseParameters(2, (char**)argv, error));
     242 | +    BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), expected_bool);
    


    promag commented at 2:56 PM on November 6, 2019:

    nit, if you have to push again maybe include BOOST_CHECK(error.empty());.

  11. ryanofsky force-pushed on Nov 6, 2019
  12. promag commented at 3:02 PM on November 6, 2019: member

    ACK fc76edec6140be51cfc1b44900aaf0139fdc4950, agree that splitting the cases now will lead to unnecessary discussions in this PR which adds a nice test - and surprising results to me. Also agree that the best place to discuss whether a case should be changed is in the respective PR.

  13. promag commented at 3:03 PM on November 6, 2019: member

    ACK 286f197, more surprising results 😱

  14. laanwj commented at 4:00 PM on November 6, 2019: member

    ACK 286f197704e82045c762d332aba5d1ac52e0212d

  15. laanwj referenced this in commit 6f4e247357 on Nov 6, 2019
  16. laanwj merged this on Nov 6, 2019
  17. laanwj closed this on Nov 6, 2019

  18. deadalnix referenced this in commit 06cb9dd780 on Nov 4, 2020
  19. PastaPastaPasta referenced this in commit 4ff4ac6e03 on Jun 27, 2021
  20. PastaPastaPasta referenced this in commit 8a853a0d92 on Jun 28, 2021
  21. PastaPastaPasta referenced this in commit 95c3972474 on Jun 29, 2021
  22. DrahtBot locked this on Dec 16, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-25 00:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me