From 3174f0c7cfcb03f69b39d89acf6d9821172c7f44 Mon Sep 17 00:00:00 2001 From: Marius Oehler Date: Wed, 8 Jul 2020 11:11:30 +0200 Subject: [PATCH] Closes #816 - Fixing bug in the revisionaccess' file listing (#817) --- .../file/accessor/git/RevisionAccess.java | 29 ++++++++---- .../accessor/git/RevisionAccessIntTest.java | 47 ++++++++++++++++++- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccess.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccess.java index 4b6d1858c4..ffb9cd3c94 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccess.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccess.java @@ -132,7 +132,9 @@ protected List listFiles(String path) { } } - return collectFiles(treeWalk); + ArrayList files = new ArrayList<>(); + collectFiles(treeWalk, files); + return files; } catch (IOException e) { log.error("Exception while listing files in path '{}'.", path, e); return Collections.emptyList(); @@ -146,34 +148,41 @@ protected List listFiles(String path) { /** * Collects the files within the current path of the given {@link TreeWalk}. * - * @param treeWalk The {@link TreeWalk} to traverse. + * @param treeWalk The {@link TreeWalk} to traverse. + * @param resultList the list which will be filled with the found files * @return The files within the current tree. * @throws IOException in case the repository cannot be read */ - private List collectFiles(TreeWalk treeWalk) throws IOException { - List resultList = new ArrayList<>(); + private boolean collectFiles(TreeWalk treeWalk, List resultList) throws IOException { + int initialDepth = treeWalk.getDepth(); + boolean hasNext; do { String name = treeWalk.getNameString(); - FileInfo.FileInfoBuilder fileBuilder = FileInfo.builder().name(name); if (treeWalk.isSubtree()) { treeWalk.enterSubtree(); treeWalk.next(); - List nestedFiles = collectFiles(treeWalk); + + List nestedFiles = new ArrayList<>(); + hasNext = collectFiles(treeWalk, nestedFiles); fileBuilder .type(FileInfo.Type.DIRECTORY) .children(nestedFiles); } else { fileBuilder.type(FileInfo.Type.FILE); + hasNext = treeWalk.next(); } - FileInfo fileInfo = fileBuilder.build(); - resultList.add(fileInfo); - } while (treeWalk.next()); + resultList.add(fileBuilder.build()); + + if (hasNext && initialDepth != treeWalk.getDepth()) { + return true; + } + } while (hasNext); - return resultList; + return false; } } diff --git a/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccessIntTest.java b/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccessIntTest.java index b4249d9f26..9e389bbee1 100644 --- a/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccessIntTest.java +++ b/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/file/accessor/git/RevisionAccessIntTest.java @@ -18,7 +18,8 @@ import java.util.List; import java.util.Optional; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -100,6 +101,50 @@ public void listFiles() { }); }); } + + @Test + public void listFiles_Deep() throws GitAPIException { + createTestFiles("files/sub_a/deep/file_x.yml=x", "files/z_file_z.yml"); + versioningManager.commitAllChanges("third"); + + List result = versioningManager.getWorkspaceRevision().listConfigurationFiles(""); + + assertThat(result).hasSize(4); + assertThat(result).anySatisfy(fileInfo -> { + assertThat(fileInfo.getName()).isEqualTo("file_a.yml"); + assertThat(fileInfo.getType()).isEqualTo(FileInfo.Type.FILE); + assertThat(fileInfo.getChildren()).isNull(); + }); + assertThat(result).anySatisfy(fileInfo -> { + assertThat(fileInfo.getName()).isEqualTo("file_b.yml"); + assertThat(fileInfo.getType()).isEqualTo(FileInfo.Type.FILE); + assertThat(fileInfo.getChildren()).isNull(); + }); + assertThat(result).anySatisfy(fileInfo -> { + assertThat(fileInfo.getName()).isEqualTo("sub_a"); + assertThat(fileInfo.getType()).isEqualTo(FileInfo.Type.DIRECTORY); + + List subChildren = fileInfo.getChildren(); + + assertThat(subChildren).hasOnlyOneElementSatisfying(subFileInfo -> { + assertThat(subFileInfo.getName()).isEqualTo("deep"); + assertThat(subFileInfo.getType()).isEqualTo(FileInfo.Type.DIRECTORY); + + List deepSubChildren = subFileInfo.getChildren(); + + assertThat(deepSubChildren).hasOnlyOneElementSatisfying(deepSubFileInfo -> { + assertThat(deepSubFileInfo.getName()).isEqualTo("file_x.yml"); + assertThat(deepSubFileInfo.getType()).isEqualTo(FileInfo.Type.FILE); + assertThat(deepSubFileInfo.getChildren()).isNull(); + }); + }); + }); + assertThat(result).anySatisfy(fileInfo -> { + assertThat(fileInfo.getName()).isEqualTo("z_file_z.yml"); + assertThat(fileInfo.getType()).isEqualTo(FileInfo.Type.FILE); + assertThat(fileInfo.getChildren()).isNull(); + }); + } } @Nested