Skip to content

Commit

Permalink
fix: translations download without preserve hierarchy (#643)
Browse files Browse the repository at this point in the history
* fix: translations download without preserve hierarchy
  • Loading branch information
katerina20 committed Sep 28, 2023
1 parent 4ff1f55 commit 4099eea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
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

0 comments on commit 4099eea

Please sign in to comment.