From 759a1f8103e9557ee5f20ee35f6101ec941b7b5c Mon Sep 17 00:00:00 2001 From: Baron Bloomer Date: Tue, 22 Nov 2022 11:18:00 +0000 Subject: [PATCH] Raise error if provided file path cannot be found This will help developers debug issues related to an incorrect file path as early as possible. --- lib/data_migrate.rb | 2 +- lib/data_migrate/config.rb | 6 ++++++ spec/data_migrate/config_spec.rb | 14 +++++++++++++- .../data_migration_generator_spec.rb | 8 +++++--- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/data_migrate.rb b/lib/data_migrate.rb index 5820e5c9..39ce3a88 100644 --- a/lib/data_migrate.rb +++ b/lib/data_migrate.rb @@ -20,6 +20,6 @@ module DataMigrate def self.root - File.dirname(__dir__) + File.dirname(__FILE__) end end diff --git a/lib/data_migrate/config.rb b/lib/data_migrate/config.rb index 426ec409..a7aff17a 100644 --- a/lib/data_migrate/config.rb +++ b/lib/data_migrate/config.rb @@ -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 diff --git a/spec/data_migrate/config_spec.rb b/spec/data_migrate/config_spec.rb index 6f264bfc..60cd0db9 100644 --- a/spec/data_migrate/config_spec.rb +++ b/spec/data_migrate/config_spec.rb @@ -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| @@ -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 diff --git a/spec/generators/data_migration/data_migration_generator_spec.rb b/spec/generators/data_migration/data_migration_generator_spec.rb index 26a92a3d..47547154 100644 --- a/spec/generators/data_migration/data_migration_generator_spec.rb +++ b/spec/generators/data_migration/data_migration_generator_spec.rb @@ -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 @@ -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|