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 #776

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -29,7 +29,10 @@ public static Map<String, File> findObsoleteProjectFiles(

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrii-bodnar What do you think of this approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that files with the null export pattern will be considered obsolete and deleted, or the opposite?

Copy link
Contributor Author

@anbraten anbraten May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It basically uses all projectFiles (files received by the api) and checks for each file:

  • if that file matches the config patterns (source-pattern, ignore and preserveHierarchy)
  • if it matches the exportPattern (if the api returns null for the export pattern we ignore it now by this change)
  • and if the file has isFileDontHaveUpdate=true (which seems to check if the file is not part of the local files anymore)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted some var and method names so they match the rest of the code and are hopefully more descriptive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anbraten great, I just tested it locally and the fix works for me, thank you! 🚀

return true;
}
return exportPattern.equals(Utils.normalizePath(fileExportPattern));
}

public static SortedMap<String, Long> findObsoleteProjectDirectories(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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> projectFiles = 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 pattern = "/test/en/*.md";
String exportPattern = "/test/%two_letters_code%/%original_path%/%original_file_name%";
List<String> ignorePattern = Arrays.asList("**/.*");

Map<String, File> obsoleteFiles = ObsoleteSourcesUtils.findObsoleteProjectFiles(projectFiles, preserveHierarchy,
filesToUpload, pattern, exportPattern, ignorePattern);

assertEquals(1, obsoleteFiles.size());
anbraten marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(true, obsoleteFiles.containsKey(Utils.normalizePath("test/en/support.md")));
}

}
Loading