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: translations download without preserve hierarchy #643

Merged
merged 2 commits into from
Sep 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private Map<String, String> getFiles(

return this.doTranslationMapping(
forLanguages, fb.getDest(), fb.getTranslation(), serverLanguageMapping, languageMapping,
translationReplace, sources, fb.getSource(), basePath, placeholderUtil);
translationReplace, sources, fb.getSource(), basePath, placeholderUtil, preserveHierarchy);
}

private ProjectBuild buildTranslation(ProjectClient client, BuildProjectTranslationRequest request) {
Expand Down Expand Up @@ -495,7 +495,8 @@ private Map<String, String> doTranslationMapping(
List<String> sources,
String source,
String basePath,
PlaceholderUtil placeholderUtil
PlaceholderUtil placeholderUtil,
boolean preserveHierarchy
) {
Map<String, String> mapping = new HashMap<>();

Expand All @@ -505,6 +506,9 @@ private Map<String, String> doTranslationMapping(

String translationProject1 = placeholderUtil.replaceLanguageDependentPlaceholders(translation, projLanguageMapping, language);
String translationFile1 = placeholderUtil.replaceLanguageDependentPlaceholders(translation, languageMapping, language);
if (!preserveHierarchy) {
translationProject1 = StringUtils.remove(translationProject1, PlaceholderUtil.PLACEHOLDER_ORIGINAL_PATH);
}

for (String projectFile : sources) {
String file = StringUtils.removeStart(projectFile, basePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,62 @@ public void testProjectOneFittingFile_WithLanguageMapping() throws IOException {
verifyNoMoreInteractions(files);
}

@Test
public void testProjectOneFittingFile_UploadedWithoutHierarchy() throws IOException {
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("files" + Utils.PATH_SEPARATOR + "*", Utils.PATH_SEPARATOR + "%original_path%" + Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();

project.addFile("files" + Utils.PATH_SEPARATOR + "first.po");

ProjectClient client = mock(ProjectClient.class);
when(client.downloadFullProject(null))
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addFile("first.po", "gettext", 101L, null, null, "/%original_file_name%-CR-%locale%").build());
CrowdinTranslationCreateProjectBuildForm buildProjectTranslationRequest = new CrowdinTranslationCreateProjectBuildForm();
long buildId = 42L;
when(client.startBuildingTranslation(eq(buildProjectTranslationRequest)))
.thenReturn(buildProjectBuild(buildId, Long.parseLong(pb.getProjectId()), "finished", 100));
URL urlMock = MockitoUtils.getMockUrl(getClass());
when(client.downloadBuild(eq(buildId)))
.thenReturn(urlMock);

FilesInterface files = mock(FilesInterface.class);
AtomicReference<File> zipArchive = new AtomicReference<>();
AtomicReference<File> tempDir = new AtomicReference<>();
when(files.extractZipArchive(any(), any()))
.thenAnswer((invocation -> {
zipArchive.set(invocation.getArgument(0));
tempDir.set(invocation.getArgument(1));
return new ArrayList<File>() {{
add(new File(tempDir.get().getAbsolutePath() + Utils.PATH_SEPARATOR + "first.po-CR-uk-UA"));
add(new File(tempDir.get().getAbsolutePath() + Utils.PATH_SEPARATOR + "first.po-CR-ru-RU"));
}};
}));

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

verify(client).downloadFullProject(null);
verify(client).startBuildingTranslation(eq(buildProjectTranslationRequest));
verify(client).downloadBuild(eq(buildId));
verifyNoMoreInteractions(client);

verify(files).writeToFile(any(), any());
verify(files).extractZipArchive(any(), any());
verify(files).copyFile(
new File(tempDir.get().getAbsolutePath() + Utils.PATH_SEPARATOR + "first.po-CR-ru-RU"),
new File(pb.getBasePath() + "files/first.po-CR-ru-RU"));
verify(files).copyFile(
new File(tempDir.get().getAbsolutePath() + Utils.PATH_SEPARATOR + "first.po-CR-uk-UA"),
new File(pb.getBasePath() + "files/first.po-CR-uk-UA"));
verify(files).deleteFile(eq(zipArchive.get()));
verify(files).deleteDirectory(tempDir.get());
verifyNoMoreInteractions(files);
}

@Test
public void testProjectOneFittingFile_FailBuild() {
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
Expand Down