Skip to content

Commit

Permalink
Raise error if provided file path cannot be found
Browse files Browse the repository at this point in the history
This will help developers debug issues related to an incorrect file path as early as possible.
  • Loading branch information
bazay authored and ilyakatz committed Jan 2, 2023
1 parent eb6da04 commit 759a1f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/data_migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

module DataMigrate
def self.root
File.dirname(__dir__)
File.dirname(__FILE__)
end
end
6 changes: 6 additions & 0 deletions lib/data_migrate/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ def initialize
@db_configuration = nil
@spec_name = nil
end

def data_template_path=(value)
@data_template_path = value.tap do |path|
raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exists?(path)
end
end
end
end
14 changes: 13 additions & 1 deletion spec/data_migrate/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
end
end

let(:data_template_path) { "lib/awesome/templates/data_migration.rb" }
let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end

after do
DataMigrate.configure do |config|
Expand All @@ -50,5 +52,15 @@
it "equals the custom data template path" do
is_expected.to eq data_template_path
end

context "when path does not exist" do
subject { DataMigrate.config.data_template_path = invalid_path }

let(:invalid_path) { "lib/awesome/templates/data_migration.rb" }

it "checks that file exists on setting config var" do
expect { subject }.to raise_error { ArgumentError.new("File not found: '#{data_template_path}'") }
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

let(:default_source_root) do
File.expand_path(
File.dirname(File.join(DataMigrate.root, "lib", "generators", "data_migration", "templates", "data_migration.rb"))
File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb"))
)
end

Expand All @@ -82,8 +82,10 @@
end
end

let(:data_template_path) { "lib/awesome/templates/data_migration.rb" }
let(:expected_source_root) { File.expand_path(File.dirname(File.join(DataMigrate.root, data_template_path))) }
let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end
let(:expected_source_root) { File.dirname(data_template_path) }

after do
DataMigrate.configure do |config|
Expand Down

0 comments on commit 759a1f8

Please sign in to comment.