From 7050407d0f6eafe19f0311b7f132bb598140572f Mon Sep 17 00:00:00 2001 From: Yevheniy Oliynyk Date: Mon, 25 Nov 2024 20:19:25 +0100 Subject: [PATCH] fix: file lookup w/o extension --- .../commands/actions/FileUploadAction.java | 2 +- .../commands/actions/UploadSourcesAction.java | 2 +- .../actions/UploadTranslationsAction.java | 5 +++-- .../functionality/ProjectFilesUtils.java | 22 +++++++++++++++++++ .../functionality/ProjectFilesUtilsTest.java | 15 ++++++++++--- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/crowdin/cli/commands/actions/FileUploadAction.java b/src/main/java/com/crowdin/cli/commands/actions/FileUploadAction.java index 3362e9c1b..ba8fee35e 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/FileUploadAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/FileUploadAction.java @@ -83,7 +83,7 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien fileFullPath = (nonNull(branchName) ? branchName + Utils.PATH_SEPARATOR : "") + filePath; fileDestName = fileFullPath.substring(fileFullPath.lastIndexOf(Utils.PATH_SEPARATOR) + 1); Map paths = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos()); - FileInfo projectFile = paths.get(fileFullPath); + FileInfo projectFile = ProjectFilesUtils.fileLookup(fileFullPath, paths); if (nonNull(projectFile)) { if (!autoUpdate) { diff --git a/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java b/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java index 05da1b208..e96a7b156 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java @@ -180,7 +180,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) { uploadedSources.add(fileFullPath); } - FileInfo projectFile = !isStringsBasedProject ? finalPaths.get(fileFullPath) : null; + FileInfo projectFile = !isStringsBasedProject ? ProjectFilesUtils.fileLookup(fileFullPath, finalPaths) : null; if (!isStringsBasedProject && autoUpdate && projectFile != null) { final UpdateFileRequest request = new UpdateFileRequest(); request.setExportOptions(buildExportOptions(sourceFile, file, pb.getBasePath())); diff --git a/src/main/java/com/crowdin/cli/commands/actions/UploadTranslationsAction.java b/src/main/java/com/crowdin/cli/commands/actions/UploadTranslationsAction.java index 64d5807c7..c962b6996 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/UploadTranslationsAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/UploadTranslationsAction.java @@ -112,7 +112,8 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) { ? PropertiesBeanUtils.prepareDest(file.getDest(), StringUtils.removeStart(source, pb.getBasePath()), placeholderUtil) : StringUtils.removeStart(source, pb.getBasePath() + commonPath)); - if (!paths.containsKey(filePath)) { + var sourceFile = ProjectFilesUtils.fileLookup(filePath, paths); + if (sourceFile == null) { containsErrors.set(true); if (!plainView) { out.println(ERROR.withIcon(String.format( @@ -121,7 +122,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) { } return; } - Long fileId = paths.get(filePath).getId(); + Long fileId = sourceFile.getId(); // build filePath to each source and project language String fileSource = StringUtils.removeStart(source, pb.getBasePath()); diff --git a/src/main/java/com/crowdin/cli/commands/functionality/ProjectFilesUtils.java b/src/main/java/com/crowdin/cli/commands/functionality/ProjectFilesUtils.java index 9077d2780..6bec3bb93 100644 --- a/src/main/java/com/crowdin/cli/commands/functionality/ProjectFilesUtils.java +++ b/src/main/java/com/crowdin/cli/commands/functionality/ProjectFilesUtils.java @@ -35,6 +35,28 @@ public static Map buildFilePaths( return filePathsToId; } + public static T fileLookup(String filePath, Map files) { + if (files.containsKey(filePath)) { + return files.get(filePath); + } + + for (var entry : files.entrySet()) { + if (ProjectFilesUtils.equalsIgnoreExtension(filePath, entry.getKey())) { + return entry.getValue(); + } + } + + return null; + } + + public static boolean equalsIgnoreExtension(String file1, String file2) { + return removeExtension(file1).equals(removeExtension(file2)); + } + + public static String removeExtension(String file) { + return file.substring(0, file.lastIndexOf('.')); + } + public static Map buildFilePaths( Map directories, List files ) { diff --git a/src/test/java/com/crowdin/cli/commands/functionality/ProjectFilesUtilsTest.java b/src/test/java/com/crowdin/cli/commands/functionality/ProjectFilesUtilsTest.java index e6198d223..9d098c48c 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/ProjectFilesUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/ProjectFilesUtilsTest.java @@ -13,11 +13,10 @@ import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.params.provider.Arguments.arguments; public class ProjectFilesUtilsTest { @@ -77,4 +76,14 @@ private static Stream isProjectFileSatisfiesThePatternsTest() { ); } + @Test + public void testFileLookup() { + String file1 = "path/to/file.json"; + String file2 = "path/to/file.txt"; + String file3 = "path/to/test.txt"; + + assertEquals(file1, ProjectFilesUtils.fileLookup(file1, Map.of(file1, file1, file2, file2))); + assertEquals(file2, ProjectFilesUtils.fileLookup(file1, Map.of(file2, file2, file3, file3))); + assertNull(ProjectFilesUtils.fileLookup(file3, Map.of(file1, file1, file2, file2))); + } }