From 3e0d60d1101bb72d6668e9b6f09520d6a3cf20e4 Mon Sep 17 00:00:00 2001 From: Jean Luis Urena Date: Fri, 12 Jul 2024 14:15:46 -0400 Subject: [PATCH] Final misc touches --- .github/workflows/ruby.yml | 2 +- Dockerfile.test | 34 ++++++++++++----- README.md | 10 ++++- cached_resource.gemspec | 4 +- lib/cached_resource/caching.rb | 17 ++++++--- scripts/docker/run_tests.sh | 57 ++++++++++++++++++++++------ spec/cached_resource/caching_spec.rb | 14 +------ 7 files changed, 93 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index c071d53..e285292 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -19,7 +19,7 @@ jobs: runs-on: ubuntu-20.04 strategy: matrix: - ruby-version: ['3.1', '3.2', '3.3'] + ruby-version: ['3.0', '3.1', '3.2', '3.3'] rails-version: ['6.1', '7.0', '7.1'] steps: - uses: actions/checkout@v4 diff --git a/Dockerfile.test b/Dockerfile.test index c977759..98f1c0c 100644 --- a/Dockerfile.test +++ b/Dockerfile.test @@ -2,9 +2,7 @@ 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 +ENV ASDF_DIR=/root/.asdf # Install dependencies RUN apk update && \ @@ -13,6 +11,7 @@ RUN apk update && \ git \ libffi-dev \ openssl-dev \ + perl \ readline-dev \ tzdata \ yaml-dev \ @@ -23,6 +22,13 @@ RUN git clone https://github.com/asdf-vm/asdf.git /root/.asdf --branch v0.14.0 & asdf plugin add ruby ## For parallelism, build in stages +# Ruby 3.0.7 +FROM base AS ruby-3.0.7 +RUN . "$ASDF_DIR/asdf.sh" && \ + asdf install ruby 3.0.7 && \ + asdf global ruby 3.0.7 && \ + gem install bundler + # Ruby 3.1.4 FROM base AS ruby-3.1.4 RUN . "$ASDF_DIR/asdf.sh" && \ @@ -37,24 +43,32 @@ RUN . "$ASDF_DIR/asdf.sh" && \ asdf global ruby 3.2.2 && \ gem install bundler +# Ruby 3.3.4 +FROM base AS ruby-3.3.4 +RUN . "$ASDF_DIR/asdf.sh" && \ + asdf install ruby 3.3.4 && \ + asdf global ruby 3.3.4 && \ + 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.0.7 /root/.asdf /tmp/.asdf-3.0.7 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 +COPY --from=ruby-3.3.4 /root/.asdf /tmp/.asdf-3.3.4 +RUN cp -r /tmp/.asdf-3.0.7/* /root/.asdf/ && \ + cp -r /tmp/.asdf-3.1.4/* /root/.asdf/ && \ + cp -r /tmp/.asdf-3.2.2/* /root/.asdf/ && \ + cp -r /tmp/.asdf-3.3.4/* /root/.asdf/ && \ + rm -rf /tmp/.asdf* WORKDIR /app COPY gemfiles gemfiles COPY lib lib COPY spec spec -COPY cached_resource.gemspec Gemfile Rakefile scripts/docker/run_tests.sh . +COPY cached_resource.gemspec Gemfile Rakefile scripts/docker/run_tests.sh .standard.yml . -# Run tests or any other final commands CMD ["./run_tests.sh"] diff --git a/README.md b/README.md index 1a26c8d..4bdc2a2 100644 --- a/README.md +++ b/README.md @@ -118,13 +118,19 @@ MyActiveResource.find(:one, from: "/admin/shop.json", uid: "unique value") 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. Examples: -- `TEST_RAILS_VERSION=7.1 bundle exec rspec # runs test suite + linter` -- `bundle exec rspec # Runs test suite` +```sh +TEST_RAILS_VERSION=7.1 bundle exec rspec # runs test suite + linter +bundle exec rspec # Runs test suite +``` #### With Docker ```sh docker build -t cached_resource_test -f Dockerfile.test . docker run --rm -v ${PWD}/coverage:/app/coverage cached_resource_test + +# Coverage report can be found in coverage/index.html +# RSpec reports can be found in coverage/spec_results/${ruby-version}-${rails-version}.index.html +# Linter reports can be found in coverage/linter-results.index.html ``` ### Code Coverage diff --git a/cached_resource.gemspec b/cached_resource.gemspec index 96950bc..b921563 100644 --- a/cached_resource.gemspec +++ b/cached_resource.gemspec @@ -11,8 +11,8 @@ Gem::Specification.new do |s| s.description = "Enables request-based caching for ActiveResource" s.licenses = ["MIT"] - s.files = `git ls-files`.split("\n") - s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } + s.files = Dir.glob("lib/**/*") + Dir.glob("bin/**/*") + s.executables = Dir.glob("bin/*").map { |f| File.basename(f) } s.require_paths = ["lib"] s.required_ruby_version = ">= 3.0" diff --git a/lib/cached_resource/caching.rb b/lib/cached_resource/caching.rb index c4346e7..90ab6da 100644 --- a/lib/cached_resource/caching.rb +++ b/lib/cached_resource/caching.rb @@ -174,14 +174,19 @@ def cache_key(*arguments) end def name_key - prefix = if cached_resource.cache_key_prefix.nil? - "" - elsif cached_resource.cache_key_prefix.respond_to?(:call) - cached_resource.cache_key_prefix.call + cache_key_prefix + name.parameterize.tr("-", "/") + end + + def cache_key_prefix + prefix = cached_resource.cache_key_prefix + return "" if prefix.nil? + + if prefix.respond_to?(:call) + result = prefix.call + result.nil? ? "" : "#{result}/" else - "#{cached_resource.cache_key_prefix}/" + "#{prefix}/" end - prefix.to_s + name.parameterize.tr("-", "/") end # Make a full duplicate of an ActiveResource record. diff --git a/scripts/docker/run_tests.sh b/scripts/docker/run_tests.sh index 12ea1eb..913a78c 100755 --- a/scripts/docker/run_tests.sh +++ b/scripts/docker/run_tests.sh @@ -2,27 +2,60 @@ set -e +export BUNDLE_SILENCE_ROOT_WARNING=1 +export BUNDLE_IGNORE_MESSAGES=1 + +. "$ASDF_DIR/asdf.sh" +SPEC_RESULTS="coverage/spec_results" +mkdir -p "$SPEC_RESULTS" + # Matrix of Ruby and Rails versions -RUBY_VERSIONS="3.1.4 3.2.2" +RUBY_VERSIONS=$(asdf list ruby) RAILS_VERSIONS="6.1 7.0 7.1" +# Maximum number of concurrent processes +MAX_PARALLEL=3 +CURRENT_PARALLEL=0 + +run_rspec() { + local ruby_version="$1" + local rails_version="$2" + + echo "********* Testing with Ruby $ruby_version and Rails $rails_version *********" + + # Install gems + TEST_RAILS_VERSION="$rails_version" bundle install --quiet --no-cache + + # Run tests + TEST_RAILS_VERSION="$rails_version" bundle exec rspec \ + --format failures \ + --format html --out "$SPEC_RESULTS/$ruby_version-$rails_version.index.html" +} + # Iterate over Ruby versions for RUBY_VERSION in $RUBY_VERSIONS; do - . "$ASDF_DIR/asdf.sh" - asdf local ruby $RUBY_VERSION + asdf reshim ruby $RUBY_VERSION + asdf shell 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 + # Parallelize with arbitrary limit to prevent crashing + while [ $CURRENT_PARALLEL -ge $MAX_PARALLEL ]; do + sleep 1 + CURRENT_PARALLEL=$(jobs | wc -l) + done - # Run tests - bundle exec rspec + (run_rspec "$RUBY_VERSION" "$RAILS_VERSION") & CURRENT_PARALLEL=$((CURRENT_PARALLEL + 1)) done + + wait + + CURRENT_PARALLEL=0 done -bundle exec standardrb --generate-todo +echo "********* Running Linter *********" +bundle exec standardrb \ + --format html --out "coverage/linter-results.index.html" \ + --format offenses + +echo "********* DONE *********" diff --git a/spec/cached_resource/caching_spec.rb b/spec/cached_resource/caching_spec.rb index 28adf55..c2ac3bd 100644 --- a/spec/cached_resource/caching_spec.rb +++ b/spec/cached_resource/caching_spec.rb @@ -274,23 +274,13 @@ def read_from_cache(key, model = Thing) context "cache_key_prefix is a callable" do before do - # Gets called 3 times - allow(thing_cached_resource).to receive(:cache_key_prefix) - .and_return( - proc { "prefix123/" }, - proc { "prefix123/" }, - proc { "prefix123/" }, - proc { "prefix456/" }, - proc { "prefix456/" }, - proc { "prefix456/" } - ) + allow(thing_cached_resource).to receive(:cache_key_prefix).and_return(proc { "prefix123" }) end it "caches with the cache_key_prefix" do result = Thing.find(1) expect(read_from_cache("prefix123/thing/1")).to eq(result) - # Test for dynamicness - Thing.instance_variable_set(:@name_key, nil) # Remove memoization + allow(thing_cached_resource).to receive(:cache_key_prefix).and_return(proc { "prefix456" }) result = Thing.find(1, reload: true) expect(read_from_cache("prefix456/thing/1")).to eq(result) end