Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all functions #36

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
426 changes: 260 additions & 166 deletions R/make-traceability-matrix.R

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions man/create_pkg_env.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion man/get_all_functions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/get_testing_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/map_functions_to_scripts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/source_pkg_code.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ create_package_template <- function(
create_testing_package <- function(
pkg_name = "mypackage",
type = c("pass_success", "pass_warning", "pass_notes", "pass_no_test", "pass_no_test_suite",
"pass_no_functions", "pass_no_R_dir","pass_export_patterns",
"pass_no_functions", "pass_no_R_dir","pass_export_patterns", "pass_no_docs",
"fail_func_syntax", "fail_test"),
nest_results_dir = TRUE
){
Expand Down Expand Up @@ -155,12 +155,13 @@ create_testing_package <- function(
if(!(type %in% c("pass_no_functions", "pass_no_R_dir"))){
writeLines(func_lines, pkg_setup_dirs$r_file)
# Export function
if(type != "fail_func_syntax"){
if(!(type %in% c("fail_func_syntax", "pass_no_docs"))){
# Basically run `devtools::document()`, if the function is suitable
roxygen2::roxygenise(pkg_setup_dirs$pkg_dir, load_code = roxygen2::load_source) %>% suppressMessages()
}else if(type == "pass_export_patterns"){
ns_file <- file.path(pkg_setup_dirs$pkg_dir, "NAMESPACE")
write("exportPatterns(\"^[[:alpha:]]+\")", file = ns_file, append = TRUE)
if(type == "pass_export_patterns"){
ns_file <- file.path(pkg_setup_dirs$pkg_dir, "NAMESPACE")
write("exportPattern(\"^[[:alpha:]]+\")", file = ns_file)
}
}else{
# Manually export function if syntax issue is present (only way this scenario could happen)
ns_file <- file.path(pkg_setup_dirs$pkg_dir, "NAMESPACE")
Expand Down Expand Up @@ -219,7 +220,7 @@ create_testing_package <- function(
setup_multiple_pkgs <- function(){

pkg_types <- c("pass_success", "pass_warning", "pass_notes", "pass_no_test", "pass_no_test_suite",
"pass_no_functions", "pass_no_R_dir", "pass_export_patterns",
"pass_no_functions", "pass_no_R_dir", "pass_export_patterns", "pass_no_docs",
"fail_func_syntax", "fail_test")
pkg_names <- paste0("package", seq_along(pkg_types))

Expand Down Expand Up @@ -298,7 +299,7 @@ pkg_dirs <- setup_multiple_pkgs()
# only need a subset of these for majority of tests
pkg_select <- pkg_dirs$pkg_setups_df %>%
dplyr::filter(pkg_type %in% c(
"pass_success", "pass_warning", "pass_no_functions",
"pass_success", "pass_no_docs", "pass_no_functions",
"fail_func_syntax", "fail_test"
))
pkg_tars <- pkg_select %>% dplyr::pull(tar_file)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-format-report.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ describe("formatting functions", {
expect_equal(flex_df$body$dataset$Score, c(1, 1))
expect_equal(flex_df$body$dataset$Result, c("Passing (score: 1)", "100%"))

## Medium rcmdcheck score ##
result_dir <- result_dirs_select[["pass_warning"]]
## High rcmdcheck score ##
result_dir <- result_dirs_select[["pass_no_docs"]]
json_path <- get_result_path(result_dir, "scorecard.json")
pkg_scores <- jsonlite::fromJSON(json_path)

# map scores to risk and format into tables to be written to PDF
formatted_pkg_scores <- format_scores_for_render(pkg_scores, risk_breaks = c(0.3, 0.7))
flex_df <- format_testing_scores(formatted_pkg_scores)

expect_equal(flex_df$body$dataset$Score, c(0.5, 1))
expect_equal(flex_df$body$dataset$Result, c("Passing (score: 0.5)", "100%"))
expect_equal(flex_df$body$dataset$Score, c(0.75, 1))
expect_equal(flex_df$body$dataset$Result, c("Passing (score: 0.75)", "100%"))


## Failing rcmdcheck score ##
Expand Down
93 changes: 83 additions & 10 deletions tests/testthat/test-make-traceability-matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
describe("Traceability Matrix", {

it("make_traceability_matrix - success integration test", {

pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_success")
result_dir_x <- pkg_setup_select$pkg_result_dir
pkg_tar_x <- pkg_setup_select$tar_file
Expand All @@ -13,6 +12,8 @@ describe("Traceability Matrix", {
on.exit(fs::file_delete(export_doc_path), add = TRUE)

# Confirm values - documentation
expect_equal(unique(trac_mat$exported_function), "myfunction")
expect_equal(unique(trac_mat$code_file), "R/myscript.R")
expect_equal(unique(trac_mat$documentation), "man/myfunction.Rd")
expect_equal(
trac_mat %>% tidyr::unnest(test_files) %>% pull(test_files) %>% unique(),
Expand All @@ -22,25 +23,96 @@ describe("Traceability Matrix", {


it("make_traceability_matrix - missing documentation integration test", {
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_no_docs")
result_dir_x <- pkg_setup_select$pkg_result_dir
pkg_tar_x <- pkg_setup_select$tar_file

expect_message(
trac_mat <- make_traceability_matrix(pkg_tar_path = pkg_tar_x, result_dir_x, verbose = TRUE),
as.character(glue::glue("No documentation was found in `man/` for package `{pkg_setup_select$pkg_name}`\n\n"))
)
export_doc_path <- get_result_path(result_dir_x, "export_doc.rds")
expect_true(fs::file_exists(export_doc_path))
on.exit(fs::file_delete(export_doc_path), add = TRUE)

# Confirm values
expect_equal(unique(trac_mat$exported_function), "myfunction")
expect_equal(unique(trac_mat$code_file), "R/myscript.R")
expect_true(is.na(unique(trac_mat$documentation)))
expect_equal(
trac_mat %>% tidyr::unnest(test_files) %>% pull(test_files) %>% unique(),
"test-myscript.R"
)
})


it("make_traceability_matrix - export patterns integration test", {
# Normal behavior
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_export_patterns")
result_dir_x <- pkg_setup_select$pkg_result_dir
pkg_tar_x <- pkg_setup_select$tar_file

trac_mat <- make_traceability_matrix(pkg_tar_path = pkg_tar_x, result_dir_x, verbose = TRUE)
export_doc_path <- get_result_path(result_dir_x, "export_doc.rds")
expect_true(fs::file_exists(export_doc_path))
on.exit(fs::file_delete(export_doc_path), add = TRUE)

# Confirm values
expect_equal(unique(trac_mat$exported_function), "myfunction")
expect_equal(unique(trac_mat$code_file), "R/myscript.R")
expect_equal(unique(trac_mat$documentation), "man/myfunction.Rd")
expect_equal(
trac_mat %>% tidyr::unnest(test_files) %>% pull(test_files) %>% unique(),
"test-myscript.R"
)

# Syntax error (cant find R script)
# Add syntax error to new script
r_script <- file.path(pkg_setup_select$pkg_dir, "R", "myscript_error.R")
on.exit(fs::file_delete(r_script), add = TRUE)
func_lines <- "myfunction2 <- function(x { x + 1"
writeLines(func_lines, r_script)

exports_df <- get_exports(pkg_setup_select$pkg_dir)

expect_warning(
exports_df <- map_functions_to_scripts(exports_df, pkg_setup_select$pkg_dir, verbose = TRUE),
"Failed to parse"
)
expect_equal(unique(exports_df$exported_function), "myfunction")
expect_equal(unique(exports_df$code_file), "R/myscript.R")

})


it("make_traceability_matrix - cant link exports integration test", {
# Bad package - no documentation (at all)
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "fail_func_syntax")
result_dir_x <- pkg_setup_select$pkg_result_dir
pkg_tar_x <- pkg_setup_select$tar_file

# Check for two separate notes
# Check for two separate notes due to parsing error
# - missing documentation
# - cant find R script
res <- testthat::evaluate_promise(
trac_mat <- make_traceability_matrix(pkg_tar_path = pkg_tar_x, result_dir_x, verbose = TRUE)
)
expect_equal(
res$messages,
as.character(glue::glue("No documentation was found in `man/` for package `{pkg_setup_select$pkg_name}`\n\n"))
c(
glue::glue("The following exports were not found in R/ for {pkg_setup_select$pkg_name}:\n{trac_mat$exported_function}\n\n\n"),
glue::glue("No documentation was found in `man/` for package `{pkg_setup_select$pkg_name}`\n\n")
)
)
expect_true(grepl("Failed to parse", res$warnings))

export_doc_path <- get_result_path(result_dir_x, "export_doc.rds")
expect_true(fs::file_exists(export_doc_path))
on.exit(fs::file_delete(export_doc_path), add = TRUE)

# Confirm values - documentation
# Confirm values
expect_equal(unique(trac_mat$exported_function), "myfunction")
expect_true(is.na(unique(trac_mat$code_file)))
expect_true(is.na(unique(trac_mat$documentation)))
expect_equal(
trac_mat %>% tidyr::unnest(test_files) %>% pull(test_files) %>% unique(),
Expand Down Expand Up @@ -74,8 +146,8 @@ describe("Traceability Matrix", {
)
})

it("make_traceability_matrix - no R directory or exported functions integration test", {

it("make_traceability_matrix - no R directory or exported functions integration test", {
# No R directory
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_no_R_dir")
result_dir_x <- pkg_setup_select$pkg_result_dir
Expand All @@ -97,6 +169,7 @@ describe("Traceability Matrix", {
)
})


it("get_exports", {
# Test individual exports
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_success")
Expand All @@ -109,6 +182,7 @@ describe("Traceability Matrix", {
expect_equal(exports_df$exported_function, "myfunction")
})


it("get_all_functions: identify functions and the script they're coded in", {
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_success")
r_dir <- file.path(pkg_setup_select$pkg_dir, "R")
Expand All @@ -121,9 +195,9 @@ describe("Traceability Matrix", {
function(x) {x + 1}" # multi-line declaration
)
func_lines2 <- c(
"setGeneric(\"myfunc5\")", # setGeneric
"setGeneric('myfunc6')", # different quotes
"setGeneric ( 'myfunc7' )" # spacing
"setGeneric(\"myfunc5\", function(x) attributes(x))", # setGeneric
"setGeneric('myfunc6', plot)", # different quotes, existing function
"setGeneric ( 'myfunc7', function(x) mtcars)" # spacing
)
func_names <- paste0("myfunc", 1:7)

Expand Down Expand Up @@ -160,7 +234,7 @@ describe("Traceability Matrix", {
)

# Not documented, but still exported
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "fail_func_syntax")
pkg_setup_select <- pkg_dirs$pkg_setups_df %>% dplyr::filter(pkg_type == "pass_no_docs")
exports_df <- get_exports(pkg_setup_select$pkg_dir)
expect_equal(
template_df,
Expand Down Expand Up @@ -217,7 +291,6 @@ describe("Traceability Matrix", {
expect_equal(map_tests_df$exported_function, rep("myfunction", 2))
expect_equal(map_tests_df$test_files, c("test-myscript.R", "test-new_tests.R"))
expect_equal(map_tests_df$test_dirs, c("tests/testthat", "inst/other_tests"))

})


Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-summarize-package-results.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ describe("summarize_package_results", {
expect_equal(
pkg_results$check_status,
order_cases(c(
pass_success = 0, pass_warning = 0, pass_no_functions = 0,
pass_success = 0, pass_no_docs = 0, pass_no_functions = 0,
fail_func_syntax = 1, fail_test = 1
))
)
expect_equal(
pkg_results$covr_success,
order_cases(c(
pass_success = TRUE, pass_warning = TRUE, pass_no_functions = TRUE,
pass_success = TRUE, pass_no_docs = TRUE, pass_no_functions = TRUE,
fail_func_syntax = FALSE, fail_test = FALSE
))
)
expect_equal(
pkg_results$overall_score,
order_cases(c(
pass_success = 0.5, pass_warning = 0.4, pass_no_functions = 0.3,
pass_success = 0.5, pass_no_docs = 0.45, pass_no_functions = 0.3,
fail_func_syntax = 0.1, fail_test = 0.1
))
)
Expand Down