Skip to content

use_lintr() proposal - #580

Closed
leonawicz wants to merge 4 commits into
r-lib:masterfrom
leonawicz:use-lintr
Closed

leonawicz wants to merge 4 commits into
r-lib:masterfrom
leonawicz:use-lintr

Conversation

@leonawicz

Copy link
Copy Markdown

This is an initial version of a use_lintr function that provides three typical methods for how to integrate code linting into package development. It is not ready to be merged yet but this can start the discussion about how it can be improved. It is also lacking unit tests currently.

Fixes #579

@leonawicz

Copy link
Copy Markdown
Author

This is a new issue and PR but it is part of Tidyverse Dev Day. @batpigandme can you add a dev day label?

Comment thread R/linting.R Outdated
cat(paste0("linters: with_defaults(\n ", "line_length_linter(80))\n"))
sink()

if(type == "test" && !file.exists(".lintr")) file.symlink("inst/.lintr", ".lintr")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This symbolic link approach is problematic. This is the "old way" of making lintr used as a testthat unit test work with Travis CI. I think there is a newer, better way to do this that does not involve a symbolic link to a .lintr file located inside inst but I do not know how this should be altered to get rid of the linking approach for the unit test setup.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usethis uses the fs package for all file system and file path operations. So that should be on the radar as you resolve this.

@batpigandme batpigandme added the tidy-dev-day 🤓 Tidyverse Developer Day label Jan 19, 2019
Comment thread R/linting.R Outdated
type <- match.arg(type)

if(type == "test"){
dir.create("inst", showWarnings = FALSE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the internal create_directory() or exported use_directory() for consistency with the rest of usethis.

Comment thread R/linting.R Outdated

sink(.lintr)
cat(paste0("linters: with_defaults(\n ", "line_length_linter(80))\n"))
sink()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have existing helpers for writing (into) files. See the functions in write.R.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I'll look into all these changes I need to make to resolve these inconsistencies vis a vis usethis and fs functions.

Comment thread R/linting.R Outdated
cat(paste0("linters: with_defaults(\n ", "line_length_linter(80))\n"))
sink()

if(type == "test" && !file.exists(".lintr")) file.symlink("inst/.lintr", ".lintr")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usethis uses the fs package for all file system and file path operations. So that should be on the radar as you resolve this.

expect_proj_file(".lintr")
})

# test_that("use_lintr('test') creates build-ignored, promised files", {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test cannot be run because the testthat unit test setup option for use_lintr still depends on the old, unacceptable symlink approach. I am hoping someone knows of an alternative approach. Obviously the symbolic link cannot be created without elevated privileges.

Separately, I am unsure how to test the writing out of a test file. If I tentatively uncomment this test and elevate my privileges to enable writing of the symbolic link, the test subsequently fails because of an inability to write out the test file during testing.

Comment thread tests/testthat/test-use-lintr.R Outdated
@@ -0,0 +1,24 @@
context("use_lintr")

test_that("use_lintr('local') creates build-ignored, promised file", {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add lintr to Suggests in DESCRIPTION?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in general we don't necessarily add a package to Suggests here in usethis, just because usethis offers some support for it. It would not be very sustainable. They way we test related functionality is to use a test mock to get around the presence test. See, for example, use_tutorial() is tested vis-a-vis learnr.

@jennybc jennybc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are working towards a near term release. We can just let this one age a bit more, but I pointed out a few more things.

Comment thread R/linting.R Outdated
#' Use code linting
#'
#' `use_lintr()` Adds support for code linting using the `lintr` package in one of three ways:
#' local interactive linting only, non-failing (post-build) Travis CI linting, or failing local and Travis CI `testthat` unit test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you use a bullet list. Note that we use markdown syntax for roxygen comments. If you look around, you will find many other bullet lists to emulate re: whitespace, etc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you add the respective argument values like so:

* "local": local interactive listing only
* "travis": non-failing, post-build lining on Travis CI
* "test": testthat unit test

Comment thread R/linting.R Outdated
#' `use_lintr()` Adds support for code linting using the `lintr` package in one of three ways:
#' local interactive linting only, non-failing (post-build) Travis CI linting, or failing local and Travis CI `testthat` unit test.
#'
#' @param type Options are \code{local}, \code{travis} or \code{test}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' @param type Options are \code{local}, \code{travis} or \code{test}.
#' @param type `"local"`, `"travis"`, or `"test"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if "type" is the right name for this argument. mode also comes to mind, as in "usage mode".

Comment thread tests/testthat/test-use-lintr.R Outdated
@@ -0,0 +1,24 @@
context("use_lintr")

test_that("use_lintr('local') creates build-ignored, promised file", {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in general we don't necessarily add a package to Suggests here in usethis, just because usethis offers some support for it. It would not be very sustainable. They way we test related functionality is to use a test mock to get around the presence test. See, for example, use_tutorial() is tested vis-a-vis learnr.

Comment thread R/linting.R Outdated
} else {
ui_todo("Code linting set up for interactive use. Edit {ui_path('.lintr')} as needed.")
}
use_package("lintr", "suggests")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should near the top of the body, since we would exit early lintr is not installed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll make the suggested changes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the changes. I also had to tentatively comment out all three unit tests. I'm still having difficulty understanding how to make those work in this context.

@hadley

hadley commented Mar 17, 2019

Copy link
Copy Markdown
Member

@jennybc I think we need to agree on one way of using lintr before adding the support for 3 different styles.

@leonawicz

leonawicz commented Mar 17, 2019 •

Copy link
Copy Markdown
Author

Even though I use lintr as a unit test in packages, that mode is excessive for most users. Maybe the best option for one agreed upon mode is the simplest one: lintr for interactive use only, where all that is created is a default config file?

Also, since users may not initially be familiar with how to edit this file to customize their linting preferences, is it better to create one that includes some linting defaults rather than an empty config file? Placing entries in the config file that are already lintr defaults is technically redundant, but would help show users how the file is structured. Thoughts?

@jennybc

jennybc commented Mar 17, 2019

Copy link
Copy Markdown
Member

I think we need to agree on one way of using lintr before adding the support for 3 different styles.

Yeah and the challenge is that I, at least, don't really use lintr with any regularity. Which is one reason to give this a bit more time to simmer.

@jennybc jennybc changed the title Added initial version of function. Proposal for use_lintr() Mar 17, 2019
@jennybc jennybc changed the title Proposal for use_lintr() use_lintr() proposal Mar 17, 2019
@jennybc jennybc added this to the Backlog milestone Mar 28, 2019
@jennybc jennybc removed this from the Backlog milestone Apr 10, 2019
@ThierryO

ThierryO commented Dec 2, 2019

Copy link
Copy Markdown

May I suggest my workflow on this? It might be useful in the discussion on the potential workflows.

  • A unit test in tests/testthat/test_zzz_coding_style.R, this file is in .gitignore and runs only locally, integrated in the unit tests.
  • A separate step in .travis.yml. As tests/testthat/test_zzz_coding_style.R is never commited, lintr is only run as a separate step and not during the unit tests.
  • If I need it interactively I use lintr::lint_package() or the RStudio addin.

@hadley

hadley commented Mar 13, 2020

Copy link
Copy Markdown
Member

Over a year later and I think it's more obvious how this should now work: as a github action. So I'm going to close this issue — thanks @leonawicz for all your hard work, and we're sorry it didn't end up making it into usethis for before other technology moved on.

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

Labels

tidy-dev-day 🤓 Tidyverse Developer Day

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FR: use_lintr function to help set up code linting for package

5 participants