/*
 * Copyright 2014-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.test.cache;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

import com.facebook.buck.testutil.integration.DebuggableTemporaryFolder;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;

public class TestCacheIntegrationTest {

  private ProjectWorkspace workspace;

  @Rule
  public DebuggableTemporaryFolder temporaryFolder = new DebuggableTemporaryFolder();

  @Before
  public void setupWorkspace() throws IOException {
    workspace = TestDataHelper.createProjectWorkspaceForScenario(
        this, "one_test", temporaryFolder);
    workspace.setUp();
  }

  @Test
  public void shouldUseCacheByDefault() throws IOException {
    assertThat("First run should not be cached",
        run("test", "--all"), not(containsString("CACHED")));
    assertThat("Second run should be cached",
        run("test", "--all"), containsString("CACHED"));
  }

  @Test
  public void shouldAvoidCacheWithCommandLineOption() throws IOException {
    assertThat("First run should not be cached",
        run("test", "--all"), not(containsString("CACHED")));
    assertThat("Second run should also not be cached",
        run("test", "--all", "--no-results-cache"), not(containsString("CACHED")));
  }

  @Test
  public void shouldAvoidCacheWithConfigFileOption() throws IOException {
    String newBuckProject = Joiner.on('\n').join(
        "[test]",
        "  use_cached_results = no");
    Files.write(newBuckProject, workspace.getFile(".buckconfig"), Charsets.UTF_8);

    assertThat("First run should not be cached",
        run("test", "--all"), not(containsString("CACHED")));
    assertThat("Second run should also not be cached",
        run("test", "--all", "--no-results-cache"), not(containsString("CACHED")));
  }

  private String run(String... args) throws IOException {
    ProjectWorkspace.ProcessResult result = workspace.runBuckCommand(args);
    result.assertSuccess();
    return result.getStderr();
  }
}

@@ -1114,16 +1114,41 @@
 Before;%0A
+import org.junit.Ignore;%0A
 import o
@@ -1162,16 +1162,16 @@
 t.Rule;%0A
-
 import o
@@ -1582,32 +1582,42 @@
 e.setUp();%0A  %7D%0A%0A
+  @Ignore%0A
   @Test%0A  public
@@ -1880,32 +1880,42 @@
 CACHED%22));%0A  %7D%0A%0A
+  @Ignore%0A
   @Test%0A  public
@@ -2236,16 +2236,26 @@
 );%0A  %7D%0A%0A
+  @Ignore%0A
   @Test%0A

Disable flaky tests.