private function lintFile($file, $linter, $working_copy)
 {
     $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 = json_decode($config, true);
         if (!is_array($config)) {
             throw new Exception("Invalid configuration in test '{$basename}', not valid JSON.");
         }
     } else {
         $config = array();
     }
     /* TODO: ?
        validate_parameter_list(
          $config,
          array(
          ),
          array(
            'project' => true,
            'path' => true,
            'hook' => true,
          ));
        */
     $exception = null;
     $after_lint = null;
     $messages = null;
     $exception_message = false;
     $caught_exception = false;
     try {
         $path = idx($config, 'path', 'lint/' . $basename . '.php');
         $engine = new UnitTestableArcanistLintEngine();
         $engine->setWorkingCopy($working_copy);
         $engine->setPaths(array($path));
         $engine->setCommitHookMode(idx($config, 'hook', false));
         $linter->addPath($path);
         $linter->addData($path, $data);
         $linter->setConfig(idx($config, 'config', array()));
         $engine->addLinter($linter);
         $engine->addFileData($path, $data);
         $results = $engine->run();
         $this->assertEqual(1, count($results), 'Expect one result returned by linter.');
         $result = reset($results);
         $patcher = ArcanistLintPatcher::newFromArcanistLintResult($result);
         $after_lint = $patcher->getModifiedFileContent();
     } catch (ArcanistPhutilTestTerminatedException $ex) {
         throw $ex;
     } catch (Exception $exception) {
         $caught_exception = true;
         if ($exception instanceof PhutilAggregateException) {
             $caught_exception = false;
             foreach ($exception->getExceptions() as $ex) {
                 if ($ex instanceof ArcanistUsageException) {
                     $this->assertSkipped($ex->getMessage());
                 } else {
                     $caught_exception = true;
                 }
             }
         }
         $exception_message = $exception->getMessage() . "\n\n" . $exception->getTraceAsString();
     }
     switch ($basename) {
         default:
             $this->assertEqual(false, $caught_exception, $exception_message);
             $this->compareLint($basename, $expect, $result);
             $this->compareTransform($xform, $after_lint);
             break;
     }
 }
Ejemplo n.º 2
0
 private function lintFile($file, ArcanistLinter $linter)
 {
     $linter = clone $linter;
     $contents = Filesystem::readFile($file);
     $contents = preg_split('/^~{4,}\\n/m', $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);
         }
         $working_copy = ArcanistWorkingCopyIdentity::newFromRootAndConfigFile($dir, $config_file, 'Unit Test');
         $configuration_manager = new ArcanistConfigurationManager();
         $configuration_manager->setWorkingCopyIdentity($working_copy);
         $engine = new UnitTestableArcanistLintEngine();
         $engine->setWorkingCopy($working_copy);
         $engine->setConfigurationManager($configuration_manager);
         $engine->setPaths(array($path));
         $engine->setCommitHookMode(idx($config, 'hook', false));
         $path_name = idx($config, 'path', $path);
         $linter->addPath($path_name);
         $linter->addData($path_name, $data);
         $config = idx($config, 'config', array());
         foreach ($config as $key => $value) {
             $linter->setLinterConfigurationValue($key, $value);
         }
         $engine->addLinter($linter);
         $engine->addFileData($path_name, $data);
         $results = $engine->run();
         $this->assertEqual(1, count($results), 'Expect one result returned by linter.');
         $result = reset($results);
         $patcher = ArcanistLintPatcher::newFromArcanistLintResult($result);
         $after_lint = $patcher->getModifiedFileContent();
     } catch (ArcanistPhutilTestTerminatedException $ex) {
         throw $ex;
     } catch (Exception $exception) {
         $caught_exception = true;
         if ($exception instanceof PhutilAggregateException) {
             $caught_exception = false;
             foreach ($exception->getExceptions() as $ex) {
                 if ($ex instanceof ArcanistUsageException) {
                     $this->assertSkipped($ex->getMessage());
                 } else {
                     $caught_exception = true;
                 }
             }
         } else {
             if ($exception instanceof ArcanistUsageException) {
                 $this->assertSkipped($exception->getMessage());
             }
         }
         $exception_message = $exception->getMessage() . "\n\n" . $exception->getTraceAsString();
     }
     $this->assertEqual(false, $caught_exception, $exception_message);
     $this->compareLint($basename, $expect, $result);
     $this->compareTransform($xform, $after_lint);
 }