package org.approvaltests.reporters;

import java.io.IOException;

import com.spun.util.StringUtils;
import com.spun.util.io.FileUtils;
import com.spun.util.logger.SimpleLogger;
import org.approvaltests.core.ApprovalFailureReporter;

import com.spun.util.ObjectUtils;

public class FileCaptureReporter implements ApprovalFailureReporter
{
  private String message = "*** adding received file via FileCaptureReporter for further inspection";
  public FileCaptureReporter()
  {
  }
  public FileCaptureReporter(String message)
  {
    this.message = message;
  }
  @Override
  public void report(String received, String approved)
  {
    run("git add --force " + received);
    run(String.format("git commit -m '%s'", message));
    run("git push");
  }
  private void run(String command)
  {
    SimpleLogger.event(command);
    try
    {
      Process process = Runtime.getRuntime().exec(command);
      process.waitFor();
      final int exitValue = process.exitValue();
      if (exitValue != 0)
      {
        final String stdout = FileUtils.readStream(process.getInputStream());
        final String stderr = FileUtils.readStream(process.getErrorStream());
        SimpleLogger.warning(String.format("stdout:\n%s\nstderr:\n%s", stdout, stderr));
      }
    }
    catch (IOException | InterruptedException e)
    {
      throw ObjectUtils.throwAsError(e);
    }
  }
}

@@ -63,119 +63,33 @@
 on;%0A
-%0A
 import 
-com.spun.util.StringUtils;%0Aimport com.spun.util.io.FileUtils;%0Aimport com.spun.util.logger.SimpleLogger;
+java.util.Arrays;%0A
 %0Aimp
@@ -174,16 +174,93 @@
 ctUtils;
+%0Aimport com.spun.util.io.FileUtils;%0Aimport com.spun.util.logger.SimpleLogger;
 %0A%0Apublic
@@ -632,24 +632,28 @@
 %22git
- add 
+%22, %22add%22, %22
 --force
- %22 +
+%22,
  rec
@@ -672,51 +672,44 @@
 run(
-String.format(%22git commit -m '%25s'%22,
+%22git%22, %22commit%22, %22-m%22, %22'%22 +
  message
 ));%0A
@@ -704,17 +704,22 @@
  message
-)
+ + %22'%22
 );%0A    r
@@ -725,17 +725,20 @@
 run(%22git
- 
+%22, %22
 push%22);%0A
@@ -766,16 +766,19 @@
 n(String
+...
  command
@@ -806,24 +806,40 @@
 r.event(
+Arrays.toString(
 command)
 ;%0A    tr
@@ -826,24 +826,25 @@
 ing(command)
+)
 ;%0A    try%0A  

B!! execute commands with spaces properly