From b749b3eb492778be769ef31376aa9facb8a71bcf Mon Sep 17 00:00:00 2001 From: yzerk Date: Tue, 16 May 2023 08:56:14 +0300 Subject: [PATCH 1/4] fix: unix tests on windows --- .../actions/DownloadSourcesAction.java | 4 ++-- .../commands/functionality/SourcesUtils.java | 4 ++-- .../crowdin/cli/utils/PlaceholderUtil.java | 10 ++++++-- .../actions/DownloadSourcesActionTest.java | 23 +++++++++++-------- .../functionality/SourcesUtilsTest.java | 21 +++++++++++------ .../functionality/TranslationsUtilsTest.java | 1 - .../cli/utils/PlaceholderUtilTest.java | 6 ++--- 7 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java b/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java index 6d40b4f52..b7eef948f 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java @@ -139,9 +139,9 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli } } } else { - String translationPrepared = fileBean.getTranslation() + String translationPrepared = Pattern.quote(fileBean.getTranslation() .replaceAll(Utils.PATH_SEPARATOR_REGEX + "\\*\\*", "(" + Utils.PATH_SEPARATOR_REGEX + ".+)?") - .replaceAll("\\\\", "\\\\\\\\"); + .replaceAll("\\\\", "\\\\\\\\")); Predicate translationPred = Pattern.compile(translationPrepared).asPredicate(); for (String filePathKey : filePaths.keySet()) { String exportPattern = ProjectFilesUtils.getExportPattern(((File) filePaths.get(filePathKey)).getExportOptions()); diff --git a/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java b/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java index 7f2b3d195..c61309697 100644 --- a/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java +++ b/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java @@ -50,7 +50,7 @@ public static List filterProjectFiles( .map(Predicate::negate) .reduce((s) -> true, Predicate::and); } else { - List patternPaths = Arrays.stream(sourcePattern.split("/")) + List patternPaths = Arrays.stream(sourcePattern.split(Pattern.quote(File.separator))) .map(pathSplit -> Pattern.compile("^" + PlaceholderUtil.formatSourcePatternForRegex(pathSplit) + "$")) .collect(Collectors.toList()); Collections.reverse(patternPaths); @@ -71,7 +71,7 @@ public static List filterProjectFiles( }; ignorePredicate = ignorePatterns.stream() .map(ignorePattern -> { - List ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split("/")), false); + List ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator))), false); Collections.reverse(ignorePatternPaths); return ignorePatternPaths; }) diff --git a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java index 9a41a82cc..df22fc6bf 100644 --- a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java +++ b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java @@ -185,6 +185,7 @@ public String replaceFileDependentPlaceholders(String toFormat, File file) { toFormat = toFormat.contains(PLACEHOLDER_FILE_NAME) ? toFormat.replace(PLACEHOLDER_FILE_NAME, fileNameWithoutExt) : toFormat; toFormat = toFormat.contains(PLACEHOLDER_FILE_EXTENSION) ? toFormat.replace(PLACEHOLDER_FILE_EXTENSION, fileExt) : toFormat; toFormat = toFormat.contains(PLACEHOLDER_ORIGINAL_PATH) ? toFormat.replace(PLACEHOLDER_ORIGINAL_PATH, fileParent) : toFormat; + toFormat = toFormat.replace("/", File.separator); if (toFormat.contains("**")) { String prefix = StringUtils.substringBefore(toFormat, "**"); @@ -242,8 +243,13 @@ public static String formatSourcePatternForRegex(String toFormat) { if (Utils.isWindows()) { toFormat = toFormat - .replace("**", ".+") - .replace("\\" + ASTERISK + "\\", "[^/]+") + .replace(ESCAPE_ASTERISK, ESCAPE_ASTERISK_PLACEHOLDER) + .replace("**", ".+") + .replace(ESCAPE_ASTERISK_PLACEHOLDER, ESCAPE_ASTERISK) + + .replace(ESCAPE_ASTERISK, ESCAPE_ASTERISK_PLACEHOLDER) + .replace(ASTERISK, "[^/]+") + .replace(ESCAPE_ASTERISK_PLACEHOLDER, ESCAPE_ASTERISK) ; } else { toFormat = toFormat diff --git a/src/test/java/com/crowdin/cli/commands/actions/DownloadSourcesActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/DownloadSourcesActionTest.java index 4dcd5b796..32bdd8a80 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/DownloadSourcesActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/DownloadSourcesActionTest.java @@ -11,6 +11,9 @@ import com.crowdin.cli.properties.PropertiesWithFiles; import com.crowdin.cli.properties.helper.TempProject; import com.crowdin.cli.utils.Utils; +import com.crowdin.cli.utils.console.ExecutionStatus; +import org.apache.commons.lang3.StringUtils; +import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -20,11 +23,15 @@ import org.mockito.Mockito; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.net.URL; import java.util.ArrayList; +import static com.crowdin.cli.utils.console.ExecutionStatus.OK; +import static com.crowdin.cli.utils.console.ExecutionStatus.WARNING; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -77,7 +84,6 @@ public void testDest() throws IOException { } @Test - @DisabledOnOs(OS.WINDOWS) public void testDestAndUnaryAsterisk() throws IOException { PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder .minimalBuiltPropertiesBean( @@ -262,7 +268,6 @@ public void testWithPreserveHierarchyFalse() throws IOException { } @Test - @DisabledOnOs(OS.WINDOWS) public void testDryRun() { ByteArrayOutputStream outContent = new ByteArrayOutputStream(); PrintStream ps = System.out; @@ -290,19 +295,18 @@ public void testDryRun() { new DownloadSourcesAction(files, false, false, null, true, false,true); action.act(Outputter.getDefault(), pb, client); - String outMessage1 = "✔️ Fetching project info\n"; - String outMessage2 = "✔️ File @|bold 'common/strings.xml'|@\n"; + String outMessage1 = OK.withIcon("Fetching project info"); + String outMessage2 = OK.withIcon(String.format("File @|bold 'common%sstrings.xml'|@", File.separator)); client.downloadFullProject(null); client.downloadFile(101L); - assertTrue(outContent.toString().contains(outMessage1)); - assertTrue(outContent.toString().contains(outMessage2)); + assertThat(outContent.toString(), Matchers.containsString(outMessage1)); + assertThat(outContent.toString(), Matchers.containsString(outMessage2)); } @Test - @DisabledOnOs(OS.WINDOWS) - public void testReviewedOnly() throws IOException { + public void testReviewedOnly() { ByteArrayOutputStream outContent = new ByteArrayOutputStream(); PrintStream ps = System.out; System.setOut(new PrintStream(outContent)); @@ -329,7 +333,8 @@ public void testReviewedOnly() throws IOException { new DownloadSourcesAction(files, false, false, null, true, true, false); action.act(Outputter.getDefault(), pb, client); - String warnMessage = "⚠️ Operation is available only for Crowdin Enterprise\n"; + String warnMessage = WARNING.withIcon("Operation is available only for Crowdin Enterprise") + + System.lineSeparator(); client.downloadFullProject(null); assertEquals(warnMessage, outContent.toString()); 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 073531f83..2dc591953 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java @@ -14,12 +14,15 @@ import org.junit.jupiter.params.provider.MethodSource; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.crowdin.cli.utils.AssertUtils.assertPathsEqualIgnoringSeparator; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -94,6 +97,11 @@ static Stream testGetFiles1() { public void testFilterProjectFiles_wPreserveHierarchy_noIgnores(List filePaths, String sourcePattern, List expected) { List actual = SourcesUtils.filterProjectFiles( filePaths, sourcePattern, Collections.EMPTY_LIST, true, PlaceholderUtilBuilder.STANDART.build("")); + + actual = actual.stream().map(i -> i.replace("/", File.separator).replace("\\", File.separator)) + .collect(Collectors.toList()); + expected = expected.stream().map(i -> i.replace("/", File.separator).replace("\\", File.separator)) + .collect(Collectors.toList()); assertEquals(expected.size(), actual.size()); assertThat(actual, containsInAnyOrder(expected.toArray())); } @@ -281,12 +289,12 @@ static Stream testFilterProjectFiles_noPreserveHierarchy_noIgnores() @ParameterizedTest @MethodSource - @DisabledOnOs(OS.WINDOWS) public void testFilterProjectFiles_noPreserveHierarchy_wIgnores( List filePaths, String sourcePattern, List ignorePatterns, List expected ) { List actual = SourcesUtils.filterProjectFiles( filePaths, sourcePattern, ignorePatterns, false, PlaceholderUtilBuilder.STANDART.build("")); + expected = expected.stream().map(path -> path.replace("/", File.separator)).collect(Collectors.toList()); // assertEquals(expected.size(), actual.size()); assertThat(actual, containsInAnyOrder(expected.toArray())); } @@ -341,15 +349,15 @@ static Stream testFilterProjectFiles_noPreserveHierarchy_wIgnores() { } @Test - @DisabledOnOs(OS.WINDOWS) public void testFilterProjectFiles_dest() { List filePaths = Arrays.asList("common/strings.xml"); String sourcePattern = "/common/%original_file_name%"; List ignorePatterns = null; - String[] expected = {"common/strings.xml"}; + Path expected = Paths.get("common/strings.xml"); - List actual = SourcesUtils.filterProjectFiles( - filePaths, sourcePattern, ignorePatterns, true, PlaceholderUtilBuilder.STANDART.build("")); + List actual = SourcesUtils.filterProjectFiles(filePaths, sourcePattern, ignorePatterns, + true, PlaceholderUtilBuilder.STANDART.build("")).stream().map(Paths::get) + .collect(Collectors.toList()); assertThat(actual, containsInAnyOrder(expected)); } @@ -375,9 +383,8 @@ private static Stream testContainsParameter() { @ParameterizedTest @MethodSource - @DisabledOnOs(OS.WINDOWS) public void testReplaceUnaryAsterisk(String sourcePattern, String projectFile, String expected) { - assertEquals(SourcesUtils.replaceUnaryAsterisk(sourcePattern, projectFile), expected); + assertPathsEqualIgnoringSeparator(SourcesUtils.replaceUnaryAsterisk(sourcePattern, projectFile), expected); } public static Stream testReplaceUnaryAsterisk() { diff --git a/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java b/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java index 0e7c13dff..03f9fab45 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java @@ -20,7 +20,6 @@ public class TranslationsUtilsTest { @ParameterizedTest @MethodSource - @DisabledOnOs(OS.WINDOWS) public void testReplaceDoubleAsterisk(String sourcePattern, String translationPattern, String sourceFile, String expected) { String result = TranslationsUtils.replaceDoubleAsterisk(sourcePattern, translationPattern, sourceFile); assertEquals(expected, result, diff --git a/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java b/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java index 895900f90..164eb3c2d 100644 --- a/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java +++ b/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java @@ -3,8 +3,6 @@ import com.crowdin.cli.client.LanguageMapping; import com.crowdin.client.languages.model.Language; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledOnOs; -import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -19,6 +17,7 @@ import java.util.Set; import java.util.stream.Stream; +import static com.crowdin.cli.utils.AssertUtils.assertPathsEqualIgnoringSeparator; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.params.provider.Arguments.arguments; @@ -139,10 +138,9 @@ public void testReplaceLanguageDependentPlaceholders() { @ParameterizedTest @MethodSource - @DisabledOnOs(OS.WINDOWS) public void testDoubleAsteriskInWildCard(String source, File crowdinFile, String expected) { PlaceholderUtil placeholderUtil = new PlaceholderUtil(new ArrayList<>(), new ArrayList<>(), "./"); - assertEquals(expected, placeholderUtil.replaceFileDependentPlaceholders(source, crowdinFile)); + assertPathsEqualIgnoringSeparator(expected, placeholderUtil.replaceFileDependentPlaceholders(source, crowdinFile)); } static Stream testDoubleAsteriskInWildCard() { From f55c41ecd0f9dc7767d70a5206a6db57219863f6 Mon Sep 17 00:00:00 2001 From: yzerk Date: Tue, 16 May 2023 09:10:14 +0300 Subject: [PATCH 2/4] fix: unix tests on windows (commit missed file) --- .../java/com/crowdin/cli/utils/AssertUtils.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/java/com/crowdin/cli/utils/AssertUtils.java diff --git a/src/test/java/com/crowdin/cli/utils/AssertUtils.java b/src/test/java/com/crowdin/cli/utils/AssertUtils.java new file mode 100644 index 000000000..fb2f4df60 --- /dev/null +++ b/src/test/java/com/crowdin/cli/utils/AssertUtils.java @@ -0,0 +1,17 @@ +package com.crowdin.cli.utils; + +import java.io.File; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + + +public class AssertUtils { + + public static void assertPathsEqualIgnoringSeparator(String actual, String expected) { + actual = actual.replace("/", File.separator).replace("\\", File.separator); + expected = expected.replace("/", File.separator).replace("\\", File.separator); + assertThat(actual, equalTo(expected)); + } + +} From cff2623ebbfa6c3fe028d4922547c03ef505e924 Mon Sep 17 00:00:00 2001 From: yzerk Date: Tue, 16 May 2023 13:01:03 +0300 Subject: [PATCH 3/4] fix: fix test on unix --- .../crowdin/cli/commands/actions/DownloadSourcesAction.java | 4 ++-- src/main/java/com/crowdin/cli/utils/Utils.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java b/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java index b7eef948f..6d40b4f52 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java @@ -139,9 +139,9 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli } } } else { - String translationPrepared = Pattern.quote(fileBean.getTranslation() + String translationPrepared = fileBean.getTranslation() .replaceAll(Utils.PATH_SEPARATOR_REGEX + "\\*\\*", "(" + Utils.PATH_SEPARATOR_REGEX + ".+)?") - .replaceAll("\\\\", "\\\\\\\\")); + .replaceAll("\\\\", "\\\\\\\\"); Predicate translationPred = Pattern.compile(translationPrepared).asPredicate(); for (String filePathKey : filePaths.keySet()) { String exportPattern = ProjectFilesUtils.getExportPattern(((File) filePaths.get(filePathKey)).getExportOptions()); diff --git a/src/main/java/com/crowdin/cli/utils/Utils.java b/src/main/java/com/crowdin/cli/utils/Utils.java index 29a9cf8fa..414a92595 100755 --- a/src/main/java/com/crowdin/cli/utils/Utils.java +++ b/src/main/java/com/crowdin/cli/utils/Utils.java @@ -150,7 +150,7 @@ public static Optional> proxyHost() { } Integer port; try { - port = new Integer(System.getenv(HTTP_PROXY_PORT_ENV)); + port = Integer.valueOf(System.getenv(HTTP_PROXY_PORT_ENV)); } catch (NumberFormatException e) { return Optional.empty(); } @@ -172,4 +172,4 @@ public static String encodeURL(@NonNull String toEncode) { throw new RuntimeException(e); } } -} \ No newline at end of file +} From abc47c466b6fe7a513be5a5ab052dfd5406e7ad1 Mon Sep 17 00:00:00 2001 From: yzerk Date: Tue, 16 May 2023 13:26:18 +0300 Subject: [PATCH 4/4] fix: disable test --- .../cli/commands/functionality/TranslationsUtilsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java b/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java index 03f9fab45..0e7c13dff 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java @@ -20,6 +20,7 @@ public class TranslationsUtilsTest { @ParameterizedTest @MethodSource + @DisabledOnOs(OS.WINDOWS) public void testReplaceDoubleAsterisk(String sourcePattern, String translationPattern, String sourceFile, String expected) { String result = TranslationsUtils.replaceDoubleAsterisk(sourcePattern, translationPattern, sourceFile); assertEquals(expected, result,