Skip to content

Commit

Permalink
Add new encoding arg
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Mar 12, 2024
1 parent 6871751 commit 6f45f3f
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 24 deletions.
6 changes: 3 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -----------------------------------------------------------
# CITATION file created with {cffr} R package, v1.0.0.9000
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# -----------------------------------------------------------
# --------------------------------------------

cff-version: 1.2.0
message: 'To cite package "cffr" in publications use:'
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cffr (development version)

- Update `cff_write()` to add a new `encoding` argument to make it work with
different encodings, see `iconv()`.

# cffr 1.0.0

This is a major release with some notable changes. The change mainly **affects
Expand Down
23 changes: 11 additions & 12 deletions R/cff_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#' @param validate validate Logical `TRUE/FALSE`. Should the new file be
#' validated using `cff_validate()`?
#'
#' @param encoding The name of the encoding to be assumed. Default is `"UTF-8"`,
#' but it can be any other value as accepted by [iconv()], such as
#' `"ASCII//TRANSLIT"`.
#'
#' @inheritParams cff_create
#' @inheritParams cff_validate
#'
Expand Down Expand Up @@ -62,7 +66,8 @@
cff_write <- function(x, outfile = "CITATION.cff", keys = list(),
cff_version = "1.2.0", gh_keywords = TRUE,
dependencies = TRUE, validate = TRUE,
verbose = TRUE, authors_roles = c("aut", "cre")) {
verbose = TRUE, authors_roles = c("aut", "cre"),
encoding = "UTF-8") {
# # On missing use NULL
if (missing(x)) x <- getwd()

Expand Down Expand Up @@ -91,21 +96,15 @@ cff_write <- function(x, outfile = "CITATION.cff", keys = list(),

# Write CITATION with comment
com <- c(
"# -----------------------------------------------------------",
paste0(
"# CITATION file created with {cffr} R package, v",
packageDescription("cffr")$Version
),
paste(
"# See also:",
strsplit(packageDescription("cffr")$URL, ",")[[1]][1]
),
"# -----------------------------------------------------------",
"# --------------------------------------------",
"# CITATION file created with {cffr} R package",
"# See also: https://docs.ropensci.org/cffr/",
"# --------------------------------------------",
" "
)

full_text <- c(com, yaml::as.yaml(citat))
fh <- file(outfile, encoding = "UTF-8")
fh <- file(outfile, encoding = encoding)
on.exit(if (isOpen(fh)) close(fh))
writeLines(full_text, fh)

Expand Down
7 changes: 6 additions & 1 deletion man/cff_write.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/_snaps/cff_write/appendkeys.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: 'To cite package "basicdesc" in publications use:'
type: software
license: GPL-3.0-only
title: 'basicdesc: A Basic Description'
version: 0.1.6
abstract: A very basic description. Should parse without problems.
authors:
- family-names: Basic
given-names: Marc
email: [email protected]
- family-names: author
given-names: New
website: https://stackoverflow.com/
country: IT
repository-code: https://github.com/basic/package
url: https://basic.github.io/package
contact:
- family-names: Basic
given-names: Marc
email: [email protected]

12 changes: 12 additions & 0 deletions tests/testthat/_snaps/cff_write/asci_trans.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: If you use this software, please cite it using these metadata.
title: My Research Software
authors:
- family-names: Perez
given-names: Diego

23 changes: 23 additions & 0 deletions tests/testthat/_snaps/cff_write/basic.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: 'To cite package "basicdesc" in publications use:'
type: software
license: GPL-3.0-only
title: 'basicdesc: A Basic Description'
version: 0.1.6
abstract: A very basic description. Should parse without problems.
authors:
- family-names: Basic
given-names: Marc
email: [email protected]
repository-code: https://github.com/basic/package
url: https://basic.github.io/package
contact:
- family-names: Basic
given-names: Marc
email: [email protected]

12 changes: 12 additions & 0 deletions tests/testthat/_snaps/cff_write/fix_extension.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: If you use this software, please cite it using these metadata.
title: My Research Software
authors:
- family-names: Pérez
given-names: Diego

23 changes: 23 additions & 0 deletions tests/testthat/_snaps/cff_write/new_folder.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: 'To cite package "basicdesc" in publications use:'
type: software
license: GPL-3.0-only
title: 'basicdesc: A Basic Description'
version: 0.1.6
abstract: A very basic description. Should parse without problems.
authors:
- family-names: Basic
given-names: Marc
email: [email protected]
repository-code: https://github.com/basic/package
url: https://basic.github.io/package
contact:
- family-names: Basic
given-names: Marc
email: [email protected]

29 changes: 29 additions & 0 deletions tests/testthat/_snaps/cff_write/newkeys.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: This overwrites fields
type: software
license: GPL-3.0-only
title: 'basicdesc: A Basic Description'
version: 0.1.6
abstract: New abstract
authors:
- family-names: Nadie
given-names: Don
repository-code: https://github.com/basic/package
url: https://basic.github.io/package
date-released: '1900-01-01'
contact:
- family-names: Basic
given-names: Marc
email: [email protected]
keywords:
- A
- new
- list
- of
- keywords

26 changes: 26 additions & 0 deletions tests/testthat/_snaps/cff_write/noencoding.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: 'To cite package "idonthavencoding" in publications use:'
type: software
license: GPL-3.0-only
title: 'idonthavencoding: No Encoding'
version: 0.1.6
abstract: I don't have Encoding field. Should parse without problems. Maybe some warning,
but because the missing Encoding field.
authors:
- family-names: Basic
given-names: Marc
email: [email protected]
orcid: https://orcid.org/0000-0000-0000-001X
repository-code: https://github.com/idonthavencoding/package
url: https://idonthavencoding.github.io/package
contact:
- family-names: Basic
given-names: Marc
email: [email protected]
orcid: https://orcid.org/0000-0000-0000-001X

12 changes: 12 additions & 0 deletions tests/testthat/_snaps/cff_write/utf8.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# --------------------------------------------
# CITATION file created with {cffr} R package
# See also: https://docs.ropensci.org/cffr/
# --------------------------------------------

cff-version: 1.2.0
message: If you use this software, please cite it using these metadata.
title: My Research Software
authors:
- family-names: Pérez
given-names: Diego

51 changes: 44 additions & 7 deletions tests/testthat/test-cff_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ test_that("Write basic", {
system.file("examples/DESCRIPTION_basic", package = "cffr")
desc_file <- cff_create(desc_file)
expect_s3_class(desc_file, "cff")
tmp <- tempfile(fileext = ".cff")
tmp <- file.path(tempdir(), "basic.cff")
expect_message(cff_write(desc_file, outfile = tmp, validate = FALSE))
expect_silent(cff_write(desc_file, outfile = tmp, verbose = FALSE))
expect_true(file_exist_abort(tmp))

# Validate from file
expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
Expand All @@ -20,7 +21,7 @@ test_that("Write to a non-existing folder", {
system.file("examples/DESCRIPTION_basic", package = "cffr")
desc_file <- cff_create(desc_file)
expect_s3_class(desc_file, "cff")
tmp <- tempfile("/test_new_folder/recursive/", fileext = ".cff")
tmp <- file.path(tempdir(), "/test_new_folder/recursive/new_folder.cff")
cff_write(
desc_file,
outfile = tmp,
Expand All @@ -35,6 +36,7 @@ test_that("Write to a non-existing folder", {

# Validate from file
expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
Expand All @@ -45,7 +47,7 @@ test_that("Write no encoding", {
system.file("examples/DESCRIPTION_no_encoding", package = "cffr")
desc_file <- cff_create(desc_file)
expect_s3_class(desc_file, "cff")
tmp <- tempfile(fileext = ".cff")
tmp <- file.path(tempdir(), "noencoding.cff")
cff_write(
desc_file,
outfile = tmp,
Expand All @@ -57,6 +59,7 @@ test_that("Write no encoding", {

# Validate from file
expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
Expand All @@ -67,7 +70,7 @@ test_that("Add new keys", {
system.file("examples/DESCRIPTION_basic", package = "cffr")
desc_file <- cff_create(desc_file)
expect_s3_class(desc_file, "cff")
tmp <- tempfile(fileext = ".cff")
tmp <- file.path(tempdir(), "newkeys.cff")


# Add additional keys
Expand Down Expand Up @@ -96,6 +99,7 @@ test_that("Add new keys", {

# Validate from file
expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
Expand Down Expand Up @@ -126,7 +130,7 @@ test_that("Append keys", {
)
)

tmp <- tempfile(fileext = ".cff")
tmp <- file.path(tempdir(), "appendkeys.cff")
s <- cff_write(
desc_file,
outfile = tmp,
Expand All @@ -137,21 +141,54 @@ test_that("Append keys", {
expect_snapshot(s)

expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
})

test_that("Fix extension of the file", {
cffobj <- cff()

tmp <- tempfile()
cffobj <- cff_modify(cffobj, authors = as_cff_person("Diego Pérez"))
tmp <- file.path(tempdir(), "fix_extension")
expect_silent(cff_write(cffobj, tmp, verbose = FALSE))

expect_false(file_exist_abort(tmp))
expect_true(file_exist_abort(paste0(tmp, ".cff")))
expect_true(cff_validate(paste0(tmp, ".cff"), verbose = FALSE))
expect_snapshot_file(paste0(tmp, ".cff"))

file.remove(paste0(tmp, ".cff"))
expect_false(file_exist_abort(paste0(tmp, ".cff")))
})

test_that("test encoding utf8", {
cffobj <- cff()
cffobj <- cff_modify(cffobj, authors = as_cff_person("Diego Pérez"))
tmp <- file.path(tempdir(), "utf8.cff")
expect_silent(cff_write(cffobj, tmp, verbose = FALSE))

expect_true(file_exist_abort(tmp))
expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
})

test_that("test encoding others", {
cffobj <- cff()
cffobj <- cff_modify(cffobj, authors = as_cff_person("Diego Pérez"))
tmp <- file.path(tempdir(), "asci_trans.cff")
expect_silent(cff_write(cffobj, tmp,
verbose = FALSE,
encoding = "ASCII//TRANSLIT"
))

expect_true(file_exist_abort(tmp))
expect_true(cff_validate(tmp, verbose = FALSE))
expect_snapshot_file(tmp)

file.remove(tmp)
expect_false(file_exist_abort(tmp))
})
Loading

0 comments on commit 6f45f3f

Please sign in to comment.