<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.util;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.facebook.buck.io.MorePaths;
import com.google.common.collect.ImmutableSet;

import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.regex.Pattern;

public class LicenseCheckTest {

  // Files where we're okay with the license not being the normal Facebook apache one. We also
  // exclude all files under "test/**/testdata/"
  private static final ImmutableSet<Path> NON_APACHE_LICENSE_WHITELIST = ImmutableSet.of(
      // Because it's not originally our code.
      Paths.get("src/com/facebook/buck/jvm/java/coverage/ReportGenerator.java"),
      Paths.get("src/com/facebook/buck/util/WindowsCreateProcessEscape.java"),
      Paths.get(
          "test/com/facebook/buck/util/WindowsCreateProcessEscapeTest.java"));

  private static final ImmutableSet<Path> NON_APACHE_LICENSE_DIRS_WHITELIST = ImmutableSet.of(
      Paths.get("src/com/facebook/buck/cli/quickstart/android/"));

  @Test
  public void ensureAllSrcFilesHaveTheApacheLicense() throws IOException {
    Files.walkFileTree(Paths.get("src"), new JavaCopyrightVisitor(false));
    Files.walkFileTree(Paths.get("test"), new JavaCopyrightVisitor(true));
  }

  private static class JavaCopyrightVisitor extends SimpleFileVisitor<Path> {

    private static final Pattern LICENSE_FRAGMENT = Pattern.compile(
        // TODO(shs96c): This is very lame.
        // The newline character doesn't match "\w", "\\n" so do a non-greedy match until the next
        // part of the copyright.
        "^/\\\\*.*?" +
        "\\\\* Copyright 20\\d\\d-present Facebook, Inc\\..*?" +
        "\\\\* Licensed under the Apache License, Version 2.0 \\(the \"License\"\\); you may.*",<commit_msg>Ignore auto-generated plugin files from the license check.<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.util;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.facebook.buck.io.MorePaths;
import com.google.common.collect.ImmutableSet;

import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.regex.Pattern;

public class LicenseCheckTest {

  // Files where we're okay with the license not being the normal Facebook apache one. We also
  // exclude all files under "test/**/testdata/"
  private static final ImmutableSet<Path> NON_APACHE_LICENSE_WHITELIST = ImmutableSet.of(
      // Because it's not originally our code.
      Paths.get("src/com/facebook/buck/jvm/java/coverage/ReportGenerator.java"),
      Paths.get("src/com/facebook/buck/util/WindowsCreateProcessEscape.java"),
      Paths.get(
          "test/com/facebook/buck/util/WindowsCreateProcessEscapeTest.java"));

  private static final ImmutableSet<Path> NON_APACHE_LICENSE_DIRS_WHITELIST = ImmutableSet.of(
      // Ignore the files in the template for quickstarting an Android project
      Paths.get("src/com/facebook/buck/cli/quickstart/android/"),

      // Ignore the generated parsing files for the plugin
      Paths.get("src/com/facebook/buck/intellij/plugin/gen/")
  );

  @Test
  public void ensureAllSrcFilesHaveTheApacheLicense() throws IOException {
    Files.walkFileTree(Paths.get("src"), new JavaCopyrightVisitor(false));
    Files.walkFileTree(Paths.get("test"), new JavaCopyrightVisitor(true));
  }

  private static class JavaCopyrightVisitor extends SimpleFileVisitor<Path> {

    private static final Pattern LICENSE_FRAGMENT = Pattern.compile(
        // TODO(shs96c): This is very lame.
        // The newline character doesn't match "\w", "\\n" so do a non-greedy match until the next
        // part of the copyright.
        "^/\\\\*.*?" +
        "\\\\* Copyright 20\\d\\d-present Facebook, Inc\\..*?" +
        "\\\\* Licensed under the Apache License, Version 2.0 \\(the \"License\"\\); you may.*",
{'fraction_non_alphanumeric': 0.11169064748201439, 'fraction_numerical': 0.005035971223021582, 'mean_word_length': 3.7367972742759794, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3514930', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1551, 'n_words': 476}