From 8b9f5aff00030f7b32da9ea6de95ba67a88315d5 Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Thu, 16 Nov 2023 18:23:34 +0200 Subject: [PATCH] tests and refactor --- .../crowdin/cli/utils/PlaceholderUtil.java | 22 ++++++++----- .../functionality/SourcesUtilsTest.java | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java index 6ba403d2b..acb15a224 100644 --- a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java +++ b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java @@ -53,13 +53,16 @@ public class PlaceholderUtil { private static final String SET_CLOSE_BRACKET = "]"; public static final String ROUND_BRACKET_OPEN = "("; public static final String ROUND_BRACKET_CLOSE = ")"; - public static final String ESCAPE_ROUND_BRACKET_OPEN = isWindows() ? "^(" : "\\("; - public static final String ESCAPE_ROUND_BRACKET_CLOSE = isWindows() ? "^)" : "\\)"; - private static final String ESCAPE_DOT = isWindows() ? "^." : "\\."; + public static final String ESCAPE_ROUND_BRACKET_OPEN = "\\("; + public static final String ESCAPE_ROUND_BRACKET_CLOSE = "\\)"; + private static final String ESCAPE_DOT = "\\."; + private static final String ESCAPE_DOT_WIN = "^."; private static final String ESCAPE_DOT_PLACEHOLDER = "{ESCAPE_DOT}"; - private static final String ESCAPE_QUESTION = isWindows() ? "^?" : "\\?"; + private static final String ESCAPE_QUESTION = "\\?"; + private static final String ESCAPE_QUESTION_WIN = "^?"; private static final String ESCAPE_QUESTION_PLACEHOLDER = "{ESCAPE_QUESTION_MARK}"; - private static final String ESCAPE_ASTERISK = isWindows() ? "^*" : "\\*"; + private static final String ESCAPE_ASTERISK = "\\*"; + private static final String ESCAPE_ASTERISK_WIN = "^*"; private static final String ESCAPE_ASTERISK_PLACEHOLDER = "{ESCAPE_ASTERISK}"; private static final String ESCAPE_ASTERISK_REPLACEMENT_FROM = ".+" + Utils.PATH_SEPARATOR; private static final String ESCAPE_ASTERISK_REPLACEMENT_TO = "(.+" + Utils.PATH_SEPARATOR_REGEX + ")?"; @@ -230,13 +233,16 @@ public List formatForRegex(List toFormat, boolean onProjectLangs } public static String formatSourcePatternForRegex(String toFormat) { - if(isWindows()){ + if(isWindows()) { toFormat = toFormat - .replace("\\", "\\\\"); + .replace("\\", "\\\\") + .replace(ESCAPE_DOT_WIN, ESCAPE_DOT) + .replace(ESCAPE_ASTERISK_WIN, ESCAPE_ASTERISK) + .replace(ESCAPE_QUESTION_WIN, ESCAPE_QUESTION); } toFormat = toFormat .replace(ESCAPE_DOT, ESCAPE_DOT_PLACEHOLDER) - .replace(DOT, "\\.") + .replace(DOT, ESCAPE_DOT) .replace(ESCAPE_DOT_PLACEHOLDER, ESCAPE_DOT) .replace(ESCAPE_QUESTION, ESCAPE_QUESTION_PLACEHOLDER) diff --git a/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java b/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java index 2dc591953..057ad52c2 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java @@ -361,6 +361,39 @@ public void testFilterProjectFiles_dest() { assertThat(actual, containsInAnyOrder(expected)); } + @ParameterizedTest + @MethodSource + @DisabledOnOs({OS.LINUX, OS.MAC}) + public void testFilterProjectFiles_escapeSymbols_windows(List filePaths, String sourcePattern, List expected) { + List actual = SourcesUtils.filterProjectFiles( + filePaths, sourcePattern, Collections.emptyList(), true, PlaceholderUtilBuilder.STANDART.build("")); + assertEquals(expected.size(), actual.size()); + assertThat(actual, containsInAnyOrder(expected.toArray())); + } + + static Stream testFilterProjectFiles_escapeSymbols_windows() { + String file1 = "file?1.po"; + String file2Here = "here\\file*2.po"; + String file3There = "here\\there\\file.3.po"; + return Stream.of( + arguments( + Arrays.asList(file1, file2Here, file3There), + "file^?1.po", + Arrays.asList(file1) + ), + arguments( + Arrays.asList(file1, file2Here, file3There), + "here\\file^*2.po", + Arrays.asList(file2Here) + ), + arguments( + Arrays.asList(file1, file2Here, file3There), + "here\\there\\file^.3.po", + Arrays.asList(file3There) + ) + ); + } + @ParameterizedTest @MethodSource public void testContainsParameter(String sourcePattern, boolean expected) {