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: file lookup w/o extension #870

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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)));
}
}
Loading