Skip to content

Commit

Permalink
fix: unix tests on windows (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzerk committed Jun 26, 2023
1 parent f0df87a commit 9be90fc
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static List<String> filterProjectFiles(
.map(Predicate::negate)
.reduce((s) -> true, Predicate::and);
} else {
List<Pattern> patternPaths = Arrays.stream(sourcePattern.split("/"))
List<Pattern> patternPaths = Arrays.stream(sourcePattern.split(Pattern.quote(File.separator)))
.map(pathSplit -> Pattern.compile("^" + PlaceholderUtil.formatSourcePatternForRegex(pathSplit) + "$"))
.collect(Collectors.toList());
Collections.reverse(patternPaths);
Expand All @@ -71,7 +71,7 @@ public static List<String> filterProjectFiles(
};
ignorePredicate = ignorePatterns.stream()
.map(ignorePattern -> {
List<String> ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split("/")), false);
List<String> ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator))), false);
Collections.reverse(ignorePatternPaths);
return ignorePatternPaths;
})
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "**");
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static Optional<Pair<String, Integer>> 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();
}
Expand All @@ -172,4 +172,4 @@ public static String encodeURL(@NonNull String toEncode) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -77,7 +84,6 @@ public void testDest() throws IOException {
}

@Test
@DisabledOnOs(OS.WINDOWS)
public void testDestAndUnaryAsterisk() throws IOException {
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean(
Expand Down Expand Up @@ -262,7 +268,6 @@ public void testWithPreserveHierarchyFalse() throws IOException {
}

@Test
@DisabledOnOs(OS.WINDOWS)
public void testDryRun() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream ps = System.out;
Expand Down Expand Up @@ -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));
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,6 +97,11 @@ static Stream<Arguments> testGetFiles1() {
public void testFilterProjectFiles_wPreserveHierarchy_noIgnores(List<String> filePaths, String sourcePattern, List<String> expected) {
List<String> 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()));
}
Expand Down Expand Up @@ -281,12 +289,12 @@ static Stream<Arguments> testFilterProjectFiles_noPreserveHierarchy_noIgnores()

@ParameterizedTest
@MethodSource
@DisabledOnOs(OS.WINDOWS)
public void testFilterProjectFiles_noPreserveHierarchy_wIgnores(
List<String> filePaths, String sourcePattern, List<String> ignorePatterns, List<String> expected
) {
List<String> 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()));
}
Expand Down Expand Up @@ -341,15 +349,15 @@ static Stream<Arguments> testFilterProjectFiles_noPreserveHierarchy_wIgnores() {
}

@Test
@DisabledOnOs(OS.WINDOWS)
public void testFilterProjectFiles_dest() {
List<String> filePaths = Arrays.asList("common/strings.xml");
String sourcePattern = "/common/%original_file_name%";
List<String> ignorePatterns = null;
String[] expected = {"common/strings.xml"};
Path expected = Paths.get("common/strings.xml");

List<String> actual = SourcesUtils.filterProjectFiles(
filePaths, sourcePattern, ignorePatterns, true, PlaceholderUtilBuilder.STANDART.build(""));
List<Path> actual = SourcesUtils.filterProjectFiles(filePaths, sourcePattern, ignorePatterns,
true, PlaceholderUtilBuilder.STANDART.build("")).stream().map(Paths::get)
.collect(Collectors.toList());
assertThat(actual, containsInAnyOrder(expected));
}

Expand All @@ -375,9 +383,8 @@ private static Stream<Arguments> 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<Arguments> testReplaceUnaryAsterisk() {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/crowdin/cli/utils/AssertUtils.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
6 changes: 2 additions & 4 deletions src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Arguments> testDoubleAsteriskInWildCard() {
Expand Down

0 comments on commit 9be90fc

Please sign in to comment.