如何在 groovy(Spock) 中测试创建的和预期的文件树? 现在,我正在使用 Set,在其中指定我希望获得的路径并以这种方式收集实际路径:
Set<String> getCreatedFilePaths(String root) {
Set<String> createFilePaths = new HashSet<>()
new File(root).eachFileRecurse {
createFilePaths << it.absolutePath
}
return createFilePaths
}
但是测试的可读性不是很好。 是否有可能在 groovy 中将预期路径写成树,然后与实际路径进行比较
例如,预期:
region:
usa:
new_york.json
california.json
europe:
spain.json
italy.json
而actual会被转换成这种树。
最佳答案
不确定是否可以使用内置的递归方法来完成。当然有一些功能强大的,但这是您可以使用的标准递归代码:
def path = new File("/Users/me/Downloads")
def printTree(File file, Integer level) {
println " " * level + "${file.name}:"
file.eachFile {
println " " * (level + 1) + it.name
}
file.eachDir {
printTree(it, level + 1)
}
}
printTree(path, 1)
打印你描述的格式
关于testing - 在 groovy(Spock) 中测试文件结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51710225/