Skip to content

Commit

Permalink
tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 committed Nov 17, 2023
1 parent 2a6f5e6 commit 083d93a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ public List<String> formatForRegex(List<String> toFormat, boolean onProjectLangs
}

public static String formatSourcePatternForRegex(String toFormat) {
if(isWindows()){
if (isWindows()) {
toFormat = toFormat
.replace("\\", "\\\\");
.replace("\\", "\\\\");

Check warning on line 235 in src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java#L235

Added line #L235 was not covered by tests
}
toFormat = toFormat
.replace(ESCAPE_DOT, ESCAPE_DOT_PLACEHOLDER)
Expand All @@ -254,10 +254,15 @@ public static String formatSourcePatternForRegex(String toFormat) {

toFormat = toFormat
.replace(ROUND_BRACKET_OPEN, ESCAPE_ROUND_BRACKET_OPEN)

.replace(ROUND_BRACKET_CLOSE, ESCAPE_ROUND_BRACKET_CLOSE)

.replace(ESCAPE_ASTERISK_REPLACEMENT_FROM, ESCAPE_ASTERISK_REPLACEMENT_TO);

if (isWindows()) {
toFormat = toFormat
.replace(ESCAPE_ASTERISK, "\\*")
.replace(ESCAPE_DOT, "\\.")
.replace(ESCAPE_QUESTION, "\\?");

Check warning on line 264 in src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java#L261-L264

Added lines #L261 - L264 were not covered by tests
}
return toFormat
.replace(PLACEHOLDER_FILE_EXTENSION, "[^/]+")
.replace(PLACEHOLDER_FILE_NAME, "[^/]+")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static String buildUserAgent() {
}

public static String unixPath(String path) {
return (path != null) ? path.replaceAll("[\\\\/]+", "/") : null;
return (path != null) ? isWindows() ? path.replaceAll("[\\\\/]+", "/") : path.replaceAll("\\\\{2,}", "/") : null;
}

public static String windowsPath(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,90 @@ public void testFilterProjectFiles_dest() {
assertThat(actual, containsInAnyOrder(expected));
}

@ParameterizedTest
@MethodSource
@DisabledOnOs({OS.LINUX, OS.MAC})
public void testFilterProjectFiles_escapeSymbols_windows(List<String> filePaths, String sourcePattern, List<String> expected) {
List<String> actual = SourcesUtils.filterProjectFiles(
filePaths, sourcePattern, Collections.emptyList(), true, PlaceholderUtilBuilder.STANDART.build(""));
assertEquals(expected.size(), actual.size());
assertThat(actual, containsInAnyOrder(expected.toArray()));
}

static Stream<Arguments> testFilterProjectFiles_escapeSymbols_windows() {
String file1 = "here\\file-1.po";
String file1Symbol = "here\\file?1.po";
String file2 = "here\\file-2.po";
String file2Symbol = "here\\file*2.po";
String file3 = "here\\file-3.po";
String file3Symbol = "here\\file.3.po";
List<String> allFiles = Arrays.asList(file1, file1Symbol, file2, file2Symbol, file3, file3Symbol);
return Stream.of(
arguments(
allFiles,
"here\\file^?1.po",
Arrays.asList(file1Symbol)
),
arguments(
allFiles,
"here\\file^*2.po",
Arrays.asList(file2Symbol)
),
arguments(
allFiles,
"here\\file^.3.po",
Arrays.asList(file3Symbol)
),
arguments(
allFiles,
"here\\*.po",
allFiles
)
);
}

@ParameterizedTest
@MethodSource
@DisabledOnOs(OS.WINDOWS)
public void testFilterProjectFiles_escapeSymbols_unix(List<String> filePaths, String sourcePattern, List<String> expected) {
List<String> actual = SourcesUtils.filterProjectFiles(
filePaths, sourcePattern, Collections.emptyList(), true, PlaceholderUtilBuilder.STANDART.build(""));
assertEquals(expected.size(), actual.size());
assertThat(actual, containsInAnyOrder(expected.toArray()));
}

static Stream<Arguments> testFilterProjectFiles_escapeSymbols_unix() {
String file1 = "here/file-1.po";
String file1Symbol = "here/file?1.po";
String file2 = "here/file-2.po";
String file2Symbol = "here/file*2.po";
String file3 = "here/file-3.po";
String file3Symbol = "here/file.3.po";
List<String> allFiles = Arrays.asList(file1, file1Symbol, file2, file2Symbol, file3, file3Symbol);
return Stream.of(
arguments(
allFiles,
"here/file\\?1.po",
Arrays.asList(file1Symbol)
),
arguments(
allFiles,
"here/file\\*2.po",
Arrays.asList(file2Symbol)
),
arguments(
allFiles,
"here/file\\.3.po",
Arrays.asList(file3Symbol)
),
arguments(
allFiles,
"here/*.po",
allFiles
)
);
}

@ParameterizedTest
@MethodSource
public void testContainsParameter(String sourcePattern, boolean expected) {
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/com/crowdin/cli/utils/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Optional;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -36,8 +38,15 @@ public void testBuildUserAgent() {
}

@Test
public void testUnixPath() {
assertEquals("/path/to/file", Utils.unixPath("\\path\\to\\file"));
@DisabledOnOs({OS.LINUX, OS.MAC})
public void testUnixPath_windows() {
assertEquals("/path/to/file", Utils.unixPath("\\path\\to\\\\file"));
}

@Test
@DisabledOnOs(OS.WINDOWS)
public void testUnixPath_unix() {
assertEquals("/path/to/file", Utils.unixPath("/path/to\\\\file"));
}

@Test
Expand Down

0 comments on commit 083d93a

Please sign in to comment.