diff --git a/.github/actions/ci-save-split-tests/action.yml b/.github/actions/ci-save-split-tests/action.yml new file mode 100644 index 000000000..2f9e8896f --- /dev/null +++ b/.github/actions/ci-save-split-tests/action.yml @@ -0,0 +1,17 @@ +name: 'Save split-tests' +description: 'Save Junit test results and timing data, to better split future tests' + +inputs: + results_path: + description: 'Glob pattern to the JUnit files to save' + required: true + +# This should be run once the results from all runs are collected. +runs: + using: composite + steps: + - name: Save test reports + uses: actions/cache@v2 + with: + path: ${{ inputs.results_path }} + key: tests-reports-${{ github.ref }}-${{ github.sha }}-${{ github.run_id }} diff --git a/.github/actions/ci-setup-assets/action.yml b/.github/actions/ci-setup-assets/action.yml new file mode 100644 index 000000000..6db13ed24 --- /dev/null +++ b/.github/actions/ci-setup-assets/action.yml @@ -0,0 +1,26 @@ +name: 'Setup Rails assets' +description: 'Pre-compile and cache the app assets' + +runs: + using: composite + steps: + - name: Assets cache + uses: actions/cache@v2 + with: + path: | + public/assets + public/packs-test + tmp/cache/webpacker + key: asset-cache-${{ runner.os }}-${{ github.ref }}-${{ github.sha }} + restore-keys: | + asset-cache-${{ runner.os }}-${{ github.ref }}-${{ github.sha }} + asset-cache-${{ runner.os }}-${{ github.ref }}- + asset-cache-${{ runner.os }}- + + - name: Precompile assets + env: + RAILS_ENV: test + run: | + rm bin/yarn + bin/rails assets:precompile --trace + shell: bash diff --git a/.github/actions/ci-setup-rails/action.yml b/.github/actions/ci-setup-rails/action.yml new file mode 100644 index 000000000..7f4b9edb8 --- /dev/null +++ b/.github/actions/ci-setup-rails/action.yml @@ -0,0 +1,31 @@ +name: 'Setup Rails app' +description: 'Setup the environment for running the Rails app' + +runs: + using: composite + steps: + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + + - name: Setup Node + uses: actions/setup-node@v2 + with: + node-version: '14' + cache: 'yarn' + + - name: Install Node modules + run: yarn install --frozen-lockfile + shell: bash + + - name: Setup environment variables + run: cp config/env.example .env + shell: bash + + - name: Setup test database + env: + RAILS_ENV: test + DATABASE_URL: "postgres://tps_test@localhost:5432/tps_test" + run: bin/rails db:create db:schema:load db:migrate + shell: bash diff --git a/.github/actions/ci-setup-split-tests/action.yml b/.github/actions/ci-setup-split-tests/action.yml new file mode 100644 index 000000000..e49f4c2f4 --- /dev/null +++ b/.github/actions/ci-setup-split-tests/action.yml @@ -0,0 +1,46 @@ +name: 'Setup split-tests' +description: 'Setup the environment for splitting tests' + +inputs: + results_path: + description: 'Glob pattern to the JUnit files to save' + required: true + +runs: + using: composite + steps: + - name: Setup split_tests binary + run: | + curl --no-progress-meter -L https://github.com/leonid-shevtsov/split_tests/releases/download/v0.3.0/split_tests.linux.gz | gunzip -c > split_tests + chmod +x split_tests + shell: bash + + - name: Generate an unique random value + run: echo dummy_random_value=$RANDOM >> $GITHUB_ENV + shell: bash + + # Restore previous runs timing from the cache. + # + # NB: at the end of the job, the `actions/cache@v2` action will attempt + # to save the results back to the same key. However at this stage only + # tests results for a single instance will be available. + # + # To avoid the cache being overwritten with this single result, we define + # a random cache key, which the action will use to save the single-instance + # report to a dummy location. + # + # The actual retrieval uses the `restore-keys` instead. + - name: Restore previous runs timings + uses: actions/cache@v2 + with: + path: ${{ inputs.results_path }} + key: single-instance-report-${{ github.sha }}-${{ env.dummy_random_value }} + restore-keys: | + tests-reports-${{ github.ref }}-${{ github.sha }}-${{ github.run_id }} + tests-reports-${{ github.ref }}-${{ github.sha }}- + tests-reports-${{ github.ref }}- + tests-reports- + + - name: Display previous runs timings used for splitting tests + run: ls ${{ inputs.results_path }} || true + shell: bash diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 47554ed09..4c177b4ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,39 +9,28 @@ jobs: linters: name: Linters runs-on: ubuntu-latest + services: + postgres: + image: postgres:12 + env: + POSTGRES_USER: tps_test + POSTGRES_DB: tps_test + POSTGRES_PASSWORD: tps_test + ports: [ "5432:5432" ] + steps: - - name: Checkout code - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - bundler-cache: true - - - name: Setup Node - uses: actions/setup-node@v2 - with: - node-version: '14' - - name: Find yarn cache location - id: yarn-cache - run: echo "::set-output name=dir::$(yarn cache dir)" - - name: JS package cache - uses: actions/cache@v2 - with: - path: ${{ steps.yarn-cache.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- - - name: Install packages - run: | - yarn install --frozen-lockfile + - name: Setup the app code and dependancies + uses: ./.github/actions/ci-setup-rails - name: Run linters run: | bundle exec rake lint + bundle exec rake zeitwerk:check - tests: - name: Tests + unit_tests: + name: Unit tests runs-on: ubuntu-latest services: postgres: @@ -54,55 +43,91 @@ jobs: strategy: matrix: - pattern: - - bin/rake zeitwerk:check - - bin/rspec spec/controllers/*_spec.rb - - bin/rspec spec/controllers/[a-l]**/*_spec.rb - - bin/rspec spec/controllers/[m-z]**/*_spec.rb - - bin/rspec spec/system - - bin/rspec spec/helpers spec/lib spec/middlewares - - bin/rspec spec/mailers spec/jobs spec/policies - - bin/rspec spec/models - - bin/rspec spec/serializers spec/services - - bin/rspec spec/views + instances: [0, 1, 2, 3, 4, 5] steps: - - name: Checkout code - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 + - name: Setup the app runtime and dependencies + uses: ./.github/actions/ci-setup-rails + + - name: Pre-compile assets + uses: ./.github/actions/ci-setup-assets + + - name: Setup split tests + uses: ./.github/actions/ci-setup-split-tests with: - bundler-cache: true - - - name: Setup Node - uses: actions/setup-node@v2 - with: - node-version: '14' - - name: Find yarn cache location - id: yarn-cache - run: echo "::set-output name=dir::$(yarn cache dir)" - - name: JS package cache - uses: actions/cache@v2 - with: - path: ${{ steps.yarn-cache.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- - - name: Install packages - run: | - yarn install --frozen-lockfile - - - name: Setup test database - env: - RAILS_ENV: test - DATABASE_URL: "postgres://tps_test@localhost:5432/tps_test" - run: | - bundle exec rake db:create db:schema:load db:migrate - - - name: Setup environment variables - run: | - cp config/env.example .env + results_path: tmp/*.junit.xml - name: Run tests - run: ${{ matrix.pattern }} + run: | + SPEC_FILES=$(./split_tests -glob='spec/**/*_spec.rb' -exclude-glob='spec/system/**' -split-index=${{ strategy.job-index }} -split-total=${{ strategy.job-total }} -junit -junit-path=tmp/*.junit.xml) + echo "Running tests for bin/rspec $SPEC_FILES" + bin/rspec $SPEC_FILES --format progress --format RspecJunitFormatter --out tmp/rspec_${{ github.job }}_${{ strategy.job-index }}.junit.xml + + - name: Upload test results for this instance + uses: actions/upload-artifact@v2 + with: + name: test-reports + path: tmp/rspec_${{ github.job }}_${{ strategy.job-index }}.junit.xml + + system_tests: + name: System tests + runs-on: ubuntu-latest + services: + postgres: + image: postgres:12 + env: + POSTGRES_USER: tps_test + POSTGRES_DB: tps_test + POSTGRES_PASSWORD: tps_test + ports: ["5432:5432"] + + strategy: + matrix: + instances: [0, 1] + + steps: + - uses: actions/checkout@v2 + + - name: Setup the app runtime and dependencies + uses: ./.github/actions/ci-setup-rails + + - name: Pre-compile assets + uses: ./.github/actions/ci-setup-assets + + - name: Setup split tests + uses: ./.github/actions/ci-setup-split-tests + with: + results_path: tmp/*.junit.xml + + - name: Run tests + run: | + SPEC_FILES=$(./split_tests -glob='spec/system/**/*_spec.rb' -split-index=${{ strategy.job-index }} -split-total=${{ strategy.job-total }} -junit -junit-path=tmp/*.junit.xml) + echo "Running tests for bin/rspec $SPEC_FILES" + bin/rspec $SPEC_FILES --format progress --format RspecJunitFormatter --out tmp/rspec_${{ github.job }}_${{ strategy.job-index }}.junit.xml + + - name: Upload test results for this instance + uses: actions/upload-artifact@v2 + with: + name: test-reports + path: tmp/rspec_${{ github.job }}_${{ strategy.job-index }}.junit.xml + + save_test_reports: + name: Save test reports + needs: [unit_tests, system_tests] + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Collect test results from all instances + uses: actions/download-artifact@v2 + with: + name: test-reports + path: tmp + + - name: Save test results and timing data, to better split future tests + uses: ./.github/actions/ci-save-split-tests + with: + results_path: tmp/*.junit.xml diff --git a/.rspec b/.rspec index 0f30adeb0..d958652df 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,3 @@ --color --require rails_helper +--profile diff --git a/Gemfile b/Gemfile index 5fbc1aa53..0890ef679 100644 --- a/Gemfile +++ b/Gemfile @@ -92,6 +92,7 @@ group :test do gem 'factory_bot' gem 'launchy' gem 'rails-controller-testing' + gem 'rspec_junit_formatter' gem 'selenium-webdriver' gem 'shoulda-matchers', require: false gem 'timecop' diff --git a/Gemfile.lock b/Gemfile.lock index 0e778a197..913c4f27a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -596,6 +596,8 @@ GEM rspec-mocks (~> 3.10) rspec-support (~> 3.10) rspec-support (3.10.2) + rspec_junit_formatter (0.4.1) + rspec-core (>= 2, < 4, != 2.12.0) rubocop (1.10.0) parallel (~> 1.10) parser (>= 3.0.0.0) @@ -856,6 +858,7 @@ DEPENDENCIES rgeo-geojson rqrcode rspec-rails + rspec_junit_formatter rubocop rubocop-rails_config rubocop-rspec-focused diff --git a/yarn.lock b/yarn.lock index 1361a306e..f7e857088 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3525,15 +3525,10 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" lodash.uniq "^4.5.0" -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001039, caniuse-lite@^1.0.30001219: - version "1.0.30001228" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001228.tgz#bfdc5942cd3326fa51ee0b42fbef4da9d492a7fa" - integrity sha512-QQmLOGJ3DEgokHbMSA8cj2a+geXqmnpyOFT0lhQV6P3/YOJvGDEwoedcwxEQ30gJIwIIunHIicunJ2rzK5gB2A== - -caniuse-lite@^1.0.30001254: - version "1.0.30001257" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001257.tgz#150aaf649a48bee531104cfeda57f92ce587f6e5" - integrity sha512-JN49KplOgHSXpIsVSF+LUyhD8PUp6xPpAXeRrrcBh4KBeP7W864jHn6RvzJgDlrReyeVjMFJL3PLpPvKIxlIHA== +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001039, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001254: + version "1.0.30001271" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001271.tgz" + integrity sha512-BBruZFWmt3HFdVPS8kceTBIguKxu4f99n5JNp06OlPD/luoAMIaIK5ieV5YjnBLH3Nysai9sxj9rpJj4ZisXOA== cardinal@^2.1.1: version "2.1.1"