<commit_before>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.android;

import com.facebook.buck.android.device.TargetDevice;
import com.facebook.buck.android.toolchain.AndroidPlatformTarget;
import com.facebook.buck.core.build.context.BuildContext;
import com.facebook.buck.core.filesystems.AbsPath;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.rules.BuildRuleParams;
import com.facebook.buck.core.rules.BuildRuleResolver;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.facebook.buck.core.sourcepath.resolver.SourcePathResolverAdapter;
import com.facebook.buck.core.toolchain.tool.Tool;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.jvm.core.JavaLibrary;
import com.facebook.buck.jvm.java.ForkMode;
import com.facebook.buck.jvm.java.JavaTest;
import com.facebook.buck.jvm.java.TestType;
import com.facebook.buck.rules.args.Arg;
import com.facebook.buck.step.Step;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Stream;

@SuppressWarnings("PMD.TestClassWithoutTestCases")
public class RobolectricTest extends JavaTest {

  private final AndroidPlatformTarget androidPlatformTarget;
  private final RobolectricTestHelper robolectricTestHelper;

  protected RobolectricTest(
      BuildTarget buildTarget,
      ProjectFilesystem projectFilesystem,
      BuildRuleParams buildRuleParams,
      AndroidPlatformTarget androidPlatformTarget,
      JavaLibrary compiledTestsLibrary,
      Set<String> labels,
      Set<String> contacts,
      TestType testType,
      String targetLevel,
      List<Arg> vmArgs,
      Map<String, String> nativeLibsEnvironment,
      Optional<DummyRDotJava> optionalDummyRDotJava,
      Optional<UnitTestOptions> unitTestOptions,
      Optional<Long> testRuleTimeoutMs,
      Optional<Long> testCaseTimeoutMs,
      ImmutableMap<String, Arg> env,
      boolean runTestSeparately,
      ForkMode forkMode,
      Optional<Level> stdOutLogLevel,
      Optional<Level> stdErrLogLevel,
      Optional<SourcePath> unbundledResourcesRoot,
      Optional<SourcePath> robolectricRuntimeDependency,
      Optional<SourcePath> robolectricManifest,
      boolean passDirectoriesInFile,
      Tool javaRuntimeLauncher,
      boolean withDownwardApi) {
    super(
        buildTarget,
        projectFilesystem,
        buildRuleParams,
        compiledTestsLibrary,
        Optional.of(
            resolver -> {
              ImmutableList.Builder<Path> builder = ImmutableList.builder();
              optionalDummyRDotJava.ifPresent(
                  dummyRDotJava ->
                      builder.add(
                          resolver
                              .getAbsolutePath(dummyRDotJava.getSourcePathToOutput())
                              .getPath()));
              unitTestOptions.ifPresent(
                  options ->
                      builder.add(
                          resolver.getAbsolutePath(options.getSourcePathToOutput()).getPath()));
              return builder.build();
            }),
        labels,
        contacts,
        testType,
        targetLevel,
        javaRuntimeLauncher,
        vmArgs,
        nativeLibsEnvironment,
        testRuleTimeoutMs,
        testCaseTimeoutMs,
        env,
        runTestSeparately,
        forkMode,
        stdOutLogLevel,
        stdErrLogLevel,
        unbundledResourcesRoot,
        withDownwardApi);
    this.androidPlatformTarget = androidPlatformTarget;
    this.robolectricTestHelper =
        new RobolectricTestHelper(
            getBuildTarget(),
            optionalDummyRDotJava,
            robolectricRuntimeDependency,
            robolectricManifest,
            getProjectFilesystem(),
            passDirectoriesInFile);
  }

  @Override
  protected ImmutableSet<Path> getBootClasspathEntries() {
    return androidPlatformTarget.getBootclasspathEntries().stream()
        .map(AbsPath::getPath)
        .collect(ImmutableSet.toImmutableSet());
  }

  @Override
  protected void addPreTestSteps(
      BuildContext buildContext, ImmutableList.Builder<Step> stepsBuilder) {
    robolectricTestHelper.addPreTestSteps(buildContext, stepsBuilder);
  }

  @Override
  protected void onAmendVmArgs(
      ImmutableList.Builder<String> vmArgsBuilder,
      SourcePathResolverAdapter pathResolver,
      Optional<TargetDevice> targetDevice) {
    super.onAmendVmArgs(vmArgsBuilder, pathResolver, targetDevice);
    robolectricTestHelper.amendVmArgs(vmArgsBuilder, pathResolver);
  }

  @Override
  public void onPreTest(BuildContext buildContext) throws IOException {
    robolectricTestHelper.onPreTest(buildContext);
  }

  @Override
  public Stream<BuildTarget> getRuntimeDeps(BuildRuleResolver buildRuleResolver) {
    return Stream.concat(
        // Inherit any runtime deps from `JavaTest`.
        super.getRuntimeDeps(buildRuleResolver),
        robolectricTestHelper.getExtraRuntimeDeps(buildRuleResolver, getBuildDeps()));
  }

  @VisibleForTesting
  RobolectricTestHelper getRobolectricTestHelper() {
    return robolectricTestHelper;
  }
}<commit_msg>Fix robolectric tests running with an external runner on Java 11<commit_after>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.android;

import com.facebook.buck.android.device.TargetDevice;
import com.facebook.buck.android.toolchain.AndroidPlatformTarget;
import com.facebook.buck.core.build.context.BuildContext;
import com.facebook.buck.core.filesystems.AbsPath;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.rules.BuildRuleParams;
import com.facebook.buck.core.rules.BuildRuleResolver;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.facebook.buck.core.sourcepath.resolver.SourcePathResolverAdapter;
import com.facebook.buck.core.toolchain.tool.Tool;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.jvm.core.JavaLibrary;
import com.facebook.buck.jvm.java.ForkMode;
import com.facebook.buck.jvm.java.JavaTest;
import com.facebook.buck.jvm.java.TestType;
import com.facebook.buck.rules.args.Arg;
import com.facebook.buck.step.Step;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Stream;

@SuppressWarnings("PMD.TestClassWithoutTestCases")
public class RobolectricTest extends JavaTest {

  private final AndroidPlatformTarget androidPlatformTarget;
  private final RobolectricTestHelper robolectricTestHelper;

  protected RobolectricTest(
      BuildTarget buildTarget,
      ProjectFilesystem projectFilesystem,
      BuildRuleParams buildRuleParams,
      AndroidPlatformTarget androidPlatformTarget,
      JavaLibrary compiledTestsLibrary,
      Set<String> labels,
      Set<String> contacts,
      TestType testType,
      String targetLevel,
      List<Arg> vmArgs,
      Map<String, String> nativeLibsEnvironment,
      Optional<DummyRDotJava> optionalDummyRDotJava,
      Optional<UnitTestOptions> unitTestOptions,
      Optional<Long> testRuleTimeoutMs,
      Optional<Long> testCaseTimeoutMs,
      ImmutableMap<String, Arg> env,
      boolean runTestSeparately,
      ForkMode forkMode,
      Optional<Level> stdOutLogLevel,
      Optional<Level> stdErrLogLevel,
      Optional<SourcePath> unbundledResourcesRoot,
      Optional<SourcePath> robolectricRuntimeDependency,
      Optional<SourcePath> robolectricManifest,
      boolean passDirectoriesInFile,
      Tool javaRuntimeLauncher,
      boolean withDownwardApi) {
    super(
        buildTarget,
        projectFilesystem,
        buildRuleParams,
        compiledTestsLibrary,
        Optional.of(
            resolver -> {
              ImmutableList.Builder<Path> builder = ImmutableList.builder();
              optionalDummyRDotJava.ifPresent(
                  dummyRDotJava ->
                      builder.add(
                          resolver
                              .getAbsolutePath(dummyRDotJava.getSourcePathToOutput())
                              .getPath()));
              unitTestOptions.ifPresent(
                  options ->
                      builder.add(
                          resolver.getAbsolutePath(options.getSourcePathToOutput()).getPath()));
              return builder.build();
            }),
        labels,
        contacts,
        testType,
        targetLevel,
        javaRuntimeLauncher,
        vmArgs,
        nativeLibsEnvironment,
        testRuleTimeoutMs,
        testCaseTimeoutMs,
        env,
        runTestSeparately,
        forkMode,
        stdOutLogLevel,
        stdErrLogLevel,
        unbundledResourcesRoot,
        withDownwardApi);
    this.androidPlatformTarget = androidPlatformTarget;
    this.robolectricTestHelper =
        new RobolectricTestHelper(
            getBuildTarget(),
            optionalDummyRDotJava,
            robolectricRuntimeDependency,
            robolectricManifest,
            getProjectFilesystem(),
            passDirectoriesInFile);
  }

  @Override
  protected ImmutableSet<Path> getBootClasspathEntries() {
    return androidPlatformTarget.getBootclasspathEntries().stream()
        .map(AbsPath::getPath)
        .collect(ImmutableSet.toImmutableSet());
  }

  @Override
  protected void addPreTestSteps(
      BuildContext buildContext, ImmutableList.Builder<Step> stepsBuilder) {
    robolectricTestHelper.addPreTestSteps(buildContext, stepsBuilder);
  }

  @Override
  protected void onAmendVmArgs(
      ImmutableList.Builder<String> vmArgsBuilder,
      SourcePathResolverAdapter pathResolver,
      Optional<TargetDevice> targetDevice) {
    super.onAmendVmArgs(vmArgsBuilder, pathResolver, targetDevice);
    robolectricTestHelper.amendVmArgs(vmArgsBuilder, pathResolver);
  }

  @Override
  public void onPreTest(BuildContext buildContext) throws IOException {
    super.onPreTest(buildContext);
    robolectricTestHelper.onPreTest(buildContext);
  }

  @Override
  public Stream<BuildTarget> getRuntimeDeps(BuildRuleResolver buildRuleResolver) {
    return Stream.concat(
        // Inherit any runtime deps from `JavaTest`.
        super.getRuntimeDeps(buildRuleResolver),
        robolectricTestHelper.getExtraRuntimeDeps(buildRuleResolver, getBuildDeps()));
  }

  @VisibleForTesting
  RobolectricTestHelper getRobolectricTestHelper() {
    return robolectricTestHelper;
  }
}
{'fraction_non_alphanumeric': 0.07800131926121372, 'fraction_numerical': 0.0008245382585751979, 'mean_word_length': 3.3179067283730865, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 49, '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': '4933681', 'n_tokens_mistral': 3323, 'n_tokens_neox': 3226, 'n_words': 665}