From e153471c8e0941c231ae85f0c7c82ddd765f9ab0 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 12:36:51 +0530 Subject: [PATCH 01/11] Add testcase for Elementary_Theme class --- .../classes/test-class-elementary-theme.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/php/inc/classes/test-class-elementary-theme.php diff --git a/tests/php/inc/classes/test-class-elementary-theme.php b/tests/php/inc/classes/test-class-elementary-theme.php new file mode 100644 index 00000000..7d25c2b2 --- /dev/null +++ b/tests/php/inc/classes/test-class-elementary-theme.php @@ -0,0 +1,51 @@ +assertTrue( class_exists( 'Elementary_Theme\Elementary_Theme' ) ); + } + + /** + * Test if class Elementary_Theme is a singleton. + * + * @since 1.0.0 + */ + public function test_class_is_singleton() { + $this->assertTrue( Elementary_Theme::get_instance() instanceof Elementary_Theme ); + } + + /** + * Test if class Elementary_Theme has a method 'setup_hooks'. + * + * @since 1.0.0 + */ + public function test_class_has_method_setup_hooks() { + $this->assertTrue( method_exists( 'Elementary_Theme\Elementary_Theme', 'setup_hooks' ) ); + } + + /** + * Test if class Elementary_Theme has a method 'elementary_theme_support'. + * + * @since 1.0.0 + */ + public function test_class_has_method_elementary_theme_support() { + $this->assertTrue( method_exists( 'Elementary_Theme\Elementary_Theme', 'elementary_theme_support' ) ); + } +} From 417c6fa7917ddd55fd9e5d01a8e376a13463e443 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 13:13:05 +0530 Subject: [PATCH 02/11] Fix npm scripts --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a6b032fc..4ef263e3 100644 --- a/package.json +++ b/package.json @@ -57,9 +57,9 @@ "prepare": "husky install && npm run init", "start": "wp-scripts start", "test": "npm-run-all --parallel test:*", - "test:js": "wp-scripts test-unit-js --config=tests/js/jest.config.js", + "test:js": "wp-scripts test-unit-js --config=tests/js/jest.config.js --passWithNoTests", "test:js:watch": "npm run test:js -- --watch", - "pretest:php": "wp-env run composer 'install --no-interaction'", + "pretest:php": "wp-env run composer 'install --no-interaction --no-scripts'", "test:php": "wp-env run phpunit 'phpunit -c /var/www/html/wp-content/themes/$(basename \"$PWD\")/phpunit.xml.dist --verbose'", "wp-env": "wp-env" } From ab219e447fabeec11253cbaac1d1087710a7a196 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 13:14:20 +0530 Subject: [PATCH 03/11] Add package-json linting --- .lintstagedrc.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 1c39e238..3f1ee4b0 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -8,4 +8,7 @@ module.exports = { "**/*.{css,scss}": [ "npm run lint:css" ], + "package.json": [ + "npm run lint:package-json" + ], }; From e826e59d226c02e128b934a92905add1fc2a2744 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 13:34:45 +0530 Subject: [PATCH 04/11] Add script to detemine modified files --- .github/bin/determine-modified-files-count.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/bin/determine-modified-files-count.js diff --git a/.github/bin/determine-modified-files-count.js b/.github/bin/determine-modified-files-count.js new file mode 100644 index 00000000..5ec81272 --- /dev/null +++ b/.github/bin/determine-modified-files-count.js @@ -0,0 +1,33 @@ +/** + * Determine the modified files count. + * + * Usage: + * node determine-modified-files-count.js [path/to/dir] + * + * Example: + * node determine-modified-files-count.js "foo\/bar|bar*" "foo/bar/baz\nquux" "foo/bar" + * + * Output: 1 + */ +const args = process.argv.slice(2); +const pattern = args[0]; +const modifiedFiles = args[1].split('\n'); +const dirInclude = args[2]; + +let count; + +if ( 'all' === dirInclude ) { + count = modifiedFiles.reduce((count, file) => { + if (pattern.split('|').some(pattern => file.match(pattern))) { + return count; + } + + return count + 1; + }, 0); +} else { + count = modifiedFiles.filter( ( file ) => { + return file.match( pattern ); + } ).length; +} + +console.log( count ); From 42d951dfe643fddfee9a74ef290fc9fb2d4e31c1 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 13:35:06 +0530 Subject: [PATCH 05/11] Add workflow for test and measure --- .github/workflows/test-measure.yml | 228 +++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 .github/workflows/test-measure.yml diff --git a/.github/workflows/test-measure.yml b/.github/workflows/test-measure.yml new file mode 100644 index 00000000..5b4f824d --- /dev/null +++ b/.github/workflows/test-measure.yml @@ -0,0 +1,228 @@ +name: Test and Measure + +on: + push: + branches: + - main + pull_request: + types: + - opened + - synchronize + - ready_for_review + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }} + cancel-in-progress: true + +jobs: + pre-run: + name: 'Pre run' + runs-on: ubuntu-latest + outputs: + changed-file-count: ${{ steps.determine-file-counts.outputs.count }} + changed-css-count: ${{ steps.determine-file-counts.outputs.css-count }} + changed-js-count: ${{ steps.determine-file-counts.outputs.js-count }} + changed-php-count: ${{ steps.determine-file-counts.outputs.php-count }} + changed-gha-workflow-count: ${{ steps.determine-file-counts.outputs.gha-workflow-count }} + + steps: + - name: Checkout including last 2 commits + # Fetch last 2 commits if it's not a PR, so that we can determine the list of modified files. + if: ${{ github.base_ref == null }} + uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - name: Checkout + # Do usual checkout if it's a PR. + if: ${{ github.base_ref != null }} + uses: actions/checkout@v3 + + - name: Fetch base branch + # Only fetch base ref if it's a PR. + if: ${{ github.base_ref != null }} + run: git fetch --depth=1 --no-tags origin ${{ github.base_ref }} + + - name: Determine modified files for PR + if: ${{ github.base_ref != null }} + run: echo "MODIFIED_FILES=$(git diff --name-only FETCH_HEAD HEAD | base64 -w 0)" >> $GITHUB_ENV + + - name: Determine modified files for commit + if: ${{ github.base_ref == null }} + run: echo "MODIFIED_FILES=$(git diff --name-only HEAD~1 HEAD | base64 -w 0)" >> $GITHUB_ENV + + - id: determine-file-counts + name: Determine if modified files should make the workflow run continue + run: | + MODIFIED_FILES=$(echo "$MODIFIED_FILES" | base64 -d) + echo -e "Modified files:\n$MODIFIED_FILES\n" + + MODIFIED_FILES_DATA=$(node .github/bin/determine-modified-files-count.js "$IGNORE_PATH_REGEX" "$MODIFIED_FILES" "all") + CSS_FILE_COUNT=$(node .github/bin/determine-modified-files-count.js ".+\.s?css|package\.json|package-lock\.json" "$MODIFIED_FILES") + JS_FILE_COUNT=$(node .github/bin/determine-modified-files-count.js ".+\.(js|snap)|package\.json|package-lock\.json" "$MODIFIED_FILES") + PHP_FILE_COUNT=$(node .github/bin/determine-modified-files-count.js ".+\.php|composer\.(json|lock)|phpstan\.neon\.dist" "$MODIFIED_FILES") + GHA_WORKFLOW_COUNT=$(node .github/bin/determine-modified-files-count.js "(\.github\/workflows\/.+\.yml)" "$MODIFIED_FILES") + + echo "Changed file count: $MODIFIED_FILES_DATA" + echo "Changed CK theme CSS file count: $CSS_FILE_COUNT" + echo "Changed CK theme JS file count: $JS_FILE_COUNT" + echo "Changed CK theme PHP file count: $PHP_FILE_COUNT" + echo "Changed GHA workflow count: $GHA_WORKFLOW_COUNT" + + echo "::set-output name=count::$MODIFIED_FILES_DATA" + echo "::set-output name=css-count::$CSS_FILE_COUNT" + echo "::set-output name=js-count::$JS_FILE_COUNT" + echo "::set-output name=php-count::$PHP_FILE_COUNT" + echo "::set-output name=gha-workflow-count::$GHA_WORKFLOW_COUNT" + env: + # Ignore Paths: + # - .github/ + # - !.github/workflows + # - .wordpress-org/ + # - docs/ + IGNORE_PATH_REGEX: \.github\/(?!workflows)|\.wordpress-org\/|docs\/ + + lint-css: + needs: pre-run + if: needs.pre-run.outputs.changed-css-count > 0 || needs.pre-run.outputs.changed-gha-workflow-count > 0 + name: 'Lint CSS' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3.2.0 + with: + node-version-file: '.nvmrc' + cache: npm + + - name: Install Node dependencies + run: npm ci --ignore-scripts + env: + CI: true + + - name: Detect coding standard violations (stylelint) + run: npm run lint:css + + lint-js: + needs: pre-run + if: needs.pre-run.outputs.changed-js-count > 0 || needs.pre-run.outputs.changed-gha-workflow-count > 0 + name: 'Lint JS' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3.2.0 + with: + node-version-file: '.nvmrc' + cache: npm + + - name: Install Node dependencies + run: npm ci --ignore-scripts + env: + CI: true + + - name: Detect coding standard violations (eslint) + run: npm run lint:js + + unit-tests-js: + needs: pre-run + if: needs.pre-run.outputs.changed-js-count > 0 || needs.pre-run.outputs.changed-gha-workflow-count > 0 + name: 'Run JS unit tests' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3.2.0 + with: + node-version-file: '.nvmrc' + cache: npm + + - name: Install Node dependencies + run: npm ci --ignore-scripts + env: + CI: true + + - name: Run unit tests + run: npm run test:js + env: + CI: true + + lint-php: + needs: pre-run + if: needs.pre-run.outputs.changed-php-count > 0 || needs.pre-run.outputs.changed-gha-workflow-count > 0 + name: 'Lint PHP' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + coverage: none + tools: cs2pr + + - name: Get Composer Cache Directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Configure Composer cache + uses: actions/cache@v3.0.2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + - name: Install Composer dependencies + run: composer install --prefer-dist --optimize-autoloader --no-progress --no-interaction --no-scripts + + - name: Validate composer.json + run: composer --no-interaction validate --no-check-all + + - name: Detect coding standard violations (PHPCS) + run: vendor/bin/phpcs -q --report=checkstyle --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 | cs2pr --graceful-warnings + + unit-test-php: + name: "PHP Unit test" + runs-on: ubuntu-latest + needs: pre-run + steps: + # Note: The repeated `needs.pre-run.outputs.changed-php-count > 0` checks would be avoided if a step could short- + # circuit per . The reason why the if statement can't be put on the + # job as a whole is because the name is variable based on the matrix, and if the condition is not met then the + # name won't be interpolated in order to match the required jobs set up in branch protection. + - name: Notice + if: needs.pre-run.outputs.changed-php-count > 0 + run: echo "No PHP files were changed in CK theme so no PHP unit tests will run" + + - name: Checkout + if: needs.pre-run.outputs.changed-php-count > 0 + uses: actions/checkout@v3 + + - name: Setup Node + if: needs.pre-run.outputs.changed-php-count > 0 + uses: actions/setup-node@v3.2.0 + with: + node-version-file: '.nvmrc' + cache: npm + + - name: Install Node dependencies + if: needs.pre-run.outputs.changed-php-count > 0 + run: npm ci --ignore-scripts + env: + CI: true + + - name: Start WP environment + if: needs.pre-run.outputs.changed-php-count > 0 + run: npm run wp-env start + + - name: Run tests + if: ${{ needs.pre-run.outputs.changed-php-count > 0 }} + run: npm run test:php From 0c07b252d3d29d67d9beaf9eba60bfde265a38c8 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 13:36:09 +0530 Subject: [PATCH 06/11] Remove old test and measure workflow for css and js --- .github/workflows/lint-js-css.yml | 54 ------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 .github/workflows/lint-js-css.yml diff --git a/.github/workflows/lint-js-css.yml b/.github/workflows/lint-js-css.yml deleted file mode 100644 index c5495104..00000000 --- a/.github/workflows/lint-js-css.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: Linting - -on: - pull_request: - types: - - synchronize - - opened - - ready_for_review - - paths: - - '**.js' - - '.eslint*' - - '**.css' - - '**.scss' - - '.nvmrc' - - '.stylelint*' - - '**/package.json' - - 'package-lock.json' - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - lint: - name: Lint JS, CSS, package.json - runs-on: ubuntu-latest - timeout-minutes: 15 - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Setup Node - uses: actions/setup-node@v3 - with: - node-version-file: '.nvmrc' - cache: npm - - - name: Install dependencies - run: npm ci - - - name: JS Lint - run: npm run lint:js - - - name: CSS Lint - run: npm run lint:css - - - name: JS Lint Report - run: npm run lint:js:report - continue-on-error: true - - - name: package.json Lint - run: npm run lint:package-json From 862f0ef57d2a8f5b4f9352ec681991218d9cce8d Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 13:43:38 +0530 Subject: [PATCH 07/11] Fix composer lock file --- composer.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.lock b/composer.lock index 55523a01..6a2df1bc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "da2b9f39095eae52426769390fdb0373", + "content-hash": "75818bf257db6e08fd7ca7a6f759bd58", "packages": [], "packages-dev": [ { @@ -2675,9 +2675,9 @@ } ], "aliases": [], - "minimum-stability": "stable", + "minimum-stability": "dev", "stability-flags": [], - "prefer-stable": false, + "prefer-stable": true, "prefer-lowest": false, "platform": [], "platform-dev": [], From f776bd3f7dd95fbe3fc69233a596c044567a7f07 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 14:18:58 +0530 Subject: [PATCH 08/11] Add build step with label script --- .github/workflows/test-measure.yml | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/.github/workflows/test-measure.yml b/.github/workflows/test-measure.yml index 5b4f824d..4b148b29 100644 --- a/.github/workflows/test-measure.yml +++ b/.github/workflows/test-measure.yml @@ -226,3 +226,63 @@ jobs: - name: Run tests if: ${{ needs.pre-run.outputs.changed-php-count > 0 }} run: npm run test:php + + build-prod: + name: 'Build production' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3.2.0 + with: + node-version-file: '.nvmrc' + cache: npm + + - name: Install Node dependencies + run: npm ci --ignore-scripts + env: + CI: true + + - name: Build production + run: npm run build:prod + continue-on-error: true + env: + CI: true + + - name: Comment on PR with build status + - uses: actions/github-script@v6 + if: ${{ steps.build-prod.outputs.exit-code }} != 0 + with: + - name: build-passed-label + script: | + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['Build:Failing'] + }) + github.rest.issues.removeLabel({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + name: 'Build:Passing' + }) + if: ${{ steps.build-prod.outputs.exit-code }} == 0 + - name: build-failed-label + script: | + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['Build:Passing'] + }) + github.rest.issues.removeLabel({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + name: 'Build:Failing' + }) + + From 565f562214b191b78d9b091061005f947c4cadba Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 15:14:49 +0530 Subject: [PATCH 09/11] Fix syntax error --- .github/workflows/test-measure.yml | 49 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test-measure.yml b/.github/workflows/test-measure.yml index 4b148b29..85091c55 100644 --- a/.github/workflows/test-measure.yml +++ b/.github/workflows/test-measure.yml @@ -246,43 +246,44 @@ jobs: CI: true - name: Build production + id: build run: npm run build:prod continue-on-error: true env: CI: true - name: Comment on PR with build status - - uses: actions/github-script@v6 - if: ${{ steps.build-prod.outputs.exit-code }} != 0 - with: - - name: build-passed-label - script: | - github.rest.issues.addLabels({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - labels: ['Build:Failing'] - }) - github.rest.issues.removeLabel({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - name: 'Build:Passing' - }) - if: ${{ steps.build-prod.outputs.exit-code }} == 0 - - name: build-failed-label - script: | - github.rest.issues.addLabels({ + uses: actions/github-script@v6 + with: + script: | + if ( '${{ steps.build.outcome }}' != 'success' ) { + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['Build:Failing'] + }) + if ( ${{ github.event.label.name == 'Build:Passing' }} ) { + github.rest.issues.removeLabel({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - labels: ['Build:Passing'] + name: 'Build:Passing' }) + } + } else { + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['Build:Passing'] + }) + if ( ${{ github.event.label.name == 'Build:Failing' }} ) { github.rest.issues.removeLabel({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, name: 'Build:Failing' }) - - + } + } From 8aca57ef438cf52594a561d18b391dc08d7b288f Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 15:23:03 +0530 Subject: [PATCH 10/11] Remove seperate PHPCS inspection workflow --- .github/workflows/phpcs_on_pull_request.yml | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .github/workflows/phpcs_on_pull_request.yml diff --git a/.github/workflows/phpcs_on_pull_request.yml b/.github/workflows/phpcs_on_pull_request.yml deleted file mode 100644 index eb3690fe..00000000 --- a/.github/workflows/phpcs_on_pull_request.yml +++ /dev/null @@ -1,17 +0,0 @@ -on: pull_request - -name: Inspections -jobs: - runPHPCSInspection: - name: Run PHPCS inspection - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - ref: ${{ github.event.pull_request.head.sha }} - - name: Run PHPCS inspection - uses: rtCamp/action-phpcs-code-review@v2 - env: - GH_BOT_TOKEN: ${{ secrets.GH_BOT_TOKEN }} - with: - args: "WordPress,WordPress-Core,WordPress-Docs" From fa790fdec05ed3da36e1994bf92d935e5f99d627 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Fri, 27 May 2022 19:44:45 +0530 Subject: [PATCH 11/11] Add modified file condition on build-prod job --- .github/workflows/test-measure.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-measure.yml b/.github/workflows/test-measure.yml index 85091c55..d29e4ce8 100644 --- a/.github/workflows/test-measure.yml +++ b/.github/workflows/test-measure.yml @@ -228,6 +228,8 @@ jobs: run: npm run test:php build-prod: + needs: pre-run + if: needs.pre-run.outputs.changed-js-count > 0 || needs.pre-run.outputs.changed-gha-workflow-count > 0 || needs.pre-run.outputs.changed-css-count > 0 name: 'Build production' runs-on: ubuntu-latest steps: