package nl.hsac.fitnesse.testrun;

import fitnesse.FitNesseContext;
import fitnesse.testrunner.run.FileBasedTestRunFactory;
import fitnesse.util.partitioner.ListPartitioner;
import fitnesse.wiki.WikiPage;

import java.io.File;
import java.util.List;
import java.util.Optional;

/**
 * Partitions test pages based on the test-results.csv file in the files section,
 * if it is present.
 */
public class TestResultsBasedTestRunFactory extends FileBasedTestRunFactory {
    public static final String SKIP_ARG = "ignoreTestResultsFile";

    public TestResultsBasedTestRunFactory(FitNesseContext context) {
        super(context);
    }

    @Override
    protected ListPartitioner<WikiPage> createPartitioner(File f) {
        List<DurationRecord<String>> durations = new TestResultCsvParser().parse(f);
        return new DurationBasedWikiPagePartitioner(durations);
    }

    @Override
    protected Optional<File> getFile(List<WikiPage> pages) {
        return super.getFile(pages).filter(File::exists);
    }

    @Override
    protected String getFilename(List<WikiPage> pages) {
        return pages.get(0).getVariable(SKIP_ARG) != null ? null : "test-results.csv";
    }
}

@@ -495,20 +495,33 @@
 ing 
-SKIP
+TEST_RESULTS_FILE
 _ARG = %22
 igno
@@ -520,15 +520,9 @@
  = %22
-ignoreT
+t
 estR
@@ -940,32 +940,114 @@
 iPage%3E pages) %7B%0A
+        // if the test results file does not exist this factory cannot make a run%0A
         return s
@@ -1174,22 +1174,37 @@
         
-return
+String explicitFile =
  pages.g
@@ -1225,34 +1225,69 @@
 ble(
-SKIP_ARG) != null ?
+TEST_RESULTS_FILE_ARG);%0A        return explicitFile ==
  null 
-:
+?
  %22te
@@ -1301,16 +1301,31 @@
 lts.csv%22
+ : explicitFile
 ;%0A    %7D%0A

allow different filename for test results csv