test: Add format-dependent comparison to bctest #9032

pull laanwj wants to merge 1 commits into bitcoin:master from laanwj:2016_10_bctest_smart_compare changing 1 files +27 −3
  1. laanwj commented at 12:10 PM on October 27, 2016: member

    This splits the output comparison for bitcoin-tx into two steps:

    • First, check for data mismatch, parsing the data as json or hex depending on the extension of the output file
    • Then, check if the literal string matches

    For either of these cases give a different error.

    This prevents wild goose chases when e.g. a trailing space doesn't match exactly (by helping narrow down the problem), and makes sure that both test output and examples are valid data of the purported format.

  2. laanwj added the label Tests on Oct 27, 2016
  3. JeremyRubin commented at 2:20 PM on October 27, 2016: contributor

    utAck, seems useful!

  4. jnewbery commented at 3:20 PM on October 27, 2016: member

    Yes, makes sense. I should have included something like this when I added the json tests.

    I think #9023 (which outputs a contextual diff for failing test cases) should make it much easier to track down formatting errors, since it'll point you to the exact line that's different.

    However, I think this is still useful for verifying that the output is indeed valid json.

    Only nit is that compare_output() can raise errors which aren't caught anywhere and don't result in useful logging. For example, here's the logging if the output file is invalid json:

    ~/bitcoin/src$ test/bitcoin-util-test.py --srcdir='.'
    Traceback (most recent call last):
      File "test/bitcoin-util-test.py", line 32, in <module>
        bctest.bctester(srcdir + "/test/data", "bitcoin-util-test.json", buildenv, verbose = verbose)
      File "/home/vagrant/bitcoin/src/test/bctest.py", line 72, in bctester
        bctest(testDir, testObj, buildenv.exeext)
      File "/home/vagrant/bitcoin/src/test/bctest.py", line 50, in bctest
        if not compare_output(outs[0], outputData, outputType):
      File "/home/vagrant/bitcoin/src/test/bctest.py", line 14, in compare_output
        match = (json.loads(a) == json.loads(b))
      File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
        obj, end = self.scan_once(s, idx)
    ValueError: Invalid control character at: line 3 column 14 (char 95)
    

    It'd be nice to wrap the json calls in try/except, and maybe have the final else branch of compare_output() print an error and exit rather than raise an error.

  5. laanwj commented at 3:33 PM on October 27, 2016: member

    Yea, that error 'Invalid control character at: line 3 column 14 (char 95)" is actually very good and specific. The only thing missing is context of what file it occurs in. Would make sense to print it with the filename.

    I think #9023 (which outputs a contextual diff for failing test cases) should make it much easier to track down formatting errors, since it'll point you to the exact line that's different.

    I too think #9023 is useful, and we should do that too, however this solves a different issue: say the JSON library is changed to pretty-print slightly differently. JSON itself is defined by a standard, but pretty-printing is not. This verifies separately that the functional part (the data) still matches. Once you now that, you can harmlessly copy the new formatting if you agree with it.

  6. in src/test/bctest.py:None in c481fcf54e outdated
      11 | +def compare_output(a, b, fmt):
      12 | +	'''Data-format dependent output comparison.'''
      13 | +	if fmt == 'json': # json: compare parsed data
      14 | +		match = (json.loads(a) == json.loads(b))
      15 | +	elif fmt == 'hex': # hex: parse and compare binary data
      16 | +		match = (binascii.a2b_hex(a.rstrip()) == binascii.a2b_hex(b.rstrip()))
    


    JeremyRubin commented at 4:23 PM on October 27, 2016:

    Why is this rstripped rather than stripped? Is there a case where you want to fail here if leading whitespace differs?


    laanwj commented at 4:24 PM on October 27, 2016:

    Leading whitespace is much harder to accidentally introduce than trailing whitespace (e.g. trailing spaces, newlines etc). But I'm fine with changing this to strip() instead.

  7. laanwj force-pushed on Oct 28, 2016
  8. laanwj commented at 12:06 PM on October 28, 2016: member
    • Improved error reporting:
    Error parsing expected output blanktx.json as json: Expecting property name: line 4 column 18 (char 179)
    Error parsing expected output blanktx.hex as hex: Non-hexadecimal digit found
    Error parsing command output as hex: Non-hexadecimal digit found
    
    • Replaced rstrip with strip
  9. test: Add format-dependent comparison to bctest
    This splits the output comparison for `bitcoin-tx` into two steps:
    
    - First, check for data mismatch, parsing the data as json or hex
      depending on the extension of the output file
    
    - Then, check if the literal string matches
    
    For either of these cases give a different error.
    
    This prevents wild goose chases when e.g. a trailing space doesn't match
    exactly, and makes sure that both test output and examples are valid
    data of the purported format.
    6c5cd9d022
  10. laanwj force-pushed on Oct 28, 2016
  11. jnewbery commented at 2:33 PM on October 28, 2016: member

    Looks good. Tested ACK

  12. MarcoFalke commented at 5:38 PM on October 29, 2016: member

    utACK 6c5cd9d

  13. laanwj merged this on Nov 2, 2016
  14. laanwj closed this on Nov 2, 2016

  15. laanwj referenced this in commit 6a8be7ba99 on Nov 2, 2016
  16. codablock referenced this in commit 1a4aee9224 on Sep 19, 2017
  17. codablock referenced this in commit 88f9dc2f16 on Jan 13, 2018
  18. andvgal referenced this in commit 1d7ba526e3 on Jan 6, 2019
  19. CryptoCentric referenced this in commit 8ad2c7f3b5 on Feb 15, 2019
  20. zkbot referenced this in commit 44f5ef7c9d on Nov 9, 2020
  21. zkbot referenced this in commit bd5ff7c889 on Nov 9, 2020
  22. MarcoFalke locked this on Sep 8, 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-13 15:15 UTC

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