Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: double asterisks in paths #733

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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