Skip to content

Instantly share code, notes, and snippets.

@russHyde
Last active February 25, 2021 04:06
Show Gist options
  • Save russHyde/568fd5af558c860d97e1b932ca773ff7 to your computer and use it in GitHub Desktop.
Save russHyde/568fd5af558c860d97e1b932ca773ff7 to your computer and use it in GitHub Desktop.
# compare the object-usage lints obtained before/after a given PR
# the PR is always compared against master
library(magrittr)
library(dplyr)
library(purrr)
library(tibble)
# In lintr directory
lint_all_packages <- function(pkgs, linter) {
map(pkgs, function(x) {
lint_dir(x, linters = linter, parse_settings = FALSE)
}) %>%
set_names(basename(pkgs))
}
format_lints <- function(x) {
x %>%
map(as_tibble) %>%
bind_rows(.id = "package")
}
run_workflow <- function(pkg_dir, pr, linter_name) {
stopifnot(dir.exists(pkg_dir))
if(!file.exists("lintr.Rproj")) {
"compare_branches.R should be run inside the lintr-package directory"
}
pkgs <- dir(pkg_dir, full.names=TRUE)
# lints from current-master branch
git2r::checkout(branch = "master")
devtools::load_all()
linter <- get(linter_name)()
main_lints <- pkgs %>%
lint_all_packages(linter) %>%
format_lints()
# load the new version of lintr
usethis::pr_fetch(pr)
devtools::load_all()
linter <- get(linter_name)()
pr_lints <- pkgs %>%
lint_all_packages(linter) %>%
format_lints()
# revert back to main
git2r::checkout(branch = "master")
list(
main = main_lints,
pr = pr_lints
)
}
###############################################################################
# TODO: handle both command line args and interactive runs
# TODO: handle the case when working directory is not the lintr directory
# TODO: convert to the original branch (if this was not master)
# - at the end of the workflow (currently this always converts back to
# master)
# - and if there is any error when running the workflow
# TODO: save data.frame of lints to file
###############################################################################
# User should define the following in an interactive session:
# - 'pr' the number of the pull request that is to be compared against master
# - 'linter_name' the linter whose behaviour should be compared before/after
# the PR-associated changes
# - 'pkg_dir' pathname for a directory that contains the repositories that are
# to be analysed;
#
# ... and then source this script
pr <- if (exists("pr")) pr else 709
linter_name <- if (exists("linter_name")) linter_name else "object_usage_linter"
pkg_dir <- if (exists("pkg_dir")) {
pkg_dir
} else {
file.path("~", "proj", "code_as_data", "data", "packages")
}
message(pr)
message(linter_name)
message("Any package repo found in this directory will be analysed:", pkg_dir)
lints <- run_workflow(pkg_dir, pr, linter_name)
@russHyde
Copy link
Author

At present, you have to

  • prepare a directory (referred to as pkg_dir) that contains repositories for multiple R packages (each package being of standard form with DESCRIPTION etc at the top level);
  • have your 'lintr' directory as the working directory
  • run this interactively in R
  • define the variables "pr", "linter_name" and "pkg_dir" before sourcing this script

@russHyde
Copy link
Author

This script is for use during development of 'lintr'.
It identifies the lints called by lintr on a given set of packages by both the master-branch of lintr, and a PR-branch (specified numerically).
These lints can then be compared to find any differences introduced by the PR-associated changes

@russHyde
Copy link
Author

Updated to handle linters = assignment_linter() syntax (rather than linters = assignment_linter).

@AshesITR
Copy link

Thanks for sharing this. There's a small refac error (pkgs_dir vs. pkg_dir).
I'm currently trying this out on lintr#709 with the top 100 cran packages from last month plus shiny, with devtools::load_all(x) added in line 13 just before lint_dir.

@russHyde
Copy link
Author

Fixed the typo. Thanks @AshesITR

@russHyde
Copy link
Author

A modified version of this script is being added to lintr: r-lib/lintr#760

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