Skip to content

Commit

Permalink
Closes #816 - Fixing bug in the revisionaccess' file listing (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe authored Jul 8, 2020
1 parent 592354a commit 3174f0c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ protected List<FileInfo> listFiles(String path) {
}
}

return collectFiles(treeWalk);
ArrayList<FileInfo> files = new ArrayList<>();
collectFiles(treeWalk, files);
return files;
} catch (IOException e) {
log.error("Exception while listing files in path '{}'.", path, e);
return Collections.emptyList();
Expand All @@ -146,34 +148,41 @@ protected List<FileInfo> 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<FileInfo> collectFiles(TreeWalk treeWalk) throws IOException {
List<FileInfo> resultList = new ArrayList<>();
private boolean collectFiles(TreeWalk treeWalk, List<FileInfo> 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<FileInfo> nestedFiles = collectFiles(treeWalk);

List<FileInfo> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<FileInfo> 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<FileInfo> subChildren = fileInfo.getChildren();

assertThat(subChildren).hasOnlyOneElementSatisfying(subFileInfo -> {
assertThat(subFileInfo.getName()).isEqualTo("deep");
assertThat(subFileInfo.getType()).isEqualTo(FileInfo.Type.DIRECTORY);

List<FileInfo> 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
Expand Down

0 comments on commit 3174f0c

Please sign in to comment.