Skip to content

Commit

Permalink
fix: file lookup w/o extension (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Dec 4, 2024
1 parent 53f4acd commit e68bec0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, FileInfo> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ public static <T extends FileInfo> Map<String, T> buildFilePaths(
return filePathsToId;
}

public static <T> T fileLookup(String filePath, Map<String, T> 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 <T extends FileInfo> Map<String, T> buildFilePaths(
Map<Long, Directory> directories, List<T> files
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -77,4 +76,14 @@ private static Stream<Arguments> 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)));
}
}

0 comments on commit e68bec0

Please sign in to comment.