From 771a11ae8cd059e278d9d3fa1f6d368df62ba36e Mon Sep 17 00:00:00 2001 From: Matt Dooley Date: Thu, 9 Mar 2017 15:04:40 -0500 Subject: [PATCH] Feature/blt phpcbf (#1019) * BLT-977: Adds phpcbf task. BLT-977: Updates fix phing targets. BLT-977: Cleans up comments and fix:phpcbf:files target. BLT-977: Adds files.php.custom.themes to phpcbf filesets. * Adds phpunit test for missing file param. * BLT-977: Prevents file-list script from hanging. * BAC-977: Adds additional phpunit tests for Fix task. * BAC-977: Fixes PHPCS violations. Ironically. * BAC-977: Updates phpunit tests to include phpcbf tests. --- phing/build.xml | 3 + phing/build.yml | 7 ++ phing/tasks/fix.xml | 61 ++++++++++ scripts/blt/phpcbf-file-list.sh | 36 ++++++ tests/phpunit/BltProject/FixTest.php | 159 +++++++++++++++++++++++++++ 5 files changed, 266 insertions(+) create mode 100644 phing/tasks/fix.xml create mode 100755 scripts/blt/phpcbf-file-list.sh create mode 100644 tests/phpunit/BltProject/FixTest.php diff --git a/phing/build.xml b/phing/build.xml index 60c501c23..4541911a4 100644 --- a/phing/build.xml +++ b/phing/build.xml @@ -71,6 +71,9 @@ + + + diff --git a/phing/build.yml b/phing/build.yml index 47baf3450..c5e4062d6 100644 --- a/phing/build.yml +++ b/phing/build.yml @@ -106,6 +106,13 @@ phpcs: haltonerror: true haltonwarning: true +phpcbf: + filesets: + - files.php.custom.modules + - files.php.tests + - files.php.custom.themes + - files.frontend.custom.themes + project: local: uri: ${project.local.protocol}://${project.local.hostname} diff --git a/phing/tasks/fix.xml b/phing/tasks/fix.xml new file mode 100644 index 000000000..6d17c4718 --- /dev/null +++ b/phing/tasks/fix.xml @@ -0,0 +1,61 @@ + + + + + + diff --git a/scripts/blt/phpcbf-file-list.sh b/scripts/blt/phpcbf-file-list.sh new file mode 100755 index 000000000..b84cb5dcb --- /dev/null +++ b/scripts/blt/phpcbf-file-list.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +GIT_ROOT=$( git rev-parse --show-toplevel ) +PHPCBF_BIN=${GIT_ROOT}/vendor/bin/phpcbf +FILENAME=$1 + +if [ ! -f $PHPCBF_BIN ]; + then + echo "PHP Code Beautifier was not found in this project's bin directory. Please run composer install." + exit 1 +fi + +if [ -z $FILENAME ]; + then + echo "Missing file list parameter." + exit 1 +fi + +# Get list of files to fix from input csv file. +LIST=$( cat $FILENAME | awk -F '^\"|\",\"*' '{print $2}' | sort -u ) + +# Replace report file contents with contents of $LIST. +> $FILENAME +if [ -z "$LIST" ]; + then + echo "No fixable files found." + exit + else + for i in $LIST; do + echo $i >> $FILENAME + done + echo "Files that can be fixed by PHPCBF:" + cat $FILENAME +fi + +exit diff --git a/tests/phpunit/BltProject/FixTest.php b/tests/phpunit/BltProject/FixTest.php new file mode 100644 index 000000000..b201d9f5c --- /dev/null +++ b/tests/phpunit/BltProject/FixTest.php @@ -0,0 +1,159 @@ +executePhpcbfFileListScript(); + $this->assertNotContains("PHP Code Beautifier was not found in this project's bin directory. Please run composer install.", $this->output); + $this->assertEquals(1, $this->returnVar); + } + + /** + * Tests the handling of a missing file parameter. + */ + public function testFixPhpcbfExitsWhenMissingArguments() { + $this->executePhpcbfFileListScript(); + $this->assertContains("Missing file list parameter.", $this->output); + $this->assertEquals(1, $this->returnVar); + } + + /** + * Tests that a file list is not generated, given a file without violations. + */ + public function testFixPhpcbfWithNotFixableFile() { + $file = $this->createTestFile('file_without_violations.php', FALSE); + $report = $this->createTestReport('report_without_violations.txt', $file); + $this->executePhpcbfFileListScript($report); + $this->assertNotContains("Missing file list parameter.", $this->output); + $this->assertContains("No fixable files found.", $this->output); + $this->assertEquals(0, $this->returnVar); + } + + /** + * Tests that a file list is generated, given a file with a fixable violation. + */ + public function testFixPhpcbfWithFixableFile() { + $file = $this->createTestFile('file_with_violations.php'); + $report = $this->createTestReport('report_with_violations.txt', $file); + $this->executePhpcbfFileListScript($report); + $this->assertNotContains("Missing file list parameter.", $this->output); + + foreach ($this->output as $value) { + if (!file_exists($value)) { + $this->assertEquals('Files that can be fixed by PHPCBF:', $value); + } + else { + $this->assertStringEndsWith('file_with_violations.php', $value); + $this->assertStringEndsNotWith('file_without_violations.php', $value); + $phpcbf_first_pass = $this->executePhpcbf($value); + $this->assertContains("Processing file_with_violations.php", $phpcbf_first_pass); + + // Run phpcbf a second time to ensure the file has been fixed. + $phpcbf_second_pass = $this->executePhpcbf($value); + $this->assertContains("No fixable errors were found", $phpcbf_second_pass); + } + } + $this->assertEquals(0, $this->returnVar); + } + + /** + * Helper method that executes the phpcbf-file-list.sh script. + * + * @param string $file + * The full path to the file to be processed. + */ + protected function executePhpcbfFileListScript($file = '') { + chdir($this->projectDirectory); + $command = "sh vendor/acquia/blt/scripts/blt/phpcbf-file-list.sh $file"; + exec($command, $this->output, $this->returnVar); + } + + /** + * Helper method to run phpcbf. + * + * @param string $file + * The full path to the file to be processed. + * + * @return string + * The output of the executed phpcbf command. + */ + protected function executePhpcbf($file) { + chdir($this->projectDirectory); + return shell_exec("./vendor/bin/phpcbf --standard=vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml {$file}"); + } + + /** + * Helper method to create a phpcs csv file report. + * + * @param string $report_name + * The name of the report file to be generated. + * @param string $file + * The full path to the file to be processed. + * + * @return string + * The full path to the generated csv report file. + */ + protected function createTestReport($report_name, $file) { + chdir($this->projectDirectory); + $report_path = sys_get_temp_dir() . '/' . $report_name; + exec("./vendor/bin/phpcs --standard=vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml --report=csv --report-file={$report_path} {$file}"); + return $report_path; + } + + /** + * Helper method to create a php file. + * + * @param string $file_name + * The name of the file to be generated. + * @param bool $with_violations + * Boolean indicating whether the file should contain a fixable violation. + * + * @return string + * The full path to the generated php file. + */ + protected function createTestFile($file_name, $with_violations = TRUE) { + $file_path = sys_get_temp_dir() . '/' . $file_name; + + $file_contents = "