<commit_before>// Copyright 2017 The Bazel Authors. All rights reserved.
//
// 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.google.devtools.build.lib.rules.platform;

import static com.google.common.truth.Truth.assertThat;
import static java.util.stream.Collectors.joining;

import com.google.common.collect.ImmutableList;
import com.google.common.truth.IterableSubject;
import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.platform.DeclaredToolchainInfo;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.skyframe.RegisteredToolchainsValue;
import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
import com.google.devtools.build.lib.skylark.util.SkylarkTestCase;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.SkyKey;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.Before;

/** Utility methods for setting up platform and toolchain related tests. */
public abstract class ToolchainTestCase extends SkylarkTestCase {

  public PlatformInfo linuxPlatform;
  public PlatformInfo macPlatform;

  public ConstraintSettingInfo setting;
  public ConstraintValueInfo linuxConstraint;
  public ConstraintValueInfo macConstraint;

  public Label testToolchainTypeLabel;
  public ToolchainTypeInfo testToolchainType;

  protected static IterableSubject assertToolchainLabels(
      RegisteredToolchainsValue registeredToolchainsValue) {
    return assertToolchainLabels(registeredToolchainsValue, null);
  }

  protected static IterableSubject assertToolchainLabels(
      RegisteredToolchainsValue registeredToolchainsValue,
      @Nullable PackageIdentifier packageRoot) {
    assertThat(registeredToolchainsValue).isNotNull();
    ImmutableList<DeclaredToolchainInfo> declaredToolchains =
        registeredToolchainsValue.registeredToolchains();
    List<Label> labels = collectToolchainLabels(declaredToolchains, packageRoot);
    return assertThat(labels);
  }

  protected static List<Label> collectToolchainLabels(
      List<DeclaredToolchainInfo> toolchains, @Nullable PackageIdentifier packageRoot) {
    return toolchains.stream()
        .map((toolchain -> toolchain.toolchainLabel()))
        .filter(label -> filterLabel(packageRoot, label))
        .collect(Collectors.toList());
  }

  protected static boolean filterLabel(@Nullable PackageIdentifier packageRoot, Label label) {
    if (packageRoot == null) {
      return true;
    }

    // Make sure the label is under the packageRoot.
    if (!label.getPackageIdentifier().getRepository().equals(packageRoot.getRepository())) {
      return false;
    }

    return label
        .getPackageIdentifier()
        .getPackageFragment()
        .startsWith(packageRoot.getPackageFragment());
  }

  private static String formatConstraints(Collection<String> constraints) {<commit_msg>Automatic code cleanup.<commit_after>// Copyright 2017 The Bazel Authors. All rights reserved.
//
// 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.google.devtools.build.lib.rules.platform;

import static com.google.common.truth.Truth.assertThat;
import static java.util.stream.Collectors.joining;

import com.google.common.collect.ImmutableList;
import com.google.common.truth.IterableSubject;
import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.platform.DeclaredToolchainInfo;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.skyframe.RegisteredToolchainsValue;
import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
import com.google.devtools.build.lib.skylark.util.SkylarkTestCase;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.SkyKey;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.Before;

/** Utility methods for setting up platform and toolchain related tests. */
public abstract class ToolchainTestCase extends SkylarkTestCase {

  public PlatformInfo linuxPlatform;
  public PlatformInfo macPlatform;

  public ConstraintSettingInfo setting;
  public ConstraintValueInfo linuxConstraint;
  public ConstraintValueInfo macConstraint;

  public Label testToolchainTypeLabel;
  public ToolchainTypeInfo testToolchainType;

  protected static IterableSubject assertToolchainLabels(
      RegisteredToolchainsValue registeredToolchainsValue) {
    return assertToolchainLabels(registeredToolchainsValue, null);
  }

  protected static IterableSubject assertToolchainLabels(
      RegisteredToolchainsValue registeredToolchainsValue,
      @Nullable PackageIdentifier packageRoot) {
    assertThat(registeredToolchainsValue).isNotNull();
    ImmutableList<DeclaredToolchainInfo> declaredToolchains =
        registeredToolchainsValue.registeredToolchains();
    List<Label> labels = collectToolchainLabels(declaredToolchains, packageRoot);
    return assertThat(labels);
  }

  protected static List<Label> collectToolchainLabels(
      List<DeclaredToolchainInfo> toolchains, @Nullable PackageIdentifier packageRoot) {
    return toolchains.stream()
        .map(toolchain -> toolchain.toolchainLabel())
        .filter(label -> filterLabel(packageRoot, label))
        .collect(Collectors.toList());
  }

  protected static boolean filterLabel(@Nullable PackageIdentifier packageRoot, Label label) {
    if (packageRoot == null) {
      return true;
    }

    // Make sure the label is under the packageRoot.
    if (!label.getPackageIdentifier().getRepository().equals(packageRoot.getRepository())) {
      return false;
    }

    return label
        .getPackageIdentifier()
        .getPackageFragment()
        .startsWith(packageRoot.getPackageFragment());
  }

  private static String formatConstraints(Collection<String> constraints) {
{'fraction_non_alphanumeric': 0.08581504702194358, 'fraction_numerical': 0.0020898641588296763, 'mean_word_length': 4.899075500770416, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960160', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1969, 'n_words': 502}