diff --git a/CHANGELOG.md b/CHANGELOG.md index bb2869cc..3314fbb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Provide a 'Changelog' link on Rubygems: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/114 * Fix bundling and CONTRIBUTE.md instructions: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/123 * Fix order of arguments in `truncate_tables` expectation https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/124 +* Add Docker to make it easier to run tests locally for maintainers and contributors https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/109 ## v2.2.1 2025-05-13 diff --git a/CONTRIBUTE.md b/CONTRIBUTE.md index 4c0940ce..79e1886d 100644 --- a/CONTRIBUTE.md +++ b/CONTRIBUTE.md @@ -12,15 +12,28 @@ upstream: The gem uses Appraisal to configure different Gemfiles to test different Rails versions. -- You can run all the databases through docker if needed with `docker compose up` (you can also have them running on your system, just comment out the ones you don't need from the `docker-compose.yml` file) -- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it +### Run tests without Docker (or using Docker only for the databases) + +- You can run all the databases through docker if needed with `docker compose -f docker-compose.db.yml up` (you can also have them running on your system, just comment out the ones you don't need from the `docker-compose.db.yml` file) +- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it as needed - `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle install` (change `6.1` with any version from the `gemfiles` directory) - `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle exec rake` Note that if you don't have all the supported databases installed and running, some tests will fail. -> Note that you can check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work +> Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work + +### Run tests with Docker + +- Open `docker-compose.yml` and configure the Ruby version and Gemfile file to use +- Copy `spec/support/sample.docker.config.yml` to `spec/support/config.yml` (not this config file is specific for the Docker setup) +- Run `docker compose up` to start the container, run the tests, and exit +- Run `docker compose run ruby bash` to open `bash` inside the container for more control, run `rake` to run the tests + +> Note that the code is mounted inside the docker container, so changes in the container will reflect in the code. There's no need to re-build the container for code changes, but changing the Ruby version or Gemfile in the docker-compose.yml will require a container re-build with `docker compose build --no-cache` + +> Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work ## 3. Prepare your contribution diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..691a8bbc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +ARG RUBY_VERSION=3.3 +FROM ruby:${RUBY_VERSION} + +# Set the working directory in the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +# This is copied so we can bundle the application, but it's replaced +# by a mounted volume with the current code when executed with docker compose +COPY . /app + +ARG BUNDLE_GEMFILE=Gemfile +ENV BUNDLE_GEMFILE=${BUNDLE_GEMFILE} + +# Install any needed packages specified in Gemfile +RUN ./bin/setup + +# Command to run the application +CMD ["bash"] \ No newline at end of file diff --git a/README.md b/README.md index c05de91a..3d7d49c3 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,10 @@ test: min_messages: WARNING +## Development + +Check the CONTRIBUTE.md file for instructions running tests with and withour Docker. + ## COPYRIGHT See [LICENSE](LICENSE) for details. diff --git a/database_cleaner-active_record.gemspec b/database_cleaner-active_record.gemspec index 73508357..147445ae 100644 --- a/database_cleaner-active_record.gemspec +++ b/database_cleaner-active_record.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.executables = [] spec.require_paths = ["lib"] - spec.add_dependency "database_cleaner-core", "~>2.1.0" + spec.add_dependency "database_cleaner-core", "~>2.0" spec.add_dependency "activerecord", ">= 5.a" spec.add_development_dependency "bundler" diff --git a/docker-compose.db.yml b/docker-compose.db.yml new file mode 100644 index 00000000..300d6a73 --- /dev/null +++ b/docker-compose.db.yml @@ -0,0 +1,19 @@ +services: + postgres: + image: postgres:16 # specify the version needed for a given app + environment: + - POSTGRES_PASSWORD=postgres # this is required + ports: + - "127.0.0.1:5432:5432" # so we can use `localhost` as the host + mysql: + image: mysql:5.7 + environment: + - MYSQL_ROOT_PASSWORD=mysql + ports: + - "127.0.0.1:3306:3306" + redis: + image: redis:6.2-alpine + restart: always + ports: + - "127.0.0.1:6379:6379" + command: redis-server --save 20 1 --loglevel warning diff --git a/docker-compose.yml b/docker-compose.yml index 41cf4c22..eb138423 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,17 +3,24 @@ services: image: postgres:16 # specify the version needed for a given app environment: - POSTGRES_PASSWORD=postgres # this is required - ports: - - "127.0.0.1:5432:5432" # so we can use `localhost` as the host mysql: - image: mysql:9.3 + image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=mysql - ports: - - "127.0.0.1:3306:3306" redis: image: redis:6.2-alpine restart: always - ports: - - "127.0.0.1:6379:6379" command: redis-server --save 20 1 --loglevel warning + ruby: + build: + context: . + args: + BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version + RUBY_VERSION: 3.3 # Manually change this based on the desired Ruby version + volumes: + - ".:/app:delegated" + command: rake + depends_on: + - mysql + - postgres + - redis \ No newline at end of file diff --git a/spec/support/sample.docker.config.yml b/spec/support/sample.docker.config.yml new file mode 100644 index 00000000..728002d8 --- /dev/null +++ b/spec/support/sample.docker.config.yml @@ -0,0 +1,33 @@ +mysql2: + adapter: mysql2 + database: database_cleaner_test + username: root + password: mysql + host: mysql + port: 3306 + encoding: utf8 + +trilogy: + adapter: trilogy + database: database_cleaner_test + username: root + password: mysql + host: mysql + port: 3306 + encoding: utf8 + +postgres: + adapter: postgresql + database: database_cleaner_test + username: postgres + password: postgres + host: postgres + encoding: unicode + template: template0 + +sqlite3: + adapter: sqlite3 + database: tmp/database_cleaner_test.sqlite3 + pool: 5 + timeout: 5000 + encoding: utf8