Skip to content

scripted-diff: replace bare assert == with assert_equal in functional tests - #34761

Closed
Bortlesboat wants to merge 2 commits into
bitcoin:masterfrom
Bortlesboat:scripted-diff-bare-asserts
Closed

Bortlesboat wants to merge 2 commits into
bitcoin:masterfrom
Bortlesboat:scripted-diff-bare-asserts

Conversation

@Bortlesboat

@Bortlesboat Bortlesboat commented Mar 6, 2026 •

Copy link
Copy Markdown
Contributor

Replace bare assert x == y with assert_equal(x, y) across functional test files for better failure diagnostics.

Split into two commits per review feedback from @l0rinc:

  1. Manual commit (17 conversions, 14 files): Handles cases requiring new imports (6 files) or complex formatting (spaces in operands, inline comments, list/bytes literals).

  2. Scripted-diff commit (72 conversions, 25 files): Simple \S+ regex matches only single-token operands — no risk of mishandling compound expressions, messages, or comments.

Lines with assertion messages, and/or compound conditions, and comprehensions are intentionally left unchanged.

Consolidates the approach from #34752 and #34753 per feedback from @l0rinc.

@DrahtBot

DrahtBot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

♻️ Automatically closing for now based on heuristics. Please leave a comment, if this was erroneous.
Generally, please focus on creating high-quality, original content that demonstrates a clear
understanding of the project's requirements and goals.

📝 Moderators: If this is spam, please replace the title with ., so that the thread does not appear in
search results.

@DrahtBot DrahtBot closed this Mar 6, 2026
@Bortlesboat

Copy link
Copy Markdown
Contributor Author

This was specifically requested by @l0rinc in #34753:

Please don't do it file-by-file, if you insist on doing this, make it a scripted-diff across multiple files.

I closed #34752 and #34753 and consolidated them into this single scripted-diff as suggested. The verify script reproduces the diff exactly. Could this be reopened?

@DrahtBot

DrahtBot commented Mar 6, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
Approach NACK l0rinc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@l0rinc

l0rinc commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Let's open it again, I want to review this.

@Bortlesboat

Copy link
Copy Markdown
Contributor Author

not sure if i am able to reopen it or the @DrahtBot needs to

@sedited sedited reopened this Mar 6, 2026
Comment thread test/functional/wallet_conflicts.py Resolved
@Bortlesboat
Bortlesboat force-pushed the scripted-diff-bare-asserts branch 3 times, most recently from 1f4a62b to 9631d40 Compare March 7, 2026 22:11

@l0rinc l0rinc left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approach NACK

Lots of tests are failing, it's obvious you haven't even tried it locally:

feature_fee_estimation.py                                                        | ✖ Failed  | 250 s
feature_posix_fs_permissions.py                                                  | ✖ Failed  | 1 s                                                                                               
feature_reindex.py                                                               | ✖ Failed  | 38 s                                                                                              
feature_reindex_readonly.py                                                      | ✖ Failed  | 2 s                                                                                               
mempool_updatefromblock.py                                                       | ✖ Failed  | 282 s                                                                                             
mining_getblocktemplate_longpoll.py                                              | ✖ Failed  | 2 s                                                                                               
p2p_handshake.py                                                                 | ✖ Failed  | 3 s                                                                                               
p2p_handshake.py --v2transport                                                   | ✖ Failed  | 3 s                                                                                               
p2p_headers_sync_with_minchainwork.py                                            | ✖ Failed  | 228 s                                                                                             
p2p_invalid_tx.py --v1transport                                                  | ✖ Failed  | 5 s                                                                                               
p2p_invalid_tx.py --v2transport                                                  | ✖ Failed  | 5 s                                                                                               
wallet_disable.py                                                                | ✖ Failed  | 1 s 

Now that's I've spent time with this, I will push an alternative PR instead of babysitting this one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sed -i --regexp-extended '/ or /b; / and /b; / #/b; / for /b; s/^(\s*)assert ([^,][^=!<>]) == ([^,])$/\1assert_equal(\2, \3)/' $(git grep -l 'assert .* == ' -- 'test/functional/' ':(exclude)test/functional/test_framework/')

The point of the scripted diff is to help the reviewer be more confident in the changes.
This vibe-regex doesn't help me with that.
But we should be able to do it in two commits instead: the first one covering the values that a simple regex cannot reliably detect (e.g. because of complicated formatting or new imports). The second commit could be a simple regex via sed/grep/python for the remaining trivial cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call — restructured into two commits:

  1. Manual commit: adds imports, converts lines with spaces in operands, inline comments, list/bytes literals (17 conversions)
  2. Scripted-diff using \S+ — only matches single-token operands, so no exclusion heuristics needed:
sed -i --regexp-extended 's/^(\s*)assert (\S+) == (\S+)$/\1assert_equal(\2, \3)/' \
  $(git grep -l 'assert [^ ]* == [^ ]*$' -- 'test/functional/' ':(exclude)test/functional/test_framework/')

@@ -22,12 +22,12 @@ def skip_test_if_missing_module(self):
def check_directory_permissions(self, dir):
mode = os.lstat(dir).st_mode
self.log.info(f"{stat.filemode(mode)} {dir}")
assert mode == (stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
assert_equal(mode, (stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Have you tried this locally? This is missing a from test_framework.util import assert_equal

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed — added the missing import. All 6 files that needed new assert_equal imports are handled in commit 1 now. Verified with py_compile across all 30 changed files.

…matting

Convert assert == to assert_equal() for cases that cannot be handled by
a simple scripted-diff regex:

- Add assert_equal import to 6 files that were missing it
- Convert lines with spaces in operands (arithmetic, bitwise, stat flags)
- Convert lines with inline comments (preserving the comments)
- Convert lines with list/bytes literals containing commas or spaces

Lines with assertion messages, compound conditions (and/or), and
comprehensions are intentionally left unchanged.
… tests

Replace remaining simple assert x == y with assert_equal(x, y) for
better failure diagnostics. Only matches single-token operands (no
spaces), so compound expressions, assertion messages, and inline
comments are unaffected.

-BEGIN VERIFY SCRIPT-
sed -i --regexp-extended 's/^(\s*)assert (\S+) == (\S+)$/\1assert_equal(\2, \3)/' $(git grep -l 'assert [^ ]* == [^ ]*$' -- 'test/functional/' ':(exclude)test/functional/test_framework/')
-END VERIFY SCRIPT-
@Bortlesboat
Bortlesboat force-pushed the scripted-diff-bare-asserts branch from 9631d40 to 04a8cb6 Compare March 8, 2026 16:52
@Bortlesboat

Copy link
Copy Markdown
Contributor Author

Force-pushed with the two-commit approach:

Commit 1 (manual): Adds assert_equal imports to the 6 files that were missing them, and converts 17 lines that have complex formatting (spaces in operands, inline comments, list/bytes literals). Not a scripted-diff — reviewer can inspect each change directly.

Commit 2 (scripted-diff): Uses \S+ to match only single-token operands — no spaces means no risk of matching compound expressions, assertion messages, or inline comments. The verify script is one sed command:

sed -i --regexp-extended 's/^(\s*)assert (\S+) == (\S+)$/\1assert_equal(\2, \3)/' \
  $(git grep -l 'assert [^ ]* == [^ ]*$' -- 'test/functional/' ':(exclude)test/functional/test_framework/')

72 conversions across 25 files in the scripted-diff, 17 manual conversions in commit 1. Lines with assertion messages, and/or compound conditions, and comprehensions are left unchanged.

Sorry for the noise on the earlier pushes — should have caught the missing imports before pushing.

@sedited

sedited commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Maybe try to review some other pull requests locally first before contributing a change yourself. You are required to run the tests and understand what they in the context of your change before opening a pull request. We generally also don't accept trivial refactoring PRs from fresh contributors, see the contributing guidelines: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#refactoring .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants