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 16, 2023
1 parent 2a6f5e6 commit 7d09b92
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ")?";
Expand Down Expand Up @@ -230,13 +233,16 @@ public List<String> formatForRegex(List<String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,72 @@ 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 = "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
@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 = "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) {
Expand Down

0 comments on commit 7d09b92

Please sign in to comment.