Address review feedback: use helper for error snapshots and add test function#2522
Address review feedback: use helper for error snapshots and add test function#2522
Conversation
- Update test-centrality.R to use expect_snapshot_igraph_error() - Add test_error_with_source() function to demonstrate error formatting - Create test-error-formatting.R to test error formatting functionality - Update snapshot to use :xx format instead of :<linenumber> Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
The error handler automatically adds punctuation via maybe_add_punctuation(), so we should not include it in the message itself. Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
NAMESPACE
Outdated
| export(subgraph_isomorphic) | ||
| export(subgraph_isomorphisms) | ||
| export(tail_of) | ||
| export(test_error_with_source) |
There was a problem hiding this comment.
This doesn't need to be exported.
There was a problem hiding this comment.
Fixed in f628871. Removed the export - the function is now internal-only and accessible via igraph:::test_error_with_source() for testing.
| @@ -0,0 +1,5 @@ | |||
| test_that("error messages include source file and line information", { | |||
| expect_snapshot_igraph_error({ | |||
There was a problem hiding this comment.
This needs to be a regular expect_snapshot(transform = ), the purpose of the test is to ensure that we don't lose information.
There was a problem hiding this comment.
Fixed in f628871. Changed to use expect_snapshot(transform = function(x) sub(":(\\d+)", ":<linenumber>", x)) to preserve the file name while masking just the line number. The snapshot now shows Source: rinterface_extra.c:<linenumber> ensuring we verify the full source location format.
- Remove export of test_error_with_source() - not needed for internal test function - Change test-error-formatting.R to use expect_snapshot(transform=) instead of expect_snapshot_igraph_error() to preserve file name and verify line number format - Update snapshot to use <linenumber> format to ensure we don't lose information Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Checked in f628871. Searched for all uses of |
There was a problem hiding this comment.
Pull request overview
This PR addresses review feedback from #2515 by consolidating error snapshot testing patterns and adding test infrastructure for verifying error formatting with source location information (file:line).
Changes:
- Refactored
test-centrality.Rto use the existingexpect_snapshot_igraph_error()helper instead of an inline transform - Added
test_error_with_source()test function with C implementation that throws an error with source location - Created new test file
test-error-formatting.Rto verify complete source location format
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/testthat/test-error-formatting.R | New test file to verify error messages include source file and line information |
| tests/testthat/_snaps/error-formatting.md | Snapshot file for error formatting test |
| tests/testthat/test-centrality.R | Refactored to use expect_snapshot_igraph_error() helper |
| tests/testthat/_snaps/centrality.md | Updated snapshot to use :xx pattern consistently |
| src/rinterface_extra.c | Added C test function Rx_igraph_test_error_with_source() |
| src/cpp11.cpp | Registered new test function in R's call table |
| R/utils.R | Added R wrapper function for test (internal, not exported) |
| @@ -0,0 +1,8 @@ | |||
| test_that("error messages include source file and line information", { | |||
| expect_snapshot( | |||
| error = TRUE, | |||
There was a problem hiding this comment.
The test is missing a transform parameter to replace line numbers in the snapshot. The snapshot file shows <linenumber>, but without a transform, the snapshot will contain actual line numbers that will change when the C code is modified.
According to the PR description, this test should use transform = function(x) sub(":(\\d+)", ":<linenumber>", x), but this is missing from the test. Alternatively, consider using the existing helper function expect_snapshot_igraph_error() which already handles this transformation (though it uses :xx instead of :<linenumber>).
| error = TRUE, | |
| error = TRUE, | |
| transform = function(x) sub(":(\\d+)", ":<linenumber>", x), |
| Condition | ||
| Error in `test_error_with_source()`: | ||
| ! Test error message for verifying source location formatting. Invalid value | ||
| Source: rinterface_extra.c:<linenumber> |
There was a problem hiding this comment.
The snapshot uses <linenumber> as a placeholder, but the codebase convention is to use :xx. The helper function expect_snapshot_igraph_error() (tests/testthat/helper.R:47-56) uses gsub(":(\\d+)", ":xx", y), and this pattern is used in the vast majority of error snapshots (e.g., tests/testthat/_snaps/aaa-auto.md and tests/testthat/_snaps/centrality.md).
Only a couple of tests in test-games.R use <linenumber>, which appears to be an exception to the established convention. For consistency, consider using the expect_snapshot_igraph_error() helper and updating the snapshot to use :xx instead of <linenumber>.
Addresses review feedback on #2515 by consolidating error snapshot testing patterns and adding dedicated test infrastructure for error formatting with source location information.
Changes
test-centrality.Rto useexpect_snapshot_igraph_error()instead of inline transform functiontest_error_with_source()that calls C code which throws an error with file:line informationRx_igraph_test_error_with_source()inrinterface_extra.cutils.R(internal, not exported)test-error-formatting.Rwithexpect_snapshot(transform = )to verify complete source location format (file + line number)Example
Implementation Details
The test function is intentionally not exported - it's marked as internal and accessible via
igraph:::test_error_with_source()for testing purposes only. The test uses a custom transformsub(":(\\d+)", ":<linenumber>", x)to preserve the file name while masking line numbers, ensuring we verify the complete source location format without line number brittleness.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.