Skip to content

Commit

Permalink
MOBILE-3947 ci: Configure coverage reports
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Jan 15, 2024
1 parent eec775b commit 3ea1836
Show file tree
Hide file tree
Showing 9 changed files with 897 additions and 3 deletions.
240 changes: 240 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
name: Coverage

on: [push, pull_request]

jobs:

build:
runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v3
with:
path: app

- uses: actions/setup-node@v3
with:
node-version-file: 'app/.nvmrc'

- name: Install npm dependencies
working-directory: app
run: npm ci --no-audit

- name: Build app
working-directory: app
run: npx ionic build --configuration=testing -- --source-map=true
env:
NODE_ENV: testing
NODE_OPTIONS: --max-old-space-size=16384
MOODLE_APP_COVERAGE: true

- name: Build Behat plugin
working-directory: app
run: ./scripts/build-behat-plugin.js ../plugin

- uses: actions/cache/save@v3
with:
key: build-${{ github.sha }}
path: |
app/www/**/*
app/node_modules/**/*
plugin/**/*
jest:
runs-on: ubuntu-latest
needs: build

steps:

- uses: actions/checkout@v3
with:
path: app

- uses: actions/setup-node@v3
with:
node-version-file: 'app/.nvmrc'

- uses: actions/cache/restore@v3
with:
key: build-${{ github.sha }}
path: |
app/www/**/*
app/node_modules/**/*
plugin/**/*
- name: Run Jest tests
working-directory: app
run: |
NODE_ENV=testing npx gulp
npx jest --coverage --coverageReporters=json
mkdir ../coverage-jsons
mv coverage/coverage-final.json ../coverage-jsons/jest.json
- uses: actions/upload-artifact@v3
with:
name: coverage-jsons
path: coverage-jsons

behat:
runs-on: ubuntu-latest
needs: build
continue-on-error: true

strategy:
matrix:
tags: [
'@app_others',
'@block_timeline',
'@competency',
'@core_auth',
'@core_calendar',
'@core_comments',
'@core_course',
'@core_grades',
'@core_message',
'@core_reminders',
'@core_reportbuilder',
'@core_search',
'@core_settings',
'@core_user',
'@mod_assign',
'@mod_bigbluebuttonbn',
'@mod_book',
'@mod_chat',
'@mod_choice',
'@mod_data',
'@mod_feedback',
'@mod_forum',
'@mod_glossary',
'@mod_lesson',
'@mod_quiz',
'@mod_scorm',
'@mod_survey',
'@mod_workshop',
]

services:

postgres:
image: postgres:13
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3

steps:

- uses: actions/checkout@v3
with:
path: app

- uses: actions/setup-node@v3
with:
node-version-file: 'app/.nvmrc'

- uses: shivammathur/setup-php@v2
with:
php-version: 8.1
ini-values: max_input_vars=5000
coverage: none

- uses: actions/cache/restore@v3
with:
key: build-${{ github.sha }}
path: |
app/www/**/*
app/node_modules/**/*
plugin/**/*
- name: Launch Docker images
working-directory: app
run: |
docker run -d --rm -p 8001:80 --name moodleapp -v ./www:/usr/share/nginx/html -v ./nginx.conf:/etc/nginx/conf.d/default.conf nginx:alpine
docker run -d --rm -p 8002:80 --name bigbluebutton moodlehq/bigbluebutton_mock:latest
- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci dev-main
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
- name: Install Behat Snapshots plugin
run: moodle-plugin-ci add-plugin NoelDeMartin/moodle-local_behatsnapshots

- name: Install moodle-plugin-ci
run: moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
env:
DB: pgsql
MOODLE_BRANCH: MOODLE_403_STABLE
MOODLE_BEHAT_IONIC_WWWROOT: http://localhost:8001
MOODLE_BEHAT_DEFAULT_BROWSER: chrome

- name: Update config
run: |
moodle-plugin-ci add-config "\$CFG->behat_extraallowedsettings = ['forced_plugin_settings'];"
moodle-plugin-ci add-config "\$CFG->forced_plugin_settings = ['local_moodleappbehat' => ['coverage_path' => '$GITHUB_WORKSPACE/moodle/coverage/']];"
moodle-plugin-ci add-config 'define("TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER", "http://localhost:8002/hash" . sha1($CFG->wwwroot));'
- name: Run Behat tests
run: moodle-plugin-ci behat --auto-rerun 3 --profile chrome --tags="@app&&~@local&&~@performance&&$BEHAT_TAGS"
env:
BEHAT_TAGS: ${{ matrix.tags }}

- name: Merge Coverage jsons
working-directory: app
run: |
mkdir ../coverage-jsons
mkdir -p ../moodle/coverage/
echo "{}" > ../moodle/coverage/base.json
npx nyc merge ../moodle/coverage/ ../coverage-jsons/$BEHAT_TAGS.json
env:
BEHAT_TAGS: ${{ matrix.tags }}

- uses: actions/upload-artifact@v3
with:
name: coverage-jsons
path: coverage-jsons

report:
runs-on: ubuntu-latest
needs: [jest, behat]

steps:

- uses: actions/checkout@v3
with:
path: app

- uses: actions/setup-node@v3
with:
node-version-file: 'app/.nvmrc'

- uses: actions/cache/restore@v3
with:
key: build-${{ github.sha }}
path: |
app/www/**/*
app/node_modules/**/*
- uses: actions/download-artifact@v3
with:
name: coverage-jsons
path: coverage-jsons

- name: Generate final report
working-directory: app
run: |
npx nyc merge ../coverage-jsons/ .nyc_output/out.json
npx nyc report --reporter=html-spa
cp .nyc_output/out.json coverage/coverage-final.json
- name: Upload Coverage report
uses: actions/upload-artifact@v3
with:
name: coverage-html
path: app/coverage/*
2 changes: 1 addition & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"builder": "@angular-builders/custom-webpack:dev-server",
"options": {
"browserTarget": "app:build",
"disableHostCheck": true,
Expand Down
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ module.exports = {
setupFilesAfterEnv: ['<rootDir>/src/testing/setup.ts'],
testMatch: ['**/?(*.)test.ts'],
collectCoverageFrom: [
'src/**/*.{ts,html}',
'src/**/*.ts',
'!src/**/*.{test,stories}.ts',
'!src/assets/**/*',
'!src/testing/**/*',
'!src/storybook/**/*',
'!src/core/initializers/index.ts',
'!src/core/features/emulators/services/zip.ts',
],
transformIgnorePatterns: ['node_modules/(?!@stencil|@angular|@ionic|@moodlehq|@ngx-translate|swiper)'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),
Expand Down
33 changes: 33 additions & 0 deletions local_moodleappbehat/tests/behat/behat_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_once(__DIR__ . '/behat_app_helper.php');

use Behat\Behat\Hook\Scope\ScenarioScope;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\DriverException;
use Behat\Mink\Exception\ExpectationException;
Expand All @@ -46,6 +47,10 @@ class behat_app extends behat_app_helper {

protected $featurepath = '';

protected $coveragepath;
protected $scenarioslug;
protected $scenariolaststep;

/**
* @BeforeScenario
*/
Expand All @@ -56,10 +61,38 @@ public function before_scenario(ScenarioScope $scope) {
return;
}

$steps = $scope->getScenario()->getSteps();

$this->scenarioslug = $this->get_scenario_slug($scope);
$this->scenariolaststep = $steps[count($steps) - 1];
$this->featurepath = dirname($feature->getFile());
$this->coveragepath = get_config('local_moodleappbehat', 'coverage_path') ?: ($this->featurepath . DIRECTORY_SEPARATOR . 'coverage' . DIRECTORY_SEPARATOR);
$this->configure_performance_logs();
}

/**
* @AfterStep
*/
public function after_step(AfterStepScope $scope) {
$step = $scope->getStep();

if ($step !== $this->scenariolaststep || empty($this->coveragepath)) {
return;
}

if (!is_dir($this->coveragepath)) {
if (!@mkdir($this->coveragepath, 0777, true)) {
throw new Exception("Cannot create {$this->coveragepath} directory.");
}
}

$coverage = $this->runtime_js('getCoverage()');

if (!is_null($coverage)) {
file_put_contents($this->coveragepath . $this->scenarioslug . '.json', $coverage);
}
}

/**
* Opens the Moodle App in the browser and optionally logs in.
*
Expand Down
19 changes: 18 additions & 1 deletion local_moodleappbehat/tests/behat/behat_app_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');

use Behat\Behat\Hook\Scope\ScenarioScope;
use Behat\Mink\Exception\DriverException;
use Moodle\BehatExtension\Exception\SkippedException;

Expand Down Expand Up @@ -351,7 +352,7 @@ protected function notify_unload(): void {
* @return mixed Result.
*/
protected function runtime_js(string $script) {
return $this->evaluate_script("window.behat.$script");
return $this->evaluate_script("window.behat?.$script");
}

/**
Expand Down Expand Up @@ -466,6 +467,22 @@ function($context) use ($successXPath) {
$this->i_wait_the_app_to_restart();
}

/**
* Get scenario slug.
*
* @param ScenarioScope $scope Scenario scope.
* @return string Slug.
*/
protected function get_scenario_slug(ScenarioScope $scope): string {
$text = $scope->getFeature()->getTitle() . ' ' . $scope->getScenario()->getTitle();
$text = trim($text);
$text = strtolower($text);
$text = preg_replace('/\s+/', '-', $text);
$text = preg_replace('/[^a-z0-9-]/', '', $text);

return $text;
}

/**
* Returns the current mobile url scheme of the site.
*/
Expand Down
Loading

0 comments on commit 3ea1836

Please sign in to comment.