New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore non active record subclasses #50
Changes from all commits
212291f
011979c
4054285
d3a5c2f
9c94f17
da7e169
b6bfd2e
8fecabd
7a082ad
6f83d71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
module ActiveMocker | ||
class FileWriter | ||
include Virtus.model | ||
delegate :name, to: :model, prefix: true | ||
|
||
attribute :model, Object | ||
attribute :file, String | ||
attribute :display_errors | ||
attribute :config | ||
attribute :model_names, Array | ||
|
||
def write! | ||
assure_dir_path_exists! | ||
|
||
safe_write { |f| process!(f) } | ||
end | ||
|
||
private | ||
|
||
def process!(file_out) | ||
result = create_mock(file_out) | ||
status = collect_errors(result.errors) | ||
|
||
ok = result.completed? && status.successful? | ||
return unless ok | ||
|
||
display_errors.success_count += 1 | ||
end | ||
|
||
def safe_write | ||
File.open(mock_file_path, "w") do |file_out| | ||
begin | ||
yield file_out | ||
rescue StandardError => e | ||
rescue_clean_up(e, file_out) | ||
end | ||
end | ||
end | ||
|
||
def rescue_clean_up(e, file_out) | ||
display_errors.failed_models << model_name | ||
file_out.close unless file_out.closed? | ||
File.delete(file_out.path) if File.exist?(file_out.path) | ||
display_errors.wrap_an_exception(e, model_name) | ||
end | ||
|
||
def scrapper | ||
@scrapper ||= ActiveRecordSchemaScrapper.new(model: model) | ||
end | ||
|
||
def mock_file_path | ||
File.join(Config.mock_dir, mock_file_name) | ||
end | ||
|
||
def mock_file_name | ||
"#{model_name.underscore}_#{config.mock_append_name.underscore}.rb" | ||
end | ||
|
||
def assure_dir_path_exists! | ||
unless File.exist?(File.dirname(mock_file_path)) | ||
FileUtils.mkdir_p(File.dirname(mock_file_path)) | ||
end | ||
end | ||
|
||
def create_mock(file_out) | ||
MockCreator.new(file: File.open(file), | ||
file_out: file_out, | ||
schema_scrapper: scrapper, | ||
klasses_to_be_mocked: model_names, | ||
enabled_partials: enabled_partials, | ||
mock_append_name: config.mock_append_name).create | ||
end | ||
|
||
OtherErrors = Struct.new(:successful?) | ||
|
||
def collect_errors(create_mock_errors) | ||
add_errors! | ||
|
||
if create_mock_errors.present? || schema.attribute_errors? | ||
display_errors.failed_models << model_name | ||
File.delete(mock_file_path) if File.exist?(mock_file_path) | ||
display_errors.add(create_mock_errors) | ||
OtherErrors.new(false) | ||
else | ||
OtherErrors.new(true) | ||
end | ||
end | ||
|
||
def add_errors! | ||
add_error(schema.association_errors, :associations) | ||
add_error(schema.attribute_errors, :attributes) | ||
end | ||
|
||
def add_error(error, type) | ||
display_errors.wrap_errors(error, model_name, type: type) | ||
end | ||
|
||
def enabled_partials | ||
if config.disable_modules_and_constants | ||
MockCreator.enabled_partials_default - [*:modules_constants] | ||
else | ||
MockCreator.enabled_partials_default | ||
end | ||
end | ||
|
||
def schema | ||
@schema ||= Schema.new(ActiveRecordSchemaScrapper.new(model: model)) | ||
end | ||
|
||
class Schema < SimpleDelegator | ||
def attribute_errors? | ||
attribute_errors.any? { |e| e.level == :error } | ||
end | ||
|
||
def association_errors | ||
associations.errors | ||
end | ||
|
||
def attribute_errors | ||
attributes.errors | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,18 @@ | |
|
||
RSpec.describe ActiveMocker::Generate do | ||
describe ".new" do | ||
let(:not_found_dir) { File.join(File.expand_path("../test_mock_dir", __FILE__)) } | ||
let(:not_found_dir) { File.expand_path("../test_mock_dir", __FILE__) } | ||
|
||
before do | ||
ActiveMocker::Config.set do |config| | ||
config.model_dir = File.join(File.expand_path("../", __FILE__)) | ||
config.model_dir = File.expand_path("../", __FILE__) | ||
config.mock_dir = not_found_dir | ||
config.error_verbosity = 0 | ||
config.progress_bar = false | ||
end | ||
stub_const("ActiveRecord::Base", class_double("ActiveRecord::Base")) | ||
stub_const("Model", class_double("Model", ancestors: [ActiveRecord::Base])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [81/80] |
||
stub_const("SomeNamespace::SomeModule", class_double("SomeNamespace::SomeModule", ancestors: [Module])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [109/80] |
||
end | ||
|
||
before do | ||
|
@@ -68,10 +71,9 @@ | |
context "when old mock exist" do | ||
let(:current_mock_path) { File.join(not_found_dir, "model_mock.rb") } | ||
let(:old_mock_path) { File.join(not_found_dir, "old_mock_from_deleted_model_mock.rb") } | ||
let(:models_dir) { File.join(File.expand_path("../../", __FILE__), "models") } | ||
let(:models_dir) { File.expand_path("../../models", __FILE__) } | ||
|
||
before do | ||
stub_const("ActiveRecord::Base", class_double("ActiveRecord::Base")) | ||
FileUtils.mkdir_p(not_found_dir) | ||
File.open(old_mock_path, "w") { |w| w.write "" } | ||
File.open(current_mock_path, "w") { |w| w.write "" } | ||
|
@@ -93,11 +95,31 @@ | |
end | ||
end | ||
|
||
describe "#active_record_models" do | ||
let(:models_dir) { File.expand_path("../../models", __FILE__) } | ||
|
||
before do | ||
ActiveMocker::Config.model_dir = models_dir | ||
end | ||
|
||
context "with some non ActiveRecord subclasses" do | ||
before do | ||
stub_const("NonActiveRecordModel", class_double("NonActiveRecordModel", ancestors: [Object])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [103/80] |
||
end | ||
|
||
it "ignores non ActiveRecord subclasses" do | ||
result = described_class.new.call | ||
expect(result.active_record_models).to eq [Model] | ||
end | ||
end | ||
end | ||
|
||
describe "ActiveMocker::Config.disable_modules_and_constants" do | ||
before do | ||
ActiveMocker::Config.disable_modules_and_constants = set_to | ||
ActiveMocker::Config.progress_bar = false | ||
ActiveMocker::Config.single_model_path = File.join(File.expand_path("../../", __FILE__), "models/model.rb") | ||
ActiveMocker::Config.model_dir = File.expand_path("../../models", __FILE__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [103/80] |
||
ActiveMocker::Config.single_model_path = File.expand_path("../../models/model.rb", __FILE__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [112/80] |
||
end | ||
|
||
context "when true" do | ||
|
@@ -122,7 +144,8 @@ | |
describe "ActiveMocker::Config.mock_append_name" do | ||
before do | ||
ActiveMocker::Config.progress_bar = false | ||
ActiveMocker::Config.single_model_path = File.join(File.expand_path("../../models", __FILE__), "model.rb") | ||
ActiveMocker::Config.model_dir = File.expand_path("../../models", __FILE__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [91/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this bot is still set to complain at 80 characters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just merged in your RuboCop config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [91/80] |
||
ActiveMocker::Config.single_model_path = File.expand_path("../../models/model.rb", __FILE__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [100/80] |
||
end | ||
|
||
context "defaults" do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class NonActiveRecordModel | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module SomeNamespace | ||
module SomeModule | ||
def some_method | ||
"some return value" | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra empty line detected at module body end.