Skip to content

Commit

Permalink
Final misc touches
Browse files Browse the repository at this point in the history
  • Loading branch information
jlurena committed Jul 12, 2024
1 parent 9fc5092 commit 3e0d60d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 24 additions & 10 deletions Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand All @@ -13,6 +11,7 @@ RUN apk update && \
git \
libffi-dev \
openssl-dev \
perl \
readline-dev \
tzdata \
yaml-dev \
Expand All @@ -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" && \
Expand All @@ -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"]
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cached_resource.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 11 additions & 6 deletions lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
57 changes: 45 additions & 12 deletions scripts/docker/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 *********"
14 changes: 2 additions & 12 deletions spec/cached_resource/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3e0d60d

Please sign in to comment.