pkgsrc-Changes-HG archive

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

[pkgsrc/trunk]: pkgsrc/pkgtools/R2pkg/files pkgtools/R2pkg: refactorings, tests



details:   https://anonhg.NetBSD.org/pkgsrc/rev/a40d036ef236
branches:  trunk
changeset: 416050:a40d036ef236
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Fri Oct 18 21:42:20 2019 +0000

description:
pkgtools/R2pkg: refactorings, tests

diffstat:

 pkgtools/R2pkg/files/R2pkg.R      |   74 +++++++++-------------
 pkgtools/R2pkg/files/R2pkg_test.R |  124 ++++++++++++++++++++++++++-----------
 2 files changed, 116 insertions(+), 82 deletions(-)

diffs (truncated from 337 to 300 lines):

diff -r 6533b2155c6d -r a40d036ef236 pkgtools/R2pkg/files/R2pkg.R
--- a/pkgtools/R2pkg/files/R2pkg.R      Fri Oct 18 18:33:31 2019 +0000
+++ b/pkgtools/R2pkg/files/R2pkg.R      Fri Oct 18 21:42:20 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: R2pkg.R,v 1.12 2019/10/18 17:18:03 rillig Exp $
+# $NetBSD: R2pkg.R,v 1.13 2019/10/18 21:42:20 rillig Exp $
 #
 # Copyright (c) 2014,2015,2016,2017,2018,2019
 #      Brook Milligan.  All rights reserved.
@@ -693,55 +693,50 @@
   df
 }
 
-element <- function(df,key,value,quiet=FALSE)
+element <- function(mklines, varname, field, quiet=FALSE)
 {
-  key.index <- match(key,df$key,0)
-  if (key.index != 0 && df$key_value[key.index])
-    result <- df[key.index,value]
-  else
+  i <- match(varname, mklines$key, 0)
+  if (i != 0 && mklines$key_value[i])
+    return(mklines[i, field])
+
+  if (!quiet)
     {
-      result <- '???'
-      if (!quiet)
-        {
-          if (key.index == 0)
-            level.warning(key,' not found')
-          else
-            level.warning(key,' is not a key-value field')
-        }
+      if (i == 0)
+        level.warning(varname, ' not found')
+      else
+        level.warning(varname, ' is not a key-value field')
     }
-  result
+  '???'
 }
 
-make.categories <- function(df)
+make.categories <- function(mklines)
 {
-  # message('===> make.categories():')
   directory <- basename(dirname(getwd()))
-  categories <- unlist(element(df,'CATEGORIES','old_value'))
-  categories <- unlist(strsplit(categories,'[[:blank:]]+'))
-  categories <- c(directory,categories)
-  categories <- categories[ categories != 'R' ]
+  categories <- unlist(element(mklines, 'CATEGORIES', 'old_value'))
+  categories <- unlist(strsplit(categories, '[[:blank:]]+'))
+  categories <- c(directory, categories)
+  categories <- categories[categories != 'R']
   if (directory != 'wip')
-    categories <- categories[ categories != 'wip' ]
+    categories <- categories[categories != 'wip']
   categories <- categories[!duplicated(categories)]
-  categories <- paste(categories,collapse=' ')
-  categories
+  paste(categories, collapse = ' ')
 }
 
-make.maintainer <- function(df)
+make.maintainer <- function(mklines)
 {
-  old.maintainer <- element(df,'MAINTAINER','old_value')
-  new.maintainer <- element(df,'MAINTAINER','new_value')
-  ifelse(old.maintainer == '',new.maintainer,old.maintainer)
+  old.maintainer <- element(mklines, 'MAINTAINER', 'old_value')
+  new.maintainer <- element(mklines, 'MAINTAINER', 'new_value')
+  if (old.maintainer == '') new.maintainer else old.maintainer
 }
 
-make.comment <- function(df)
+make.comment <- function(mklines)
 {
-  old.comment <- element(df,'COMMENT','old_value')
-  new.comment <- element(df,'COMMENT','new_value')
-  comment <- old.comment
-  if (!weakly.equals(old.comment,new.comment))
-    comment <- paste0(comment,'\t# [R2pkg] updated to: ',new.comment)
-  comment
+  old.comment <- element(mklines, 'COMMENT', 'old_value')
+  new.comment <- element(mklines, 'COMMENT', 'new_value')
+  if (weakly.equals(old.comment, new.comment))
+    old.comment
+  else
+    paste0(old.comment, '\t# [R2pkg] updated to: ', new.comment)
 }
 
 make.new_license <- function(df,license)
@@ -809,12 +804,8 @@
   value
 }
 
-write.makefile <- function(lines) write(lines,'Makefile')
-
 update.Makefile.with.metadata <- function(df,metadata)
 {
-  # message('===> update.Makefile.with.metadata():')
-
   df$new_value <- NA
 
   df <- make.new_license(df,metadata$License)
@@ -823,9 +814,6 @@
   df$new_value[df$key == 'MAINTAINER'] <- arg.maintainer_email
   df$new_value[df$key == 'COMMENT'] <- one.line(metadata$Title)
   df$new_value[df$key == 'R_PKGVER'] <- one.line(metadata$Version)
-
-  # str(df)
-  # print(df)
   df
 }
 
@@ -1072,7 +1060,7 @@
   df.buildlink3 <- make.df.buildlink3(df,BUILDLINK3.MK)
   df.makefile <- make.df.makefile(df,df.conflicts,df.depends,df.buildlink3)
 
-  write.makefile(df.makefile[,'new_line'])
+  write(df.makefile[, 'new_line'], 'Makefile')
 }
 
 create.Makefile <- function(metadata)
diff -r 6533b2155c6d -r a40d036ef236 pkgtools/R2pkg/files/R2pkg_test.R
--- a/pkgtools/R2pkg/files/R2pkg_test.R Fri Oct 18 18:33:31 2019 +0000
+++ b/pkgtools/R2pkg/files/R2pkg_test.R Fri Oct 18 21:42:20 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: R2pkg_test.R,v 1.7 2019/10/18 17:18:03 rillig Exp $
+# $NetBSD: R2pkg_test.R,v 1.8 2019/10/18 21:42:20 rillig Exp $
 #
 # Copyright (c) 2019
 #      Roland Illig.  All rights reserved.
@@ -38,7 +38,7 @@
 arg.recursive <- FALSE
 arg.update <- FALSE
 
-package.dir <- file.path(Sys.getenv('PKGSRCDIR'), 'pkgtools', 'R2pkg')
+package_dir <- file.path(Sys.getenv('PKGSRCDIR'), 'pkgtools', 'R2pkg')
 
 expect_printed <- function(obj, ...) {
     out <- ''
@@ -369,13 +369,13 @@
 })
 
 test_that('depends.pkg', {
-    local_dir(package.dir)
+    local_dir(package_dir)
 
     expect_equal(depends.pkg('ellipsis'), '../../math/R-ellipsis')
 })
 
 test_that('new.depends.pkg', {
-    local_dir(package.dir)
+    local_dir(package_dir)
 
     if (dir.exists('../../wip'))
         expect_equal(new.depends.pkg('C50'), '../../wip/R-C50')
@@ -406,7 +406,7 @@
 # })
 
 test_that('buildlink3.file with matching version number', {
-    local_dir(package.dir)
+    local_dir(package_dir)
     dependency <- make.dependency('bitops(>=0.1)')
 
     bl3 <- buildlink3.file(dependency)
@@ -417,7 +417,7 @@
 # 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(package.dir)
+    local_dir(package_dir)
     dependency <- make.dependency('bitops(>=1000.0)')
 
     bl3 <- buildlink3.file(dependency)
@@ -426,7 +426,7 @@
 })
 
 test_that('buildlink3.line', {
-    local_dir(package.dir)
+    local_dir(package_dir)
 
     expect_equal(
         buildlink3.line(make.dependency('ellipsis')),
@@ -469,14 +469,48 @@
 # test_that('copy.description', {
 # })
 
-# test_that('write.Makefile', {
-# })
+test_that('write.Makefile', {
+    tmpdir <- paste(tempdir(), 'category', 'pkgdir', sep = '/')
+    dir.create(tmpdir, recursive = TRUE)
+    local_dir(tmpdir)
+    metadata <- make.metadata(linesConnection(
+        'Package: pkgname',
+        'Version: 1.3',
+        'Depends: ellipsis'))
+
+    write.Makefile(metadata)
+
+    expect_equal(readLines('Makefile'),c(
+        mkcvsid,
+        '',
+        'R_PKGNAME=\tpkgname',
+        'R_PKGVER=\t1.3',
+        'CATEGORIES=\tcategory',
+        '',
+        'MAINTAINER=\t',  # FIXME
+        'COMMENT=\tNA',  # FIXME
+        'LICENSE=\tNA',  # FIXME
+        '',
+        'USE_LANGUAGES=\t# none',
+        '',
+        '.include "../../math/R/Makefile.extension"',
+        '.include "../../mk/bsd.pkg.mk"'
+    ))
+})
 
 # test_that('construct.line', {
 # })
 
-# test_that('element', {
-# })
+test_that('element', {
+    mklines <- read.Makefile.as.dataframe(linesConnection(
+        'COMMENT=\tThe comment',
+        'EMPTY='))
+
+    expect_equal(element(mklines, 'COMMENT', 'order'), 1)
+    expect_equal(element(mklines, 'COMMENT', 'old_value'), 'The comment')
+    expect_equal(element(mklines, 'UNKNOWN', 'order'), '???')  # FIXME: should be a number
+    expect_equal(element(mklines, 'EMPTY', 'old_value'), '')
+})
 
 # test_that('make.categories', {
 # })
@@ -484,8 +518,16 @@
 # test_that('make.maintainer', {
 # })
 
-# test_that('make.comment', {
-# })
+test_that('make.comment', {
+    mklines <- read.Makefile.as.dataframe(linesConnection(
+        'COMMENT=\tOld comment'))
+
+    mklines$new_value[[1]] <- 'New comment'
+    expect_equal(make.comment(mklines), 'Old comment\t# [R2pkg] updated to: New comment')
+
+    mklines$new_value[[1]] <- 'old Comment'
+    expect_equal(make.comment(mklines), 'Old comment')
+})
 
 # test_that('make.new_license', {
 # })
@@ -502,33 +544,40 @@
 # test_that('make.r_pkgver', {
 # })
 
-# test_that('find.order', {
-# })
+test_that('find.order', {
+    mklines <- read.Makefile.as.dataframe(linesConnection(
+        'CATEGORIES=',
+        'HOMEPAGE=',
+        'USE_TOOLS+=',
+        '.include "other.mk"',
+        '# comment'))
 
-# test_that('write.makefile', {
-# })
+    vars_order <- find.order(mklines, 'key_value', 'order')
+    include_order <- find.order(mklines, 'buildlink3.mk', 'order')
+
+    expect_equal(mklines[, 'key_value'], c(TRUE, TRUE, TRUE, FALSE, FALSE))
+    expect_equal(mklines[, 'buildlink3.mk'], c(FALSE, FALSE, FALSE, FALSE, FALSE))
+    expect_equal(vars_order, c(1))
+    expect_equal(include_order, NA_integer_)
+})
 
 test_that('update.Makefile.with.metadata', {
+    local_dir(package_dir)  # to get a realistic category
     df <- read.Makefile.as.dataframe(linesConnection(
-        'CATEGORIES=',
-        'MAINTAINER=',
-        'COMMENT=',
-        'R_PKGVER='))
+        'CATEGORIES=\told categories',
+        'MAINTAINER=\told_maintainer%example.org@localhost',
+        'COMMENT=\told comment',
+        'R_PKGVER=\t1.0'))
     metadata = list(Title = 'Package comment', Version = '19.3', License = 'license')
 
     updated <- update.Makefile.with.metadata(df, metadata)
 
-    expect_printed(updated,



Home | Main Index | Thread Index | Old Index