thoughtbot / appraisal

A Ruby library for testing your library against different versions of dependencies.
https://thoughtbot.com
MIT License
1.25k stars 107 forks source link

Implement Bundler install_if #176

Closed n-rodriguez closed 2 years ago

n-rodriguez commented 3 years ago

This an attempt to fix: https://github.com/thoughtbot/appraisal/issues/131

The trick here is to store the conditional as plain string. As explained here https://github.com/thoughtbot/appraisal/pull/132#discussion_r154461946 :

We dont want the lambda evaluated on generate, we want it evaluated on bundle install. No idea how to do that and im too pneumonia sick to think :D

IMHO the easiest way is to write/store it as a string and render it directly in the generated file (verbatim).

So in Appraisal file :

install_if -> { ENV["DB_ADAPTER"] == "mysql2" } do
  gem 'mysql'
end

becomes

install_if '-> { ENV["DB_ADAPTER"] == "mysql2" }' do
  gem 'mysql'
end

After generation it becomes (in gemfiles/*.gemfile) :

install_if -> { ENV["DB_ADAPTER"] == "mysql2" } do
  gem 'mysql'
end

It should help to solve most use cases since you can put any valid Ruby code here :).

At first I tried https://github.com/banister/method_source but it immediately failed.

Because :

Cannot return source for dynamically defined methods.

So I ended up with this easy solution that does the job.

n-rodriguez commented 3 years ago

With real world example :

n-rodriguez commented 3 years ago

The nice thing : it even merges gems with duplicated conditions :

install_if -> { ENV["DB_ADAPTER"] == "oracle_enhanced" } do
  gem "activerecord-oracle_enhanced-adapter", "~> 5.2.0"
  gem "ruby-oci8"
end

Here the yaml structure that generate gemfiles : https://github.com/jbox-web/ajax-datatables-rails/compare/wip/appraisal#diff-a7e1dc0d2145599ae30f5d8cdfa163e79e7708040d2f7cf36b36fab4559974a8R30

n-rodriguez commented 3 years ago

@nickcharlton wdyt?

n-rodriguez commented 3 years ago

Hi there! any news?

n-rodriguez commented 3 years ago

Ping @nickcharlton

n-rodriguez commented 3 years ago

Hi there! Any news?

n-rodriguez commented 3 years ago

It would be nice to have some news...

I hope I've not worked for nothing....

n-rodriguez commented 3 years ago

Ping @nickcharlton

n-rodriguez commented 3 years ago

Ping @nickcharlton

n-rodriguez commented 3 years ago

Ping @nickcharlton

n-rodriguez commented 2 years ago

@nickcharlton ping

nickcharlton commented 2 years ago

Thanks for the ping! I'm going to merge this now.

It'll go out in our next release, which will be once we fix #173.

adrianthedev commented 1 year ago

For some reason, the install_if method doesn't work for me.

install_if -> { ENV["CI"] } do
  gem "pluggy", path: "./gems/pluggy"
end

CleanShot 2023-02-01 at 15 30 06

CleanShot 2023-02-01 at 15 29 35

nickcharlton commented 1 year ago

Can you open a new issue? Ideally with something we can try and replicate with!

n-rodriguez commented 1 year ago

From your screenshot I see appraisal-2.4.1. This feature doesn't exist in this version, you need to use the main/master branch of appraisal.

By the way be sure to use quotes in your Appraisal file.

See: https://github.com/thoughtbot/appraisal/pull/176#issue-777324564

n-rodriguez commented 1 year ago

@nickcharlton maybe it's time to cut a new release? https://github.com/thoughtbot/appraisal/compare/v2.4.1...master :)

adrianthedev commented 1 year ago

Thanks @n-rodriguez. I only checked to see if I'm on the latest version. The main/master instruction missed me.

Will try now.

nickcharlton commented 1 year ago

There will be a new issue soon. I'm tracking a few issues/catching up with lots of bundler changes so I think it's important to test these well.