package gw.vark;

import gw.util.ProcessStarter;
import gw.util.Shell;
import gw.util.ShellProcess;
import gw.util.StreamUtil;
import gw.vark.testapi.AardvarkTestCase;
import org.apache.tools.ant.launch.Locator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 */
public class AardvarkShellTest extends AardvarkTestCase {

  private File _testDir;
  private ShellProcess _proc;

  @Override
  public void setUp() throws Exception {
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    _testDir = new File(tmpDir, getClass().getSimpleName());
    deleteRecursively(_testDir);
    _testDir.mkdir();
    File varkFile = new File(_testDir, "build.vark");
    writeToFile(varkFile, "" +
            "function hello() {\n" +
            "  print(\"Hello World\")\n" +
            "}\n");

    String javaCommand = System.getProperty("java.home") + "/bin/java";
    String classpathString = Locator.getClassSource(gw.vark.launch.Launcher.class).getPath()
            + File.pathSeparator
            + Locator.getClassSource(org.apache.tools.ant.launch.Launcher.class).getPath();
    String command = javaCommand
            + " -Daardvark.dev=true"
            + " -cp " + classpathString + " gw.vark.launch.Launcher"
            + " -f " + varkFile
            + " -i";
    ProcessStarter processStarter = Shell.buildProcess(command);
    Process process = processStarter.start();
    _proc = new ShellProcess(process);
    readFromProcess();
  }

  @Override
  public void tearDown() throws Exception {
    writeToProcessAndRead("quit\n");
    deleteRecursively(_testDir);
  }

  public void testHello() {
    String read = writeToProcessAndRead("hello\n");
    assertThat(read).contains("hello:\nHello World\n");
  }

  private String writeToProcessAndRead(String write) {
    writeToProcess(write);
    return readFromProcess();
  }

  private void writeToProcess(String write) {
    _proc.write(write);
    System.out.print(write);
  }

  private String readFromProcess() {
    String read = _proc.readUntil("vark> ", true);
    System.out.print(read);
    return read;
  }

  private void writeToFile(File file, String content) throws IOException {
    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(new FileWriter(file));
      writer.write(content);
    } finally {
      try {
        StreamUtil.close(writer);
      }
      catch (IOException closeException) {
        closeException.printStackTrace();
      }
    }
  }

  private static void deleteRecursively(File file) {
    if (file.isDirectory()) {
      for (File child : file.listFiles()) {
        deleteRecursively(child);
      }
    }
    file.delete();
  }
}

@@ -151,24 +151,26 @@
 Aardvark
-TestCase
+Assertions
 ;%0Aimport
@@ -206,16 +206,98 @@
 Locator;
+%0Aimport org.junit.AfterClass;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Test;
 %0A%0Aimport
@@ -457,16 +457,18 @@
 vark
-TestCase
+Assertions
  %7B%0A%0A
@@ -477,16 +477,23 @@
 private 
+static 
 File _te
@@ -509,16 +509,23 @@
 private 
+static 
 ShellPro
@@ -536,39 +536,49 @@
  _proc;%0A%0A  @
-Override%0A  publ
+BeforeClass%0A  public stat
 ic void setU
@@ -702,18 +702,31 @@
 ir, 
-getC
+AardvarkShellTest.c
 lass
-()
 .get
@@ -1648,23 +1648,32 @@
 %0A  @
-Override%0A  publ
+AfterClass%0A  public stat
 ic v
@@ -1781,16 +1781,24 @@
 );%0A  %7D%0A%0A
+  @Test%0A
   public
@@ -1921,16 +1921,178 @@
  World%5Cn
+%5CnBUILD SUCCESSFUL%5CnTotal time: %22);%0A  %7D%0A%0A  @Test%0A  public void testBlankLine() %7B%0A    String read = writeToProcessAndRead(%22%5Cn%22);%0A    assertThat(read).isEqualTo(%22%5Cn
 %22);%0A  %7D%0A
@@ -2098,24 +2098,31 @@
 %0A%0A  private 
+static 
 String write
@@ -2218,32 +2218,39 @@
 %0A  %7D%0A%0A  private 
+static 
 void writeToProc
@@ -2337,16 +2337,23 @@
 private 
+static 
 String r
@@ -2415,19 +2415,20 @@
 ark%3E %22, 
-tru
+fals
 e);%0A    
@@ -2483,16 +2483,23 @@
 private 
+static 
 void wri

add a shell test