From c5fbb92e23a1ce6ddd7fe7641826778a59f28226 Mon Sep 17 00:00:00 2001 From: Mark Allen <3417310+maallen@users.noreply.github.com> Date: Tue, 1 Aug 2023 11:59:30 +0100 Subject: [PATCH] Add parameter to allow skipping of plurals in comment checker --- .../cli/command/checks/CliCheckerOptions.java | 5 +++++ .../command/checks/CliCheckerParameters.java | 3 ++- .../checks/ContextAndCommentCliChecker.java | 8 +++++++ .../ContextAndCommentCliCheckerTest.java | 21 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerOptions.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerOptions.java index 38cb1b0b1c..d762993ec0 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerOptions.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerOptions.java @@ -1,5 +1,6 @@ package com.box.l10n.mojito.cli.command.checks; +import static com.box.l10n.mojito.cli.command.checks.CliCheckerParameters.CONTEXT_COMMENT_PLURAL_SKIP; import static com.box.l10n.mojito.cli.command.checks.CliCheckerParameters.CONTEXT_COMMENT_REJECT_PATTERN_KEY; import static com.box.l10n.mojito.cli.command.checks.CliCheckerParameters.DICTIONARY_ADDITIONS_PATH_KEY; import static com.box.l10n.mojito.cli.command.checks.CliCheckerParameters.DICTIONARY_AFFIX_FILE_PATH_KEY; @@ -58,6 +59,10 @@ public String getRecommendStringIdLabelIgnorePattern() { return optionsMap.get(RECOMMEND_STRING_ID_LABEL_IGNORE_PATTERN_KEY.getKey()); } + public Boolean getPluralsSkipped() { + return Boolean.valueOf(optionsMap.get(CONTEXT_COMMENT_PLURAL_SKIP.getKey())); + } + public ImmutableMap getOptionsMap() { return optionsMap; } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerParameters.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerParameters.java index 604f3ecef1..ac6242e9ed 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerParameters.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/CliCheckerParameters.java @@ -6,7 +6,8 @@ public enum CliCheckerParameters { DICTIONARY_FILE_PATH_KEY("dictionaryFilePath"), DICTIONARY_AFFIX_FILE_PATH_KEY("dictionaryAffixFilePath"), CONTEXT_COMMENT_REJECT_PATTERN_KEY("contextCommentRejectPattern"), - RECOMMEND_STRING_ID_LABEL_IGNORE_PATTERN_KEY("recommendStringIdLabelIgnorePattern"); + RECOMMEND_STRING_ID_LABEL_IGNORE_PATTERN_KEY("recommendStringIdLabelIgnorePattern"), + CONTEXT_COMMENT_PLURAL_SKIP("contextCommentSkipPlurals"); private String key; diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliChecker.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliChecker.java index 652e6d90cd..2f533c91fe 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliChecker.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliChecker.java @@ -4,6 +4,7 @@ import com.box.l10n.mojito.cli.command.extraction.AssetExtractionDiff; import com.box.l10n.mojito.okapi.extractor.AssetExtractorTextUnit; +import com.google.common.base.Strings; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -106,6 +107,9 @@ private String checkTextUnit(AssetExtractorTextUnit assetExtractorTextUnit) { String failureText = null; String[] splitNameArray = assetExtractorTextUnit.getName().split("---"); String context = null; + if (isPlural(assetExtractorTextUnit) && cliCheckerOptions.getPluralsSkipped()) { + return failureText; + } if (splitNameArray.length > 1) { context = splitNameArray[1]; } @@ -126,6 +130,10 @@ private String checkTextUnit(AssetExtractorTextUnit assetExtractorTextUnit) { return failureText; } + private boolean isPlural(AssetExtractorTextUnit assetExtractorTextUnit) { + return !Strings.isNullOrEmpty(assetExtractorTextUnit.getPluralForm()); + } + private boolean isBlank(String string) { return StringUtils.isBlank(string); } diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliCheckerTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliCheckerTest.java index 1c9e273fc3..154921fc62 100644 --- a/cli/src/test/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliCheckerTest.java +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/checks/ContextAndCommentCliCheckerTest.java @@ -213,4 +213,25 @@ public void testStringsOnlyContainingWhitespace() { + System.lineSeparator(), result.getNotificationText()); } + + @Test + public void testPluralStringsSkippedIfOptionEnabled() { + List addedTUs = new ArrayList<>(); + AssetExtractorTextUnit assetExtractorTextUnit = new AssetExtractorTextUnit(); + assetExtractorTextUnit.setName("Some string id --- "); + assetExtractorTextUnit.setSource("A source string with no errors."); + assetExtractorTextUnit.setComments(null); + assetExtractorTextUnit.setPluralForm("one"); + addedTUs.add(assetExtractorTextUnit); + List assetExtractionDiffs = new ArrayList<>(); + AssetExtractionDiff assetExtractionDiff = new AssetExtractionDiff(); + assetExtractionDiff.setAddedTextunits(addedTUs); + assetExtractionDiffs.add(assetExtractionDiff); + ImmutableMap optionsMap = + ImmutableMap.of(CliCheckerParameters.CONTEXT_COMMENT_PLURAL_SKIP.getKey(), "true"); + contextAndCommentCliChecker.setCliCheckerOptions( + new CliCheckerOptions(Sets.newHashSet(SINGLE_BRACE_REGEX), Sets.newHashSet(), optionsMap)); + CliCheckResult result = contextAndCommentCliChecker.run(assetExtractionDiffs); + Assert.assertTrue(result.isSuccessful()); + } }