diff --git a/R/utils.R b/R/utils.R index b9c51b8bbbc..d172590cc0d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -94,3 +94,23 @@ modify_list <- function(x, y) { utils::modifyList(x, y) } + +#' Test function to verify error formatting with file and line information +#' +#' @description +#' This is a test function that throws an error from C code with file and line +#' information. +#' The error message should include the source file and line number where the +#' error occurred. +#' +#' @return This function never returns; it always throws an error. +#' @keywords internal +#' @noRd +#' @examples +#' \dontrun{ +#' # This will throw an error with source location information +#' test_error_with_source() +#' } +test_error_with_source <- function() { + .Call(Rx_igraph_test_error_with_source) +} diff --git a/src/cpp11.cpp b/src/cpp11.cpp index 60908db7edb..31b32abbe32 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -492,6 +492,7 @@ extern SEXP Rx_igraph_st_vertex_connectivity(SEXP, SEXP, SEXP); extern SEXP Rx_igraph_star(SEXP, SEXP, SEXP); extern SEXP Rx_igraph_subcomponent(SEXP, SEXP, SEXP); extern SEXP Rx_igraph_subisomorphic_lad(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP); +extern SEXP Rx_igraph_test_error_with_source(void); extern SEXP Rx_igraph_transitivity_local_undirected_all(SEXP, SEXP); extern SEXP Rx_igraph_union(SEXP, SEXP); extern SEXP Rx_igraph_vcount(SEXP); @@ -980,6 +981,7 @@ static const R_CallMethodDef CallEntries[] = { {"Rx_igraph_star", (DL_FUNC) &Rx_igraph_star, 3}, {"Rx_igraph_subcomponent", (DL_FUNC) &Rx_igraph_subcomponent, 3}, {"Rx_igraph_subisomorphic_lad", (DL_FUNC) &Rx_igraph_subisomorphic_lad, 7}, + {"Rx_igraph_test_error_with_source", (DL_FUNC) &Rx_igraph_test_error_with_source, 0}, {"Rx_igraph_transitivity_local_undirected_all", (DL_FUNC) &Rx_igraph_transitivity_local_undirected_all, 2}, {"Rx_igraph_union", (DL_FUNC) &Rx_igraph_union, 2}, {"Rx_igraph_vcount", (DL_FUNC) &Rx_igraph_vcount, 1}, diff --git a/src/rinterface_extra.c b/src/rinterface_extra.c index 9e8c68c9332..d88ba20810e 100644 --- a/src/rinterface_extra.c +++ b/src/rinterface_extra.c @@ -7760,6 +7760,13 @@ SEXP Rx_igraph_create_bipartite(SEXP types, SEXP edges, SEXP directed) { return R_igraph_create_bipartite(types, edges, directed); } +/* Test function to verify error formatting with file and line information */ +attribute_visible SEXP Rx_igraph_test_error_with_source(void) { + igraph_errorf("Test error message for verifying source location formatting", + __FILE__, __LINE__, IGRAPH_EINVAL); + return R_NilValue; +} + SEXP Rx_igraph_finalizer(void) { return R_igraph_finalizer(); } diff --git a/tests/testthat/_snaps/centrality.md b/tests/testthat/_snaps/centrality.md index a676e0e14ef..7a8ebc83914 100644 --- a/tests/testthat/_snaps/centrality.md +++ b/tests/testthat/_snaps/centrality.md @@ -49,7 +49,7 @@ Condition Error in `arpack()`: ! ARPACK error. N must be positive - Source: linalg/arpack.c: + Source: linalg/arpack.c:xx --- diff --git a/tests/testthat/_snaps/error-formatting.md b/tests/testthat/_snaps/error-formatting.md new file mode 100644 index 00000000000..eb6013ebd61 --- /dev/null +++ b/tests/testthat/_snaps/error-formatting.md @@ -0,0 +1,9 @@ +# error messages include source file and line information + + Code + test_error_with_source() + Condition + Error in `test_error_with_source()`: + ! Test error message for verifying source location formatting. Invalid value + Source: rinterface_extra.c: + diff --git a/tests/testthat/test-centrality.R b/tests/testthat/test-centrality.R index 2f983e74f8c..65e0a407c0c 100644 --- a/tests/testthat/test-centrality.R +++ b/tests/testthat/test-centrality.R @@ -845,13 +845,9 @@ test_that("eigen_centrality() deprecated scale argument", { test_that("arpack() errors well", { f <- function(x, extra = NULL) x - expect_snapshot( - error = TRUE, - { - arpack(f, options = list(nev = 2, ncv = 4), sym = TRUE) - }, - transform = function(x) sub("\\:[0-9]+", ":", x) - ) + expect_snapshot_igraph_error({ + arpack(f, options = list(nev = 2, ncv = 4), sym = TRUE) + }) expect_snapshot(error = TRUE, { arpack( f, diff --git a/tests/testthat/test-error-formatting.R b/tests/testthat/test-error-formatting.R new file mode 100644 index 00000000000..5e19acb2a66 --- /dev/null +++ b/tests/testthat/test-error-formatting.R @@ -0,0 +1,8 @@ +test_that("error messages include source file and line information", { + expect_snapshot( + error = TRUE, + { + test_error_with_source() + } + ) +})