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 more unit tests



details:   https://anonhg.NetBSD.org/pkgsrc/rev/285864ea932e
branches:  trunk
changeset: 402647:285864ea932e
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sun Oct 13 19:13:47 2019 +0000

description:
pkgtools/R2pkg: add more unit tests

diffstat:

 pkgtools/R2pkg/Makefile           |    4 +-
 pkgtools/R2pkg/files/R2pkg_test.R |  125 +++++++++++++++++++++++++++++++++----
 2 files changed, 112 insertions(+), 17 deletions(-)

diffs (194 lines):

diff -r 047dc37384e3 -r 285864ea932e pkgtools/R2pkg/Makefile
--- a/pkgtools/R2pkg/Makefile   Sun Oct 13 18:13:03 2019 +0000
+++ b/pkgtools/R2pkg/Makefile   Sun Oct 13 19:13:47 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.8 2019/10/13 18:13:03 rillig Exp $
+# $NetBSD: Makefile,v 1.9 2019/10/13 19:13:47 rillig Exp $
 #
 
 VERS=          0.6.2
@@ -29,7 +29,7 @@
        cd ${FILESDIR} && ${CP} R2pkg*.* ${WRKSRC}
 
 do-test:
-       cd ${WRKSRC} && LC_ALL=C ${PREFIX}/bin/Rscript R2pkg_test.R
+       cd ${WRKSRC} && LC_ALL=C PKGSRCDIR=${PKGSRCDIR} ${PREFIX}/bin/Rscript R2pkg_test.R
 
 do-install:
        ${INSTALL_SCRIPT} ${WRKSRC}/R2pkg.sh ${DESTDIR}${PREFIX}/sbin/R2pkg
diff -r 047dc37384e3 -r 285864ea932e pkgtools/R2pkg/files/R2pkg_test.R
--- a/pkgtools/R2pkg/files/R2pkg_test.R Sun Oct 13 18:13:03 2019 +0000
+++ b/pkgtools/R2pkg/files/R2pkg_test.R Sun Oct 13 19:13:47 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: R2pkg_test.R,v 1.1 2019/10/13 18:13:03 rillig Exp $
+# $NetBSD: R2pkg_test.R,v 1.2 2019/10/13 19:13:47 rillig Exp $
 #
 # Copyright (c) 2019
 #      Roland Illig.  All rights reserved.
@@ -32,6 +32,14 @@
 library(testthat)
 library(withr)
 
+package.dir <- file.path(Sys.getenv('PKGSRCDIR'), 'pkgtools', 'R2pkg')
+
+expect_printed <- function(obj, expected) {
+    out <- ''
+    with_output_sink(textConnection('out', 'w', local = TRUE), print(obj))
+    expect_equal(out, expected)
+}
+
 test_that('make.imports', {
     imports <- make.imports('first (>= 1.0)', 'second')
 
@@ -45,23 +53,23 @@
 })
 
 test_that('buildlink3.file with matching version number', {
-    local_dir('..')
-    dependency <- make.dependency('ellipsis(>=0.1)')
+    local_dir(package.dir)
+    dependency <- make.dependency('bitops(>=0.1)')
 
     bl3 <- buildlink3.file(dependency)
 
-    expect_equal(bl3, '../../math/R-ellipsis/buildlink3.mk')
+    expect_equal(bl3, '../../math/R-bitops/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)')
+    local_dir(package.dir)
+    dependency <- make.dependency('bitops(>=1000.0)')
 
     bl3 <- buildlink3.file(dependency)
 
-    expect_equal(bl3, '../../math/R-ellipsis/buildlink3.mk')
+    expect_equal(bl3, '../../math/R-bitops/buildlink3.mk')
 })
 
 test_that('level.warning', {
@@ -76,6 +84,47 @@
     expect_equal(output, '[ 123 ] WARNING: message text\n')
 })
 
+test_that('read.file.as.value, exactly 1 variable assignment, no space', {
+    filename <- ''
+    local_tempfile('filename')
+    writeLines(c('VAR=value'), filename)
+
+    str <- read.file.as.value(filename)
+
+    expect_equal(str, NA_character_)
+})
+
+test_that('read.file.as.value, exactly 1 variable assignment', {
+    filename <- ''
+    local_tempfile('filename')
+    writeLines(c('VAR=\tvalue'), filename)
+
+    str <- read.file.as.value(filename)
+
+    expect_equal(str, 'value')
+})
+
+test_that('read.file.as.value, commented variable assignment', {
+    filename <- ''
+    local_tempfile('filename')
+    writeLines(c('#VAR=\tvalue'), filename)
+
+    str <- read.file.as.value(filename)
+
+    # TODO: Check whether commented variables should really be considered.
+    expect_equal(str, 'value')
+})
+
+test_that('read.file.as.value, multiple variable assignments', {
+    filename <- ''
+    local_tempfile('filename')
+    writeLines(c('VAR=\tvalue', 'VAR=\tvalue2'), filename)
+
+    str <- read.file.as.value(filename)
+
+    expect_equal(str, '')
+})
+
 test_that('read.file.as.dataframe', {
     content <- textConnection('VAR=value\nVAR2=value2\n')
 
@@ -101,12 +150,7 @@
 
     df <- read.Makefile.as.dataframe(textConnection(content))
 
-    out <- ''
-    with_output_sink(textConnection('out', 'w', local = TRUE), {
-        print(df)
-    })
-
-    expected.out <- c(
+    expect_printed(df, 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',
@@ -123,6 +167,57 @@
         '5     <NA>      <NA>      <NA>     <NA>',
         '6     <NA>      <NA>      <NA>     <NA>',
         '7     <NA>      <NA>      <NA>     <NA>'
-    )
-    expect_equal(out, expected.out)
+    ))
 })
+
+test_that('update.Makefile.with.metadata', {
+    df <- read.Makefile.as.dataframe(textConnection(paste0(
+        'CATEGORIES=\n',
+        'MAINTAINER=\n',
+        'COMMENT=\n',
+        'R_PKGVER=\n'
+    )))
+    metadata = list(Title = 'Package comment', Version = '19.3', License = 'license')
+
+    updated <- update.Makefile.with.metadata(df, metadata)
+
+    expect_printed(updated, c(
+        '         line order category key_value        key depends buildlink3.mk',
+        '1 CATEGORIES=     1       NA      TRUE CATEGORIES   FALSE         FALSE',
+        '2 MAINTAINER=     2       NA      TRUE MAINTAINER   FALSE         FALSE',
+        '3    COMMENT=     3       NA      TRUE    COMMENT   FALSE         FALSE',
+        '4   R_PKGVER=     4       NA      TRUE   R_PKGVER   FALSE         FALSE',
+        '5                 5       NA     FALSE       <NA>   FALSE         FALSE',
+        '  operator delimiter old_value old_todo       new_value',
+        '1        =                                        R2pkg',
+        '2        =                                             ',
+        '3        =                              Package comment',
+        '4        =                                         19.3',
+        '5     <NA>      <NA>      <NA>     <NA>            <NA>'
+    ))
+})
+
+# If the variable has been removed from the Makefile, it is not updated.
+test_that('update.Makefile.with.metadata without CATEGORIES', {
+    df <- read.Makefile.as.dataframe(textConnection(paste0(
+        'MAINTAINER=\n',
+        'COMMENT=\n',
+        'R_PKGVER=\n'
+    )))
+    metadata = list(Title = 'Package comment', Version = '19.3', License = 'license')
+
+    updated <- update.Makefile.with.metadata(df, metadata)
+
+    expect_printed(updated, c(
+        '         line order category key_value        key depends buildlink3.mk',
+        '1 MAINTAINER=     1       NA      TRUE MAINTAINER   FALSE         FALSE',
+        '2    COMMENT=     2       NA      TRUE    COMMENT   FALSE         FALSE',
+        '3   R_PKGVER=     3       NA      TRUE   R_PKGVER   FALSE         FALSE',
+        '4                 4       NA     FALSE       <NA>   FALSE         FALSE',
+        '  operator delimiter old_value old_todo       new_value',
+        '1        =                                             ',
+        '2        =                              Package comment',
+        '3        =                                         19.3',
+        '4     <NA>      <NA>      <NA>     <NA>            <NA>'
+    ))
+})



Home | Main Index | Thread Index | Old Index