diff --git a/src/classes/check/code_sniffer.php b/src/classes/check/code_sniffer.php new file mode 100644 index 0000000..bb5aafa --- /dev/null +++ b/src/classes/check/code_sniffer.php @@ -0,0 +1,114 @@ +, all contributors + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * - Neither the name of the project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * This software is provided by the copyright holders and contributors "as is" + * and any express or implied warranties, including, but not limited to, the + * implied warranties of merchantability and fitness for a particular purpose + * are disclaimed. in no event shall the copyright owner or contributors be + * liable for any direct, indirect, incidental, special, exemplary, or + * consequential damages (including, but not limited to, procurement of + * substitute goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether in + * contract, strict liability, or tort (including negligence or otherwise) + * arising in any way out of the use of this software, even if advised of the + * possibility of such damage. + * + * @package php-commit-hooks + * @version $Revision$ + * @license http://www.opensource.org/licenses/bsd-license.html New BSD license + */ + +include_once 'PHP/CodeSniffer.php'; + +/** + * Check for valid files + * + * Implements linter checks for all added or modified files. + * + * @package php-commit-hooks + * @version $Revision$ + * @license http://www.opensource.org/licenses/bsd-license.html New BSD license + */ +class pchCodeSnifferCheck extends pchCheck +{ + /** + * Configured standard to check files with + * + * @var array + */ + protected $standard; + + /** + * Construct from CodeSniffer standard name + * + * @param string $standard + * @return void + */ + public function __construct( $standard ) + { + $this->standard = $standard; + } + + /** + * Validate the current check + * + * Validate the check on the specified repository. Returns an array of + * found issues. + * + * @param pchRepository $repository + * @return void + */ + public function validate( pchRepository $repository ) + { + $cs = new PHP_CodeSniffer(); + $cs->process( array(), $this->standard ); + + foreach ( $this->getChangedFiles( $repository ) as $file ) + { + $cs->processFile( $file, stream_get_contents( $this->getFileContents( $repository, $file ) ) ); + } + + $issues = array(); + foreach ( $cs->getFilesErrors() as $file => $messages ) + { + foreach ( $messages['errors'] as $errors ) + { + foreach ( $errors as $subErrors ) + { + foreach ( $subErrors as $error ) + { + $issues[] = new pchIssue( E_ERROR, $file, null, $error['source'] . ': ' . $error['message'] ); + } + } + } + + foreach ( $messages['warnings'] as $errors ) + { + foreach ( $errors as $subErrors ) + { + foreach ( $subErrors as $error ) + { + $issues[] = new pchIssue( E_WARNING, $file, null, $error['source'] . ': ' . $error['message'] ); + } + } + } + } + + return $issues; + } +} + diff --git a/src/environment.php b/src/environment.php index 21330f3..620c41c 100644 --- a/src/environment.php +++ b/src/environment.php @@ -11,6 +11,7 @@ require __DIR__ . '/classes/reporter/mail.php'; require __DIR__ . '/classes/check.php'; +require __DIR__ . '/classes/check/code_sniffer.php'; require __DIR__ . '/classes/check/commit_message.php'; require __DIR__ . '/classes/check/lint.php'; require __DIR__ . '/classes/check/lint/base.php'; diff --git a/tests/check/code_sniffer_tests.php b/tests/check/code_sniffer_tests.php new file mode 100644 index 0000000..cbcb2a6 --- /dev/null +++ b/tests/check/code_sniffer_tests.php @@ -0,0 +1,72 @@ +, all contributors + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * - Neither the name of the project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * This software is provided by the copyright holders and contributors "as is" + * and any express or implied warranties, including, but not limited to, the + * implied warranties of merchantability and fitness for a particular purpose + * are disclaimed. in no event shall the copyright owner or contributors be + * liable for any direct, indirect, incidental, special, exemplary, or + * consequential damages (including, but not limited to, procurement of + * substitute goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether in + * contract, strict liability, or tort (including negligence or otherwise) + * arising in any way out of the use of this software, even if advised of the + * possibility of such damage. + * + * @package php-commit-hooks + * @version $Revision$ + * @license http://www.opensource.org/licenses/bsd-license.html New BSD license + */ + +/** + * Tests for the commit message parser + */ +class pchCodeSnifferCheckTests extends PHPUnit_Framework_TestCase +{ + /** + * Return test suite + * + * @return PHPUnit_Framework_TestSuite + */ + public static function suite() + { + return new PHPUnit_Framework_TestSuite( __CLASS__ ); + } + + public function testValidLints() + { + $parser = new pchCodeSnifferCheck( 'Arbit' ); + + $this->assertEquals( + array( + new pchIssue( E_ERROR, 'cs_errors.php', null, 'PEAR.Classes.ClassDeclaration: Opening brace of a class must be on the line after the definition' ), + ), + $parser->validate( new pchRepositoryVersion( __DIR__ . '/../data/', 4 ) ) + ); + } + + public function testNoErrors() + { + $parser = new pchCodeSnifferCheck( 'Arbit' ); + + $this->assertEquals( + array(), + $parser->validate( new pchRepositoryVersion( __DIR__ . '/../data/', 5 ) ) + ); + } +} + diff --git a/tests/check_suite.php b/tests/check_suite.php index 8ee607a..3158af9 100644 --- a/tests/check_suite.php +++ b/tests/check_suite.php @@ -43,6 +43,7 @@ * Commit message parser tests */ require 'check/lint_tests.php'; +require 'check/code_sniffer_tests.php'; /** * Test suite for pch @@ -60,6 +61,7 @@ public function __construct() $this->setName( 'php-commit-hooks - checks' ); $this->addTest( pchLintCheckTests::suite() ); + $this->addTest( pchCodeSnifferCheckTests::suite() ); } /** diff --git a/tests/data/db/current b/tests/data/db/current index 00750ed..7ed6ff8 100644 --- a/tests/data/db/current +++ b/tests/data/db/current @@ -1 +1 @@ -3 +5 diff --git a/tests/data/db/rep-cache.db b/tests/data/db/rep-cache.db index 174c2ed..2441f9a 100644 Binary files a/tests/data/db/rep-cache.db and b/tests/data/db/rep-cache.db differ diff --git a/tests/data/db/revprops/0/4 b/tests/data/db/revprops/0/4 new file mode 100644 index 0000000..905e6c9 --- /dev/null +++ b/tests/data/db/revprops/0/4 @@ -0,0 +1,14 @@ +K 10 +svn:author +V 4 +kore +K 8 +svn:date +V 27 +2010-05-17T14:24:47.801407Z +K 7 +svn:log +V 44 +- Added: File with one intentional CS error + +END diff --git a/tests/data/db/revprops/0/5 b/tests/data/db/revprops/0/5 new file mode 100644 index 0000000..de99a0b --- /dev/null +++ b/tests/data/db/revprops/0/5 @@ -0,0 +1,14 @@ +K 10 +svn:author +V 4 +kore +K 8 +svn:date +V 27 +2010-05-17T14:25:52.484765Z +K 7 +svn:log +V 17 +# Fixed CS error + +END diff --git a/tests/data/db/revs/0/4 b/tests/data/db/revs/0/4 new file mode 100644 index 0000000..fb2e70f Binary files /dev/null and b/tests/data/db/revs/0/4 differ diff --git a/tests/data/db/revs/0/5 b/tests/data/db/revs/0/5 new file mode 100644 index 0000000..8b03db0 Binary files /dev/null and b/tests/data/db/revs/0/5 differ diff --git a/tests/data/db/txn-current b/tests/data/db/txn-current index 00750ed..7ed6ff8 100644 --- a/tests/data/db/txn-current +++ b/tests/data/db/txn-current @@ -1 +1 @@ -3 +5