pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/R2pkg pkgtools/R2pkg: add unit tests



details:   https://anonhg.NetBSD.org/pkgsrc/rev/047dc37384e3
branches:  trunk
changeset: 402646:047dc37384e3
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sun Oct 13 18:13:03 2019 +0000

description:
pkgtools/R2pkg: add unit tests

diffstat:

 pkgtools/R2pkg/Makefile           |    9 ++-
 pkgtools/R2pkg/files/R2pkg.R      |   18 ++--
 pkgtools/R2pkg/files/R2pkg_test.R |  128 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+), 11 deletions(-)

diffs (227 lines):

diff -r 4df179e16da3 -r 047dc37384e3 pkgtools/R2pkg/Makefile
--- a/pkgtools/R2pkg/Makefile   Sun Oct 13 17:23:29 2019 +0000
+++ b/pkgtools/R2pkg/Makefile   Sun Oct 13 18:13:03 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.7 2019/10/13 13:26:22 rillig Exp $
+# $NetBSD: Makefile,v 1.8 2019/10/13 18:13:03 rillig Exp $
 #
 
 VERS=          0.6.2
@@ -16,6 +16,8 @@
 AUTO_MKDIRS=   yes
 
 DEPENDS+=      curl>=7.52.1:../../www/curl
+TEST_DEPENDS+= R-testthat>=2.2.1:../../devel/R-testthat
+TEST_DEPENDS+= R-withr>=2.1.2:../../devel/R-withr
 
 SUBST_CLASSES+=        R
 SUBST_STAGE.R= pre-configure
@@ -24,7 +26,10 @@
 LIBDIR=                ${PREFIX}/lib/R2pkg
 
 do-extract:
-       cd ${FILESDIR} && ${CP} R2pkg.* ${WRKSRC}
+       cd ${FILESDIR} && ${CP} R2pkg*.* ${WRKSRC}
+
+do-test:
+       cd ${WRKSRC} && LC_ALL=C ${PREFIX}/bin/Rscript R2pkg_test.R
 
 do-install:
        ${INSTALL_SCRIPT} ${WRKSRC}/R2pkg.sh ${DESTDIR}${PREFIX}/sbin/R2pkg
diff -r 4df179e16da3 -r 047dc37384e3 pkgtools/R2pkg/files/R2pkg.R
--- a/pkgtools/R2pkg/files/R2pkg.R      Sun Oct 13 17:23:29 2019 +0000
+++ b/pkgtools/R2pkg/files/R2pkg.R      Sun Oct 13 18:13:03 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: R2pkg.R,v 1.5 2019/10/13 17:23:29 rillig Exp $
+# $NetBSD: R2pkg.R,v 1.6 2019/10/13 18:13:03 rillig Exp $
 #
 # Copyright (c) 2014,2015,2016,2017,2018,2019
 #      Brook Milligan.  All rights reserved.
@@ -271,7 +271,7 @@
   df
 }
 
-read.Makefile.as.dataframe <- function()
+read.Makefile.as.dataframe <- function(filename = 'Makefile.orig')
 {
   # message('===> read.Makefile.as.dataframe():')
 
@@ -322,7 +322,7 @@
     re.value,
     '$')
 
-  df <- read.file.as.dataframe('Makefile.orig')
+  df <- read.file.as.dataframe(filename)
 
   df$order <- 1:nrow(df)
   df$category <- NA
@@ -498,9 +498,9 @@
 }
 sed.r_pkgver <- function(r_pkgver) make.sed.command('R_PKGVER',r_pkgver)
 
-find.Rcpp <- function(s1, s2) grepl('Rcpp', paste(s1, s2))
+find.Rcpp <- function(imps, deps) grepl('Rcpp', paste(imps, deps))
 
-buildlink3.mk <- function(s1,s2)
+buildlink3.mk <- function(imps,deps)
 {
   BUILDLINK3.MK <- data.frame()
   buildlink3.mk.list <- read.file.as.list('BUILDLINK3.MK')
@@ -511,7 +511,7 @@
       value <- line
       BUILDLINK3.MK <- rbind(BUILDLINK3.MK,data.frame(key=key,value=value))
     }
-  if (find.Rcpp(s1,s2))
+  if (find.Rcpp(imps,deps))
     {
       buildlink3.line <- '.include "../../devel/R-Rcpp/buildlink3.mk"'
       key <- 'devel'
@@ -696,13 +696,13 @@
     level.warning('error updating dependency for ',depends(dependency))
 }
 
-make.depends <- function(s1,s2)
+make.depends <- function(imps,deps)
 {
-  imports <- make.imports(s1,s2)
+  imports <- make.imports(imps,deps)
   # XXX  message('===> imports:')
   # XXX print(imports)
   DEPENDS <- data.frame()
-  BUILDLINK3.MK <- buildlink3.mk(s1,s2)
+  BUILDLINK3.MK <- buildlink3.mk(imps,deps)
   if (length(imports) > 0)
     {
       for (i in 1:length(imports))
diff -r 4df179e16da3 -r 047dc37384e3 pkgtools/R2pkg/files/R2pkg_test.R
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/pkgtools/R2pkg/files/R2pkg_test.R Sun Oct 13 18:13:03 2019 +0000
@@ -0,0 +1,128 @@
+# $NetBSD: R2pkg_test.R,v 1.1 2019/10/13 18:13:03 rillig Exp $
+#
+# Copyright (c) 2019
+#      Roland Illig.  All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+# 3. Neither the name of the author nor the names of any contributors
+#    may be used to endorse or promote products derived from this software
+#    without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+
+source('R2pkg.R')
+library(testthat)
+library(withr)
+
+test_that('make.imports', {
+    imports <- make.imports('first (>= 1.0)', 'second')
+
+    expect_equal(imports, c('first(>=1.0)', 'second'))
+})
+
+test_that('make.dependency', {
+    imports <- make.dependency('first(>=1.0)')
+
+    expect_equal(imports, c('first', '>=1.0'))
+})
+
+test_that('buildlink3.file with matching version number', {
+    local_dir('..')
+    dependency <- make.dependency('ellipsis(>=0.1)')
+
+    bl3 <- buildlink3.file(dependency)
+
+    expect_equal(bl3, '../../math/R-ellipsis/buildlink3.mk')
+})
+
+# The version number of the dependency is not checked against
+# the resolved buildlink3 file.
+test_that('buildlink3.file with too high version number', {
+    local_dir('..')
+    dependency <- make.dependency('ellipsis(>=1000.0)')
+
+    bl3 <- buildlink3.file(dependency)
+
+    expect_equal(bl3, '../../math/R-ellipsis/buildlink3.mk')
+})
+
+test_that('level.warning', {
+    output <- ''
+    mock_message <- function(...) output <<- paste0(output, ..., '\n')
+
+    arg.level <<- 123  # XXX: should use with_environment instead
+    with_mock(message = mock_message, {
+        level.warning('mess', 'age', ' text')
+    })
+
+    expect_equal(output, '[ 123 ] WARNING: message text\n')
+})
+
+test_that('read.file.as.dataframe', {
+    content <- textConnection('VAR=value\nVAR2=value2\n')
+
+    df <- read.file.as.dataframe(content)
+
+    expect_equal(length(df$line), 3)
+    expect_equal(df$line[[1]], 'VAR=value')
+    expect_equal(df$line[[2]], 'VAR2=value2')
+    expect_equal(df$line[[3]], '')
+})
+
+test_that('read.Makefile.as.dataframe', {
+    lines <- c(
+        '# comment',
+        'VAR= value',
+        '',
+        '.include "other.mk"',
+        '.if 0',
+        '.endif'
+    )
+    content <- paste0(paste(lines, collapse = '\n'), '\n')
+    expect_equal(content, '# comment\nVAR= value\n\n.include "other.mk"\n.if 0\n.endif\n')
+
+    df <- read.Makefile.as.dataframe(textConnection(content))
+
+    out <- ''
+    with_output_sink(textConnection('out', 'w', local = TRUE), {
+        print(df)
+    })
+
+    expected.out <- c(
+        '                 line order category key_value  key depends buildlink3.mk',
+        '1           # comment     1       NA     FALSE <NA>   FALSE         FALSE',
+        '2          VAR= value     2       NA      TRUE  VAR   FALSE         FALSE',
+        '3                         3       NA     FALSE <NA>   FALSE         FALSE',
+        '4 .include "other.mk"     4       NA     FALSE <NA>   FALSE         FALSE',
+        '5               .if 0     5       NA     FALSE <NA>   FALSE         FALSE',
+        '6              .endif     6       NA     FALSE <NA>   FALSE         FALSE',
+        '7                         7       NA     FALSE <NA>   FALSE         FALSE',
+        '  operator delimiter old_value old_todo',
+        '1     <NA>      <NA>      <NA>     <NA>',
+        '2        =               value         ',
+        '3     <NA>      <NA>      <NA>     <NA>',
+        '4     <NA>      <NA>      <NA>     <NA>',
+        '5     <NA>      <NA>      <NA>     <NA>',
+        '6     <NA>      <NA>      <NA>     <NA>',
+        '7     <NA>      <NA>      <NA>     <NA>'
+    )
+    expect_equal(out, expected.out)
+})



Home | Main Index | Thread Index | Old Index