From 869d5f1799e71f26ade29b4c878999652cd0744d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 18 Jan 2024 13:09:26 +0100 Subject: [PATCH] feat: add git clean exercise (#16) --- NAMESPACE | 1 + R/clean-dir.R | 67 +++++++++++++++++++++++++++++ _pkgdown.yml | 1 + inst/exo_clean_dir-Rprofile.R | 20 +++++++++ man/exo_clean_dir.Rd | 36 ++++++++++++++++ tests/testthat/_snaps/clean-dir.md | 8 ++++ tests/testthat/_snaps/create-all.md | 8 ++++ tests/testthat/test-clean-dir.R | 7 +++ 8 files changed, 148 insertions(+) create mode 100644 R/clean-dir.R create mode 100644 inst/exo_clean_dir-Rprofile.R create mode 100644 man/exo_clean_dir.Rd create mode 100644 tests/testthat/_snaps/clean-dir.md create mode 100644 tests/testthat/test-clean-dir.R diff --git a/NAMESPACE b/NAMESPACE index 61de0a6..10df3b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(create_all_exercises) +export(exo_clean_dir) export(exo_committed_to_main) export(exo_committed_to_wrong) export(exo_latest_message) diff --git a/R/clean-dir.R b/R/clean-dir.R new file mode 100644 index 0000000..fad43b1 --- /dev/null +++ b/R/clean-dir.R @@ -0,0 +1,67 @@ +#' "Hey I'd like to remove these untracked files I created to test stuff!" +#' +#' @description +#' If debugging for instance created now useless untracked files and directories, +#' there's no need to remove them "manually". +#' The tool for that is `git clean`: +#' - `git clean -n` for a dry run; +#' - `git clean -f` to run it; +#' Add `-d` to also remove directories. +#' See . +#' +#' +#' @inheritParams exo_one_small_change +#' +#' @section Git commands: +#' `git clean`. +#' @return The path to the new project +#' @export +#' +#' @examplesIf interactive() +#' parent_path <- withr::local_tempdir() +#' path <- exo_clean_dir(parent_path = parent_path) +exo_clean_dir <- function(parent_path) { + + path <- file.path(parent_path, "clean-dir") + + withr::local_options(usethis.quiet = TRUE) + + dir_create(path) + original_dir <- getwd() + + withr::local_dir(path) + gert::git_init() + + file.copy( + system.file("exo_clean_dir-Rprofile.R", package = "saperlipopette"), + ".Rprofile" + ) + + create_project(path = getwd()) + # Ignore Rproj that might otherwise get edited when we open the project + rproj <- fs::dir_ls(glob = "*.Rproj") + usethis::local_project(getwd(), force = TRUE) + usethis::use_git_ignore(rproj) + usethis::use_git_ignore(".Rprofile") + gert::git_add("*") + git_commit("First commit") + + new_script <- file.path("R", "script.R") + fs::file_create(new_script) + script_lines <- c("a <- 1", "b <- 2") + brio::write_lines(text = script_lines, path = new_script) + gert::git_add(new_script) + git_commit("feat: add script") + + fs::dir_create("debugging") + fs::file_create("debug1") + fs::file_create("debug2") + fs::file_create("debug3") + fs::file_create("debug4") + + usethis::local_project(original_dir, force = TRUE) + + cli::cli_alert_info("Follow along in {path}!") + + return(path) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 7cd508a..981560a 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -15,6 +15,7 @@ reference: - title: Other exercises contents: - exo_split_changes + - exo_clean_dir - exo_rebase_i - title: All exercises at once contents: diff --git a/inst/exo_clean_dir-Rprofile.R b/inst/exo_clean_dir-Rprofile.R new file mode 100644 index 0000000..befde54 --- /dev/null +++ b/inst/exo_clean_dir-Rprofile.R @@ -0,0 +1,20 @@ +if (file.exists("~/.Rprofile")) { + base::sys.source("~/.Rprofile", envir = environment()) +} + +cli::cli_alert_danger('"Hey, how do I remove all my debugging left-over stuff at once?"') +cli::cli_alert_danger("I want to remove the 'debugging' folder and 'debug' files.") +cli::cli_alert_info("See {.url https://git-scm.com/docs/git-clean}") +cli::cli_alert_info("For more help use {.run tip()}") + +tip <- function() { + cli::cli_li( + items = c( + "Examine Git history and your files.", + "{.code git clean -n -d}", + "{.code git clean -f -d}", + "Examine Git history and your files." + ) + ) + +} diff --git a/man/exo_clean_dir.Rd b/man/exo_clean_dir.Rd new file mode 100644 index 0000000..5fc2465 --- /dev/null +++ b/man/exo_clean_dir.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean-dir.R +\name{exo_clean_dir} +\alias{exo_clean_dir} +\title{"Hey I'd like to remove these untracked files I created to test stuff!"} +\usage{ +exo_clean_dir(parent_path) +} +\arguments{ +\item{parent_path}{Path where to create the exercise repo} +} +\value{ +The path to the new project +} +\description{ +If debugging for instance created now useless untracked files and directories, +there's no need to remove them "manually". +The tool for that is \verb{git clean}: +\itemize{ +\item \verb{git clean -n} for a dry run; +\item \verb{git clean -f} to run it; +Add \code{-d} to also remove directories. +See \url{https://git-scm.com/docs/git-clean}. +} +} +\section{Git commands}{ + +\verb{git clean}. +} + +\examples{ +\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +parent_path <- withr::local_tempdir() +path <- exo_clean_dir(parent_path = parent_path) +\dontshow{\}) # examplesIf} +} diff --git a/tests/testthat/_snaps/clean-dir.md b/tests/testthat/_snaps/clean-dir.md new file mode 100644 index 0000000..9613800 --- /dev/null +++ b/tests/testthat/_snaps/clean-dir.md @@ -0,0 +1,8 @@ +# exo_clean_dir() works + + Code + gert::git_log(repo = path)[["commit"]] + Output + [1] "2a61d9da6865eb7f7272c9f901f13c1da377a01e" + [2] "e227ecc55e421f70b6e30602e6a2eee02aad42e0" + diff --git a/tests/testthat/_snaps/create-all.md b/tests/testthat/_snaps/create-all.md index efe8685..9c3278d 100644 --- a/tests/testthat/_snaps/create-all.md +++ b/tests/testthat/_snaps/create-all.md @@ -4,6 +4,14 @@ create_all_exercises(parent_path) Output + +-- clean-dir + | +-- R + | | \-- script.R + | +-- debug1 + | +-- debug2 + | +-- debug3 + | +-- debug4 + | \-- debugging +-- committed-to-main | +-- R | \-- bla diff --git a/tests/testthat/test-clean-dir.R b/tests/testthat/test-clean-dir.R new file mode 100644 index 0000000..3c844c2 --- /dev/null +++ b/tests/testthat/test-clean-dir.R @@ -0,0 +1,7 @@ +test_that("exo_clean_dir() works", { + rlang::local_options(cli.default_handler = function(msg) invisible(NULL)) + parent_path <- withr::local_tempdir() + path <- exo_clean_dir(parent_path = parent_path) + expect_equal(fs::path_file(path), "clean-dir") + expect_snapshot(gert::git_log(repo = path)[["commit"]]) +})