<commit_before>/*
 * Copyright 2013-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.shell;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.util.ExitCode;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;

public class ShBinaryRuleIntegrationTest {

  @Rule public TemporaryPaths temporaryFolder = new TemporaryPaths();

  @Test
  public void testTrivialShBinaryRule() throws IOException {
    // sh_binary is not available on Windows. Ignore this test on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "sh_binary_trivial", temporaryFolder);
    workspace.setUp();

    Path outputFile = workspace.buildAndReturnOutput("//:run_example");

    // Verify contents of example_out.txt
    String output = new String(Files.readAllBytes(outputFile), UTF_8);
    assertEquals("arg1\narg2\n", output);
  }

  @Test
  public void testExecutableOnRebuild() throws IOException {
    // sh_binary is not available on Windows. Ignore this test on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "sh_binary_with_caching", temporaryFolder);
    workspace.setUp();
    workspace.enableDirCache();

    // First build only the sh_binary rule itself.
    ProcessResult buildResult = workspace.runBuckCommand("build", "//:example_sh", "-v", "2");
    buildResult.assertSuccess();

    // Make sure the sh_binary output is executable to begin with.
    String outputPath = "buck-out/gen/__example_sh__/example_sh.sh";
    Path output = workspace.getPath(outputPath);
    assertTrue("Output file should be written to '" + outputPath + "'.", Files.exists(output));
    assertTrue("Output file must be executable.", Files.isExecutable(output));

    // Now delete the buck-out directory (but not buck-cache).
    workspace.runBuckCommand("clean", "--keep-cache");

    // Now run the genrule that depends on the sh_binary above. This will force buck to fetch the
    // sh_binary output from cache. If the executable flag is lost somewhere along the way, this
    // will fail.
    buildResult = workspace.runBuckCommand("build", "//:run_example", "-v", "2");
    buildResult.assertSuccess("Build failed when rerunning sh_binary from cache.");

    // Note that previously we used to verify that the //:example_sh rule was
    // fetched from the cache here.  However, caching for sh_binary() rules has
    // since been disabled.

    // In addition to running the build, explicitly check that the output file is still executable.
    assertTrue(
        "Output file must be retrieved from cache at '" + outputPath + ".", Files.exists(output));
    assertTrue("Output file retrieved from cache must be executable.", Files.isExecutable(output));
  }

  @Test
  public void testShBinaryWithMappedResources() throws IOException {
    List<String> lines = testMappedResources("same_cell");
    String expectedPlatform = Platform.detect().getPrintableName();<commit_msg>ShBinaryRuleIntegrationTest compat with new buck-out layout<commit_after>/*
 * Copyright 2013-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.shell;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import com.facebook.buck.core.model.BuildTargetFactory;
import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.util.ExitCode;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;

public class ShBinaryRuleIntegrationTest {

  @Rule public TemporaryPaths temporaryFolder = new TemporaryPaths();

  @Test
  public void testTrivialShBinaryRule() throws IOException {
    // sh_binary is not available on Windows. Ignore this test on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "sh_binary_trivial", temporaryFolder);
    workspace.setUp();

    Path outputFile = workspace.buildAndReturnOutput("//:run_example");

    // Verify contents of example_out.txt
    String output = new String(Files.readAllBytes(outputFile), UTF_8);
    assertEquals("arg1\narg2\n", output);
  }

  @Test
  public void testExecutableOnRebuild() throws IOException {
    // sh_binary is not available on Windows. Ignore this test on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "sh_binary_with_caching", temporaryFolder);
    workspace.setUp();
    workspace.enableDirCache();

    // First build only the sh_binary rule itself.
    ProcessResult buildResult = workspace.runBuckCommand("build", "//:example_sh", "-v", "2");
    buildResult.assertSuccess();

    // Make sure the sh_binary output is executable to begin with.
    Path outputPath =
        workspace
            .getGenPath(BuildTargetFactory.newInstance("//:example_sh"), "__%s__")
            .resolve("example_sh.sh");
    Path output = workspace.getPath(outputPath);
    assertTrue("Output file should be written to '" + outputPath + "'.", Files.exists(output));
    assertTrue("Output file must be executable.", Files.isExecutable(output));

    // Now delete the buck-out directory (but not buck-cache).
    workspace.runBuckCommand("clean", "--keep-cache");

    // Now run the genrule that depends on the sh_binary above. This will force buck to fetch the
    // sh_binary output from cache. If the executable flag is lost somewhere along the way, this
    // will fail.
    buildResult = workspace.runBuckCommand("build", "//:run_example", "-v", "2");
    buildResult.assertSuccess("Build failed when rerunning sh_binary from cache.");

    // Note that previously we used to verify that the //:example_sh rule was
    // fetched from the cache here.  However, caching for sh_binary() rules has
    // since been disabled.

    // In addition to running the build, explicitly check that the output file is still executable.
    assertTrue(
        "Output file must be retrieved from cache at '" + outputPath + ".", Files.exists(output));
    assertTrue("Output file retrieved from cache must be executable.", Files.isExecutable(output));
  }

  @Test
  public void testShBinaryWithMappedResources() throws IOException {
    List<String> lines = testMappedResources("same_cell");
    String expectedPlatform = Platform.detect().getPrintableName();
{'fraction_non_alphanumeric': 0.09798746288353678, 'fraction_numerical': 0.003079291762894534, 'mean_word_length': 4.120495495495495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4891557', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2416, 'n_words': 777}