-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26a968a
commit eeb1dd8
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |