]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Reduce boilerplate code in `ArcanistLinterTestCase` subclasses
authorJoshua Spence <josh@joshuaspence.com>
Sun, 4 Jan 2015 07:38:32 +0000 (18:38 +1100)
committerJoshua Spence <josh@joshuaspence.com>
Sun, 4 Jan 2015 07:40:57 +0000 (18:40 +1100)
Summary: This is a bit magical and is maybe a terrible idea, but it seems okayish.

Test Plan: `arc unit`

Reviewers: chad, #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D11172

27 files changed:
src/lint/linter/__tests__/ArcanistCSSLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistClosureLinterTestCase.php
src/lint/linter/__tests__/ArcanistCoffeeLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistCommitLinterTestCase.php
src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php
src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php
src/lint/linter/__tests__/ArcanistFlake8LinterTestCase.php
src/lint/linter/__tests__/ArcanistGoLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistHLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistJSHintLinterTestCase.php
src/lint/linter/__tests__/ArcanistJSONLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php
src/lint/linter/__tests__/ArcanistJscsLinterTestCase.php
src/lint/linter/__tests__/ArcanistLesscLinterTestCase.php
src/lint/linter/__tests__/ArcanistLinterTestCase.php
src/lint/linter/__tests__/ArcanistMergeConflictLinterTestCase.php
src/lint/linter/__tests__/ArcanistPEP8LinterTestCase.php
src/lint/linter/__tests__/ArcanistPhpLinterTestCase.php
src/lint/linter/__tests__/ArcanistPhpcsLinterTestCase.php
src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php
src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php
src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php
src/lint/linter/__tests__/ArcanistRubyLinterTestCase.php
src/lint/linter/__tests__/ArcanistTextLinterTestCase.php
src/lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php
src/lint/linter/__tests__/ArcanistXMLLinterTestCase.php

index 5e8699668f3fab67bd944631399cd04ff933e2af..0bae09d265935c7d1f2e67906eb482866b953f57 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistCSSLintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testCSSLintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/csslint/',
-      new ArcanistCSSLintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/csslint/');
   }
 
 }
index 9619f6ab92854b3653224d0fb22f41bdadc243d1..6b9b72dc4f0dfda3f77516dd19820249c464f6b0 100644 (file)
@@ -7,9 +7,7 @@ final class ArcanistClosureLinterTestCase
     $linter = new ArcanistClosureLinter();
     $linter->setFlags(array('--additional_extensions=lint-test'));
 
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/gjslint/',
-      $linter);
+    $this->executeTestsInDirectory(dirname(__FILE__).'/gjslint/', $linter);
   }
 
 }
index 9fe5ef333f80b9fc7ff65a13330f89cc663096db..e359acd4a2ce026739d0cc044e4d4dabd18edde3 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistCoffeeLintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testCoffeeLintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/coffeelint/',
-      new ArcanistCoffeeLintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/coffeelint/');
   }
 
 }
index 293cf7cca56bd5f998363a7aece009d9c6b1b7f1..6b612ec2b05b077d420bc91f678b6e17a6f83e72 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistCommitLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testCommitLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/commit/',
-      new ArcanistCommitLinter());
+    return $this->executeTestsInDirectory(dirname(__FILE__).'/commit/');
   }
 
 }
index a8993900c3895b32e7a5358939b3ebbf96846dd5..c2c7471b8905778aeaf7cba871caa013ff15103d 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistCppcheckLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testCppcheckLint() {
-    return $this->executeTestsInDirectory(
-      dirname(__FILE__).'/cppcheck/',
-      new ArcanistCppcheckLinter());
+    return $this->executeTestsInDirectory(dirname(__FILE__).'/cppcheck/');
   }
 
 }
index 0a71f0e9c54c2b314e9364d9f655b56e152c9365..baecac9aa82acd3b669c088975e1eeaf2c31bdfd 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistCpplintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testCpplintLint() {
-    return $this->executeTestsInDirectory(
-      dirname(__FILE__).'/cpplint/',
-      new ArcanistCpplintLinter());
+    return $this->executeTestsInDirectory(dirname(__FILE__).'/cpplint/');
   }
 
 }
index 1c487147f718b3a62b212c8c2b05ef9003f3f4f4..39cabfafe4931b0a730b9fc1519e260a22419120 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistFlake8LinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testFlake8Lint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/flake8/',
-      new ArcanistFlake8Linter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/flake8/');
   }
 
 }
index fb9c62ebe71fbdebf7f01fdc5cf0b27cda414bbd..f906cea96bafb2e0be17822a71ec8e68fabaf8e3 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistGoLintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testGoLintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/golint/',
-      new ArcanistGoLintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/golint/');
   }
 
 }
index 581220b1548bd4a2053314383f4e95783a28640f..9a8fcf948912e7d6e11797d28329b1c2c6b11e98 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 
 final class ArcanistHLintLinterTestCase extends ArcanistArcanistLinterTestCase {
+
   public function testHlintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/hlint/',
-      new ArcanistHLintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/hlint/');
   }
+
 }
index 95405999c0f7c39214743961ad9ca2cb2e1e7ddf..b6c74e03ef5c4b983b18d10b78dd60abe9f008c7 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistJSHintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testJSHintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/jshint/',
-      new ArcanistJSHintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/jshint/');
   }
 
 }
index 6589bd8d8284058bc9e34acfd36dd8cd9706b991..143701519976845e64a99550643e23207ceaf4a9 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistJSONLintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testJSONLintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/jsonlint/',
-      new ArcanistJSONLintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/jsonlint/');
   }
 
 }
index 1913c0dc97dba846c62a6f26165ae5e6508284b2..6cfe206f49b0d5f090e89f667ed9b3446227caf7 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistJSONLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testJSONLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/jsonlint/',
-      new ArcanistJSONLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/jsonlint/');
   }
 
 }
index d72c44e01d1c71d72520fa27db80b963efd34b87..78764cac4d88b50a0c317be1b9119ec19f783149 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistJscsLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testJscsLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/jscs/',
-      new ArcanistJscsLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/jscs/');
   }
 
 }
index 9303d3713fd24c02c709ed8ee883fd63a231b33b..cca0a4f921412d3fe071cf2c802843b1234aa78a 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistLesscLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testLesscLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/lessc/',
-      new ArcanistLesscLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/lessc/');
   }
 
 }
index d8ec6a8533a90b8e33cf6cc78ce917f386bd8be8..3fa9464366ff033abfc026f6f4aba6f1e198ea26 100644 (file)
@@ -5,7 +5,33 @@
  */
 abstract class ArcanistLinterTestCase extends ArcanistPhutilTestCase {
 
-  public function executeTestsInDirectory($root, ArcanistLinter $linter) {
+  /**
+   * Executes all tests from the specified subdirectory. If a linter is not
+   * explicitly specified, it will be inferred from the name of the test class.
+   */
+  public function executeTestsInDirectory(
+    $root,
+    ArcanistLinter $linter = null) {
+
+    if (!$linter) {
+      // Infer the linter from the class name. This is a little magical, but
+      // reduces the amount of boiler plate code.
+      $count = 0;
+      $linter_name = preg_replace(
+        '/^(\w+Linter)TestCase$/',
+        '$1',
+        get_class($this),
+        1,
+        $count);
+
+      if (!$count) {
+        throw new Exception(pht('Unable to infer linter class name.'));
+      }
+
+      $linter = id(new ReflectionClass($linter_name))
+        ->newInstanceWithoutConstructor();
+    }
+
     $files = id(new FileFinder($root))
       ->withType('f')
       ->withSuffix('lint-test')
index beed0190f7a3bc8177888dca6904ba1257dd2324..9c54379d71500047137d0bceb0d1c2b551369f51 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistMergeConflictLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testMergeConflictLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/mergeconflict/',
-      new ArcanistMergeConflictLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/mergeconflict/');
   }
 
 }
index 2a055bc89311beb5682a8890e9abf7507ed2897d..41843d60d3dd6921fa514f991e676541101f57d4 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistPEP8LinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testPEP8Linter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/pep8/',
-      new ArcanistPEP8Linter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/pep8/');
   }
 
 }
index a3fc56eccba7fb0a93a3a92ab36098f5689be75d..b4bf952c32e58f9180689b96380084dbc3582174 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistPhpLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testPHPLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/php/',
-      new ArcanistPhpLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/php/');
   }
 
 }
index a88eded624888a2ce8d6bbaeb1182156fc92acf4..bff0ca07dab5bf95ce3c38434e1950aa0a032d45 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistPhpcsLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testPHPCSLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/phpcs/',
-      new ArcanistPhpcsLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/phpcs/');
   }
 
 }
index 0c259d589c9614285f35144b6d402637174e265a..01a7258d62f798ca2ab0c001249b674afb24d8b9 100644 (file)
@@ -9,9 +9,7 @@ final class ArcanistPhutilXHPASTLinterTestCase
       'deprecated_function' => 'This function is most likely deprecated.',
     ));
 
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/phlxhp/',
-      $linter);
+    $this->executeTestsInDirectory(dirname(__FILE__).'/phlxhp/', $linter);
   }
 
 }
index ced6d25d4239ff2bf3590dabe58aaef3357dcc11..19eb39cd93e3ae886bdb80f3fd3f48354fae8fe1 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistPuppetLintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testPuppetLintLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/puppet-lint/',
-      new ArcanistPuppetLintLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/puppet-lint/');
   }
 
 }
index 7f025a606a4fe2d3e6aadbc3e0296114a70a4dc3..31d3c76119126558b648b500f8048be69f90eac3 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistPyFlakesLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testPyflakesLinter() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/pyflakes/',
-      new ArcanistPyFlakesLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/pyflakes/');
   }
 
 }
index 093856a7b02600247e753819e4cf48adbd5c490e..d1686c989e40a5e0156de42e9f1d8921506dab87 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistPyLintLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testPyLintLinter() {
-    return $this->executeTestsInDirectory(
-      dirname(__FILE__).'/pylint/',
-      new ArcanistPyLintLinter());
+    return $this->executeTestsInDirectory(dirname(__FILE__).'/pylint/');
   }
 
 }
index e8e37ba5a76972b503f44692f94580b482402d60..944d4891c6bb8b5fe5a33b26d45930fbe9d78665 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistRubyLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testRubyLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/ruby/',
-      new ArcanistRubyLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/ruby/');
   }
 
 }
index 5fb6b220a5926b6a397366051609a740e9a43500..e2aba519a7051f02918de5e181096a2c9b944c17 100644 (file)
@@ -3,9 +3,7 @@
 final class ArcanistTextLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testTextLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/text/',
-      new ArcanistTextLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/text/');
   }
 
 }
index f97f5b32fc01312157c971d64f8eb792cda383f0..557ce0b545810c29263a0ff4eee23eaccc264933 100644 (file)
@@ -4,9 +4,7 @@ final class ArcanistXHPASTLinterTestCase
   extends ArcanistArcanistLinterTestCase {
 
   public function testXHPASTLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/xhpast/',
-      new ArcanistXHPASTLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/xhpast/');
   }
 
 }
index d9e04a875895bf16885382f4780e9883c6d4a510..7dc12d0ac2b22061427524e23ea6dfe7ba1861e3 100644 (file)
@@ -7,9 +7,7 @@
 final class ArcanistXMLLinterTestCase extends ArcanistArcanistLinterTestCase {
 
   public function testXMLLint() {
-    $this->executeTestsInDirectory(
-      dirname(__FILE__).'/xml/',
-      new ArcanistXMLLinter());
+    $this->executeTestsInDirectory(dirname(__FILE__).'/xml/');
   }
 
 }