Skip to content
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

add spec for require_relative #485

Closed
wants to merge 1 commit into from

Conversation

kakubin
Copy link

@kakubin kakubin commented Apr 29, 2024

just add spec

background

on rails application(7.1.3.2) with Ruby 3.3, I met an issue with code below.

# bin/ridgepole(+x)
#!/usr/bin/env ruby
require 'ridgepole'
require 'ridgepole/cli/config'

path = 'Schemafile'
env = ENV['RAILS_ENV'] || 'development'

client = Ridgepole::Client.new(Ridgepole::Config.load('config/database.yml', env))
dsl = File.read(path)
delta = client.diff(dsl) # <= `path` is not passed
delta.migrate if delta.differ?

# ./Schemafile
require_relative './lib/ridgepole/my_customize'
$ bin/ridgepole
<internal:/Users/kakubin/.rbenv/versions/3.3.1/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in `require': cannot load such file -- /Users/kakubin/sample-rails-application/(eval at /Users/kakubin/ridgepole/lib/ridgepole/dsl_parser/lib/ridgepole/my_customize (LoadError)

And this is how values were resolved in the method require_relative.

irb(#<Ridgepole::DSLParser::Conte...):001:0> File.dirname(caller[0])
=> "/Users/kakubin/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.12.0/lib/irb"
irb(#<Ridgepole::DSLParser::Conte...):002:0> File.expand_path(relative_path, File.dirname(caller[0]))
=> "/Users/kakubin/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/irb-1.12.0/lib/irb/lib/ridgepole/my_customize"

Because of lack of an argument path for Ridgepole::Client#diff, Schemafile is evaluated under (eval) and caller[0] return anything but 'Schemafile'.

def self.eval(dsl, opts = {})
ctx = new(opts)
if opts[:path]
ctx.instance_eval(dsl, opts[:path])
else
ctx.instance_eval(dsl)
end

To fix it, I just have to add argument path and no need to modify Ridgepole::DSLParser::Context in conclusion.

this issue is not reproduced with ruby 3.2 or below.

@coveralls
Copy link

coveralls commented Apr 29, 2024

Coverage Status

coverage: 97.208% (+0.1%) from 97.061%
when pulling 45939a3 on kakubin:fix_relative_load_overwriting
into 0481fdf on ridgepole:2.0.

@winebarrel
Copy link
Collaborator

Could you please tell me why you want to make such a change?
It would be helpful if you could tell me the actual problem.

@kakubin
Copy link
Author

kakubin commented Apr 29, 2024

moments please, I'm on writing 😅

@kakubin kakubin marked this pull request as draft April 29, 2024 08:18
@kakubin kakubin force-pushed the fix_relative_load_overwriting branch from 4d1045f to f8079e8 Compare April 29, 2024 09:58
@kakubin kakubin force-pushed the fix_relative_load_overwriting branch from f8079e8 to 45939a3 Compare April 29, 2024 10:01
@kakubin kakubin changed the title Fix require_relative add spec for require_relative Apr 29, 2024
@kakubin kakubin marked this pull request as ready for review April 29, 2024 10:30
@kakubin kakubin closed this May 2, 2024
@kakubin kakubin deleted the fix_relative_load_overwriting branch May 2, 2024 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants