diff --git a/.docker/ruby_versions.txt b/.docker/ruby_versions.txt new file mode 100644 index 00000000..61f4d763 --- /dev/null +++ b/.docker/ruby_versions.txt @@ -0,0 +1,4 @@ +2.7.7 +3.0.5 +3.1.3 +3.2.1 diff --git a/.docker/scripts/test_all b/.docker/scripts/test_all new file mode 100644 index 00000000..fd86b455 --- /dev/null +++ b/.docker/scripts/test_all @@ -0,0 +1,3 @@ +#!/bin/bash + +xargs -L 1 ./test_ruby < ruby_versions.txt diff --git a/.docker/scripts/test_mysql b/.docker/scripts/test_mysql new file mode 100644 index 00000000..2cdf9acd --- /dev/null +++ b/.docker/scripts/test_mysql @@ -0,0 +1,10 @@ +#!/bin/bash + +cd /src +bundle update; + +RUBY_VERSION=$(ruby -v); + +echo "Testing With MySQL and Ruby $RUBY_VERSION"; +export DATABASE_URL="mysql2://test:password@mysql:3306/test" +bundle exec rake test; diff --git a/.docker/scripts/test_postgresql b/.docker/scripts/test_postgresql new file mode 100644 index 00000000..6da5b503 --- /dev/null +++ b/.docker/scripts/test_postgresql @@ -0,0 +1,10 @@ +#!/bin/bash + +cd /src +bundle update; + +RUBY_VERSION=$(ruby -v); + +echo "Testing With PostgreSQL and Ruby $RUBY_VERSION"; +export DATABASE_URL="postgresql://postgres:password@postgres:5432/test" +bundle exec rake test; diff --git a/.docker/scripts/test_ruby b/.docker/scripts/test_ruby new file mode 100644 index 00000000..0fc3f175 --- /dev/null +++ b/.docker/scripts/test_ruby @@ -0,0 +1,7 @@ +#!/bin/bash + +rbenv global $1; + +../test_postgresql +../test_mysql +../test_sqlite diff --git a/.docker/scripts/test_sqlite b/.docker/scripts/test_sqlite new file mode 100644 index 00000000..1829d864 --- /dev/null +++ b/.docker/scripts/test_sqlite @@ -0,0 +1,10 @@ +#!/bin/bash + +cd /src +bundle update; + +RUBY_VERSION=$(ruby -v); + +echo "Testing With SQLite and Ruby $RUBY_VERSION"; +export DATABASE_URL="sqlite3:test_db" +bundle exec rake test; diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..017ae466 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +FROM buildpack-deps:bullseye + +# Install rbenv +RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv +RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build +RUN /root/.rbenv/plugins/ruby-build/install.sh +ENV PATH /root/.rbenv/bin:/root/.rbenv/shims:$PATH +RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile +RUN echo 'eval "$(rbenv init -)"' >> .bashrc + +# Install supported ruby versions +RUN echo 'gem: --no-document' >> ~/.gemrc + +COPY .docker/ruby_versions.txt / +RUN xargs -I % sh -c 'rbenv install %; rbenv global %; gem install bundler' < ruby_versions.txt +RUN rbenv rehash + +# COPY just enough to bundle. This allows for most code changes without needing to reinstall all gems +RUN mkdir src +COPY Gemfile jsonapi-resources.gemspec Rakefile ./src/ +COPY lib/jsonapi/resources/version.rb ./src/lib/jsonapi/resources/ +# This will run bundle install for each ruby version and leave the global version set as the last one. +# So we can control the default ruby version with the order in the ruby_versions.txt file, with last being the default +RUN xargs -I % sh -c 'cd src; rbenv global %; bundle install' < /ruby_versions.txt + +# Scripts +COPY .docker/scripts/* / +RUN chmod +x /test_* + +# COPY in the rest of the project +COPY lib/ ./src/lib +COPY locales/ ./src/locales +COPY test/ ./src/test +RUN ls -la + +CMD ["/test_all"] diff --git a/README.md b/README.md index 377e4930..1b29259f 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,43 @@ gem install jsonapi-resources **For further usage see the [v0.10 alpha Guide](http://jsonapi-resources.com/v0.10/guide/)** +## Development + +There is a docker compose setup that can be used for testing your code against the supported versions of ruby and +against PostgreSQL, MYSQL, and SQLite databases. + +First build the docker image: +```bash +docker compose build +``` +Be sure to rebuild after making code changes. It should be fast after the initial build. + +### Running the tests + +The default command will run everything (it may take a while): +```bash +docker compose run tests +``` + +To test just one database against the latest ruby: +```bash +docker compose run tests ./test_mysql +docker compose run tests ./test_postgresql +docker compose run tests ./test_sqlite +``` + +To test a version of ruby against all databases: +```bash +docker compose run tests ./test_ruby 2.7.7 +``` + +The tests by default run against the latest rails version. To override that you can set the RAILS_VERSION environment +variable: + +```bash +docker compose run -e RAILS_VERSION=6.1.1 tests ./test_postgresql +``` + ## Contributing 1. Submit an issue describing any new features you wish it add or the bug you intend to fix diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..adf2ee21 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,49 @@ +version: "3.9" + +networks: + back: + +services: + postgres: + image: postgres:latest + ports: + - "5432" + networks: + back: + environment: + - POSTGRES_PASSWORD=password + - POSTGRES_DB=test + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U postgres" ] + interval: 2s + retries: 15 + + mysql: + image: mysql:latest + command: --default-authentication-plugin=mysql_native_password + ports: + - 3306 + networks: + - back + environment: + - MYSQL_DATABASE=test + - MYSQL_USER=test + - MYSQL_PASSWORD=password + - MYSQL_ROOT_PASSWORD=password + healthcheck: + test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ] + interval: 2s + retries: 15 + + tests: + image: jr_tests:latest + build: + context: . + dockerfile: Dockerfile + networks: + - back + depends_on: + postgres: + condition: service_healthy + mysql: + condition: service_healthy