From 0998a353dd2ccb2f60a35509c410768632eb6a51 Mon Sep 17 00:00:00 2001 From: Matt Glaman Date: Wed, 16 Jun 2021 14:38:17 -0500 Subject: [PATCH] Issue #3219184: Remove Behat for running functional test and use commands directly --- .../scripts/validate-decpreation-index.php | 29 ++++ .../functional_test__rector_examples.yml | 70 +++++--- .github/workflows/phpstan.yml | 2 +- README-automated-testing.md | 38 +--- composer.json | 3 +- features/bootstrap/FeatureContext.php | 163 ------------------ features/deprecation-index.feature | 15 -- features/rector_examples.feature | 9 - features/tmp/.placeholder | 0 9 files changed, 81 insertions(+), 248 deletions(-) create mode 100644 .github/scripts/validate-decpreation-index.php delete mode 100644 features/bootstrap/FeatureContext.php delete mode 100644 features/deprecation-index.feature delete mode 100644 features/rector_examples.feature delete mode 100644 features/tmp/.placeholder diff --git a/.github/scripts/validate-decpreation-index.php b/.github/scripts/validate-decpreation-index.php new file mode 100644 index 00000000..9ed532a1 --- /dev/null +++ b/.github/scripts/validate-decpreation-index.php @@ -0,0 +1,29 @@ +getMessage()}" + ); +} + +# Unicode characters sometimes get added when copy & pasting. +# They are not intended and don't match as easily. +if (mb_detect_encoding($data, 'ASCII', true) === false) { + throw new \RuntimeException('The file contains non ASCII characters. + Please make sure `\` and spaces are only using ASCII characters.'); +} diff --git a/.github/workflows/functional_test__rector_examples.yml b/.github/workflows/functional_test__rector_examples.yml index 626382f8..974712a8 100644 --- a/.github/workflows/functional_test__rector_examples.yml +++ b/.github/workflows/functional_test__rector_examples.yml @@ -1,13 +1,27 @@ + name: functional_test__rector_examples # This test will run on every pull request, and on every commit on any branch on: [push, pull_request] jobs: + # TODO: Probably move to PHPUnit test instead. + validate_deprecation_index: + name: Validate deprecation index + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.0 + coverage: none # disable xdebug, pcov + tools: composer:v2 + - run: composer install + - run: php .github/scripts/validate-decpreation-index.php run_functional_test: name: Run functional test -# START: SHARED SETUP - runs-on: ubuntu-latest + # START: SHARED SETUP + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 @@ -19,39 +33,41 @@ jobs: # Uncomment to enable SSH access to Github Actions - https://github.com/marketplace/actions/debugging-with-tmate#getting-started # - name: Debugging with tmate # uses: mxschmitt/action-tmate@v2 -# END: SHARED SETUP -# START: SHARED DRUPAL INSTALL SETUP + # END: SHARED SETUP + # START: SHARED DRUPAL INSTALL SETUP - name: Setup Drupal - run: | - COMPOSER_MEMORY_LIMIT=-1 composer create-project drupal/recommended-project:~8 ../drupal --no-interaction - cd .. - mv drupal/* . - composer config minimum-stability dev - composer config prefer-stable true - composer config preferred-install dist - composer config repositories.drupal composer https://packages.drupal.org/8 - COMPOSER_MEMORY_LIMIT=-1 composer require drupal/core-dev:~8 --with-all-dependencies - # We add a local repository `repositories.0` which takes precendence over the packagist repository that is automatically added. + uses: bluehorndigital/setup-drupal@v1.0.1 + with: + version: '~8' + path: ~/drupal - name: Install Drupal Rector run: | - cd .. - composer config repositories.drupal-rector "{\"type\": \"path\", \"url\": \"$GITHUB_WORKSPACE\", \"options\": {\"symlink\": false}}" + cd ~/drupal composer require palantirnet/drupal-rector:@dev --no-progress - name: Install Drupal Rector Config run: | - cd .. + cd ~/drupal cp vendor/palantirnet/drupal-rector/rector.php . -# END: SHARED DRUPAL INSTALL SETUP - - name: Install PHPUnit in the Drupal site since it is required for some of the rules - run: | - cd .. - composer require phpunit/phpunit:~7.5 --no-progress + # END: SHARED DRUPAL INSTALL SETUP - name: Prepare rector_examples folder in the drupal modules directory run: | - cd .. + cd ~/drupal mkdir -p web/modules/custom cp -R vendor/palantirnet/drupal-rector/rector_examples web/modules/custom - - name: Install local dependencies, including Behat. - run: composer install - - name: Run Behat tests - run: vendor/bin/behat + # dry-run is expected to return exit code 1 if there are changes, which we are expecting to happen, here. + - name: Run rector against Drupal (dry-run) + run: | + cd ~/drupal + vendor/bin/rector process web/modules/custom/rector_examples --dry-run --debug || if (($? == 1)); then true; else false; fi + - name: Run rector against Drupal + run: | + cd ~/drupal + vendor/bin/rector process web/modules/custom/rector_examples --debug + # diff options: + # -r: recursive + # -u: show the joined context, like git diff + # -b: ignore whitespace + # -B: ignore lines that are only whitespace + - name: Check that the updated examples match expectations + run: | + diff -rubB rector_examples_updated ~/drupal/web/modules/custom/rector_examples diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index eb08916c..8fe91bd3 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: - php-version: 7.3 + php-version: 8.0 coverage: none # disable xdebug, pcov tools: composer:v2 extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, bcmath, gd, exif, iconv diff --git a/README-automated-testing.md b/README-automated-testing.md index a104d48a..43d0530b 100644 --- a/README-automated-testing.md +++ b/README-automated-testing.md @@ -2,40 +2,14 @@ ## Installation test -Github Workflow is used to test that this package can be installed. See `.github`. +GitHub Action workflows test that this package can be installed. See the workflows in `.github/workflows`. -## Rector automated functional tests using Behat +## Rector functional testing -Behat (the Php version of Cucumber) is used to run automated tests. +The functional test takes the `rector_examples` directory in this package and copies it to a Drupal code base. -This uses Linux / MacOS commands, so they need to be run from that environment. +The workflow then runs `vendor/bin/rector process web/modules/custom/rector_examples` to apply all of the appropriate Rector rules. -These tests assume that this repository is installed as a local composer package. This is necessary, because we need a full Drupal site to run the Rector tests. +Then, the `diff` command is run to verify the changes match the expected results as found in the `rector_examples_updated` directory. -Example setup: -``` -# This repository -/drupal-rector -# Drupal -/web/core -/web/index.php -# A Composer vendor directory -/vendor/bin/rector -... -``` - -The tests are located in `features` with a simple `/features/bootstrap/FeatureContext.php` context file which handles running Rector and comparing files. - -### Setup - -To run the Behat tests, you will need the setup mentioned above. See `.github/workflows/local_package.yml` for an example of how this is done. - -Then run `composer install` to install Behat in this repository's `vendor` directory. - -To run tests, run `vendor/bin/behat`. - -### Adding tests - -Tests should be pretty simple. By default, the main test feature `rector_examples.feature` will test the entire `rector_examples` folder and report any differences. Tests can also be made for individual files. - -The Behat tests make a copy of the file or folder we are going to test, so you don't have to worry about overwriting files in those directories. +To add new tests, create a sample file in `rector_examples` and a copy with the expected changes into `rector_examples_updated`. diff --git a/composer.json b/composer.json index 2c3b4e8f..0e5cec42 100644 --- a/composer.json +++ b/composer.json @@ -46,10 +46,11 @@ "minimum-stability": "dev", "prefer-stable": true, "require-dev": { - "behat/behat": "^3.6", + "php": "^8.0", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^0.12.82", "phpstan/phpstan-deprecation-rules": "^0.12.6", + "rector/rector-src": "~0.11.0", "symfony/yaml": "^5" } } diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php deleted file mode 100644 index 511b8a53..00000000 --- a/features/bootstrap/FeatureContext.php +++ /dev/null @@ -1,163 +0,0 @@ -iRunDrupalRectorOnThe("$this->temporaryFolderPath/$this->testPath"); - } - - /** - * @When I run Drupal Rector on the file/folder :path - */ - public function iRunDrupalRectorOnThe($path) { - chdir('..'); - - $output = NULL; - $return_value = NULL; - exec("vendor/bin/rector process $this->drupalRectorPath/$path", $output, $return_value); - - chdir($this->drupalRectorPath); - - if ($return_value !== 0) { - throw new Exception('Rector did not complete successfully.' . PHP_EOL - . 'Rector output:' . PHP_EOL - . join(PHP_EOL, $output) - ); - } - } - - /** - * @Given I create a test copy of the file/folder :path - */ - public function iCreateATestCopyOfThe($path) { - $this->testPath = $path; - - // Using Linux / MacOS commands, because they are simple and work well. - exec("rm -rf $this->temporaryFolderPath/$this->testPath"); - - // Make any necessary folders. - $path_parts = explode('/', $this->testPath); - if (count($path_parts) > 1) { - $directory_parts = $path_parts; - array_pop($directory_parts); - $directory = implode('/', $directory_parts); - - exec("mkdir -p $this->temporaryFolderPath/$directory"); - } - - exec("cp -R $this->testPath $this->temporaryFolderPath/$this->testPath"); - } - - /** - * @Then the test file/folder matches :path - */ - public function theTestMatches($path) { - // Using Linux / MacOS commands, because they are simple and work well. - $output = NULL; - $return_value = NULL; - /* - * -r: recursive - * -u: show the joined context, like git diff - * -b: ignore whitespace - * -B: ignore lines that are only whitespace - */ - exec("diff -rubB $path $this->temporaryFolderPath/$this->testPath", $output, $return_value); - - if ($return_value !== 0) { - throw new Exception('The test does not match.' . PHP_EOL . PHP_EOL - . join(PHP_EOL, $output) . PHP_EOL . PHP_EOL - ); - } - } - - /** - * @When I examine the :file file - */ - public function iExamineTheFile($file) { - $this->examinedFileContents = file_get_contents($file); - - if ($this->examinedFileContents === FALSE) { - throw new Exception("The file $file could not be read."); - } - } - - /** - * @Then The file is valid YAML - */ - public function theFileIsValidYaml() { - try { - $parsed_data = Yaml::parse($this->examinedFileContents); - } - catch (ParseException $exception) { - throw new Exception("The YAML was not valid. This is often caused by unescaped quotes. Please use `'` to escape a single quote.\n\n{$exception->getMessage()}"); - } - } - - /** - * @Then The file only uses ASCII characters. - */ - public function theFileOnlyHasASCIICharacters() { - if (mb_detect_encoding($this->examinedFileContents, 'ASCII', TRUE) === FALSE) { - throw new Exception('The file contains non ASCII characters. Please make sure `\` and spaces are only using ASCII characters.'); - } - } -} diff --git a/features/deprecation-index.feature b/features/deprecation-index.feature deleted file mode 100644 index cac25508..00000000 --- a/features/deprecation-index.feature +++ /dev/null @@ -1,15 +0,0 @@ -Feature: deprecation-index.yml - As a Drupal developer - I want the deprecation-index.yml file to parse cleanly - So that other systems can reference this file - - # Most often, quotes are not escaped. - Scenario: deprecation-index.yml is valid YAML - When I examine the "deprecation-index.yml" file - Then The file is valid YAML - - # Unicode characters sometimes get added when copy & pasting. - # They are not intended and don't match as easily. - Scenario: deprecation-index.yml only uses ASCII characters - When I examine the "deprecation-index.yml" file - Then The file only uses ASCII characters. diff --git a/features/rector_examples.feature b/features/rector_examples.feature deleted file mode 100644 index bcd8f255..00000000 --- a/features/rector_examples.feature +++ /dev/null @@ -1,9 +0,0 @@ -Feature: rector_examples module - As a Drupal developer - I want rector_example deprecations to be updated by Drupal Rector - So that I don't have to update them manually - - Scenario: Verify rector_examples updates match rector_examples_updated. - Given I create a test copy of the folder "rector_examples" - When I run Drupal Rector on the test folder - Then the test folder matches "rector_examples_updated" diff --git a/features/tmp/.placeholder b/features/tmp/.placeholder deleted file mode 100644 index e69de29b..00000000