package com.vackosar.gitflowincrementalbuild.boundary;

import com.vackosar.gitflowincrementalbuild.mocks.RepoTest;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class IT extends RepoTest {

    @Test
    public void buildWithAlsoMake() throws Exception {
        final String output = executeBuild(true);
        System.out.println(output);

        Assert.assertFalse(output.contains(" child1"));
        Assert.assertFalse(output.contains(" child2"));
        Assert.assertFalse(output.contains(" subchild1"));
        Assert.assertFalse(output.contains(" subchild42"));

        Assert.assertTrue(output.contains(" subchild2"));
        Assert.assertTrue(output.contains(" child3"));
        Assert.assertTrue(output.contains(" child4"));
        Assert.assertTrue(output.contains(" subchild41"));
        Assert.assertTrue(output.contains(" child6"));
    }

    @Test
    public void buildWithoutAlsoMake() throws Exception {
        final String output = executeBuild(false);
        System.out.println(output);

        Assert.assertFalse(output.contains(" child1"));
        Assert.assertFalse(output.contains(" child2"));
        Assert.assertFalse(output.contains(" subchild1"));
        Assert.assertFalse(output.contains(" subchild42"));
        Assert.assertFalse(output.contains(" child6"));
        Assert.assertTrue(output.contains("child6 ---\n[INFO] Tests are skipped."));

        Assert.assertTrue(output.contains(" subchild2"));
        Assert.assertTrue(output.contains(" child3"));
        Assert.assertTrue(output.contains(" child4"));
        Assert.assertTrue(output.contains(" subchild41"));
    }

    private String executeBuild(boolean alsoMake) throws IOException, InterruptedException {
        String version = Files.readAllLines(Paths.get("pom.xml")).stream().filter(s -> s.contains("<version>")).findFirst().get().replaceAll("</*version>", "").replaceAll("^[ \t]*", "");
        final Process process =
                new ProcessBuilder("cmd", "/c", "mvn", "install", alsoMake?"-am":"", "--file", "parent\\pom.xml", "-DgibVersion=" + version, "-Dgib.skipDependenciesTest=true")
                        .directory(new File("tmp/repo"))
                        .start();
        String output = convertStreamToString(process.getInputStream());
        System.out.println(convertStreamToString(process.getErrorStream()));
        process.waitFor();
        return output;
    }

    private static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
}

@@ -412,15 +412,705 @@
 ild(
+true, false);%0A        System.out.println(output);%0A%0A        Assert.assertFalse(output.contains(%22 child1%22));%0A        Assert.assertFalse(output.contains(%22 child2%22));%0A        Assert.assertFalse(output.contains(%22 subchild1%22));%0A        Assert.assertFalse(output.contains(%22 subchild42%22));%0A%0A        Assert.assertTrue(output.contains(%22 subchild2%22));%0A        Assert.assertTrue(output.contains(%22 child3%22));%0A        Assert.assertTrue(output.contains(%22 child4%22));%0A        Assert.assertTrue(output.contains(%22 subchild41%22));%0A        Assert.assertTrue(output.contains(%22 child6%22));%0A    %7D%0A%0A    @Test%0A    public void buildWithAlsoMakeSkip() throws Exception %7B%0A        final String output = executeBuild(true, 
 true);%0A
-
     
@@ -1648,32 +1648,105 @@
 ns(%22 child6%22));%0A
+        Assert.assertTrue(output.contains(%22%5BINFO%5D Tests are skipped.%22));%0A
     %7D%0A%0A    @Test
@@ -1847,16 +1847,23 @@
 teBuild(
+false, 
 false);%0A
@@ -2189,93 +2189,8 @@
 %22));
-%0A        Assert.assertTrue(output.contains(%22child6 ---%5Cn%5BINFO%5D Tests are skipped.%22));
 %0A%0A  
@@ -2469,16 +2469,35 @@
 alsoMake
+, Boolean skipTests
 ) throws
@@ -2925,13 +2925,21 @@
 est=
-true%22
+%22 + skipTests
 )%0A  

Fix IT tests.