Skip to content

Commit

Permalink
Add Rails integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock committed Feb 1, 2024
1 parent 26a968a commit eeb1dd8
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/integration/rails_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require "test_helper"
require "tempfile"
require "fileutils"
require "open3"

class RailsIntegrationTest < ActiveSupport::TestCase
CommandFailed = Class.new(StandardError)

test "running a job in a Rails app works" do
in_dummy_rails_app_with_job_iteration do |dir|
File.write("app/jobs/example_job.rb", <<~RUBY)
class ExampleJob < ApplicationJob
include JobIteration::Iteration
def build_enumerator(cursor:)
enumerator_builder.array(["it works"], cursor: cursor)
end
def each_iteration(string)
puts string
end
end
RUBY

output = run_or_raise("bin/rails", "runner", "ExampleJob.perform_now")
assert_includes(output, "it works")
end
end

private

def in_dummy_rails_app_with_job_iteration
Dir.mktmpdir do |dir|
Dir.chdir(dir) do |dir|
dir = File.join(dir, "dummy")
FileUtils.mkdir(dir)
Dir.chdir(dir) do |dir|
Bundler.with_unbundled_env do
run_or_raise("bundle", "init")
run_or_raise("bundle", "add", "rails", "--version", rails_version.to_s)
run_or_raise("rails", "new", ".", "--force")
run_or_raise("bundle", "add", "job-iteration")

yield dir
end
end
end
end
end

def run_or_raise(*args, **kwargs, &block)
stdout_and_stderr_str, status = Open3.capture2e(*args, **kwargs, &block)
raise CommandFailed, stdout_and_stderr_str unless status.success?

stdout_and_stderr_str
end

def rails_version
# We're going to install Rails in the dummy app, so we need to know the version to install.
# We don't depend on Rails though, so read the version of Active Support instead, since they always match.
Gem.loaded_specs.fetch("activesupport").version
end
end

0 comments on commit eeb1dd8

Please sign in to comment.