Skip to content

Commit

Permalink
fix: double asterisks in paths (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 authored Mar 19, 2024
1 parent 976f01d commit aa99d5e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class PlaceholderUtil {
private static final String ESCAPE_ASTERISK = isWindows() ? "^*" : "\\*";
private static final String ESCAPE_ASTERISK_REGEX = "\\*";
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_FROM = ".+" + Utils.PATH_SEPARATOR_REGEX;
private static final String ESCAPE_ASTERISK_REPLACEMENT_TO = "(.+" + Utils.PATH_SEPARATOR_REGEX + ")?";

private List<Language> supportedLanguages;
Expand Down Expand Up @@ -193,10 +193,12 @@ public String replaceFileDependentPlaceholders(String toFormat, File file) {
toFormat = toFormat.replace("/", File.separator);

if (toFormat.contains("**")) {
String prefix = StringUtils.substringBefore(toFormat, "**");
prefix = prefix.length() > 1 && file.getPath().contains(prefix) ? StringUtils.substringBefore(fileParent, Utils.noSepAtStart(prefix)) : "";
String prefixFormat = StringUtils.substringBefore(toFormat, "**");
String postfix = Utils.getParentDirectory(StringUtils.substringAfter(toFormat, "**"));
String prefix = prefixFormat.length() > 1 && file.getPath().contains(prefixFormat) ? StringUtils.substringBefore(fileParent, Utils.noSepAtStart(prefixFormat)) : "";
String doubleAsterisks =
StringUtils.removeStart(Utils.noSepAtStart(StringUtils.removeStart(fileParent, prefix)), Utils.noSepAtEnd(Utils.noSepAtStart(StringUtils.substringBefore(toFormat, "**"))));
StringUtils.removeStart(Utils.noSepAtStart(StringUtils.removeStart(fileParent, prefix)), Utils.noSepAtEnd(Utils.noSepAtStart(prefixFormat)));
doubleAsterisks = postfix.length() > 1 ? StringUtils.removeEnd(doubleAsterisks, Utils.noSepAtEnd(postfix)) : doubleAsterisks;
toFormat = toFormat.replace("**", doubleAsterisks);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,42 @@ public void testDest() throws IOException {
verifyNoMoreInteractions(files);
}

@Test
public void testDest_doubleAsterisks() throws IOException {
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean(
Utils.normalizePath("/values/**/res/strings.xml"), Utils.normalizePath("/values/**/%two_letters_code%/%original_file_name%"),
null, Utils.normalizePath("/common/**/%original_file_name%"))
.setBasePath(project.getBasePath())
.setPreserveHierarchy(true)
.build();

ProjectClient client = mock(ProjectClient.class);
when(client.downloadFullProject(null))
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addDirectory("common", 201L, null, null)
.addDirectory("values", 202L, 201L, null)
.addDirectory("first", 203L, 202L, null)
.addDirectory("second", 204L, 203L, null)
.addFile("strings.xml", "gettext", 101L, 204L, null, Utils.normalizePath("/values/first/second/%two_letters_code%/%original_file_name%")).build());
URL urlMock = MockitoUtils.getMockUrl(getClass());
when(client.downloadFile(eq(101L)))
.thenReturn(urlMock);

FilesInterface files = mock(FilesInterface.class);

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadSourcesAction(files, false, false, null, true, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject(null);
verify(client).downloadFile(eq(101L));
verifyNoMoreInteractions(client);

verify(files).writeToFile(eq(Utils.joinPaths(project.getBasePath(), "values/first/second/res/strings.xml")), any());
verifyNoMoreInteractions(files);
}

@Test
public void testDestAndUnaryAsterisk() throws IOException {
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ static Stream<Arguments> testMainFunctionality() {
new File[] {new File("resources/messages.xml")},
Utils.normalizePath("/**/%two_letters_code%_%original_file_name%"),
new String[] {Utils.normalizePath("resources/en_messages.xml")}
),
arguments(// How to treat double asterisks in the middle
new Language[] {LanguageBuilder.ENG.build()},
new Language[] {LanguageBuilder.ENG.build()},
new File[] {new File("resources/main/settings/default/messages.xml")},
Utils.normalizePath("app/**/default/%two_letters_code%_%original_file_name%"),
new String[] {Utils.normalizePath("app/resources/main/settings/default/en_messages.xml")}
)
);
}
Expand Down

0 comments on commit aa99d5e

Please sign in to comment.