我正在更新一个groovy脚本,在SoapUI中构建一个测试运行器套件。 脚本扫描文件夹并为每个文件夹中的每个文件添加测试用例。
我试图解决的问题是脚本使用的是eachFileMatch() ,它在Windows和Unix文件系统之间没有一致的行为。 我需要更新脚本,以便按字母顺序列出文件。
我对groovy很新,所以我不知道从哪里开始。 看到sort()方法确实存在,但我不确定我是否可以在eachFileMatch上使用它。
这是我需要调整的代码片段:
new File(projectPathTest+"/nord").eachDir{dir-> log.info("Dir > "+dir); operation = dir.name def wsdlTestCase = testSuite.addNewTestCase( operation ) wsdlTestCase.setFailOnError(false) dir.eachFileMatch(~/.*_request\.xml/){file-> // Need Alphabetically sorting here log.info("File >> "+file) addTestStep(operation, file, wsdlTestCase, projectPath, endPoint) }这样做的任何出发点都会非常感谢groovy方法,因为现在我没有时间深入研究groovy API。
问候 }
I am updating a groovy script which build a test runner suite inside SoapUI. The scripts scan a folders and add test case for each file in each folders.
The problem I am trying to solve is that the script is using eachFileMatch() which does not have a consistent behavior between Windows and Unix file systems. I need to update the script so that files are listed alphabetically.
I am quite new to groovy so I don't know where to get started. Saw that a sort() method does exist but I am not sure I can use it on eachFileMatch.
Here's the code snippet I need to adapt :
new File(projectPathTest+"/nord").eachDir{dir-> log.info("Dir > "+dir); operation = dir.name def wsdlTestCase = testSuite.addNewTestCase( operation ) wsdlTestCase.setFailOnError(false) dir.eachFileMatch(~/.*_request\.xml/){file-> // Need Alphabetically sorting here log.info("File >> "+file) addTestStep(operation, file, wsdlTestCase, projectPath, endPoint) }Any starting point to do it will the groovy approach would be really appreciated as for now i don't have time to delve into the groovy API.
Regards }
最满意答案
您可以在TreeSet收集文件,这些文件将维护其排序顺序,然后遍历集合以显示或以其他方式处理您的文件集合。
TreeSet<File> files = new TreeSet<File>() dir.eachFileMatch(~/.*_request\.xml/){file-> files << file } files.each { f-> // log or do whatever with the files in order }如果您需要对所有文件进行此排序,则需要将TreeSet放在eachDir闭包之外。
You can collect the files in a TreeSet which will maintain their sort order and then iterate over the set to display or otherwise act on your collection of files.
TreeSet<File> files = new TreeSet<File>() dir.eachFileMatch(~/.*_request\.xml/){file-> files << file } files.each { f-> // log or do whatever with the files in order }If you need this ordering over all the files, you will need to put the TreeSet outside of the eachDir closure.
更多推荐
发布评论