Package 'checkit'

Title: Rule Runner and General Rules for Projects
Description: Runs a set of rules against the files of a project folder and reports each finding with its fix. A project names a dated rule set in its configuration file, and a rule set never changes after release. Other packages add their own rules to the general rules of this package.
Authors: Richard Aubrey White [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-6747-1726>)
Maintainer: Richard Aubrey White <[email protected]>
License: MIT + file LICENSE
Version: 2026.12.4
Built: 2026-12-04 03:10:58 UTC
Source: https://github.com/raubreywhite/checkit

Help Index


Check a project folder against its rules

Description

check_folder() prints the lines of folder_report(). In a session that is not interactive, it quits R with status 1 after a FAIL, so a script that calls it fails. A WARN or a SKIP does not quit R.

Usage

check_folder(
  path = ".",
  config = "checkit.yml",
  rules = general_rules(),
  sets = system.file("rules", package = "checkit"),
  scope = default_scope,
  messages = checkit_messages(),
  set_pattern = "^checkit_([0-9]{4})_([0-9]{2})_([0-9]{2})$",
  reserved_ignore = function(set) character(0),
  reserved_rules = character(0)
)

Arguments

path

The project folder.

config

The name of the config file in path. A rule set MAY hold the key packages, a map from the name of a package to its minimum version. It is not a rule. The runner gives it to each rule function that has the argument packages, such as the function of rule_set_pinned.

rules

The registry: a named list of rule() entries, such as compose_rules() gives. The default is general_rules().

sets

The folder of the rule sets. The default is the folder rules of checkit.

scope

A function scope(path, ignore) that returns the named list files that each rule function gets. ignore holds the folders of the key ignore for a rule that is not locked, and is empty for a locked rule. The default is default_scope().

messages

The texts of the config messages, a named list as checkit_messages() gives.

set_pattern

A regular expression that each name of a rule set matches. Its three groups are the year, the month and the day of the set.

reserved_ignore

A function of the name of the set of the project that returns regular expressions. An entry of ignore that one of them matches is an error.

reserved_rules

A named character vector. Each name is a name that the keys rules and allow MUST NOT hold. The error is "<config>: <key>: <name> <value>", so each value is the rest of the message.

Value

Invisibly, the findings of folder_findings().

See Also

Other runner: checkit_messages(), compose_rules(), default_scope(), folder_findings(), folder_report(), general_rules(), rule(), rules()


The texts of the config messages of checkit

Description

checkit_messages() gives the default messages argument of folder_findings(). A package that runs its own rules passes its own list with the same names.

Usage

checkit_messages()

Value

A named list of five strings:

  • program: the name in the error about an unknown rule set.

  • version: the version in that error.

  • check_function: the function in the error about an unknown rule and in the error about a missing config file.

  • help: the help topic in the error about a missing config file.

  • starter_line: the one line of a new config file.

See Also

Other runner: check_folder(), compose_rules(), default_scope(), folder_findings(), folder_report(), general_rules(), rule(), rules()

Examples

checkit::checkit_messages()$check_function

Compose registries of rules

Description

compose_rules() joins registries, each a named list of rule() entries. An entry of a later registry replaces the entry of the same name of an earlier registry.

Usage

compose_rules(...)

Arguments

...

Registries: named lists of rule() entries.

Details

The result holds each name once, in the order in which the names first appear. Use compose_rules(), not c(): c() keeps both entries of a name, and [[ then returns the first.

Value

A named list of rule() entries.

See Also

Other runner: check_folder(), checkit_messages(), default_scope(), folder_findings(), folder_report(), general_rules(), rule(), rules()

Examples

pass <- function(path, files) {
  return(data.frame(
    rule = character(0),
    status = character(0),
    path = character(0),
    detail = character(0)
  ))
}
old <- checkit::rule(pass, "the old text", "fix it")
new <- checkit::rule(pass, "the new text", "fix it")
both <- checkit::compose_rules(list(a = old), list(a = new))
both[["a"]]$pass_detail

The default scope of the general rules

Description

default_scope() gives the files that each rule of general_rules() reads in the project folder path. It is the default scope of folder_findings().

Usage

default_scope(path, ignore = character(0))

Arguments

path

The project folder.

ignore

The top-level folders to leave out of entries, and so out of each element that comes from it.

Details

The list has these elements:

  • entries: each file and folder of the project, as paths relative to path. In the top folder of a git repository, these are the files that git tracks or does not ignore, and their folders. Otherwise they are the files and folders on disk. The files and folders of an OS or an editor, such as .DS_Store and .git, are left out.

  • code, scripts and scripts_sorted: the .R files of entries under the folder ⁠R/⁠. scripts_sorted has them in the C locale order.

  • exempt_paths: the paths of entries that lower_case_names passes. These are the files and folders named README.md, CHANGELOG.md, NEWS.md, AGENTS.md, CLAUDE.md, LICENSE, DESCRIPTION or NAMESPACE at any depth. So an R package passes on its standard files. The folder R at the top level holds the code, and it is exempt too. So is each file at the top level whose name starts with a dot or ends with .Rproj.

  • changelog_code: "^R/", so a change under ⁠R/⁠ needs a change to CHANGELOG.md.

  • frozen: one element for each file of entries whose name ends with .frozen.md5. Its covers are the files on disk now in the folder of the manifest, at any depth and hidden files included. A file whose name ends with .frozen.md5 is not in covers. A file under a folder .git is not in covers either, because git changes these files. Its missing_detail is NULL, so no manifest is required.

Each path in a manifest is relative to path. So write a manifest with freeze(home = path), and put it in the folder that it freezes.

Value

A named list with the elements entries, code, scripts, scripts_sorted, exempt_paths, changelog_code and frozen.

See Also

Other runner: check_folder(), checkit_messages(), compose_rules(), folder_findings(), folder_report(), general_rules(), rule(), rules()

Examples

home <- file.path(tempdir(), "checkit_scope_example")
dir.create(file.path(home, "R"), recursive = TRUE)
dir.create(file.path(home, "data"))
writeLines("x <- 1", file.path(home, "R", "f.R"))
writeLines("a,b", file.path(home, "data", "raw.csv"))
checkit::freeze("data", "data/data.frozen.md5", home = home)
scope <- checkit::default_scope(home)
scope$code
scope$frozen[[1]]$covers
unlink(home, recursive = TRUE)

Test that a rule fails or passes

Description

expect_rule_red() is a testthat expectation: found has a finding of rule with the status of level that the key allow does not cover. expect_rule_green() is the opposite: found has a PASS line of rule, so the rule ran, and no WARN or FAIL finding of rule.

Usage

expect_rule_red(rule, found, level = "fail")

expect_rule_green(rule, found)

Arguments

rule

The name of the rule.

found

The findings, as folder_findings() gives them.

level

The level of the rule in the rule set of the test project, "fail" or "warn". expect_rule_red() looks for a finding with this status.

Details

Each call records rule as red or green, whether the expectation passes or fails. expect_rules_covered() reads these records. An allowed finding has " (allowed)" in its detail, and expect_rule_red() does not count it. The helpers need the package testthat.

Value

found, invisibly.

See Also

Other test helpers: expect_rules_covered()

Examples

found <- data.frame(
  rule = "no_capitals",
  status = "FAIL",
  path = "Notes.txt",
  detail = "Notes.txt: a capital letter in the name. Fix: rename it."
)
if (requireNamespace("testthat", quietly = TRUE)) {
  checkit::expect_rule_red("no_capitals", found)
}

Test that each rule of a registry has a red and a green test

Description

expect_rules_covered() is one testthat expectation. It fails when a rule of the registry rules has no record of expect_rule_red() or no record of expect_rule_green() in this R session. The failure message names each of these rules.

Usage

expect_rules_covered(rules)

Arguments

rules

The registry: a named list of rule() entries.

Details

Call it in the test file whose name sorts last, such as test-zzz_rule_coverage.R. testthat runs the test files in the order of their names, in one R session, so that file sees the records of all the others.

Value

The names of the rules, invisibly.

See Also

Other test helpers: expect_rule_red()

Examples

# In tests/testthat/test-zzz_rule_coverage.R of a package:
# test_that("each rule has a red and a green test", {
#   checkit::expect_rules_covered(my_rules())
# })

The findings of the rules for a project folder

Description

folder_findings() reads the config file of the folder path. It runs each rule of the registry rules that the rule set of the config file has, and returns the findings. It does not print and does not quit.

Usage

folder_findings(
  path = ".",
  config = "checkit.yml",
  rules = general_rules(),
  sets = system.file("rules", package = "checkit"),
  scope = default_scope,
  messages = checkit_messages(),
  set_pattern = "^checkit_([0-9]{4})_([0-9]{2})_([0-9]{2})$",
  reserved_ignore = function(set) character(0),
  reserved_rules = character(0)
)

Arguments

path

The project folder.

config

The name of the config file in path. A rule set MAY hold the key packages, a map from the name of a package to its minimum version. It is not a rule. The runner gives it to each rule function that has the argument packages, such as the function of rule_set_pinned.

rules

The registry: a named list of rule() entries, such as compose_rules() gives. The default is general_rules().

sets

The folder of the rule sets. The default is the folder rules of checkit.

scope

A function scope(path, ignore) that returns the named list files that each rule function gets. ignore holds the folders of the key ignore for a rule that is not locked, and is empty for a locked rule. The default is default_scope().

messages

The texts of the config messages, a named list as checkit_messages() gives.

set_pattern

A regular expression that each name of a rule set matches. Its three groups are the year, the month and the day of the set.

reserved_ignore

A function of the name of the set of the project that returns regular expressions. An entry of ignore that one of them matches is an error.

reserved_rules

A named character vector. Each name is a name that the keys rules and allow MUST NOT hold. The error is "<config>: <key>: <name> <value>", so each value is the rest of the message.

Details

The config file holds four keys:

  • extends names a rule set, a file ⁠<set>.yml⁠ in the folder sets. The set gives each rule a level, fail, warn or off, and its parameters. A rule that the registry has and the set does not have does not run and gives no line.

  • rules changes the level or a parameter of a rule, merged into the entry of the set. A short form such as name: warn changes the level only. The set marks a locked rule with locked: true, and the config file cannot change its level.

  • allow lists paths for each rule. A path covers the findings with that path and with each path under it. A covered finding is WARN, with (allowed) at the end of its detail. A path that covers no finding is a WARN of the rule allow. The config file cannot allow a path for a locked rule.

  • ignore lists top-level folders that no rule reads, other than the locked rules.

The config reader stops before any rule runs on an error in the config file, with a message that names the key.

Value

A data.frame of the findings with the columns rule, status, path and detail. It has one row for each finding. A rule that runs and finds nothing has one PASS row, and a rule that is off has one OFF row. A SKIP row of a rule stays SKIP at the levels fail and warn, and the key allow does not change it. The detail of each WARN and FAIL ends with ". Fix: <fix>.". The detail of a SKIP has no fix.

See Also

Other runner: check_folder(), checkit_messages(), compose_rules(), default_scope(), folder_report(), general_rules(), rule(), rules()

Examples

home <- file.path(tempdir(), "checkit_findings_example")
dir.create(file.path(home, "rules"), recursive = TRUE)
writeLines("no_capitals: fail", file.path(home, "rules", "demo_2026_01_01.yml"))
writeLines("extends: demo_2026_01_01", file.path(home, "demo.yml"))
writeLines("x", file.path(home, "Notes.txt"))
no_capitals <- checkit::rule(
  fn = function(path, files) {
    bad <- files$entries[grepl("[A-Z]", basename(files$entries))]
    return(data.frame(
      rule = rep("no_capitals", length(bad)),
      status = rep("FAIL", length(bad)),
      path = bad,
      detail = paste0(bad, ": a capital letter in the name", recycle0 = TRUE)
    ))
  },
  pass_detail = "no capital letter in a name",
  fix = "rename it in lower case"
)
checkit::folder_findings(
  home,
  config = "demo.yml",
  rules = list(no_capitals = no_capitals),
  sets = file.path(home, "rules"),
  scope = function(path, ignore) {
    return(list(entries = setdiff(list.files(path), ignore)))
  },
  set_pattern = "^demo_([0-9]{4})_([0-9]{2})_([0-9]{2})$"
)
unlink(home, recursive = TRUE)

The lines that check_folder() prints

Description

folder_report() gives the lines of check_folder(): the line of the rules, one line for each finding and the count line.

Usage

folder_report(
  path = ".",
  config = "checkit.yml",
  rules = general_rules(),
  sets = system.file("rules", package = "checkit"),
  scope = default_scope,
  messages = checkit_messages(),
  set_pattern = "^checkit_([0-9]{4})_([0-9]{2})_([0-9]{2})$",
  reserved_ignore = function(set) character(0),
  reserved_rules = character(0)
)

Arguments

path

The project folder.

config

The name of the config file in path. A rule set MAY hold the key packages, a map from the name of a package to its minimum version. It is not a rule. The runner gives it to each rule function that has the argument packages, such as the function of rule_set_pinned.

rules

The registry: a named list of rule() entries, such as compose_rules() gives. The default is general_rules().

sets

The folder of the rule sets. The default is the folder rules of checkit.

scope

A function scope(path, ignore) that returns the named list files that each rule function gets. ignore holds the folders of the key ignore for a rule that is not locked, and is empty for a locked rule. The default is default_scope().

messages

The texts of the config messages, a named list as checkit_messages() gives.

set_pattern

A regular expression that each name of a rule set matches. Its three groups are the year, the month and the day of the set.

reserved_ignore

A function of the name of the set of the project that returns regular expressions. An entry of ignore that one of them matches is an error.

reserved_rules

A named character vector. Each name is a name that the keys rules and allow MUST NOT hold. The error is "<config>: <key>: <name> <value>", so each value is the rest of the message.

Value

A character vector of the lines. The attribute findings holds the findings of folder_findings(). The attribute counts is a named integer vector with 5 elements: the counts of PASS, WARN, FAIL, SKIP and OFF. The count line holds SKIP, after FAIL, only when the count of SKIP is more than 0.

See Also

Other runner: check_folder(), checkit_messages(), compose_rules(), default_scope(), folder_findings(), general_rules(), rule(), rules()


Freeze files with a manifest of their md5

Description

freeze() writes the md5 of each file of paths to the file manifest, in the format of md5sum. A rule can then find each file that changed after the freeze.

Usage

freeze(paths, manifest, home = ".")

Arguments

paths

The files and folders to freeze, relative to home.

manifest

The file to write, relative to home.

home

The folder that paths and manifest are relative to. The default "." is the working directory.

Details

Each line of the manifest is "<md5> <path>", with two spaces, and ends with LF. ⁠<path>⁠ is relative to home. The lines are in the order of the paths, sorted in the C locale. In a file with the extension .R or .r, freeze() reads each CR LF as LF before it takes the md5. R parses the file the same either way, so a checkout with core.autocrlf = true gives the same manifest. Every other file gets the md5 of its bytes.

A folder in paths stands for each file under it, hidden files included. The manifest has no line for itself. freeze() writes over an older manifest, and it stops before it writes when a path does not exist.

Value

The path manifest, invisibly.

Examples

home <- file.path(tempdir(), "checkit_freeze_example")
dir.create(file.path(home, "R"), recursive = TRUE)
writeLines("x <- 1", file.path(home, "run.R"))
writeLines("f <- function() return(1)", file.path(home, "R", "f.R"))
checkit::freeze(c("run.R", "R"), "frozen.md5", home = home)
readLines(file.path(home, "frozen.md5"))
unlink(home, recursive = TRUE)

The general rules of checkit

Description

general_rules() returns the registry of the rules of checkit that apply to any project. The argument texts replaces the texts of named rules, so that another package can give its own words.

Usage

general_rules(texts = list())

Arguments

texts

A list named by the names of rules. Each element is a named list with one or more of pass_detail, fix, locked_reason and the text arguments of that rule. pass_detail, fix and locked_reason replace those of the rule, see rule(). A text argument replaces the default value of that argument of the function of the rule. There are three text arguments:

  • exempt_calls of no_absolute_paths, a character vector of functions. The name of each function is the package that the call names with ::, such as c(utils = "read.csv"). The default is empty. The rule stops when it runs with a function that has no package name.

  • writer of frozen_versions_unchanged, one string.

  • holds of gitexclude_not_tracked, one string. The default is "data that git MUST NOT track".

Details

Each rule reads the files that the scope gives, see the argument scope of folder_findings() and default_scope(). Each rule parses each file and runs no code of it. The 12 rules, in the order of the registry:

  • rule_set_pinned: each package of the key packages of the rule set of the project is installed, at its minimum version or newer. The key maps the name of each package to its minimum version, such as ⁠packages: {checkit: 2026.12.3}⁠. Each FAIL has the path "".

  • lower_case_names: no path of files$entries has a capital letter in its name. The extension of a file does not count, and the paths of files$exempt_paths pass.

  • frozen_versions_unchanged, text argument writer: each manifest of files$frozen matches the files that it covers. A required manifest that is absent is a FAIL. So is a manifest line that is not "<md5> <path>", and each file that changed, was removed or was added after the manifest was written. The details name writer, the function that writes a manifest. The default of writer is "checkit::freeze()".

  • changelog_with_code: in a git repository, each uncommitted change to a path that the regular expression files$changelog_code matches comes with a change to CHANGELOG.md. Outside git it passes.

  • gitexclude_not_tracked, text argument holds: git tracks no path with gitexclude in its name. holds ends the FAIL detail and tells what such a path holds. Outside git it passes. A rule set SHOULD lock it, and its locked_reason tells why.

  • loc_per_file, parameter limit: each file of files$code has at most limit code lines, as org::loc_per_file() counts them. A file that R cannot parse is a FAIL.

  • cyclomatic_complexity, parameter limit: each function that a top-level assignment of a file of files$code defines scores at most limit, as cyclocomp::cyclocomp() scores it. A function in a list() of a top-level assignment counts too.

  • comment_share, parameter limit: in each file of files$scripts_sorted, the comment lines are at most the share limit of the lines that are not blank. limit is from 0 to 1. A section marker such as ⁠# Part 1 ----⁠ is not a comment line.

  • no_environment_variables: no file of files$scripts calls or names Sys.getenv or Sys.setenv.

  • no_local_assign, parameter forbid: no file of files$code uses a construct of forbid. The constructs are local(), assign(), delayedAssign(), assignInNamespace(), assignInMyNamespace(), ⁠<<-⁠ and ⁠->>⁠.

  • no_escape_hatches: no file of files$scripts_sorted calls suppressWarnings(), suppressMessages(), try(), tryCatch() or withCallingHandlers().

  • no_absolute_paths, text argument exempt_calls: no string of a file of files$code is an absolute path. No call of file.path() or fs::path() in these files starts from a string. The arguments of a call of exempt_calls are exempt.

A text argument is an argument of the function of a rule that has a default value. The rule functions are internal, and general_rules()[[name]]$fn gives each one. The function of rule_set_pinned has the argument packages, and the runner gives it the key packages of the rule set.

Value

A named list of rule() entries.

See Also

Other runner: check_folder(), checkit_messages(), compose_rules(), default_scope(), folder_findings(), folder_report(), rule(), rules()

Examples

names(checkit::general_rules())
own <- checkit::general_rules(list(
  no_absolute_paths = list(
    fix = "build each path from the folder argument",
    exempt_calls = c(utils = "read.csv")
  )
))
own$no_absolute_paths$fix

Define a rule

Description

rule() gives one entry of a registry, the named list of rules that folder_findings() runs. The name of the entry in the registry is the name of the rule.

Usage

rule(fn, pass_detail, fix, check_params = NULL, locked_reason = NULL)

Arguments

fn

The function of the rule, as described above.

pass_detail

The detail of the PASS line. It is a string, or a function of the entry of the rule in the rule set. The entry is a list with level and the parameters.

fix

The fix of each WARN and FAIL: a string, or a function of the detail of the finding that returns a string. The runner adds ". Fix: <fix>." to the detail.

check_params

NULL, or a function check_params(entry, name, config). The config reader calls it once for each parameter that the config file sets, other than level. entry is a named list with that one parameter, name is the name of the rule and config is the name of the config file. It returns entry with the checked value, or it stops with the config error text.

locked_reason

NULL, or one sentence that tells why the rule is locked. A rule set marks a rule locked with locked: true. The config file cannot change the level of a locked rule or allow a path for it. The config reader adds this sentence to that error.

Details

The runner calls fn(path, files, ...). path is the project folder, and files is the list that the scope argument of folder_findings() gives. The parameters of the entry of the rule set, such as limit, arrive as named arguments. When fn has an argument set, set is the name of the rule set of the project. fn returns a data.frame with the columns rule, status, path and detail, one row for each finding. Zero rows means PASS. A row MAY have the status "SKIP". At the levels fail and warn, the runner keeps it as SKIP, with no fix. The runner gives each other row that is not PASS the level of the rule.

Value

A list of class checkit_rule with the elements fn, pass_detail, fix, check_params and locked_reason.

See Also

Other runner: check_folder(), checkit_messages(), compose_rules(), default_scope(), folder_findings(), folder_report(), general_rules(), rules()

Examples

no_capitals <- checkit::rule(
  fn = function(path, files) {
    bad <- files$entries[grepl("[A-Z]", basename(files$entries))]
    return(data.frame(
      rule = rep("no_capitals", length(bad)),
      status = rep("FAIL", length(bad)),
      path = bad,
      detail = paste0(bad, ": a capital letter in the name", recycle0 = TRUE)
    ))
  },
  pass_detail = "no capital letter in a name",
  fix = "rename it in lower case"
)
names(no_capitals)

Print the rules that check a project

Description

rules() prints each rule of the set that the config file of home extends, in the order of the registry rules. It runs no rule and reads no other file of the project.

Usage

rules(
  home = ".",
  config = "checkit.yml",
  rules = general_rules(),
  sets = system.file("rules", package = "checkit"),
  messages = checkit_messages(),
  set_pattern = "^checkit_([0-9]{4})_([0-9]{2})_([0-9]{2})$",
  reserved_ignore = function(set) character(0),
  reserved_rules = character(0)
)

Arguments

home

The top folder of the project. The default "." is the working directory.

config

The name of the config file in path. A rule set MAY hold the key packages, a map from the name of a package to its minimum version. It is not a rule. The runner gives it to each rule function that has the argument packages, such as the function of rule_set_pinned.

rules

The registry: a named list of rule() entries, such as compose_rules() gives. The default is general_rules().

sets

The folder of the rule sets. The default is the folder rules of checkit.

messages

The texts of the config messages, a named list as checkit_messages() gives.

set_pattern

A regular expression that each name of a rule set matches. Its three groups are the year, the month and the day of the set.

reserved_ignore

A function of the name of the set of the project that returns regular expressions. An entry of ignore that one of them matches is an error.

reserved_rules

A named character vector. Each name is a name that the keys rules and allow MUST NOT hold. The error is "<config>: <key>: <name> <value>", so each value is the rest of the message.

Details

The first line is the line of the rules of check_folder(). Then, for each rule, rules() prints these items:

  • the name, and the level after the changes of the config file;

  • needs: the text of the PASS line;

  • parameters: the parameters, for a rule that has them;

  • fix: the fix of a finding.

When the fix is a function of the detail of a finding, rules() calls it with the detail "" and shows the text it returns.

Value

A data.frame, invisibly, with one row for each rule and the columns rule, level, needs, fix and parameters. parameters is "" for a rule without parameters. A set with no rule of the registry gives zero rows.

See Also

Other runner: check_folder(), checkit_messages(), compose_rules(), default_scope(), folder_findings(), folder_report(), general_rules(), rule()

Examples

home <- file.path(tempdir(), "checkit_rules_example")
dir.create(file.path(home, "rules"), recursive = TRUE)
writeLines(
  c("no_capitals:", "  level: warn", "  max: 3"),
  file.path(home, "rules", "demo_2026_01_01.yml")
)
writeLines("extends: demo_2026_01_01", file.path(home, "demo.yml"))
no_capitals <- checkit::rule(
  fn = function(path, files, max) {
    return(data.frame(
      rule = character(0),
      status = character(0),
      path = character(0),
      detail = character(0)
    ))
  },
  pass_detail = "no capital letter in a name",
  fix = "rename it in lower case"
)
checkit::rules(
  home,
  config = "demo.yml",
  rules = list(no_capitals = no_capitals),
  sets = file.path(home, "rules"),
  set_pattern = "^demo_([0-9]{4})_([0-9]{2})_([0-9]{2})$"
)
unlink(home, recursive = TRUE)