<commit_before><?php

/**
 * Facilitates implementation of test cases for @{class:ArcanistLinter}s.
 */
abstract class ArcanistLinterTestCase extends ArcanistPhutilTestCase {

  public function executeTestsInDirectory($root, ArcanistLinter $linter) {
    $files = id(new FileFinder($root))
      ->withType('f')
      ->withSuffix('lint-test')
      ->find();

    $test_count = 0;
    foreach ($files as $file) {
      $this->lintFile($root.$file, $linter);
      $test_count++;
    }

    $this->assertTrue(
      ($test_count > 0),
      pht('Expected to find some .lint-test tests in directory %s!', $root));
  }

  private function lintFile($file, ArcanistLinter $linter) {
    $linter = clone $linter;

    $contents = Filesystem::readFile($file);
    $contents = explode("~~~~~~~~~~\n", $contents);
    if (count($contents) < 2) {
      throw new Exception(
        "Expected '~~~~~~~~~~' separating test case and results.");
    }

    list ($data, $expect, $xform, $config) = array_merge(
      $contents,
      array(null, null));

    $basename = basename($file);

    $config = phutil_json_decode($config);
    PhutilTypeSpec::checkMap(
      $config,
      array(
        'hook' => 'optional bool',
        'config' => 'optional wild',
        'path' => 'optional string',
        'arcconfig' => 'optional map<string, string>',
      ));

    $exception = null;
    $after_lint = null;
    $messages = null;
    $exception_message = false;
    $caught_exception = false;

    try {
      $tmp = new TempFile($basename);
      Filesystem::writeFile($tmp, $data);
      $full_path = (string)$tmp;

      $dir = dirname($full_path);
      $path = basename($full_path);
      $config_file = null;
      $arcconfig = idx($config, 'arcconfig');
      if ($arcconfig) {
        $config_file = json_encode($arcconfig);
      }
<commit_msg>Don't throw an exception if no configuration is set for linter tests<commit_after><?php

/**
 * Facilitates implementation of test cases for @{class:ArcanistLinter}s.
 */
abstract class ArcanistLinterTestCase extends ArcanistPhutilTestCase {

  public function executeTestsInDirectory($root, ArcanistLinter $linter) {
    $files = id(new FileFinder($root))
      ->withType('f')
      ->withSuffix('lint-test')
      ->find();

    $test_count = 0;
    foreach ($files as $file) {
      $this->lintFile($root.$file, $linter);
      $test_count++;
    }

    $this->assertTrue(
      ($test_count > 0),
      pht('Expected to find some .lint-test tests in directory %s!', $root));
  }

  private function lintFile($file, ArcanistLinter $linter) {
    $linter = clone $linter;

    $contents = Filesystem::readFile($file);
    $contents = explode("~~~~~~~~~~\n", $contents);
    if (count($contents) < 2) {
      throw new Exception(
        "Expected '~~~~~~~~~~' separating test case and results.");
    }

    list ($data, $expect, $xform, $config) = array_merge(
      $contents,
      array(null, null));

    $basename = basename($file);

    if ($config) {
      $config = phutil_json_decode($config);
    } else {
      $config = array();
    }
    PhutilTypeSpec::checkMap(
      $config,
      array(
        'hook' => 'optional bool',
        'config' => 'optional wild',
        'path' => 'optional string',
        'arcconfig' => 'optional map<string, string>',
      ));

    $exception = null;
    $after_lint = null;
    $messages = null;
    $exception_message = false;
    $caught_exception = false;

    try {
      $tmp = new TempFile($basename);
      Filesystem::writeFile($tmp, $data);
      $full_path = (string)$tmp;

      $dir = dirname($full_path);
      $path = basename($full_path);
      $config_file = null;
      $arcconfig = idx($config, 'arcconfig');
      if ($arcconfig) {
        $config_file = json_encode($arcconfig);
      }

{'fraction_non_alphanumeric': 0.16045168067226892, 'fraction_numerical': 0.0015756302521008404, 'mean_word_length': 2.9026639344262297, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3866458', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1211, 'n_words': 278}