package com.vackosar.gitflowincrementalbuild;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class MainIT {

    @Test
    public void list() throws Exception {
        RepoMock repoMock = new RepoMock();
        final Process process = execute();
        Assert.assertEquals("child1" + System.lineSeparator(), convertStreamToString(process.getInputStream()));
        Assert.assertEquals("", convertStreamToString(process.getErrorStream()));
        repoMock.close();
    }

    @Test
    public void build() throws Exception {
        RepoMock repoMock = new RepoMock();
        final Process process = execute();
        final String modules = convertStreamToString(process.getInputStream()).replaceAll(System.lineSeparator(), "");
//        Process build = null;
//        try {
//            build = executeBuild(modules);
//        } catch (Exception e) {
//            System.out.println(e);
//        }
//        System.out.println(convertStreamToString(build.getInputStream()));
//        System.out.println(convertStreamToString(build.getErrorStream()));
        Assert.assertEquals("", convertStreamToString(process.getErrorStream()));
        repoMock.close();
    }

    private Process execute() throws IOException, InterruptedException {
        final Process process =
                new ProcessBuilder()
                        .command("java", "-cp", "../../target/*", "com.vackosar.gitflowincrementalbuild.Main", "parent/pom.xml")
                        .directory(new File("tmp/repo"))
                        .start();
        process.waitFor();
        return process;
    }

    private Process executeBuild(String modules) throws IOException, InterruptedException {
        final Process process =
                new ProcessBuilder("mvn", "-pl", modules, "parent/pom.xml")
                        .directory(new File("tmp/repo"))
                        .start();
        process.waitFor();
        return process;
    }

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

@@ -788,26 +788,24 @@
 ), %22%22);%0A
-//
         
 Process 
@@ -800,59 +800,97 @@
     
-Process build = null;%0A//        try %7B%0A//           
+Assert.assertEquals(%22%22, convertStreamToString(process.getErrorStream()));%0A        Process
  bui
@@ -917,26 +917,24 @@
 dules);%0A
-//
         
 %7D catch 
@@ -929,38 +929,77 @@
     
-%7D catch (Exception e) %7B%0A//    
+final String output = convertStreamToString(build.getInputStream());%0A
     
@@ -1025,26 +1025,17 @@
 tln(
-e);%0A//        %7D%0A//
+output);%0A
     
@@ -1080,37 +1080,37 @@
 String(build.get
-Input
+Error
 Stream()));%0A//  
@@ -1109,18 +1109,16 @@
 ));%0A
-//
         
 Syst
@@ -1117,154 +1117,144 @@
     
-System.out.println(convertStreamToString(build.getErrorStream()));%0A        Assert.assertEquals(%22%22, convertStreamToString(process.getErrorStream())
+final String actual = output.split(System.lineSeparator())%5B3%5D;%0A        Assert.assertEquals(%22%5BINFO%5D Building child1 1.0-SNAPSHOT%22, actual
 );%0A 
@@ -1869,46 +1869,52 @@
 er(%22
-mvn%22, %22-pl%22, modules, %22parent/pom.xml%22
+cmd%22, %22/c%22, %22mvn%22, %22compile%22, %22-pl%22, modules
 )%0A  
@@ -1956,32 +1956,39 @@
 w File(%22tmp/repo
+/parent
 %22))%0A            

Implement MainIT.