Skip to content

Commit

Permalink
tests: Skip examples/tests if Python not detected
Browse files Browse the repository at this point in the history
* We now skip examples/tests if we can't find our Python dependency
* We are observing a CRAN Check ERROR on the 'r-devel-windows-x86_64-gcc10-UCRT'
  flavor because it can't find our run-time Python dependency
  • Loading branch information
trevorld committed Aug 4, 2021
1 parent 5d73ff6 commit 757a803
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 95 deletions.
42 changes: 25 additions & 17 deletions R/argparse.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2012-2020 Trevor L. Davis <[email protected]>
# Copyright (c) 2012-2021 Trevor L. Davis <[email protected]>
#
# This file is free software: you may copy, redistribute and/or modify it
# under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -47,21 +47,23 @@
#' @export
#' @examples
#'
#' parser <- ArgumentParser(description='Process some integers')
#' parser$add_argument('integers', metavar='N', type = "integer", nargs='+',
#' help='an integer for the accumulator')
#' parser$add_argument('--sum', dest='accumulate', action='store_const',
#' const='sum', default='max',
#' help='sum the integers (default: find the max)')
#' parser$print_help()
#' # default args for ArgumentParser()$parse_args are commandArgs(TRUE)
#' # which is what you'd want for an Rscript but not for interactive use
#' args <- parser$parse_args(c("--sum", "1", "2", "3"))
#' accumulate_fn <- get(args$accumulate)
#' print(accumulate_fn(args$integers))
#' if (argparse:::detects_python()) {
#' parser <- ArgumentParser(description='Process some integers')
#' parser$add_argument('integers', metavar='N', type = "integer", nargs='+',
#' help='an integer for the accumulator')
#' parser$add_argument('--sum', dest='accumulate', action='store_const',
#' const='sum', default='max',
#' help='sum the integers (default: find the max)')
#' parser$print_help()
#' # default args for ArgumentParser()$parse_args are commandArgs(TRUE)
#' # which is what you'd want for an Rscript but not for interactive use
#' args <- parser$parse_args(c("--sum", "1", "2", "3"))
#' accumulate_fn <- get(args$accumulate)
#' print(accumulate_fn(args$integers))
#' }
ArgumentParser <- function(..., python_cmd = NULL) { # nolint
python_cmd <- .find_python_cmd(python_cmd)
.assert_python_cmd(python_cmd)
python_cmd <- find_python_cmd(python_cmd)
assert_python_cmd(python_cmd)
initial_python_code <- c("import argparse",
"try:",
" import json",
Expand Down Expand Up @@ -294,17 +296,23 @@ get_Rscript_filename <- function() { # nolint

# Internal function to check python cmd is okay
# @param python_cmd Python cmd to use
.assert_python_cmd <- function(python_cmd) {
assert_python_cmd <- function(python_cmd) {
if (!findpython::is_python_sufficient(python_cmd, required_modules = c("argparse", "json | simplejson"))) {
stop(paste(sprintf("python executable %s either is not installed,", python_cmd),
"is not on the path, or does not have argparse, json modules",
"please see INSTALL file"))
}
}

detects_python <- function() {
python_cmd <- find_python_cmd()
findpython::is_python_sufficient(python_cmd,
required_modules = c("argparse", "json | simplejson"))
}

# Internal function to find python cmd
# @param python_cmd Python cmd to use
.find_python_cmd <- function(python_cmd) {
find_python_cmd <- function(python_cmd = NULL) {
if (is.null(python_cmd)) {
python_cmd <- getOption("python_cmd")
}
Expand Down
26 changes: 15 additions & 11 deletions cran-comments.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
**Nota benes**
## Notes

* This update should fix the new ``r-devel-linux-x86-64-debian-gcc`` package check error
(seems to be result of non-reverse-compatible python update).
* The examples and unit tests are now skipped if an appropriate
version of Python is not found as with the 'r-devel-windows-x86_64-gcc10-UCRT'
CRAN flavor.

* As in previous uploads while in a non-interactive session (i.e. in an
Rscript) if ``parse_args()`` observes a help flag it will print a usage
Expand All @@ -13,18 +14,21 @@
* This package has a Python dependency most easily satisfied having (C)Python
3.2 or greater on the PATH. See file INSTALL for more details.

**Test environments**
## Test environments

* local (linux), R 4.0.3
* local (linux), R 4.1.0
* win-builder (windows), R devel
* appveyor (windows), R release and R devel
* travis-ci (OSX), R release
* travis-ci (linux), R release and R devel
* Github Actions (linux), R release and R devel
* Github Actions (windows), R release
* Github Actions (OSX), R release

**R CMD check --as-cran results**
## R CMD check --as-cran results

Status: OK

**Downstream dependencies**
## revdepcheck results

This package does not have any downstream dependencies.
We checked 3 reverse dependencies (0 from CRAN + 3 from Bioconductor), comparing R CMD check results across CRAN and dev versions of this package.

* We saw 0 new problems
* We failed to check 0 packages
26 changes: 14 additions & 12 deletions man/ArgumentParser.Rd

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

26 changes: 0 additions & 26 deletions tests/test_help.Rout.save

This file was deleted.

24 changes: 0 additions & 24 deletions tests/test_version.Rout.save

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_help.R → tests/testthat/scripts/test_help.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library("argparse")
p <- ArgumentParser()
p$parse_args("--help")
p$parse_args()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library("argparse")
p <- ArgumentParser()
p$add_argument("-v", "--version", action = "version", version = "1.0.1")
p$parse_args("--version")
p$parse_args()
35 changes: 32 additions & 3 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2012-2018 Trevor L Davis <[email protected]>
# Copyright (c) 2012-2021 Trevor L Davis <[email protected]>
#
# This file is free software: you may copy, redistribute and/or modify it
# under the terms of the GNU General Public License as published by the
Expand All @@ -21,10 +21,9 @@
# Python (GPL-compatible) license stack.
context("Unit tests")

options(python_cmd = .find_python_cmd(NULL))
# options(python_cmd = "/home/trevorld/tmp/python/Python-3.9.0/python") # nolint
context("print_help")
test_that("print_help works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser(description = "Process some integers.")
expect_output(parser$print_help(), "usage:")
expect_output(parser$print_help(), "optional arguments:")
Expand All @@ -42,6 +41,7 @@ test_that("print_help works as expected", {

context("convert_agument")
test_that("convert_argument works as expected", {
skip_if_not(detects_python())
expect_equal(convert_argument("foobar"), "'foobar'")
expect_equal(convert_argument(14.9), "14.9")
expect_equal(convert_argument(c(12.1, 14.9)), "(12.1, 14.9)")
Expand All @@ -50,6 +50,7 @@ test_that("convert_argument works as expected", {

context("convert_..._to_arguments")
test_that("convert_..._to_arguments works as expected", {
skip_if_not(detects_python())
# test in mode "add_argument"
c.2a <- function(...) convert_..._to_arguments("add_argument", ...)
waz <- "wazzup"
Expand All @@ -71,6 +72,7 @@ test_that("convert_..._to_arguments works as expected", {

context("add_argument")
test_that("add_argument works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser()
parser$add_argument("integers", metavar = "N", type = "integer", nargs = "+",
help = "an integer for the accumulator")
Expand Down Expand Up @@ -119,6 +121,7 @@ test_that("add_argument works as expected", {

context("version")
test_that("version flags works as expected", {
skip_if_not(detects_python())
# Feature request of Dario Beraldi
parser <- ArgumentParser()
parser$add_argument("-v", "--version", action = "version", version = "1.0.1")
Expand All @@ -136,6 +139,7 @@ test_that("version flags works as expected", {

context("ArgumentParser")
test_that("ArgumentParser works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser(prog = "foobar", usage = "%(prog)s arg1 arg2")
parser$add_argument("--hello", dest = "saying", action = "store_const",
const = "hello", default = "bye",
Expand All @@ -149,7 +153,9 @@ test_that("ArgumentParser works as expected", {
expect_error(ArgumentParser(add_help = TRUE)$parse_args("-h"), "help requested")
expect_error(ArgumentParser(add_help = FALSE)$parse_args("-h"), "unrecognized arguments")
})

test_that("parse_args works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser("foobar", usage = "%(prog)s arg1 arg2")
parser$add_argument("--hello", dest = "saying", action = "store", default = "foo",
choices = c("foo", "bar"),
Expand Down Expand Up @@ -192,6 +198,7 @@ test_that("parse_args works as expected", {
# Bug found by Erick Rocha Fonseca
context("Unicode arguments/options")
test_that("Unicode support works if Python and OS sufficient", {
skip_if_not(detects_python())
skip_on_os("windows") # Didn't work on win-builder
skip_on_cran() # Didn't work on Debian Clang
did_find_python3 <- findpython::can_find_python_cmd(minimum_version = "3.0",
Expand All @@ -202,7 +209,9 @@ test_that("Unicode support works if Python and OS sufficient", {
p$add_argument("name")
expect_equal(p$parse_args("\u8292\u679C"), list(name = "\u8292\u679C")) # 芒果
})

test_that("Unicode attempt throws error if Python or OS not sufficient", {
skip_if_not(detects_python())
skip_on_os("windows") # Didn't work on AppVeyor
skip_on_cran() # Didn't work on Debian Clang
did_find_python2 <- findpython::can_find_python_cmd(maximum_version = "2.7",
Expand All @@ -218,6 +227,7 @@ test_that("Unicode attempt throws error if Python or OS not sufficient", {
# Mutually exclusive groups is a feature request by Vince Reuter
context("Mutually exclusive groups")
test_that("mutually exclusive groups works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser(prog = "PROG")
group <- parser$add_mutually_exclusive_group()
group$add_argument("--foo", action = "store_true")
Expand All @@ -240,6 +250,7 @@ test_that("mutually exclusive groups works as expected", {
# argument groups is a feature request by Dario Beraldi
context("Add argument group")
test_that("add argument group works as expected", {
skip_if_not(detects_python())
parser <- ArgumentParser(prog = "PROG", add_help = FALSE)
group1 <- parser$add_argument_group("group1", "group1 description")
group1$add_argument("foo", help = "foo help")
Expand All @@ -252,6 +263,7 @@ test_that("add argument group works as expected", {
# subparser support is a feature request by Zebulun Arendsee
context("Supparser support")
test_that("sub parsers work as expected", {
skip_if_not(detects_python())
# create the top-level parser
parser <- ArgumentParser(prog = "PROG")
parser$add_argument("--foo", action = "store_true", help = "foo help")
Expand All @@ -276,3 +288,20 @@ test_that("sub parsers work as expected", {
expect_output(parser_a$print_help(), "usage: PROG a")
expect_output(parser_b$print_help(), "usage: PROG b")
})

context("Paths that quit()")
test_that("Paths that quit()", {
skip_if_not(detects_python())
skip_on_os("windows")
cmd <- file.path(R.home(), "bin/Rscript")
skip_if(Sys.which(cmd) == "")

expect_equal(system2(cmd, c("scripts/test_version.R", "--version"), stdout = TRUE),
"1.0.1")

help <- system2(cmd, c("scripts/test_help.R", "--help"),
stdout = TRUE, stderr = TRUE)
expect_equal(help,
c("usage: scripts/test_help.R [-h]", "", "optional arguments:",
" -h, --help show this help message and exit"))
})

0 comments on commit 757a803

Please sign in to comment.