diff --git a/R/custom-translators.R b/R/custom-translators.R index c6dd65c..5f1125b 100644 --- a/R/custom-translators.R +++ b/R/custom-translators.R @@ -1,18 +1,22 @@ +catf = function(fmt, ..., sep=" ", domain="R-potools") { + cat(gettextf(fmt, ..., domain=domain), sep=sep) +} + # tends to make some really repetitive messages with call.=TRUE, so always turn it off -stopf = function(fmt, ..., domain = NULL) { - stop(gettextf(fmt, ..., domain = NULL), domain = NA, call. = FALSE) +stopf = function(fmt, ..., domain="R-potools") { + stop(gettextf(fmt, ..., domain=domain), domain = NA, call. = FALSE) } # uncovered for now, used in msgmerge.R -warningf = function(fmt, ..., immediate. = FALSE, noBreaks. = FALSE) { - warning(gettextf(fmt, ..., domain = NULL), domain = NA, call. = FALSE, immediate. = immediate., noBreaks. = noBreaks.) +warningf = function(fmt, ..., immediate.=FALSE, noBreaks.=FALSE, domain="R-potools") { + warning(gettextf(fmt, ..., domain=domain), domain = NA, call. = FALSE, immediate. = immediate., noBreaks. = noBreaks.) } -messagef = function(fmt, ..., appendLF = TRUE) { - message(gettextf(fmt, ..., domain = NULL), domain = NA, appendLF = appendLF) +messagef = function(fmt, ..., appendLF=TRUE, domain="R-potools") { + message(gettextf(fmt, ..., domain=domain), domain = NA, appendLF = appendLF) } # not actually used yet in package src, so commenting out for test coverage, but leaving in for illustration # packageStartupMessagef = function(fmt, ..., appendLF = TRUE) { -# packageStartupMessage(gettextf(fmt, ..., domain = NULL), domain = NA, appendLF = appendLF) +# packageStartupMessage(gettextf(fmt, ..., domain = domain), domain = NA, appendLF = appendLF) # } diff --git a/R/explain_plurals.R b/R/explain_plurals.R index 92572ba..b4ac252 100644 --- a/R/explain_plurals.R +++ b/R/explain_plurals.R @@ -24,10 +24,11 @@ po_explain_plurals <- function(language, index) { plural_metadata = .potools$PLURAL_RANGE_STRINGS[language_metadata, .SD, on = "plural"] if (missing(index)) { - language_metadata[, message(domain = NA, sprintf( + language_metadata[, messagef( ngettext(nplurals, "%s (%s) has %d plural form.", "%s (%s) has %d plural forms."), - full_name_eng, full_name_native, nplurals - ))] + full_name_eng, full_name_native, nplurals, + domain = NA + )] plural_metadata[, message(domain = NA, paste( gettextf(" - plural_index = %d applies %s", plural_index, range_translation = gettext(range)), collapse = "\n" diff --git a/R/find_fuzzy_messages.R b/R/find_fuzzy_messages.R index b8feb16..f6b5a75 100644 --- a/R/find_fuzzy_messages.R +++ b/R/find_fuzzy_messages.R @@ -2,20 +2,26 @@ find_fuzzy_messages <- function(message_data, lang_file) { old_message_data = get_po_messages(lang_file) if (any(idx <- old_message_data$fuzzy == 2L)) { - messagef('Found %d translations marked as deprecated in %s.', sum(idx), lang_file) - message('Typically, this means the corresponding error messages have been refactored.') - message('Reproducing these messages here for your reference since they might still provide some utility.') + messagef( + paste( + 'Found %d translations marked as deprecated in %s.', + 'Typically, this means the corresponding error messages have been refactored.', + 'Reproducing these messages here for your reference since they might still provide some utility.', + sep = '\n' + ), + sum(idx), lang_file + ) dashes = strrep('-', 0.9*getOption('width')) old_message_data[idx & type == 'singular', { if (.N > 0L) { - message(' ** SINGULAR MESSAGES **') + cat(' ** SINGULAR MESSAGES **\n') cat(rbind(dashes, msgid, msgstr), sep='\n') } }] old_message_data[idx & type == 'plural', { if (.N > 0L) { - message(' ** PLURAL MESSAGES **') + cat(' ** PLURAL MESSAGES **\n') cat(do.call(rbind, c(list(dashes), msgid_plural, msgstr_plural)), sep='\n') } }] diff --git a/R/get_message_data.R b/R/get_message_data.R index 41a49af..f5e2104 100644 --- a/R/get_message_data.R +++ b/R/get_message_data.R @@ -92,7 +92,7 @@ get_message_data = function( } } - if (verbose && dir.exists(file.path(dir, "R"))) message('Getting R-level messages...') + if (verbose && dir.exists(file.path(dir, "R"))) cat('Getting R-level messages...\n') r_message_data = get_r_messages( dir, # nolint next: backport_linter. False positive on 'R'. @@ -101,7 +101,7 @@ get_message_data = function( is_base = is_base ) - if (verbose && dir.exists(file.path(dir, "src"))) message('Getting src-level messages...') + if (verbose && dir.exists(file.path(dir, "src"))) cat('Getting src-level messages...\n') src_message_data = get_src_messages( dir, custom_translation_functions = custom_translation_functions$src, diff --git a/R/msgmerge.R b/R/msgmerge.R index 2fe9d02..f68bace 100644 --- a/R/msgmerge.R +++ b/R/msgmerge.R @@ -12,14 +12,14 @@ run_msgmerge <- function(po_file, pot_file, previous = FALSE, verbose = TRUE) { ) if (verbose) { - message("Running system command msgmerge ", paste(msgmerge_args, collapse = " "), "...") + catf("Running system command msgmerge %s ...\n", paste(msgmerge_args, collapse = " ")) } val <- system2("msgmerge", msgmerge_args, stdout = TRUE, stderr = TRUE) if (!identical(attr(val, "status", exact = TRUE), NULL)) { # nocov these warnings? i don't know how to trigger them as of this writing. warningf("Running msgmerge on './po/%s' failed:\n %s", basename(po_file), paste(val, collapse = "\n")) } else if (verbose) { - messagef(paste(val, collapse = "\n")) + writeLines(val) } res <- tools::checkPoFile(po_file, strictPlural = TRUE) @@ -45,7 +45,7 @@ run_msgfmt = function(po_file, mo_file, verbose) { cmd = glue("msgfmt -o {shQuote(mo_file)} {shQuote(po_file)}") # nocov } if (verbose) { - message("Running system command ", cmd, "...") + catf("Running system command %s ...\n", cmd) } if (system(cmd) != 0L) { warningf( @@ -88,13 +88,13 @@ run_msginit <- function(po_path, pot_path, locale, width = 80L, verbose = TRUE) "--no-translator" # don't consult user-email etc ) if (verbose) { - message("Running system command msginit ", paste(msginit_args, collapse = " "), "...") + catf("Running system command msginit %s...\n", paste(msginit_args, collapse = " ")) } val <- system2("msginit", msginit_args, stdout = TRUE, stderr = TRUE) if (!identical(attr(val, "status", exact = TRUE), NULL)) { stopf("Running msginit on '%s' failed", pot_path) } else if (verbose) { - messagef(paste(val, collapse = "\n")) + writeLines(val) } invisible() } diff --git a/R/po_compile.R b/R/po_compile.R index 630481d..804594d 100644 --- a/R/po_compile.R +++ b/R/po_compile.R @@ -24,8 +24,8 @@ po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) { to_delete <- mo_dirs[!basename(mo_dirs) %in% c(po_metadata$language, "en@quot")] for (dir in to_delete) { - if (verbose) messagef( - "Found a compiled translation for %s at %s, but no corresponding .po; deleting", + if (verbose) catf( + "Found a compiled translation for %s at %s, but no corresponding .po; deleting\n", basename(dir), dirname(dir) ) unlink(dir, recursive = TRUE) @@ -34,7 +34,7 @@ po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) { for (ii in seq_len(nrow(po_metadata))) { row_ii <- po_metadata[ii] - if (verbose) messagef("Recompiling '%s' %s translation", row_ii$language, row_ii$type) + if (verbose) catf("Recompiling '%s' %s translation\n", row_ii$language, row_ii$type) run_msgfmt(row_ii$po, row_ii$mo, verbose = verbose) } diff --git a/R/po_create.R b/R/po_create.R index 14d25de..eee7e83 100644 --- a/R/po_create.R +++ b/R/po_create.R @@ -22,10 +22,10 @@ po_create <- function(languages, dir = ".", verbose = !is_testing()) { for (ii in seq_len(nrow(po_files))) { row_ii <- po_files[ii] if (file.exists(row_ii$po_path)) { - if (verbose) messagef("Updating '%s' %s translation", row_ii$language, row_ii$type) + if (verbose) catf("Updating '%s' %s translation\n", row_ii$language, row_ii$type) run_msgmerge(row_ii$po_path, row_ii$pot_path, previous = TRUE, verbose = verbose) } else { - if (verbose) messagef("Creating '%s' %s translation", row_ii$language, row_ii$type) + if (verbose) catf("Creating '%s' %s translation\n", row_ii$language, row_ii$type) run_msginit(row_ii$po_path, row_ii$pot_path, locale = row_ii$language, verbose = verbose) } } diff --git a/R/po_extract.R b/R/po_extract.R index e6c603b..73c4d99 100644 --- a/R/po_extract.R +++ b/R/po_extract.R @@ -27,12 +27,10 @@ po_extract <- function( n <- nrow(message_data) if (!n) { - if (verbose) message('No messages to translate') + if (verbose) catf('No messages to translate\n') return(invisible()) } - # TODO: messagef() is double-translating the ngettext() result... - # it needs an escape valve - if (verbose) messagef(ngettext(n, "Found %i message", "Found %i messages"), n) + if (verbose) catf(ngettext(n, "Found %i message", "Found %i messages"), n, domain=NA) po_dir <- file.path(dir, 'po') dir.create(po_dir, showWarnings = FALSE) diff --git a/R/po_update.R b/R/po_update.R index 1304fed..c9a0c9b 100644 --- a/R/po_update.R +++ b/R/po_update.R @@ -37,7 +37,7 @@ po_update <- function(dir = ".", lazy = TRUE, verbose = !is_testing()) { for (ii in seq_len(nrow(meta))) { row_ii <- meta[ii] - if (verbose) messagef("Updating '%s' %s translation", row_ii$language, row_ii$type) + if (verbose) catf("Updating '%s' %s translation\n", row_ii$language, row_ii$type) run_msgmerge(row_ii$po, row_ii$pot, previous = TRUE, verbose = verbose) } diff --git a/R/translate_package.R b/R/translate_package.R index f411f51..5cd9fdc 100644 --- a/R/translate_package.R +++ b/R/translate_package.R @@ -344,13 +344,13 @@ translate_package = function( if (tr_update) { # is it worthwhile to try and distinguish the creation time of the # R pot file and the src pot file? probably not... - messagef( - "Updating translation template for package '%s' (last updated %s)", + catf( + "Updating translation template for package '%s' (last updated %s)\n", package, format(file.info(r_potfile)$atime) ) } else { - messagef("Starting translations for package '%s'", package) + catf("Starting translations for package '%s'\n", package) } } if (!tr_update) dir.create(po_dir, showWarnings = FALSE) @@ -358,7 +358,7 @@ translate_package = function( message_data = get_message_data(dir, custom_translation_functions, verbose=verbose) if (!nrow(message_data)) { - if (verbose) message('No messages to translate; finishing') + if (verbose) cat('No messages to translate; finishing\n') return(invisible()) } @@ -369,7 +369,7 @@ translate_package = function( set_prompt_conn() on.exit(unset_prompt_conn()) - if (verbose) message('Running message diagnostics...') + if (verbose) cat('Running message diagnostics...\n') for (diagnostic in diagnostics) { diagnostic <- match.fun(diagnostic) @@ -387,13 +387,13 @@ translate_package = function( if (l10n_info()[["UTF-8"]]) { # on UTF-8 machines we install the en@quot messages too # TODO: streamline this -- en_quote is definitely doing some redundant stuff - message('Generating en@quot translations') + cat('Generating en@quot translations\n') update_en_quot_mo_files(dir, verbose) } if (is.null(languages)) { - if (verbose) message('No languages provided; finishing') + if (verbose) cat('No languages provided; finishing\n') return(invisible()) } @@ -411,8 +411,8 @@ translate_package = function( lang_file <- file.path(po_dir, glue("R-{language}.po")) if (tr_update && file.exists(lang_file)) { if (verbose) { - messagef( - 'Found existing R translations for %s (%s/%s) in %s. Running msgmerge...', + catf( + 'Found existing R translations for %s (%s/%s) in %s. Running msgmerge...\n', language, metadata$full_name_eng, metadata$full_name_native, lang_file ) } @@ -426,8 +426,8 @@ translate_package = function( lang_file <- file.path(po_dir, glue("{language}.po")) if (tr_update && file.exists(lang_file)) { if (verbose) { - messagef( - 'Found existing src translations for %s (%s/%s) in %s. Running msgmerge...', + catf( + 'Found existing src translations for %s (%s/%s) in %s. Running msgmerge...\n', language, metadata$full_name_eng, metadata$full_name_native, lang_file ) } @@ -448,15 +448,15 @@ translate_package = function( ] if (!length(new_idx)) { - if (verbose) messagef('Translations for %s are up to date! Skipping.', language) + if (verbose) catf('Translations for %s are up to date! Skipping.\n', language) next } if (verbose) { - messagef( - 'Beginning new translations for %s (%s/%s); found %d untranslated messages', + catf( + 'Beginning new translations for %s (%s/%s); found %d untranslated messages\n', language, metadata$full_name_eng, metadata$full_name_native, length(new_idx) ) - message("(To quit translating, press 'Esc'; progress will be saved)") + cat("(To quit translating, press 'Esc'; progress will be saved)\n") } po_params$author = prompt('Thanks! Who should be credited with these translations?') diff --git a/R/update_metadata.R b/R/update_metadata.R index ea9c086..7bf69f4 100644 --- a/R/update_metadata.R +++ b/R/update_metadata.R @@ -3,9 +3,11 @@ update_metadata = function(language) { metadata = data.table(code = language) - messagef("'%s' is not a known language. ", language, appendLF=FALSE) # perhaps refer to http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html instead - message("Please help supply some metadata about it. You can check https://l10n.gnome.org/teams/") + catf( + "'%s' is not a known language. Please help supply some metadata about it. You can check %s.\n", + language, "https://l10n.gnome.org/teams/" + ) metadata[ , "full_name_eng" := prompt("How would you refer to this language in English?")] metadata[ , "full_name_native" := prompt("How would you refer to this language in the language itself?")] metadata[ , "nplurals" := prompt( @@ -38,7 +40,7 @@ update_metadata = function(language) { } .potools$KNOWN_LANGUAGES = rbind(.potools$KNOWN_LANGUAGES, metadata) setkeyv(.potools$KNOWN_LANGUAGES, "code") - message("Thanks! Please file an issue on GitHub to get this language recognized permanently") + cat("Thanks! Please file an issue on GitHub to get this language recognized permanently\n") metadata } diff --git a/R/write_po_file.R b/R/write_po_file.R index 5b45db6..66ff54f 100644 --- a/R/write_po_file.R +++ b/R/write_po_file.R @@ -36,7 +36,7 @@ write_po_files <- function(message_data, po_dir, params, template = FALSE, use_b )) } - if (verbose) messagef('Writing %s', r_file) + if (verbose) catf('Writing %s\n', r_file) write_po_file( message_data[message_source == "R"], diff --git a/tests/testthat/_snaps/unix/po_create.md b/tests/testthat/_snaps/unix/po_create.md index 509d0d2..f6f1a0b 100644 --- a/tests/testthat/_snaps/unix/po_create.md +++ b/tests/testthat/_snaps/unix/po_create.md @@ -2,7 +2,7 @@ Code po_create("jp", verbose = TRUE) - Message + Output Creating 'jp' R translation Running system command msginit -i './po/R-test.pot' -o './po/R-jp.po' -l 'jp' -w 80 --no-translator... Created ./po/R-jp.po. @@ -11,8 +11,8 @@ Code po_create("jp", verbose = TRUE) - Message + Output Updating 'jp' R translation - Running system command msgmerge --update './po/R-jp.po' --backup=off --previous './po/R-test.pot'... + Running system command msgmerge --update './po/R-jp.po' --backup=off --previous './po/R-test.pot' ... . done. diff --git a/tests/testthat/_snaps/unix/po_update.md b/tests/testthat/_snaps/unix/po_update.md index e291d25..578fccb 100644 --- a/tests/testthat/_snaps/unix/po_update.md +++ b/tests/testthat/_snaps/unix/po_update.md @@ -2,11 +2,11 @@ Code po_update(verbose = TRUE, lazy = FALSE) - Message + Output Updating 'fr' R translation - Running system command msgmerge --update './po/R-fr.po' --backup=off --previous './po/R-test.pot'. + Running system command msgmerge --update './po/R-fr.po' --backup=off --previous './po/R-test.pot' . . done. Updating 'ja' R translation - Running system command msgmerge --update './po/R-ja.po' --backup=off --previous './po/R-test.pot'. + Running system command msgmerge --update './po/R-ja.po' --backup=off --previous './po/R-test.pot' . . done. diff --git a/tests/testthat/_snaps/windows/po_create.md b/tests/testthat/_snaps/windows/po_create.md index 8ca8bd6..a81170e 100644 --- a/tests/testthat/_snaps/windows/po_create.md +++ b/tests/testthat/_snaps/windows/po_create.md @@ -2,7 +2,7 @@ Code po_create("jp", verbose = TRUE) - Message + Output Creating 'jp' R translation Running system command msginit -i "./po/R-test.pot" -o "./po/R-jp.po" -l "jp" -w 80 --no-translator... Created ./po/R-jp.po. @@ -11,8 +11,8 @@ Code po_create("jp", verbose = TRUE) - Message + Output Updating 'jp' R translation - Running system command msgmerge --update "./po/R-jp.po" --backup=off --previous "./po/R-test.pot"... + Running system command msgmerge --update "./po/R-jp.po" --backup=off --previous "./po/R-test.pot" ... . done. diff --git a/tests/testthat/_snaps/windows/po_update.md b/tests/testthat/_snaps/windows/po_update.md index 2616919..6007b7a 100644 --- a/tests/testthat/_snaps/windows/po_update.md +++ b/tests/testthat/_snaps/windows/po_update.md @@ -2,11 +2,11 @@ Code po_update(verbose = TRUE, lazy = FALSE) - Message + Output Updating 'fr' R translation - Running system command msgmerge --update "./po/R-fr.po" --backup=off --previous "./po/R-test.pot". + Running system command msgmerge --update "./po/R-fr.po" --backup=off --previous "./po/R-test.pot" . . done. Updating 'ja' R translation - Running system command msgmerge --update "./po/R-ja.po" --backup=off --previous "./po/R-test.pot". + Running system command msgmerge --update "./po/R-ja.po" --backup=off --previous "./po/R-test.pot" . . done. diff --git a/tests/testthat/helpers.R b/tests/testthat/helpers.R index ad61f36..8c05b20 100644 --- a/tests/testthat/helpers.R +++ b/tests/testthat/helpers.R @@ -16,7 +16,7 @@ restore_package <- function(dir, expr, tmp_conn) { on.exit(options(old), add = TRUE) } - invisible(capture.output(expr)) + invisible(expr) } # TODO: I think this can just be replaced by expect_match and expect_no_match in current testthat dev diff --git a/tests/testthat/test-translate-package.R b/tests/testthat/test-translate-package.R index 5c3d5ec..d273037 100644 --- a/tests/testthat/test-translate-package.R +++ b/tests/testthat/test-translate-package.R @@ -25,25 +25,32 @@ test_that("translate_package handles empty packages", { { expect_invisible(translate_package(pkg)) - expect_message(translate_package(pkg, verbose=TRUE), "No messages to translate", fixed=TRUE) + expect_output( + translate_package(pkg, verbose=TRUE), + "No messages to translate", + fixed=TRUE + ) } ) # a package with no R directory (e.g. a data package) restore_package( pkg <- test_package("r_data_pkg"), - expect_message(translate_package(pkg, verbose=TRUE), "No messages to translate", fixed=TRUE) + expect_output( + translate_package(pkg, verbose=TRUE), + "No messages to translate", + fixed=TRUE + ) ) }) -test_that("translate_package works on a simple package", { - # simple run-through without doing translations +test_that("works on a simple package (simple run-through without doing translations)", { restore_package( pkg <- test_package("r_msg"), { - expect_messages( + expect_output( translate_package(pkg, verbose=TRUE), - c("No languages provided"), + "No languages provided", fixed = TRUE ) @@ -52,27 +59,37 @@ test_that("translate_package works on a simple package", { pot_file <- "po/R-rMsg.pot" expect_true(pot_file %in% pkg_files) # testing gettextf's ... arguments are skipped - expect_all_match(readLines(file.path(pkg, pot_file)), "don't translate me", invert=TRUE, fixed=TRUE) + expect_no_match(readLines(file.path(pkg, pot_file)), "don't translate me", fixed=TRUE) # Non-UTF-8 machines don't run en@quot translations by default. # Mostly applies to Windows, but can also apply to Unix # (e.g. r-devel-linux-x86_64-debian-clang on CRAN), #191 if (l10n_info()[["UTF-8"]]) { - expect_match(pkg_files, "inst/po/en@quot/LC_MESSAGES/R-rMsg.mo", all = FALSE) + expect_match(pkg_files, "inst/po/en@quot/LC_MESSAGES/R-rMsg.mo", all=FALSE) } } ) - # do translations with mocked input - prompts <- restore_package( - pkg, +}) + +test_that("works on a simple package (including with translations)", { + restore_package( + pkg <- test_package("r_msg"), tmp_conn = mock_translation("test-translate-package-r_msg-1.input"), { - expect_messages( - translate_package(pkg, "zh_CN", verbose=TRUE), - c("Beginning new translations", "BEGINNING TRANSLATION", "Recompiling 'zh_CN' R translation"), + expect_message( + { + translation_log <- capture.output(translate_package(pkg, "zh_CN", verbose=TRUE)) + }, + "BEGINNING TRANSLATION", fixed = TRUE ) + expect_match(translation_log, "^---^", fixed=TRUE, all=FALSE) + expect_match(translation_log, "^^", fixed=TRUE, all=FALSE) + + expect_match(translation_log, "Beginning new translations", fixed=TRUE, all=FALSE) + expect_match(translation_log, "Recompiling 'zh_CN' R translation", fixed=TRUE, all=FALSE) + pkg_files <- list.files(pkg, recursive = TRUE) expect_true("po/R-zh_CN.po" %in% pkg_files) @@ -86,13 +103,13 @@ test_that("translate_package works on a simple package", { expect_match(zh_translations, "该起床了", all = FALSE) } ) - expect_all_match(prompts, c("^---^", "^^"), fixed=TRUE) +}) - # all translations already done +test_that("works on a simple package where translations are already done", { restore_package( - pkg, + pkg <- test_package("r_msg"), { - expect_messages( + expect_output( translate_package(pkg, "fa", verbose=TRUE), "Translations for fa are up to date! Skipping", fixed = TRUE @@ -103,120 +120,125 @@ test_that("translate_package works on a simple package", { test_that("translate_package works on package with outdated (fuzzy) translations", { # simple run-through without doing translations - prompts = restore_package( + restore_package( pkg <- test_package("r_fuzzy"), tmp_conn = mock_translation("test-translate-package-r_fuzzy-1.input"), { - expect_messages( - translate_package(pkg, "zh_CN", verbose=TRUE), - c("translations marked as deprecated", "SINGULAR MESSAGES", "PLURAL MESSAGES"), - fixed = TRUE + expect_message( + expect_message( + { + translation_log <- capture.output(translate_package(pkg, "zh_CN", verbose=TRUE)) + }, + "translations marked as deprecated", + fixed = TRUE + ), + "BEGINNING TRANSLATION" ) + + expect_match(translation_log, "SINGULAR MESSAGES", fixed=TRUE, all=FALSE) + expect_match(translation_log, "PLURAL MESSAGES", fixed=TRUE, all=FALSE) + expect_match(translation_log, "a similar message was previously translated as", fixed=TRUE, all=FALSE) } ) - expect_match(prompts, "a similar message was previously translated as", all=FALSE) }) # NB: keep this test here (not in test-diagnostics) to keep coverage of the diagnostic flow in translate_package() test_that("translate_package identifies potential translations in cat() calls", { - prompts = restore_package( + restore_package( pkg <- test_package("r_cat_msg"), tmp_conn = mock_translation("test-translate-package-r_cat_message-1.input"), { - expect_messages( - translate_package(pkg, "zh_CN"), + expect_message( + { + translation_log <- capture.output(translate_package(pkg, "zh_CN")) + }, "Found 4 untranslated messaging calls passed through cat()", fixed = TRUE ) + expect_match(translation_log, 'cat(gettext("I warned you!"), fill=TRUE)', fixed=TRUE, all=FALSE) + expect_match(translation_log, 'cat(gettext("Oh no you\\ndon\'t!"))', fixed=TRUE, all=FALSE) + expect_match(translation_log, "Hixxboss", fixed=TRUE, all=FALSE) + expect_no_match(translation_log, "shouldn't be translated", fixed=TRUE) + expect_no_match(translation_log, "Miss me", fixed=TRUE) } ) - expect_all_match( - prompts, - c( - 'cat(gettext("I warned you!"), fill=TRUE)', - 'cat(gettext("Oh no you\\ndon\'t!"))', - "Hixxboss" - ), - fixed=TRUE - ) - expect_all_match( - prompts, - c("shouldn't be translated", "Miss me"), - fixed=TRUE, invert=TRUE - ) }) +# NB: this test will fail if test_that is re-run on the same R session since potools' +# internal state is altered for the remainder of the session... not sure it's worth changing... test_that('Unknown language flow works correctly', { - prompts = restore_package( + restore_package( pkg <- test_package('r_msg'), tmp_conn = mock_translation('test-translate-package-r_msg-2.input'), { - expect_messages( - # earlier, did Arabic, but now that's a chosen language. switched two Welsh on the - # (perhaps naive) judgment that it's unlikely to enter our scope anytime soon - # and because there are still several (4) plural forms - translate_package(pkg, 'cy'), - c( - 'not a known language', 'Please file an issue', - # NB: this test will fail if test_that is re-run on the same R session since potools' - # internal state is altered for the remainder of the session... not sure it's worth changing... - "Did not match any known 'plural's" + # earlier, did Arabic, but now that's a chosen language. switched two Welsh on the + # (perhaps naive) judgment that it's unlikely to enter our scope anytime soon + # and because there are still several (4) plural forms + expect_message( + expect_message( + { + translation_log <- capture.output(translate_package(pkg, 'cy')) + }, + "Did not match any known 'plural's", + fixed=TRUE ), - fixed=TRUE + "Input must be of type.*Trying again" ) + + expect_match(translation_log, 'not a known language', fixed=TRUE, all=FALSE) + expect_match(translation_log, 'Please file an issue', fixed=TRUE, all=FALSE) + + # also include coverage tests of incorrect templating in supplied translations + expect_match(translation_log, 'How would you refer to this language in English?', fixed=TRUE, all=FALSE) + expect_match(translation_log, 'received the same set of templates', fixed=TRUE, all=FALSE) + expect_match(translation_log, 'received 2 unique templated arguments', fixed=TRUE, all=FALSE) + expect_match(translation_log, 'received 4 unique templated arguments', fixed=TRUE, all=FALSE) + expect_match(translation_log, 'received 5 unique templated arguments', fixed=TRUE, all=FALSE) } ) - # also include coverage tests of incorrect templating in supplied translations - expect_all_match( - prompts, - c( - 'How would you refer to this language in English?', - 'received the same set of templates', - 'received 2 unique templated arguments', - 'received 4 unique templated arguments', - 'received 5 unique templated arguments' - ), - fixed=TRUE - ) +}) - # whitespace matching for plural is lenient, #183 - prompts = restore_package( +test_that('Whitespace matching for plural is lenient in unknown language flow, #183', { + restore_package( pkg <- test_package('r_msg'), tmp_conn = mock_translation('test-translate-package-r_msg-5.input'), { - expect_messages( - # Catalan -- romance language with >1 plural - translate_package(pkg, 'ca', diagnostics=NULL), - c("Did not match any known 'plural's"), - fixed=TRUE, invert=TRUE - ) + translation_log <- capture.output(translate_package(pkg, 'ca', diagnostics=NULL)) + + # Catalan -- romance language with >1 plural + expect_no_match(translation_log, "Did not match any known 'plural's", fixed=TRUE) + expect_match(translation_log, "when n = 1", fixed=TRUE, all=FALSE) + expect_match(translation_log, "when n is not 1", fixed=TRUE, all=FALSE) } ) - expect_all_match( - prompts, - c("when n = 1", "when n is not 1"), - fixed = TRUE - ) }) -test_that('Erroneous messages stop get_specials_metadata', { +test_that('Erroneous messages stop get_specials_metadata (invalid templated message)', { restore_package( pkg <- test_package('r_msg'), tmp_conn = mock_translation('test-translate-package-r_msg-3.input'), { expect_error( - translate_package(pkg, 'zh_CN', diagnostics = NULL), + expect_output( + translate_package(pkg, 'zh_CN', diagnostics = NULL), + "Writing R-rMsg.pot", fixed = TRUE + ), 'Invalid templated message. If any %N$', fixed = TRUE ) } ) +}) +test_that('Erroneous messages stop get_specials_metadata (all messages point to the same input)', { restore_package( pkg <- test_package('r_msg'), tmp_conn = mock_translation('test-translate-package-r_msg-4.input'), { expect_error( - translate_package(pkg, 'zh_CN', diagnostics = NULL), + expect_output( + translate_package(pkg, 'zh_CN', diagnostics = NULL), + "Writing R-rMsg.pot", fixed = TRUE + ), 'all messages pointing to the same input', fixed = TRUE ) } @@ -224,11 +246,18 @@ test_that('Erroneous messages stop get_specials_metadata', { }) test_that("Packages with src code work correctly", { - prompts = restore_package( + restore_package( pkg <- test_package('r_src_c'), tmp_conn = mock_translation('test-translate-package-r_src_c-1.input'), { - translate_package(pkg, "zh_CN", diagnostics = check_untranslated_src) + translation_log <- capture.output( + translate_package(pkg, "zh_CN", diagnostics=check_untranslated_src) + ) + expect_match(translation_log, "Rprintf(_(", fixed=TRUE, all=FALSE) + expect_match(translation_log, "warning(_(", fixed=TRUE, all=FALSE) + + # error(ngettext(...)) doesn't show error() in check_untranslated_src + expect_no_match(translation_log, "Problematic call", fixed=TRUE) pkg_files <- list.files(pkg, recursive = TRUE) expect_true("po/R-zh_CN.po" %in% pkg_files) @@ -278,38 +307,22 @@ test_that("Packages with src code work correctly", { ) } ) - - expect_all_match( - prompts, - c("Rprintf(_(", "warning(_("), - fixed = TRUE - ) - - # error(ngettext(...)) doesn't show error() in check_untranslated_src - expect_all_match( - prompts, - "Problematic call", - invert = TRUE, fixed = TRUE - ) }) test_that("Packages with src code & fuzzy messages work", { - prompts = restore_package( + restore_package( pkg <- test_package("r_src_fuzzy"), tmp_conn = mock_translation('test-translate-package-r_src_fuzzy-1.input'), { - expect_messages( - translate_package(pkg, "zh_CN", verbose = TRUE), - "Found existing src translations", - fixed = TRUE + expect_output( + expect_message( + translate_package(pkg, "zh_CN", verbose = TRUE), + "BEGINNING TRANSLATION", fixed = TRUE + ), + "Found existing src translations.*Note: a similar message was previously translated as" ) } ) - expect_all_match( - prompts, - "Note: a similar message was previously translated as", - fixed = TRUE - ) }) # TODO: separate get_message_data() tests from write_po_files() tests here @@ -317,7 +330,10 @@ test_that("Various edge cases in retrieving/outputting messages in R files are h restore_package( pkg <- test_package("unusual_msg"), { - translate_package(pkg, diagnostics = NULL) + expect_output( + translate_package(pkg, diagnostics = NULL), + "Writing R-rMsgUnusual.pot", fixed=TRUE + ) r_pot_file <- readLines(file.path(pkg, "po", "R-rMsgUnusual.pot")) src_pot_file <- readLines(file.path(pkg, "po", "rMsgUnusual.pot")) @@ -385,7 +401,11 @@ test_that("use_base_rules=FALSE produces our preferred behavior", { pkg <- test_package("unusual_msg"), tmp_conn = mock_translation("test-translate-package-unusual_msg-1.input"), { - translate_package(pkg, "es", copyright = "Mata Hari", diagnostics = NULL) + expect_output( + translate_package(pkg, "es", copyright = "Mata Hari", diagnostics = NULL), + "Writing R-rMsgUnusual.pot", fixed=TRUE + ) + r_pot_lines <- readLines(file.path(pkg, "po", "R-rMsgUnusual.pot")) src_pot_lines <- readLines(file.path(pkg, "po", "rMsgUnusual.pot")) @@ -425,7 +445,11 @@ test_that("use_base_rules=TRUE produces base-aligned behavior", { pkg <- test_package("unusual_msg"), tmp_conn = mock_translation("test-translate-package-unusual_msg-1.input"), { - translate_package(pkg, "es", use_base_rules = TRUE, diagnostics = NULL) + expect_output( + translate_package(pkg, "es", use_base_rules = TRUE, diagnostics = NULL), + "R-rMsgUnusual.pot", fixed=TRUE + ) + r_pot_lines <- readLines(file.path(pkg, "po", "R-rMsgUnusual.pot")) src_pot_lines <- readLines(file.path(pkg, "po", "rMsgUnusual.pot")) @@ -470,7 +494,10 @@ test_that("use_base_rules is auto-detected", { restore_package( pkg <- test_package("r-devel/src/library/grDevices"), { - translate_package(pkg, diagnostics = NULL) + expect_output( + translate_package(pkg, diagnostics = NULL), + "Writing R-grDevices.pot", fixed=TRUE + ) r_pot_lines <- readLines(file.path(pkg, 'po', 'R-grDevices.pot')) src_pot_lines <- readLines(file.path(pkg, 'po', 'grDevices.pot')) @@ -529,7 +556,10 @@ test_that("translation of 'base' works correctly", { file.copy(tmp_potfiles, correct_potfiles) on.exit(unlink(tmp_potfiles), add = TRUE) - translate_package(pkg, diagnostics = NULL) + expect_output( + translate_package(pkg, diagnostics = NULL), + "Writing R-base.pot", fixed=TRUE + ) expect_true(file.exists(file.path(pkg, 'po', 'R-base.pot'))) expect_true(file.exists(file.path(pkg, 'po', 'R.pot'))) @@ -555,16 +585,14 @@ test_that("translation of 'base' works correctly", { }) test_that("max_translations works as expected", { - prompts <- restore_package( + restore_package( pkg <- test_package("r_msg"), tmp_conn = mock_translation('test-translate-package-r_msg-1.input'), { - translate_package(pkg, 'es', max_translations = 1L, diagnostics = NULL) + translation_log <- capture.output( + translate_package(pkg, 'es', max_translations = 1L, diagnostics = NULL) + ) + expect_no_match(translation_log, "Oh no you don't!") } ) - expect_all_match( - prompts, - "Oh no you don't!", - fixed = TRUE, invert = TRUE - ) })