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: correct detection of obsolete files with double asterisk pattern #794

Merged
merged 12 commits into from
May 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ public static Map<String, File> findObsoleteProjectFiles(
ProjectFilesUtils.isProjectFilePathSatisfiesPatterns(sourcePattern, ignorePatterns, preserveHierarchy);
return projectFiles.entrySet().stream()
.filter(entry -> patternCheck.test(entry.getKey()))
.filter(entry -> checkExportPattern(exportPattern, entry.getValue()))
.filter(entry -> checkExportPattern(exportPattern, entry.getValue(), preserveHierarchy))
.filter(entry -> isFileNotInList(filesToUpload, entry.getKey(), preserveHierarchy))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

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

Predicate<String> patternPred = ProjectFilesUtils.isProjectFilePathSatisfiesPatterns(exportPattern, Collections.emptyList(), preserveHierarchy);
return patternPred.test(Utils.noSepAtStart(Utils.normalizePath(fileExportPattern)));
}

public static SortedMap<String, Long> findObsoleteProjectDirectories(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.crowdin.cli.commands.functionality;

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

Expand All @@ -13,29 +14,70 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ObsoleteSourcesUtilsTest {
private static final Long projectId = 602L;

private static final Long fileId1 = 42L;
private static final Long fileId2 = 43L;
private static final Long directoryId1 = 11L;

// Issue: https://github.com/crowdin/crowdin-cli/issues/775
@Test
public void testFindObsoleteProjectFiles() {
public void testFindObsoleteProjectFileWithNullishExportPattern() {
String projectFile1Path = Utils.normalizePath("test/en/file1.md");
String projectFile2Path = Utils.normalizePath("test/en/file2.md");

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());
put(Utils.normalizePath(projectFile1Path), FileBuilder.standard()
.setProjectId(projectId)
.setIdentifiers("file1.md", "md", fileId1, directoryId1, null)
.setExportPattern(null)
.build());
put(Utils.normalizePath(projectFile2Path), FileBuilder.standard()
.setProjectId(projectId)
.setIdentifiers("file2.md", "md", fileId2, directoryId1, null)
.setExportPattern(null)
.build());
}
};
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");
List<String> filesToUpload = Arrays.asList(Utils.normalizePath(projectFile1Path));
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("**/.*"));
List<String> ignorePatterns = Arrays.asList();

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")));
assertEquals(true, obsoleteFiles.containsKey(Utils.normalizePath(projectFile2Path)));
}

// Issue: https://github.com/crowdin/crowdin-cli/issues/790
@Test
public void testFindObsoleteProjectFileWithDoubleAsteriskExportPattern() {
String projectFilePath = Utils.normalizePath("someFeature/sources/file1.strings");
Map<String, File> projectFilesFromApi = new HashMap<String, File>() {
{
put(projectFilePath, FileBuilder.standard()
.setProjectId(projectId)
.setIdentifiers("file1.strings", "strings", fileId1, directoryId1, null)
.setExportPattern("/someFeature/translations/%osx_locale%/%original_file_name%")
.build());
}
};
boolean preserveHierarchy = true;
List<String> filesToUpload = Arrays.asList();
String sourcePattern = Utils.normalizePath("/**/sources/*.strings");
String exportPattern = Utils.normalizePath("/**/translations/%osx_locale%/%original_file_name%");
List<String> ignorePatterns = Arrays.asList();

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

assertEquals(1, obsoleteFiles.size());
assertEquals(true, obsoleteFiles.containsKey(projectFilePath));
}
}
Loading