Skip to content

Commit a138b4b

Browse files
committed
[TASK] Introduce testing and linting for code snippets (#118)
Releases: main, 13.4, 12.4 (cherry picked from commit 5560626)
1 parent 47e33e2 commit a138b4b

File tree

8 files changed

+625
-39
lines changed

8 files changed

+625
-39
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Check and Fix Whitespace on Schedule
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
workflow_dispatch:
7+
8+
jobs:
9+
lint:
10+
if: github.repository_owner == 'TYPO3-documentation'
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
with:
16+
token: ${{ secrets.GITHUB_TOKEN }}
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v2
20+
with:
21+
python-version: '3.x'
22+
23+
- name: Install pre-commit
24+
run: pip install pre-commit
25+
26+
- name: Run pre-commit hooks and apply fixes
27+
id: pre-commit
28+
run: |
29+
# Run pre-commit with auto-fix and ignore exit code
30+
pre-commit run --all-files --hook-stage=manual --show-diff-on-failure || true
31+
# Check if any files were modified
32+
git diff --exit-code || echo "FIX_NEEDED=true" >> $GITHUB_ENV
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Create and push changes if needed
37+
if: env.FIX_NEEDED == 'true'
38+
id: create_branch
39+
run: |
40+
git config user.name "github-actions[bot]"
41+
git config user.email "github-actions[bot]@users.noreply.github.com"
42+
# Create a new branch for the changes
43+
BRANCH_NAME="fix/whitespace-$(date +'%Y%m%d%H%M%S')"
44+
git checkout -b "$BRANCH_NAME"
45+
git add .
46+
git commit -m "fix: apply whitespace fixes"
47+
git push origin "$BRANCH_NAME"
48+
echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV
49+
env:
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
52+
- name: Open Pull Request
53+
if: env.FIX_NEEDED == 'true'
54+
uses: repo-sync/pull-request@v2
55+
with:
56+
source_branch: ${{ env.branch_name }}
57+
destination_branch: ${{ github.ref_name }}
58+
pr_title: "Fix whitespace issues"
59+
pr_body: "This PR automatically applies whitespace fixes."
60+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/tests.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
lint:
9+
name: Linting
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
php:
14+
- '8.2'
15+
- '8.3'
16+
- '8.4'
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Lint PHP
22+
run: Build/Scripts/runTests.sh -p ${{ matrix.php }} -s lint
23+
24+
quality:
25+
name: Quality
26+
runs-on: ubuntu-latest
27+
env:
28+
php: '8.2'
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
33+
- name: Install testing system
34+
run: Build/Scripts/runTests.sh -p ${{ env.php }} -s composerUpdate
35+
36+
- name: Composer validate
37+
run: Build/Scripts/runTests.sh -p ${{ env.php }} -s composerValidate
38+
39+
- name: Composer normalize
40+
run: Build/Scripts/runTests.sh -p ${{ env.php }} -s composerNormalize -n
41+
42+
- name: CGL
43+
run: Build/Scripts/runTests.sh -n -p ${{ env.php }} -s cgl -n
44+
45+
- name: Lint YAML
46+
run: Build/Scripts/runTests.sh -p ${{ env.php }} -s yamlLint

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
.TemporaryItems
1414
.webprj
1515
nbproject
16+
/composer.json.testing
17+
/composer.lock
18+
/.Build/

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0 # Use the latest version
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer

Build/.php-cs-fixer.dist.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Config;
6+
use PhpCsFixer\Finder;
7+
8+
return (new Config())
9+
->setFinder(
10+
(new Finder())
11+
->in(__DIR__.'/../Documentation')
12+
)
13+
->setRiskyAllowed(true)
14+
->setRules([
15+
'@DoctrineAnnotation' => true,
16+
// @todo: Switch to @PER-CS2.0 once php-cs-fixer's todo list is done: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7247
17+
'@PER-CS1.0' => true,
18+
'array_indentation' => true,
19+
'array_syntax' => ['syntax' => 'short'],
20+
'cast_spaces' => ['space' => 'none'],
21+
// @todo: Can be dropped once we enable @PER-CS2.0
22+
'concat_space' => ['spacing' => 'one'],
23+
'declare_equal_normalize' => ['space' => 'none'],
24+
'declare_parentheses' => true,
25+
'dir_constant' => true,
26+
// @todo: Can be dropped once we enable @PER-CS2.0
27+
'function_declaration' => [
28+
'closure_fn_spacing' => 'none',
29+
],
30+
'function_to_constant' => ['functions' => ['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']],
31+
'type_declaration_spaces' => true,
32+
'global_namespace_import' => ['import_classes' => false, 'import_constants' => false, 'import_functions' => false],
33+
'list_syntax' => ['syntax' => 'short'],
34+
// @todo: Can be dropped once we enable @PER-CS2.0
35+
'method_argument_space' => true,
36+
'modernize_strpos' => true,
37+
'modernize_types_casting' => true,
38+
'native_function_casing' => true,
39+
'no_alias_functions' => true,
40+
'no_blank_lines_after_phpdoc' => true,
41+
'no_empty_phpdoc' => true,
42+
'no_empty_statement' => true,
43+
'no_extra_blank_lines' => true,
44+
'no_leading_namespace_whitespace' => true,
45+
'no_null_property_initialization' => true,
46+
'no_short_bool_cast' => true,
47+
'no_singleline_whitespace_before_semicolons' => true,
48+
'no_superfluous_elseif' => true,
49+
'no_trailing_comma_in_singleline' => true,
50+
'no_unneeded_control_parentheses' => true,
51+
'no_unused_imports' => true,
52+
'no_useless_nullsafe_operator' => true,
53+
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
54+
'php_unit_construct' => ['assertions' => ['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']],
55+
'php_unit_mock_short_will_return' => true,
56+
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
57+
'phpdoc_no_access' => true,
58+
'phpdoc_no_empty_return' => true,
59+
'phpdoc_no_package' => true,
60+
'phpdoc_scalar' => true,
61+
'phpdoc_trim' => true,
62+
'phpdoc_types' => true,
63+
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
64+
'return_type_declaration' => ['space_before' => 'none'],
65+
'single_quote' => true,
66+
'single_space_around_construct' => true,
67+
'single_line_comment_style' => ['comment_types' => ['hash']],
68+
// @todo: Can be dropped once we enable @PER-CS2.0
69+
'single_line_empty_body' => true,
70+
'trailing_comma_in_multiline' => ['elements' => ['arguments', 'arrays', 'match', 'parameters']],
71+
'whitespace_after_comma_in_array' => ['ensure_single_space' => true],
72+
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
73+
74+
// We need this for documentation!
75+
'no_useless_else' => false, // We want to preserve else with comments only
76+
]);

0 commit comments

Comments
 (0)