From d6e4000a2ac251e18f92ef6698f84e463d7ed4cf Mon Sep 17 00:00:00 2001 From: Jean Luis Urena Date: Thu, 11 Jul 2024 15:40:38 -0400 Subject: [PATCH] Final mixed misc changes --- .gitignore | 1 + Dockerfile.test | 60 ++++++++++++++++++++ README.md | 48 +++++----------- lib/cached_resource/caching.rb | 2 +- scripts/docker/run_tests.sh | 28 +++++++++ spec/cached_resource/cached_resource_spec.rb | 2 +- 6 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 Dockerfile.test create mode 100755 scripts/docker/run_tests.sh diff --git a/.gitignore b/.gitignore index dad0222..36a67c0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ pkg/* .ruby-version .tool-versions coverage +.standard_todo.yml diff --git a/Dockerfile.test b/Dockerfile.test new file mode 100644 index 0000000..c977759 --- /dev/null +++ b/Dockerfile.test @@ -0,0 +1,60 @@ +FROM bash AS base + +ENV LANG C.UTF-8 +ENV LC_ALL C.UTF-8 +ENV ASDF_DIR=/root/.asdf \ + GEM_HOME=/root/gems \ + PATH=/root/gems/bin:$PATH + +# Install dependencies +RUN apk update && \ + apk add --no-cache \ + build-base \ + git \ + libffi-dev \ + openssl-dev \ + readline-dev \ + tzdata \ + yaml-dev \ + zlib-dev + +RUN git clone https://github.com/asdf-vm/asdf.git /root/.asdf --branch v0.14.0 && \ + . "$ASDF_DIR/asdf.sh" && \ + asdf plugin add ruby + +## For parallelism, build in stages +# Ruby 3.1.4 +FROM base AS ruby-3.1.4 +RUN . "$ASDF_DIR/asdf.sh" && \ + asdf install ruby 3.1.4 && \ + asdf global ruby 3.1.4 && \ + gem install bundler + +# Ruby 3.2.2 +FROM base AS ruby-3.2.2 +RUN . "$ASDF_DIR/asdf.sh" && \ + asdf install ruby 3.2.2 && \ + asdf global ruby 3.2.2 && \ + gem install bundler + +# Final Image with Application Code +FROM base AS final + +ENV GEM_HOME=/root/gems \ + PATH=/root/gems/bin:$PATH + +# Copy and merge installed ASDF directory from ruby versions +COPY --from=ruby-3.1.4 /root/.asdf /tmp/.asdf-3.1.4 +COPY --from=ruby-3.2.2 /root/.asdf /tmp/.asdf-3.2.2 +RUN cp -rn /tmp/.asdf-3.1.4/* /root/.asdf && rm -rf /tmp/.asdf-3.1.4 +RUN cp -rn /tmp/.asdf-3.2.2/* /root/.asdf && rm -rf /tmp/.asdf-3.2.2 + +WORKDIR /app + +COPY gemfiles gemfiles +COPY lib lib +COPY spec spec +COPY cached_resource.gemspec Gemfile Rakefile scripts/docker/run_tests.sh . + +# Run tests or any other final commands +CMD ["./run_tests.sh"] diff --git a/README.md b/README.md index d125747..f65da45 100644 --- a/README.md +++ b/README.md @@ -107,45 +107,27 @@ Since resources are cached with an argument based key, you may pass in extra dat MyActiveResource.find(:one, from: "/admin/shop.json", uid: "unique value") ``` -## Testing +## Contribution +### Testing +#### Locally +You can set `TEST_RAILS_VERSION` to a value of the supported Rails versions in the [Compatability Matrix](#compatibility), or bundler will automatically select a version. -To test the Ruby + Rails combination configured by default: +Examples: +- `TEST_RAILS_VERSION=7.1 bundle exec rspec # runs test suite + linter` +- `bundle exec rspec # Runs test suite` -```bash -$ rake +#### With Docker +```sh +docker build -t cached_resource_test -f Dockerfile.test . +docker run --rm -v ${PWD}/coverage:/app/coverage cached_resource_test ``` -or to test all supported environments...you have to do a little more work... +### Code Coverage +Coverage can be found found within `coverage/index.html` after every test run. -Switch your Ruby version to the desired version. This project's maintainer uses `asdf`, so switching to Ruby 3 looks like this: +### Linter +We unconfigurable linting rules from [Standard](https://github.com/standardrb/standard) -```bash -$ asdf local ruby 3.0.5 -``` - -If you have a `Gemfile.lock`, delete it: - -```bash -$ rm Gemfile.lock -``` - -Then reinstall your dependencies: - -```bash -$ bundle install -``` - -and finally, run the tests: - -```bash -$ rake -``` - -If you want to test with a specific Rails version, start over and install dependencies with `TEST_RAILS_VERSION` set: - -```bash -$ TEST_RAILS_VERSION=6.1 bundle install -``` ## Credit/Inspiration * quamen and [this gist](http://gist.github.com/947734) diff --git a/lib/cached_resource/caching.rb b/lib/cached_resource/caching.rb index 31603b1..9108b78 100644 --- a/lib/cached_resource/caching.rb +++ b/lib/cached_resource/caching.rb @@ -182,7 +182,7 @@ def name_key else "#{cached_resource.cache_key_prefix}/" end - prefix + name.parameterize.tr("-", "/") + prefix.to_s + name.parameterize.tr("-", "/") end end diff --git a/scripts/docker/run_tests.sh b/scripts/docker/run_tests.sh new file mode 100755 index 0000000..12ea1eb --- /dev/null +++ b/scripts/docker/run_tests.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +set -e + +# Matrix of Ruby and Rails versions +RUBY_VERSIONS="3.1.4 3.2.2" +RAILS_VERSIONS="6.1 7.0 7.1" + +# Iterate over Ruby versions +for RUBY_VERSION in $RUBY_VERSIONS; do + . "$ASDF_DIR/asdf.sh" + asdf local ruby $RUBY_VERSION + + # Iterate over Rails versions + for RAILS_VERSION in $RAILS_VERSIONS; do + # Set the Rails version environment variable + export TEST_RAILS_VERSION=$RAILS_VERSION + echo "Testing with Ruby $RUBY_VERSION and Rails $RAILS_VERSION" + + # Install gems + bundle install + + # Run tests + bundle exec rspec + done +done + +bundle exec standardrb --generate-todo diff --git a/spec/cached_resource/cached_resource_spec.rb b/spec/cached_resource/cached_resource_spec.rb index 3eddf45..7fb5b27 100644 --- a/spec/cached_resource/cached_resource_spec.rb +++ b/spec/cached_resource/cached_resource_spec.rb @@ -53,7 +53,7 @@ end it "requires concurrent/promise" do - expect { dummy_class.setup_cached_resource!(concurrent_write: true) }.not_to raise_error(LoadError) + expect { dummy_class.setup_cached_resource!(concurrent_write: true) }.not_to raise_error end context 'When "concurrent/promise" is not installed' do