From f5db41917be687040a752654bd29a754ce995b86 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Sun, 4 Jan 2015 18:38:32 +1100 Subject: [PATCH] Reduce boilerplate code in `ArcanistLinterTestCase` subclasses 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 --- .../ArcanistCSSLintLinterTestCase.php | 4 +-- .../ArcanistClosureLinterTestCase.php | 4 +-- .../ArcanistCoffeeLintLinterTestCase.php | 4 +-- .../ArcanistCommitLinterTestCase.php | 4 +-- .../ArcanistCppcheckLinterTestCase.php | 4 +-- .../ArcanistCpplintLinterTestCase.php | 4 +-- .../ArcanistFlake8LinterTestCase.php | 4 +-- .../ArcanistGoLintLinterTestCase.php | 4 +-- .../__tests__/ArcanistHLintLinterTestCase.php | 6 ++-- .../ArcanistJSHintLinterTestCase.php | 4 +-- .../ArcanistJSONLintLinterTestCase.php | 4 +-- .../__tests__/ArcanistJSONLinterTestCase.php | 4 +-- .../__tests__/ArcanistJscsLinterTestCase.php | 4 +-- .../__tests__/ArcanistLesscLinterTestCase.php | 4 +-- .../__tests__/ArcanistLinterTestCase.php | 28 ++++++++++++++++++- .../ArcanistMergeConflictLinterTestCase.php | 4 +-- .../__tests__/ArcanistPEP8LinterTestCase.php | 4 +-- .../__tests__/ArcanistPhpLinterTestCase.php | 4 +-- .../__tests__/ArcanistPhpcsLinterTestCase.php | 4 +-- .../ArcanistPhutilXHPASTLinterTestCase.php | 4 +-- .../ArcanistPuppetLintLinterTestCase.php | 4 +-- .../ArcanistPyFlakesLinterTestCase.php | 4 +-- .../ArcanistPyLintLinterTestCase.php | 4 +-- .../__tests__/ArcanistRubyLinterTestCase.php | 4 +-- .../__tests__/ArcanistTextLinterTestCase.php | 4 +-- .../ArcanistXHPASTLinterTestCase.php | 4 +-- .../__tests__/ArcanistXMLLinterTestCase.php | 4 +-- 27 files changed, 55 insertions(+), 79 deletions(-) diff --git a/src/lint/linter/__tests__/ArcanistCSSLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCSSLintLinterTestCase.php index 5e869966..0bae09d2 100644 --- a/src/lint/linter/__tests__/ArcanistCSSLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCSSLintLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistCSSLintLinterTestCase extends ArcanistArcanistLinterTestCase { public function testCSSLintLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/csslint/', - new ArcanistCSSLintLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/csslint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistClosureLinterTestCase.php b/src/lint/linter/__tests__/ArcanistClosureLinterTestCase.php index 9619f6ab..6b9b72dc 100644 --- a/src/lint/linter/__tests__/ArcanistClosureLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistClosureLinterTestCase.php @@ -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); } } diff --git a/src/lint/linter/__tests__/ArcanistCoffeeLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCoffeeLintLinterTestCase.php index 9fe5ef33..e359acd4 100644 --- a/src/lint/linter/__tests__/ArcanistCoffeeLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCoffeeLintLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistCoffeeLintLinterTestCase extends ArcanistArcanistLinterTestCase { public function testCoffeeLintLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/coffeelint/', - new ArcanistCoffeeLintLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/coffeelint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistCommitLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCommitLinterTestCase.php index 293cf7cc..6b612ec2 100644 --- a/src/lint/linter/__tests__/ArcanistCommitLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCommitLinterTestCase.php @@ -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/'); } } diff --git a/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php index a8993900..c2c7471b 100644 --- a/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php @@ -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/'); } } diff --git a/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php index 0a71f0e9..baecac9a 100644 --- a/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php @@ -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/'); } } diff --git a/src/lint/linter/__tests__/ArcanistFlake8LinterTestCase.php b/src/lint/linter/__tests__/ArcanistFlake8LinterTestCase.php index 1c487147..39cabfaf 100644 --- a/src/lint/linter/__tests__/ArcanistFlake8LinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistFlake8LinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistFlake8LinterTestCase extends ArcanistArcanistLinterTestCase { public function testFlake8Lint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/flake8/', - new ArcanistFlake8Linter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/flake8/'); } } diff --git a/src/lint/linter/__tests__/ArcanistGoLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistGoLintLinterTestCase.php index fb9c62eb..f906cea9 100644 --- a/src/lint/linter/__tests__/ArcanistGoLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistGoLintLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistGoLintLinterTestCase extends ArcanistArcanistLinterTestCase { public function testGoLintLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/golint/', - new ArcanistGoLintLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/golint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistHLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistHLintLinterTestCase.php index 581220b1..9a8fcf94 100644 --- a/src/lint/linter/__tests__/ArcanistHLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistHLintLinterTestCase.php @@ -1,9 +1,9 @@ executeTestsInDirectory( - dirname(__FILE__).'/hlint/', - new ArcanistHLintLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/hlint/'); } + } diff --git a/src/lint/linter/__tests__/ArcanistJSHintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistJSHintLinterTestCase.php index 95405999..b6c74e03 100644 --- a/src/lint/linter/__tests__/ArcanistJSHintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistJSHintLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistJSHintLinterTestCase extends ArcanistArcanistLinterTestCase { public function testJSHintLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/jshint/', - new ArcanistJSHintLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/jshint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistJSONLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistJSONLintLinterTestCase.php index 6589bd8d..14370151 100644 --- a/src/lint/linter/__tests__/ArcanistJSONLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistJSONLintLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistJSONLintLinterTestCase extends ArcanistArcanistLinterTestCase { public function testJSONLintLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/jsonlint/', - new ArcanistJSONLintLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/jsonlint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php b/src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php index 1913c0dc..6cfe206f 100644 --- a/src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistJSONLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistJSONLinterTestCase extends ArcanistArcanistLinterTestCase { public function testJSONLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/jsonlint/', - new ArcanistJSONLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/jsonlint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistJscsLinterTestCase.php b/src/lint/linter/__tests__/ArcanistJscsLinterTestCase.php index d72c44e0..78764cac 100644 --- a/src/lint/linter/__tests__/ArcanistJscsLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistJscsLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistJscsLinterTestCase extends ArcanistArcanistLinterTestCase { public function testJscsLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/jscs/', - new ArcanistJscsLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/jscs/'); } } diff --git a/src/lint/linter/__tests__/ArcanistLesscLinterTestCase.php b/src/lint/linter/__tests__/ArcanistLesscLinterTestCase.php index 9303d371..cca0a4f9 100644 --- a/src/lint/linter/__tests__/ArcanistLesscLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistLesscLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistLesscLinterTestCase extends ArcanistArcanistLinterTestCase { public function testLesscLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/lessc/', - new ArcanistLesscLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/lessc/'); } } diff --git a/src/lint/linter/__tests__/ArcanistLinterTestCase.php b/src/lint/linter/__tests__/ArcanistLinterTestCase.php index d8ec6a85..3fa94643 100644 --- a/src/lint/linter/__tests__/ArcanistLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistLinterTestCase.php @@ -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') diff --git a/src/lint/linter/__tests__/ArcanistMergeConflictLinterTestCase.php b/src/lint/linter/__tests__/ArcanistMergeConflictLinterTestCase.php index beed0190..9c54379d 100644 --- a/src/lint/linter/__tests__/ArcanistMergeConflictLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistMergeConflictLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistMergeConflictLinterTestCase extends ArcanistArcanistLinterTestCase { public function testMergeConflictLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/mergeconflict/', - new ArcanistMergeConflictLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/mergeconflict/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPEP8LinterTestCase.php b/src/lint/linter/__tests__/ArcanistPEP8LinterTestCase.php index 2a055bc8..41843d60 100644 --- a/src/lint/linter/__tests__/ArcanistPEP8LinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPEP8LinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistPEP8LinterTestCase extends ArcanistArcanistLinterTestCase { public function testPEP8Linter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/pep8/', - new ArcanistPEP8Linter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/pep8/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPhpLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPhpLinterTestCase.php index a3fc56ec..b4bf952c 100644 --- a/src/lint/linter/__tests__/ArcanistPhpLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPhpLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistPhpLinterTestCase extends ArcanistArcanistLinterTestCase { public function testPHPLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/php/', - new ArcanistPhpLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/php/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPhpcsLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPhpcsLinterTestCase.php index a88eded6..bff0ca07 100644 --- a/src/lint/linter/__tests__/ArcanistPhpcsLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPhpcsLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistPhpcsLinterTestCase extends ArcanistArcanistLinterTestCase { public function testPHPCSLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/phpcs/', - new ArcanistPhpcsLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/phpcs/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php index 0c259d58..01a7258d 100644 --- a/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php @@ -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); } } diff --git a/src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php index ced6d25d..19eb39cd 100644 --- a/src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php @@ -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/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php index 7f025a60..31d3c761 100644 --- a/src/lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistPyFlakesLinterTestCase extends ArcanistArcanistLinterTestCase { public function testPyflakesLinter() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/pyflakes/', - new ArcanistPyFlakesLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/pyflakes/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php index 093856a7..d1686c98 100644 --- a/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php @@ -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/'); } } diff --git a/src/lint/linter/__tests__/ArcanistRubyLinterTestCase.php b/src/lint/linter/__tests__/ArcanistRubyLinterTestCase.php index e8e37ba5..944d4891 100644 --- a/src/lint/linter/__tests__/ArcanistRubyLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistRubyLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistRubyLinterTestCase extends ArcanistArcanistLinterTestCase { public function testRubyLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/ruby/', - new ArcanistRubyLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/ruby/'); } } diff --git a/src/lint/linter/__tests__/ArcanistTextLinterTestCase.php b/src/lint/linter/__tests__/ArcanistTextLinterTestCase.php index 5fb6b220..e2aba519 100644 --- a/src/lint/linter/__tests__/ArcanistTextLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistTextLinterTestCase.php @@ -3,9 +3,7 @@ final class ArcanistTextLinterTestCase extends ArcanistArcanistLinterTestCase { public function testTextLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/text/', - new ArcanistTextLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/text/'); } } diff --git a/src/lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php b/src/lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php index f97f5b32..557ce0b5 100644 --- a/src/lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php @@ -4,9 +4,7 @@ final class ArcanistXHPASTLinterTestCase extends ArcanistArcanistLinterTestCase { public function testXHPASTLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/xhpast/', - new ArcanistXHPASTLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/xhpast/'); } } diff --git a/src/lint/linter/__tests__/ArcanistXMLLinterTestCase.php b/src/lint/linter/__tests__/ArcanistXMLLinterTestCase.php index d9e04a87..7dc12d0a 100644 --- a/src/lint/linter/__tests__/ArcanistXMLLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistXMLLinterTestCase.php @@ -7,9 +7,7 @@ final class ArcanistXMLLinterTestCase extends ArcanistArcanistLinterTestCase { public function testXMLLint() { - $this->executeTestsInDirectory( - dirname(__FILE__).'/xml/', - new ArcanistXMLLinter()); + $this->executeTestsInDirectory(dirname(__FILE__).'/xml/'); } }