/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.util;

import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import com.facebook.buck.util.environment.Platform;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import java.nio.CharBuffer;
import org.junit.Test;

/** Tests for {@link SimpleProcessListener}. */
public class SimpleProcessListenerTest {

  @Test
  public void echoTextReceivedOnStdout() throws Exception {
    ListeningProcessExecutor executor = new ListeningProcessExecutor();
    SimpleProcessListener listener = new SimpleProcessListener();
    ProcessExecutorParams params;
    if (Platform.detect() == Platform.WINDOWS) {
      params = ProcessExecutorParams.ofCommand("cmd.exe", "/c", "echo", "Hello");
    } else {
      params = ProcessExecutorParams.ofCommand("echo", "Hello");
    }
    ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener);
    int returnCode = executor.waitForProcess(process);
    assertThat(returnCode, equalTo(0));
    assertThat(listener.getStdout(), equalTo(String.format("Hello%n")));
    assertThat(listener.getStderr(), is(emptyString()));
  }

  @Test
  public void catTextSentToStdinReceivedOnStdout() throws Exception {
    ProcessExecutorParams params;
    if (Platform.detect() == Platform.WINDOWS) {
      params =
          ProcessExecutorParams.ofCommand(
              "python", "-c", "import sys, shutil; shutil.copyfileobj(sys.stdin, sys.stdout)");
    } else {
      params = ProcessExecutorParams.ofCommand("cat");
    }
    ListeningProcessExecutor executor = new ListeningProcessExecutor();
    SimpleProcessListener listener = new SimpleProcessListener(String.format("Meow%n"));
    ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener);
    process.wantWrite();
    int returnCode = executor.waitForProcess(process);
    assertThat(returnCode, equalTo(0));
    assertThat(listener.getStdout(), equalTo(String.format("Meow%n")));
    assertThat(listener.getStderr(), is(emptyString()));
  }

  @Test
  public void supplierBasedInput() throws Exception {
    ProcessExecutorParams params;
    if (Platform.detect() == Platform.WINDOWS) {
      params =
          ProcessExecutorParams.ofCommand(
              "python", "-c", "import sys, shutil; shutil.copyfileobj(sys.stdin, sys.stdout)");
    } else {
      params = ProcessExecutorParams.ofCommand("cat");
    }
    ListeningProcessExecutor executor = new ListeningProcessExecutor();
    SimpleProcessListener listener =
        new SimpleProcessListener(
            ImmutableList.of(CharBuffer.wrap("Meow"), CharBuffer.wrap("Wow")).iterator(),
            Charsets.UTF_8);
    ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener);
    process.wantWrite();
    int returnCode = executor.waitForProcess(process);
    assertThat(returnCode, equalTo(0));
    assertThat(listener.getStdout(), equalTo("MeowWow"));
    assertThat(listener.getStderr(), is(emptyString()));
  }
}

@@ -951,16 +951,68 @@
 leList;%0A
+import com.google.common.collect.ImmutableMultimap;%0A
 import j
@@ -1213,33 +1213,24 @@
 ption %7B%0A    
-Listening
 ProcessExecu
@@ -1236,281 +1236,261 @@
 utor
- executor = new ListeningProcessExecutor();%0A    SimpleProcessListener listener = new SimpleProcessListener();%0A    ProcessExecutorParams params;%0A    if (Platform.detect() == Platform.WINDOWS) %7B%0A      params = ProcessExecutorParams.ofCommand(%22cmd.exe%22, %22/c%22, %22echo%22, 
+Params params = ProcessExecutorParams.ofCommand(%22echo%22, %22Hello%22);%0A    FakeListeningProcessExecutor executor =%0A        new FakeListeningProcessExecutor(%0A            ImmutableMultimap.of(%0A                params, FakeListeningProcessState.ofStdout(
 %22Hello%22)
 ;%0A  
@@ -1489,93 +1489,139 @@
 lo%22)
-;%0A
+,%0A 
     
-%7D else %7B%0A
+    
       
+ 
 params
- = ProcessExecutorParams.ofCommand(%22echo%22, %22Hello%22);%0A    %7D
+, FakeListeningProcessState.ofExit(0)));%0A    SimpleProcessListener listener = new SimpleProcessListener();%0A
 %0A   
@@ -1858,32 +1858,15 @@
 lTo(
-String.format(
 %22Hello
-%25n%22)
+%22
 ));%0A

Fix flaky unit test.