Skip to content

Commit

Permalink
Issue #3219184: Remove Behat for running functional test and use comm…
Browse files Browse the repository at this point in the history
…ands directly
  • Loading branch information
mglaman committed Jun 23, 2021
1 parent 693c8e9 commit 0998a35
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 248 deletions.
29 changes: 29 additions & 0 deletions .github/scripts/validate-decpreation-index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

require_once __DIR__ . '/../../vendor/autoload.php';

$data = file_get_contents(__DIR__ . '/../../deprecation-index.yml');
if ($data === false) {
throw new \RuntimeException('The deprecation-index.yml could not be read.');
}

// Ensure YAML is parseable.
try {
$parsed_data = Yaml::parse($data);
}
catch (ParseException $exception) {
throw new \RuntimeException(
"The YAML was not valid. This is often caused by unescaped quotes.
Please use `'` to escape a single quote.\n\n{$exception->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.');
}
70 changes: 43 additions & 27 deletions .github/workflows/functional_test__rector_examples.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 6 additions & 32 deletions README-automated-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
163 changes: 0 additions & 163 deletions features/bootstrap/FeatureContext.php

This file was deleted.

15 changes: 0 additions & 15 deletions features/deprecation-index.feature

This file was deleted.

9 changes: 0 additions & 9 deletions features/rector_examples.feature

This file was deleted.

Empty file removed features/tmp/.placeholder
Empty file.

0 comments on commit 0998a35

Please sign in to comment.