Skip to content

Commit

Permalink
fix: correct detection of obsolete files (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored May 9, 2024
1 parent 2d4d0f0 commit 94fb3bf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ public class ObsoleteSourcesUtils {

public static Map<String, File> findObsoleteProjectFiles(
@NonNull Map<String, File> projectFiles, boolean preserveHierarchy,
@NonNull List<String> filesToUpload, @NonNull String pattern, @NonNull String exportPattern, List<String> ignorePattern
@NonNull List<String> filesToUpload, @NonNull String sourcePattern, @NonNull String exportPattern, List<String> ignorePatterns
) {
Predicate<String> patternCheck =
ProjectFilesUtils.isProjectFilePathSatisfiesPatterns(pattern, ignorePattern, preserveHierarchy);
ProjectFilesUtils.isProjectFilePathSatisfiesPatterns(sourcePattern, ignorePatterns, preserveHierarchy);
return projectFiles.entrySet().stream()
.filter(entry -> patternCheck.test(entry.getKey()) && checkExportPattern(exportPattern, entry.getValue()))
.filter(entry -> isFileDontHaveUpdate(filesToUpload, entry.getKey(), preserveHierarchy))
.filter(entry -> patternCheck.test(entry.getKey()))
.filter(entry -> checkExportPattern(exportPattern, entry.getValue()))
.filter(entry -> isFileNotInList(filesToUpload, entry.getKey(), preserveHierarchy))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private static boolean checkExportPattern(String exportPattern, File file) {
String fileExportPattern = ProjectFilesUtils.getExportPattern(file.getExportOptions());
return exportPattern.equals(fileExportPattern != null ? Utils.normalizePath(fileExportPattern) : null);
if (fileExportPattern == null) {
return true;
}
return exportPattern.equals(Utils.normalizePath(fileExportPattern));
}

public static SortedMap<String, Long> findObsoleteProjectDirectories(
Expand Down Expand Up @@ -71,7 +75,7 @@ public static SortedMap<String, Long> findObsoleteProjectDirectories(
return obsoleteDirs;
}

private static boolean isFileDontHaveUpdate(List<String> filesToUpload, String filePath, boolean preserveHierarchy) {
private static boolean isFileNotInList(List<String> filesToUpload, String filePath, boolean preserveHierarchy) {
String filePathRegex = "^" + (preserveHierarchy ? "" : Utils.PRESERVE_HIERARCHY_REGEX_PART) + Utils.regexPath(filePath) + "$";
return filesToUpload.stream()
.noneMatch(Pattern.compile(filePathRegex).asPredicate());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.crowdin.cli.commands.functionality;

import com.crowdin.cli.utils.Utils;
import com.crowdin.client.sourcefiles.model.File;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ObsoleteSourcesUtilsTest {

@Test
public void testFindObsoleteProjectFiles() {
Map<String, File> projectFilesFromApi = new HashMap<String, File>() {
{
put(Utils.normalizePath("test/en/test.md"), new File());
put(Utils.normalizePath("test/en/support.md"), new File());
put(Utils.normalizePath("test/en/help.md"), new File());
}
};
boolean preserveHierarchy = true;
List<String> filesToUpload = Arrays.asList(Utils.normalizePath("test/en/test.md"),
Utils.normalizePath("test/en/help.md"));
String sourcePattern = Utils.normalizePath("/test/en/*.md");
String exportPattern = Utils.normalizePath("/test/%two_letters_code%/%original_path%/%original_file_name%");
List<String> ignorePatterns = Arrays.asList(Utils.normalizePath("**/.*"));

Map<String, File> obsoleteFiles = ObsoleteSourcesUtils.findObsoleteProjectFiles(projectFilesFromApi,
preserveHierarchy,
filesToUpload, sourcePattern, exportPattern, ignorePatterns);

assertEquals(1, obsoleteFiles.size());
assertEquals(true, obsoleteFiles.containsKey(Utils.normalizePath("test/en/support.md")));
}

}

0 comments on commit 94fb3bf

Please sign in to comment.