Skip to content

Commit

Permalink
Generate initializer on installation task
Browse files Browse the repository at this point in the history
  • Loading branch information
zachahn committed Dec 4, 2024
1 parent 6b6c7cf commit 681ddb8
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

* Installs initializer on `rails g disqualified:install`. All values are
defaults

## v0.5.0 - 2024-12-02

### Added
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/disqualified/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ def copy_migration_file
basename = "20241119023328_create_disqualified_sequences.rb"
copy_file(basename, File.join(db_migrate_path, basename))
end

def copy_initializer
copy_file("initializer.rb", "config/initializers/disqualified.rb")
end
end
11 changes: 11 additions & 0 deletions lib/generators/disqualified/install/templates/initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Disqualified.configure_server do |config|
config.delay_high = 5.0
config.delay_low = 1.0
config.logger = Rails.logger
config.pool_size = 5
config.on_error do |exception, context|
puts "Unhandled Disqualified error"
pp exception
pp context
end
end
27 changes: 27 additions & 0 deletions test/disqualified/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# typed: strict

require "test_helper"

class Disqualified::ConfigurationTest < ActiveSupport::TestCase
test "initializer is fine" do
original_options = Disqualified.server_options

Disqualified.server_options = Disqualified::ServerConfiguration.new
load(File.expand_path("../../lib/generators/disqualified/install/templates/initializer.rb", __dir__))

default_server_options = Disqualified::ServerConfiguration.new
initializer_options = T.must(Disqualified.server_options)

assert_equal(default_server_options.delay_high, initializer_options.delay_high)
assert_equal(default_server_options.delay_low, initializer_options.delay_low)
assert_equal(default_server_options.logger, initializer_options.logger)
assert_equal(default_server_options.pool_size, initializer_options.pool_size)
assert_equal(default_server_options.pwd, initializer_options.pwd)
assert_equal(1, initializer_options.error_hooks.size)
assert_output(/StandardError.*orange.*mocha/m) do
T.must(initializer_options.error_hooks.first).call(StandardError.new, {orange: :mocha})
end
ensure
Disqualified.server_options = original_options
end
end
27 changes: 21 additions & 6 deletions test/dummy/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,32 @@ default: &default
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

generator_test: &generator_test
migrations_paths: db/generator_test_migrate
adapter: sqlite3
database: ":memory:"
timeout: 5000

development:
<<: *default
database: db/development.sqlite3
primary:
<<: *default
database: db/development.sqlite3
generator_test:
<<: *generator_test

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
primary:
<<: *default
database: db/test.sqlite3
generator_test:
<<: *generator_test

production:
<<: *default
database: db/production.sqlite3
primary:
<<: *default
database: db/production.sqlite3
generator_test:
<<: *generator_test
14 changes: 14 additions & 0 deletions test/dummy/db/generator_test_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 0) do
end
24 changes: 19 additions & 5 deletions test/lib/generators/disqualified/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@ class Disqualified::InstallGeneratorTest < Rails::Generators::TestCase
destination Rails.root.join("tmp/generators")
setup :prepare_destination

# test "generator runs without errors" do
# assert_nothing_raised do
# run_generator ["arguments"]
# end
# end
test "generator runs without errors" do
assert_nothing_raised do
run_generator
end

assert_operator(destination_root.join("config/initializers/disqualified.rb"), :exist?)
assert_operator(destination_root.join("db/migrate/20220703062536_create_disqualified_jobs.rb"), :exist?)
assert_operator(destination_root.join("db/migrate/20241119023328_create_disqualified_sequences.rb"), :exist?)
end

test "generator handles multiple databases" do
assert_nothing_raised do
run_generator(["--db", "generator_test"])
end

assert_operator(destination_root.join("config/initializers/disqualified.rb"), :exist?)
assert_operator(destination_root.join("db/generator_test_migrate/20220703062536_create_disqualified_jobs.rb"), :exist?)
assert_operator(destination_root.join("db/generator_test_migrate/20241119023328_create_disqualified_sequences.rb"), :exist?)
end
end

0 comments on commit 681ddb8

Please sign in to comment.