Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests back to pipeline & improve VSCode integration #581

Merged
merged 49 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bf30e9f
Add Python script to run Rspec tests in docker container
Splines Dec 18, 2023
c3541de
Merge branch 'mampf-next' into pipeline/tests
Splines Jan 7, 2024
103789d
Add Ruby Test explorer settings to `settings.json`
Splines Jan 7, 2024
68c0122
Rename `run_cypress_tests` to `test` & use buildx
Splines Jan 10, 2024
63544f8
Fix paths to files and rename to `Testing`
Splines Jan 10, 2024
b311ca1
Use lowercase github org name
Splines Jan 10, 2024
1d4e9af
Use GitHub Rspec formatter
Splines Jan 10, 2024
1eb3cf7
Clean up tests.yml & add comment with useful links
Splines Jan 10, 2024
3e51aa1
Delete unnecessary dotfiles
Splines Jan 10, 2024
84d6a45
Fix path due to renaming of `run_cypress_tests` to `test`
Splines Jan 10, 2024
1d5c757
Add more comments to tests.yml
Splines Jan 10, 2024
229dcdb
Don't deal with cypress_runner here
Splines Jan 10, 2024
840c63d
Only cache real build targets
Splines Jan 10, 2024
d11dc2d
Merge branch 'dev' into pipeline/tests
Splines Apr 24, 2024
eb0a4b7
Merge branch 'dev' into pipeline/tests
Splines Apr 27, 2024
69e12a9
Source dummy docker env variables
Splines Apr 28, 2024
faa51e8
Merge branch 'dev' into pipeline/tests
Splines Apr 30, 2024
4c18e8c
Add missing `require "rails_helper" statement
Splines Apr 30, 2024
b7f103c
Move comment to correct place in test
Splines Apr 30, 2024
38440de
Fix missing `=` in workflow file
Splines Apr 30, 2024
acd162f
Source docker-dummy.env in docker test environment
Splines Apr 30, 2024
16ca7d5
Don't source dummy docker env & add missing env variables
Splines Apr 30, 2024
439c004
Disable "can destroy users" test
Splines Apr 30, 2024
c9c6c54
Copy missing app folder in Dockerfile (test setup)
Splines Apr 30, 2024
aa9a27e
Remove creation of test database in workflow
Splines Apr 30, 2024
182d5cf
Precompile webpacker assets into docker test image
Splines May 1, 2024
ce040aa
Fix failing media search tests (first iteration)
Splines May 1, 2024
5e54d13
Fix media search tests
Splines May 1, 2024
710146b
Upgrade Node.js dev dependencies
Splines May 1, 2024
92e5b74
Merge branch 'deps/upgrade-dev-yarn-packages' into pipeline/tests
Splines May 1, 2024
5552d39
Fix wrong ESLint semicolon-key value
Splines May 1, 2024
9d2be2c
Merge branch 'deps/upgrade-dev-yarn-packages' into pipeline/tests
Splines May 1, 2024
5d7c131
Remove pending test
Splines May 1, 2024
a7e116c
Freeze string assigned to constant (rubocop)
Splines May 1, 2024
58b28c6
Try to use `delete_all` instead of `destroy_all`
Splines May 1, 2024
1983c6f
Don't delete Medium table as DatabaseClenaer takes care of it
Splines May 1, 2024
f8d1872
Improve database cleaner config according to docs
Splines May 1, 2024
415619c
Only rely on active record database cleaner gem
Splines May 7, 2024
cf08e5b
Use before() & after() instead of around() in DB cleaner
Splines May 7, 2024
4e7e995
Explicitly require database_cleaner gem
Splines May 7, 2024
4c32260
Merge branch 'dev' into pipeline/tests
Splines May 7, 2024
0b8ecac
Use append_after() instead of append()
Splines May 7, 2024
ebbe53d
Change directory to test folder
Splines May 7, 2024
0ef6766
Revert "Change directory to test folder"
Splines May 7, 2024
84646cc
Try to use transactional fixtures
Splines May 7, 2024
b509099
Use database cleaner, omit before :all (instead use before :each)
Splines May 7, 2024
1838386
Pass token to Codecov test workflow
Splines May 7, 2024
8cd49cc
Ignore test files in Codecov report
Splines May 7, 2024
d86b4ae
Use testing GitHub environment for CI/CD
Splines May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .config/.codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore test files themselves in the Codecov report
# see: https://about.codecov.io/blog/should-i-include-test-files-in-code-coverage-calculations/
ignore:
- "spec/"
- "*_spec.rb"
9 changes: 0 additions & 9 deletions .dockerignore

This file was deleted.

15 changes: 15 additions & 0 deletions .github/workflows/docker-compose-cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"target": {
"mampf": {
"cache-from": [
"type=registry,ref=ghcr.io/mampf-hd/mampf:cache"
],
"cache-to": [
"type=registry,ref=ghcr.io/mampf-hd/mampf:cache"
],
"output": [
"type=docker"
]
}
}
}
62 changes: 62 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Testing

on:
push:
branches:
- main
- dev
- experimental
pull_request:
types: [opened, reopened, synchronize, ready_for_review]

jobs:
# For the setup idea using Docker Buildx, see StackOverflow answer [1].
# [2] and [3] might also be very useful. [4] is generally about Docker
# cache management within GitHub Actions.
#
# [1] https://stackoverflow.com/a/75544124/
# [2] https://depot.dev/blog/docker-layer-caching-in-github-actions#docker-layer-caching-in-github-actions
# [3] https://www.deploysentinel.com/blog/docker-buildx-cache-with-github-actions
# [4] https://docs.docker.com/build/ci/github-actions/cache/
unit-tests:
name: Unit tests
environment: testing
runs-on: ubuntu-latest
permissions: write-all
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: recursive

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3

# see https://github.com/orgs/MaMpf-HD/packages?repo_name=mampf
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build docker images
# As the docker-compose.yml file uses contexts like "./../..", we have
# to change the working directory here.
working-directory: docker/test
run: |
docker buildx bake --file ./docker-compose.yml --file ./../../.github/workflows/docker-compose-cache.json

- name: Run unit tests
working-directory: docker/test
run: |
docker compose run --entrypoint="" mampf sh -c "RAILS_ENV=test bundle exec rspec --format RSpec::Github::Formatter"

- name: Report test coverage to codecov
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
files: ./coverage/coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
codecov_yml_path: ./config/codecov.yml
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
},
"rubyLsp.enableExperimentalFeatures": true,
//////////////////////////////////////
// Ruby Test Explorer
//////////////////////////////////////
"rubyTestExplorer.testFramework": "rspec",
"rubyTestExplorer.rspecCommand": "python3 ./spec/rspec_inside_docker.py",
"rubyTestExplorer.rspecDirectory": "./spec/",
"rubyTestExplorer.logpanel": true,
"rubyTestExplorer.filePattern": [
"*_spec.rb"
],
//////////////////////////////////////
// Files
//////////////////////////////////////
"files.exclude": {
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ group :test do
# Adds support for Capybara system testing and selenium driver
gem "selenium-webdriver"
# Easy installation and use of web drivers to run system tests with browsers
gem "database_cleaner"
gem "database_cleaner-active_record"
gem "faker"
gem "launchy"
gem "simplecov", require: false
Expand All @@ -138,6 +138,8 @@ group :test, :development, :docker_development do

gem "cypress-on-rails", "~> 1.0"
gem "simplecov-cobertura"

gem "rspec-github"
end

gem "prometheus_exporter"
7 changes: 4 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ GEM
cypress-on-rails (1.17.0)
rack
dalli (3.2.8)
database_cleaner (2.0.2)
database_cleaner-active_record (>= 2, < 3)
database_cleaner-active_record (2.1.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
Expand Down Expand Up @@ -497,6 +495,8 @@ GEM
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-github (2.4.0)
rspec-core (~> 3.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
Expand Down Expand Up @@ -686,7 +686,7 @@ DEPENDENCIES
coveralls
cypress-on-rails (~> 1.0)
dalli (>= 2.7)
database_cleaner
database_cleaner-active_record
devise
devise-bootstrap-views
erubis
Expand Down Expand Up @@ -726,6 +726,7 @@ DEPENDENCIES
responders
rgl
rqrcode
rspec-github
rspec-rails
rubocop (~> 1.63)
rubocop-performance (~> 1.21)
Expand Down
2 changes: 1 addition & 1 deletion config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
process.env.NODE_ENV = process.env.NODE_ENV || "development";
process.env.NODE_ENV = process.env.NODE_ENV || "test";

const environment = require("./environment");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@ RUN yarn install --production=false
COPY --from=build-pdfcomprezzor /go/src/pdfcomprezzor.wasm /go/src/wasm_exec.js /usr/src/app/public/pdfcomprezzor/
COPY --from=build-pdfcomprezzor /go/src/pdfcomprezzor.wasm /go/src/wasm_exec.js /

COPY ./ /usr/src/app/
RUN RAILS_ENV=test TEST_DATABASE_ADAPTER=postgresql rake assets:precompile
COPY . /usr/src/app/
COPY ./docker/production/docker.env ./docker-dummy.env

RUN set -o allexport && . ./docker-dummy.env && set +o allexport && \
RAILS_ENV=test TEST_DATABASE_ADAPTER=nulldb bundle exec rails assets:precompile
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ services:
mampf:
build:
context: ./../..
dockerfile: docker/run_cypress_tests/Dockerfile
dockerfile: docker/test/Dockerfile
image: mampf:tests
ports:
- "127.0.0.1:3000:3000"
# TODO: Use this
# entrypoint: /usr/src/app/docker/run_cypress_tests/run_tests.sh
# entrypoint: /usr/src/app/docker/test/run_tests.sh
entrypoint: ./entrypoint.sh
environment:
RAILS_ENV: test
Expand Down Expand Up @@ -76,7 +76,7 @@ services:
image: mampf-cypress
build:
context: ./../..
dockerfile: docker/run_cypress_tests/Dockerfile_cypress
dockerfile: docker/test/Dockerfile_cypress
environment:
CYPRESS_baseUrl: http://mampf:3000
entrypoint: bash -c "while ! curl -s $$CYPRESS_baseUrl > /dev/null; do echo waiting for MaMpf to come online at $$CYPRESS_baseUrl; sleep 1; done; npx cypress run $$@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ services:
image: dockage/mailcatcher:latest
networks:
- backend

mampf:
build:
context: ./../..
dockerfile: docker/run_cypress_tests/Dockerfile
dockerfile: docker/test/Dockerfile
image: mampf:tests
# TODO: Use this
# entrypoint: /usr/src/app/docker/run_cypress_tests/run_tests.sh
# entrypoint: /usr/src/app/docker/test/run_tests.sh
entrypoint: /usr/src/app/entrypoint.sh
environment:
RAILS_ENV: test
Expand All @@ -58,14 +59,16 @@ services:
ERDBEERE_API: https://erdbeere.mathi.uni-heidelberg.de/api/v1
MUESLI_SERVER: https://muesli.mathi.uni-heidelberg.de
PROJECT_EMAIL: project@localhost
FEEBACK_EMAIL: feedback@localhost
FEEDBACK_EMAIL: feedback@localhost
PROJECT_NOTIFICATION_EMAIL: project+notification@localhost
ERROR_EMAIL: mampf-error@localhost
MEDIA_FOLDER: mampf
REDIS_URL: redis://redis:6379/1
SOLR_HOST: solr
SOLR_PORT: 8983
SOLR_PATH: /solr/test
SPROCKETS_CACHE: /cache
BLOG: https://mampf.blog
volumes:
- type: bind
source: ../../spec/
Expand All @@ -79,23 +82,3 @@ services:
networks:
- backend
- frontend

cypress_runner:
image: mampf-cypress
build:
context: ./../..
dockerfile: docker/run_cypress_tests/Dockerfile_cypress
environment:
CYPRESS_baseUrl: http://mampf:3000
volumes:
- ../../spec/cypress/e2e:/cypress/e2e:ro
- ../../spec/cypress/fixtures:/cypress/fixtures:ro
- ../../spec/cypress.config.js:/cypress.config.js:ro
- ../../.git:/.git:ro
# cypress outputs are saved here (needed only locally)
#- ../../cypress/videos:/cypress/videos
#- ../../cypress/screenshots:/cypress/screenshots
depends_on:
- mampf
networks:
- frontend
File renamed without changes.
6 changes: 6 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
factory :confirmed_user, traits: [:skip_confirmation_notification,
:auto_confirmed,
:consented]

factory :confirmed_user_en, traits: [:skip_confirmation_notification,
:auto_confirmed,
:consented] do
locale { "en" }
end
end
end
2 changes: 1 addition & 1 deletion spec/models/announcement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# test traits

describe "with lecture" do
before :all do
before :each do
@announcement = FactoryBot.build(:announcement, :with_lecture)
end
it "has a valid factory" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# test traits

describe "with stuff" do
before :all do
before :each do
@answer = FactoryBot.build(:answer, :with_stuff)
end
it "has a text" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/chapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# test traits

describe "chapter with sections" do
before :all do
before :each do
@chapter = FactoryBot.build(:chapter, :with_sections)
end
it "has a valid factory" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/consumption_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# describe traits

describe "with stuff" do
before :all do
before :each do
@consumption = FactoryBot.build(:consumption, :with_stuff)
end
it "has a medium id" do
Expand Down
Loading