|
| 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 |
0 commit comments