Skip to content

Commit a3d9a9d

Browse files
committed
christ, at least upload the failing specs
1 parent 5c302ee commit a3d9a9d

File tree

2 files changed

+321
-0
lines changed

2 files changed

+321
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
require 'spec_helper'
2+
3+
require 'active_support/concern'
4+
require "active_support/core_ext/module/remove_method"
5+
require 'active_support/testing/stream'
6+
require 'rails/generators'
7+
8+
#require 'support/generator_helper'
9+
require 'generators/sorcery/install_generator'
10+
11+
=begin
12+
module Rails
13+
class << self
14+
remove_possible_method :root
15+
def root
16+
@root ||= Pathname.new(File.expand_path('../rails_app', __dir__))
17+
end
18+
end
19+
end
20+
Rails.application.config.root = Rails.root
21+
Rails.application.load_generators
22+
23+
#require "active_record"
24+
#require "action_dispatch"
25+
#require "action_view"
26+
=end
27+
28+
describe 'install generator' do
29+
include ActiveSupport::Testing::Stream
30+
31+
class Installer < Sorcery::Generators::InstallGenerator
32+
source_root File.expand_path('../../lib/generators/sorcery/templates', __dir__)
33+
34+
def self.next_migration_number(dirname)
35+
current_migration_number(dirname) + 1
36+
end
37+
end
38+
39+
let!(:test_app_root) { File.expand_path('../rails_app', __dir__) }
40+
41+
#def run_generator(args = default_arguments, config = {})
42+
# capture(:stdout) do
43+
# Installer.start(args, config.reverse_merge(destination_root: test_app_root))
44+
# end
45+
#end
46+
47+
# Instantiate the generator.
48+
def generator(*args, options: {}, config: {})
49+
Installer.new(args, options, config.reverse_merge(destination_root: test_app_root))
50+
end
51+
52+
def invoke!(action, *args, options: {}, config: {})
53+
gen = generator(*args, options: options, config: config.reverse_merge(behavior: :invoke))
54+
55+
capture(:stdout) do
56+
gen.invoke(action)
57+
end
58+
end
59+
60+
def revoke!(action, *args, options: {}, config: {})
61+
gen = generator(*args, options: options, config: config.reverse_merge(behavior: :revoke))
62+
63+
capture(:stdout) do
64+
gen.invoke(action)
65+
end
66+
end
67+
68+
#tests Installer
69+
70+
#def invoke!(action, args = [], options = {}, config = {})
71+
# capture(:stdout) do
72+
# generator(args, options, config.merge(behavior: :invoke)).invoke(action)
73+
# end
74+
#end
75+
#
76+
#def revoke!(action, args = [], options = {}, config = {})
77+
# capture(:stdout) do
78+
# generator(args, options, config.merge(behavior: :revoke)).invoke(action)
79+
# end
80+
#end
81+
82+
teardown :remove_installation!
83+
84+
def remove_installation!
85+
files = [
86+
File.join(test_app_root, 'config', 'initializers', 'sorcery.rb'),
87+
File.join(test_app_root, 'app', 'models', 'user.rb'),
88+
]
89+
90+
files += Dir.glob(File.join(test_app_root, 'db', 'migrate', '*.rb'))
91+
92+
files.each do |file|
93+
FileUtils.rm(file) if File.exists?(file)
94+
end
95+
end
96+
97+
context 'given deprecated --migrations option' do
98+
let(:installer) do
99+
generator(options: { migrations: true })
100+
end
101+
102+
it 'shows warning' do
103+
expect(installer).to receive(:warn).with(/\[DEPRECATED\] `--migrations` option is deprecated/)
104+
installer.check_deprecated_options
105+
end
106+
end
107+
108+
context 'given invalid submodule' do
109+
it 'raises error' do
110+
expect {
111+
generator('invalid_submodule').check_available_submodules
112+
}.to raise_error(ArgumentError).with_message(/invalid_submodule is not a Sorcery submodule/)
113+
end
114+
end
115+
116+
describe 'initializer' do
117+
describe 'installation' do
118+
let(:installation) { invoke!(:install_initializer) }
119+
120+
it 'creates initializer' do
121+
expect(installation).to match(/create config\/initializers\/sorcery.rb/)
122+
end
123+
end
124+
125+
describe 'configuration' do
126+
context 'given submodule(s)' do
127+
let(:initializer_contents) do
128+
File.read(File.join(test_app_root, 'config', 'initializers', 'sorcery.rb'))
129+
end
130+
131+
before do
132+
invoke!(:install_initializer, 'activity_logging')
133+
end
134+
135+
it 'adds submodule(s) to initializer' do
136+
expect(initializer_contents).to match(/Rails\.application\.config\.sorcery\.submodules = \[:activity_logging\]/)
137+
end
138+
end
139+
end
140+
141+
describe 'uninstallation' do
142+
let(:uninstallation) { revoke!(:install_initializer) }
143+
144+
before do
145+
invoke!(:install_initializer)
146+
end
147+
148+
it 'removes initializer' do
149+
expect(uninstallation).to match(/remove config\/initializers\/sorcery.rb/)
150+
end
151+
end
152+
end
153+
154+
describe 'model' do
155+
describe 'installation' do
156+
let(:installation) { invoke!(:install_model) }
157+
158+
it 'skips migration' do
159+
generator.invoke(:install_model)
160+
expect(installation).to match(/generate model User \-\-skip\-migration/)
161+
end
162+
163+
it 'creates model' do
164+
expect(installation).to match(/create app\/models\/user\.rb/)
165+
end
166+
end
167+
168+
describe 'configuration' do
169+
let(:model_contents) do
170+
File.read(File.join(test_app_root, 'app', 'models', 'user.rb'))
171+
end
172+
173+
before do
174+
invoke!(:install_model)
175+
end
176+
177+
it 'adds `authenticates_with_sorcery!`' do
178+
expect(model_contents).to match(/authenticates_with_sorcery!/)
179+
end
180+
end
181+
182+
describe 'uninstallation' do
183+
let(:uninstallation) { revoke!(:install_model) }
184+
185+
before do
186+
invoke!(:install_model)
187+
end
188+
189+
it 'removes `authenticates_with_sorcery!`' do
190+
expect(uninstallation).to match(/subtract app\/models\/user.rb/)
191+
end
192+
end
193+
end
194+
195+
describe 'migrations' do
196+
pending 'finish me'
197+
end
198+
end

spec/support/generator_helper.rb

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
=begin
2+
require 'active_support/concern'
3+
require "active_support/core_ext/module/remove_method"
4+
require 'active_support/testing/stream'
5+
require 'rails/generators'
6+
7+
module Rails
8+
class << self
9+
remove_possible_method :root
10+
def root
11+
@root ||= Pathname.new(File.expand_path("../fixtures", __dir__))
12+
end
13+
end
14+
end
15+
Rails.application.config.root = Rails.root
16+
Rails.application.load_generators
17+
18+
require "active_record"
19+
require "action_dispatch"
20+
require "action_view"
21+
22+
23+
module GeneratorHelper
24+
extend ActiveSupport::Concern
25+
include ActiveSupport::Testing::Stream
26+
include FileUtils
27+
28+
included do |base|
29+
class_attribute :current_path, default: File.expand_path(Dir.pwd)
30+
class_attribute :default_arguments, default: []
31+
class_attribute :destination_root
32+
class_attribute :generator_class
33+
34+
base.destination File.expand_path('../tmp', __dir__)
35+
base.setup :prepare_destination
36+
#base.teardown :teardown_destination
37+
end
38+
39+
module ClassMethods
40+
# Sets which generator should be tested:
41+
#
42+
# tests AppGenerator
43+
def tests(klass)
44+
self.generator_class = klass
45+
end
46+
47+
# Sets default arguments on generator invocation. This can be overwritten when
48+
# invoking it.
49+
#
50+
# arguments %w(app_name --skip-active-record)
51+
def arguments(array)
52+
self.default_arguments = array
53+
end
54+
55+
# Sets the destination of generator files:
56+
#
57+
# destination File.expand_path("../tmp", __dir__)
58+
def destination(path)
59+
self.destination_root = path
60+
end
61+
end
62+
63+
# Runs the generator configured for this class. The first argument is an array like
64+
# command line arguments:
65+
#
66+
# class AppGeneratorTest < Rails::Generators::TestCase
67+
# tests AppGenerator
68+
# destination File.expand_path("../tmp", __dir__)
69+
# setup :prepare_destination
70+
#
71+
# test "database.yml is not created when skipping Active Record" do
72+
# run_generator %w(myapp --skip-active-record)
73+
# assert_no_file "config/database.yml"
74+
# end
75+
# end
76+
#
77+
# You can provide a configuration hash as second argument. This method returns the output
78+
# printed by the generator.
79+
def run_generator(args = default_arguments, config = {})
80+
capture(:stdout) do
81+
generator_class.start(args, config.reverse_merge(destination_root: destination_root))
82+
end
83+
end
84+
85+
# Instantiate the generator.
86+
def generator(args = default_arguments, options = {}, config = {})
87+
@generator ||= generator_class.new(args, options, config.reverse_merge(destination_root: destination_root))
88+
end
89+
90+
# Create a Rails::Generators::GeneratedAttribute by supplying the
91+
# attribute type and, optionally, the attribute name:
92+
#
93+
# create_generated_attribute(:string, 'name')
94+
#def create_generated_attribute(attribute_type, name = "test", index = nil)
95+
# Rails::Generators::GeneratedAttribute.parse([name, attribute_type, index].compact.join(":"))
96+
#end
97+
98+
private
99+
def destination_root_is_set?
100+
raise "You need to configure your Rails::Generators::TestCase destination root." unless destination_root
101+
end
102+
103+
def ensure_current_path
104+
cd current_path
105+
end
106+
107+
def prepare_destination
108+
puts "preparing destiantion"
109+
teardown_destination
110+
mkdir_p(destination_root)
111+
end
112+
113+
def teardown_destination
114+
rm_rf(destination_root)
115+
end
116+
117+
def migration_file_name(relative)
118+
absolute = File.expand_path(relative, destination_root)
119+
dirname, file_name = File.dirname(absolute), File.basename(absolute).delete_suffix(".rb")
120+
Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
121+
end
122+
end
123+
=end

0 commit comments

Comments
 (0)