diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..7ba7ca0 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,22 @@ +version: 2 +updates: + - package-ecosystem: "bundler" + directory: "/" + schedule: + interval: "weekly" + cooldown: + default-days: 3 + groups: + bundler-dependencies: + patterns: + - "*" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + cooldown: + default-days: 3 + groups: + github-actions-dependencies: + patterns: + - "*" diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..680a8d7 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,162 @@ +name: Ruby + +on: + push: + branches: + - main + + pull_request: + +permissions: + contents: read + +jobs: + # Installs each gemfile's bundle once per Ruby, so the jobs below restore it from the cache. + # setup-ruby reads BUNDLE_GEMFILE, so each appraisal gets its own lockfile's gems and Bundler version. + install: + runs-on: ubuntu-latest + name: Install (Ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}) + strategy: + fail-fast: false + matrix: + ruby: &test_rubies + - '3.3' + - '3.4' + - '4.0.9' + gemfile: &gemfiles + - Gemfile + - gemfiles/bundler_2.6.9.gemfile + - gemfiles/bundler_2.7.2.gemfile + - gemfiles/bundler_4.0.24.gemfile + - gemfiles/bundler_4.1.0.beta3.gemfile + - gemfiles/gem_coop.gemfile + env: + BUNDLE_GEMFILE: ${{ matrix.gemfile }} + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - name: Set up Ruby and install gems + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + + rubocop: + needs: install + runs-on: ubuntu-latest + name: RuboCop (Ruby ${{ matrix.ruby }}) + strategy: + matrix: + ruby: &rubies + - '4.0.9' + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rubocop + + types: + needs: install + runs-on: ubuntu-latest + name: RBS and Steep (Ruby ${{ matrix.ruby }}) + strategy: + matrix: + ruby: *rubies + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rake rbs steep + + rspec: + needs: install + runs-on: ubuntu-latest + name: RSpec (Ruby ${{ matrix.ruby }}) + strategy: + matrix: + ruby: *test_rubies + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run the unit specs with coverage + env: + COVERAGE: "true" + run: bundle exec rake spec + + e2e: + needs: install + runs-on: ubuntu-latest + name: RSpec E2E (Ruby ${{ matrix.ruby }}) + strategy: + matrix: + ruby: *test_rubies + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rake e2e + + # The appraisals leave out RuboCop, which the Rakefile loads, so this runs rspec directly + appraisal-rspec: + needs: install + runs-on: ubuntu-latest + name: RSpec (Ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}) + strategy: + fail-fast: false + matrix: + ruby: *test_rubies + gemfile: *gemfiles + exclude: + - gemfile: Gemfile + env: + BUNDLE_GEMFILE: ${{ matrix.gemfile }} + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rspec spec/bundle_matrix + + appraisal-e2e: + needs: install + runs-on: ubuntu-latest + name: RSpec E2E (Ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}) + strategy: + fail-fast: false + matrix: + ruby: *test_rubies + gemfile: *gemfiles + exclude: + - gemfile: Gemfile + env: + BUNDLE_GEMFILE: ${{ matrix.gemfile }} + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rspec spec/e2e diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..e24a8de --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,7 @@ +inherit_gem: + sharp_style_rubocop: config/default.yml + +# The E2E specs describe a bundle command, not a class +RSpec/DescribeClass: + Exclude: + - 'spec/e2e/**/*' diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..888a4b7 --- /dev/null +++ b/Appraisals @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +# Appraisal2 rebuilds generated gemfiles from the parsed DSL, which drops +# comments, so keep the magic comment in the generated heading. +customize_gemfiles do + { + heading: "frozen_string_literal: true\n\nThis file was generated by Appraisal2" + } +end + +plugin "appraisal2-rubocop", require: "appraisal2/rubocop" + +# Appraisal2 copies the root Gemfile's rubygems.org source into every appraisal. +# The gem-coop appraisal sets its own global source, so drop the copied line +# to keep a single global source and avoid Bundler's warning. +# +# rubocop:disable-next Style/RedundantConstantBase +::Appraisal.transform_gemfile do |content, context| + next unless context.appraisal.name == "gem-coop" + + content.sub(%r{^source ["']https://rubygems\.org/?["']\n}, "") +end + +# Gems the Gemfile needs for development, not for running the specs against a Bundler version +DEV_GEMS = %w[ + appraisal2 + appraisal2-rubocop + guard + guard-rspec + guard-rubocop + guard-shell + guard-status + irb + ostruct + rbs + rubocop + simplecov + steep +].freeze + +JOE_GEMS = %w[ + boondoc + guard-boondoc + sharp_style_rubocop +].freeze + +# rubocop:disable-next Style/MethodCallWithArgsParentheses +def bundler_appraisal(version) + appraise "bundler-#{version}" do + (DEV_GEMS + JOE_GEMS).each { |name| remove_gem name } + + gem "bundler", version + end +end + +bundler_appraisal "2.6.9" +bundler_appraisal "2.7.2" +bundler_appraisal "4.0.24" +bundler_appraisal "4.1.0.beta3" + +appraise "gem-coop" do + source "https://gem.coop/" + + (DEV_GEMS + JOE_GEMS).each { |name| remove_gem name } + + source "https://gem.coop/@joe-sharp" do + JOE_GEMS.each { |name| gem name } + end +end diff --git a/BundleMatrix.webp b/BundleMatrix.webp new file mode 100644 index 0000000..c5eae22 Binary files /dev/null and b/BundleMatrix.webp differ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..48d58f1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +## [Unreleased] + +## [0.1.0] - 2026-11-08 + +Initial release. bundle_matrix is a Bundler plugin that repeats `bundle install`, `bundle update` and `bundle add` against every secondary gemfile in your project, so each lockfile stays in step with the main gemfile. + +- Finds secondary gemfiles (`*.gemfile` in the project root and `gemfiles/`), plus `gems.rb` or `Gemfile` in any directories listed in `matrix.paths` +- Forwards the same flags to each gemfile, handling gems, groups, `--path` and `--lockfile` per gemfile +- Runs each secondary gemfile with its own lockfile's Bundler version +- Shares the project's `.bundle/config` and `BUNDLE_*` environment variables with every gemfile +- Recognizes command aliases and prefixes (`i`, `a`, `u`) and plain `bundle`, as Bundler does +- Reports a gemfile that fails as an error and keeps going with the rest diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..766ad16 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,10 @@ +# Code of Conduct + +"bundle_matrix" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.): + +* Participants will be tolerant of opposing views. +* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks. +* When interpreting the words and actions of others, participants should always assume good intentions. +* Behaviour which can be reasonably considered harassment will not be tolerated. + +If you have any concerns about behaviour within this project, please contact us at [joesharp13@gmail.com](mailto:joesharp13@gmail.com). diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..89811be --- /dev/null +++ b/Gemfile @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +source "https://rubygems.org/" + +# Specify your gem's dependencies in bundle_matrix.gemspec +gemspec + +gem "appraisal2" +gem "appraisal2-rubocop", require: false +gem "boondoc" +gem "guard" +gem "guard-boondoc" +gem "guard-rspec" +gem "guard-rubocop" +gem "guard-shell" +gem "guard-status" +gem "irb" +gem "ostruct" # guard-rspec's DSL requires it; no longer a default gem in Ruby 4.0 +gem "rake", "~> 13.0" +gem "rbs", require: false +gem "rspec", "~> 3.0" +gem "rubocop" +gem "rubygems-generate_index", require: false +gem "sharp_style_rubocop" +gem "simplecov", require: false +gem "steep", require: false diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..cd4ab26 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,284 @@ +PATH + remote: . + specs: + bundle_matrix (0.1.0) + bundler (>= 2.6.9) + +GEM + remote: https://rubygems.org/ + specs: + appraisal2 (3.2.6) + bundler (>= 1.17.3) + rake (>= 10) + thor (>= 0.14) + appraisal2-rubocop (1.0.5) + appraisal2 (>= 3.2.6) + rubocop (>= 1.0) + version_gem (~> 1.1, >= 1.1.15) + ast (2.4.3) + boondoc (0.2.1) + prism (>= 1.0, < 3) + thor (~> 1.2) + coderay (1.1.3) + concurrent-ruby (1.3.8) + csv (3.3.7) + diff-lcs (1.6.2) + erb (6.0.7) + ffi (1.17.4) + ffi (1.17.4-x86_64-darwin) + fileutils (1.8.1) + formatador (1.2.3) + reline + guard (2.20.2) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + logger (~> 1.6) + lumberjack (>= 1.0.12, < 2.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.13.0) + shellany (~> 0.0) + thor (>= 0.18.1) + guard-boondoc (0.1.1) + boondoc + guard (~> 2.20) + guard-compat (~> 1.2) + guard-compat (1.2.1) + guard-rspec (4.7.3) + guard (~> 2.1) + guard-compat (~> 1.1) + rspec (>= 2.99.0, < 4.0) + guard-rubocop (1.5.0) + guard (~> 2.0) + rubocop (< 2.0) + guard-shell (0.7.2) + guard (>= 2.0.0) + guard-compat (~> 1.0) + guard-status (0.1.0) + guard (~> 2.20) + guard-compat (~> 1.2) + io-console (0.9.5) + irb (1.18.0) + pp (>= 0.6.0) + prism (>= 1.3.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + json (3.1.0) + language_server-protocol (3.17.0.6) + lint_roller (1.1.0) + listen (3.10.1) + logger + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) + logger (1.7.0) + lumberjack (1.4.2) + method_source (1.1.0) + nenv (0.3.0) + notiffany (0.1.3) + nenv (~> 0.1) + shellany (~> 0.0) + ostruct (0.6.3) + parallel (2.3.0) + parser (3.3.13.0) + ast (~> 2.4.1) + racc + pp (0.6.4) + prettyprint + prettyprint (0.2.1) + prism (1.9.0) + pry (0.16.1) + coderay (~> 1.1) + method_source (~> 1.0) + reline (>= 0.6.0) + racc (1.8.1) + rainbow (3.1.1) + rake (13.4.2) + rb-fsevent (0.11.2) + rb-inotify (0.11.1) + ffi (~> 1.0) + rbs (4.2.0) + logger + prism (>= 1.6.0) + tsort + rdoc (8.1.0) + erb + prism (>= 1.6.0) + rbs (>= 4.0.0) + tsort + regexp_parser (2.13.1) + reline (0.7.0) + io-console (~> 0.5) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.7) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.9) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.8) + rubocop (1.92.2) + json (>= 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (>= 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.11.3, < 3.0) + rubocop-ast (>= 1.49.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.50.0) + parser (>= 3.3.7.2) + prism (~> 1.7) + rubocop-performance (1.27.0) + lint_roller (~> 1.1) + rubocop (>= 1.89.0, < 2.0) + rubocop-ast (>= 1.47.1, < 2.0) + rubocop-rspec (3.10.2) + lint_roller (~> 1.1) + regexp_parser (>= 2.0) + rubocop (~> 1.86, >= 1.86.2) + ruby-progressbar (1.13.0) + rubygems-generate_index (1.2.0) + securerandom (0.4.1) + sharp_style_rubocop (0.1.5) + rubocop (~> 1.92) + rubocop-performance (~> 1.20) + rubocop-rspec (~> 3.0) + shellany (0.0.1) + simplecov (1.3.2) + steep (2.1.0) + concurrent-ruby (>= 1.1.10) + csv (>= 3.0.9) + fileutils (>= 1.1.0) + json (>= 2.1.0) + language_server-protocol (>= 3.17.0.4, < 4.0) + listen (~> 3.0) + logger (>= 1.3.0) + parser (>= 3.2) + prism (>= 0.25.0) + rainbow (>= 2.2.2, < 4.0) + rbs (~> 4.2) + securerandom (>= 0.1) + strscan (>= 1.0.0) + terminal-table (>= 2, < 5) + uri (>= 0.12.0) + strscan (3.1.8) + terminal-table (4.0.0) + unicode-display_width (>= 1.1.1, < 4) + thor (1.5.0) + tsort (0.2.0) + unicode-display_width (3.3.0) + unicode-emoji (~> 4.3) + unicode-emoji (4.3.0) + uri (1.1.2) + version_gem (1.1.17) + +PLATFORMS + ruby + x86_64-darwin-24 + +DEPENDENCIES + appraisal2 + appraisal2-rubocop + boondoc + bundle_matrix! + guard + guard-boondoc + guard-rspec + guard-rubocop + guard-shell + guard-status + irb + ostruct + rake (~> 13.0) + rbs + rspec (~> 3.0) + rubocop + rubygems-generate_index + sharp_style_rubocop + simplecov + steep + +CHECKSUMS + appraisal2 (3.2.6) sha256=6664d2404b9fe160dfbbed4dd9b78e7b9146402bc3468ff92ba78c1878bc318a + appraisal2-rubocop (1.0.5) sha256=91db38e8585dc2b07491eee6c288764511a6c6a0a5d06c339c91ab6a4164bea8 + ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383 + boondoc (0.2.1) sha256=3cfe5bd9b1ca5a2ab34a482d5b387778dad54eae575de5c90d850d1cb3599203 + bundle_matrix (0.1.0) + bundler (4.0.24) sha256=16ca64e20816a0cf151a948fb141d788602996d2b4d73577b74388fe84fa98bc + coderay (1.1.3) sha256=dc530018a4684512f8f38143cd2a096c9f02a1fc2459edcfe534787a7fc77d4b + concurrent-ruby (1.3.8) sha256=b2f1be836e968ccc78ccfce277ea79c72a88633f22306782c16ff23fb415d1e1 + csv (3.3.7) sha256=15a3f16f6628da8664d455eb634e7f22031bcf53044923caf56975109e71b9f8 + diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962 + erb (6.0.7) sha256=c5ca6dc25b0ef974a44dc8f59fe847577122483b1968a38dec305c60bf91ee92 + ffi (1.17.4) sha256=bcd1642e06f0d16fc9e09ac6d49c3a7298b9789bcb58127302f934e437d60acf + ffi (1.17.4-x86_64-darwin) sha256=aa70390523cf3235096cf64962b709b4cfbd5c082a2cb2ae714eb0fe2ccda496 + fileutils (1.8.1) sha256=2d541a1ee34a9ed5c4f054be8fb819a26b5d654488a31dc8806917cf448b9cc3 + formatador (1.2.3) sha256=19fa898133c2c26cdbb5d09f6998c1e137ad9427a046663e55adfe18b950d894 + guard (2.20.2) sha256=2d0574d35619669f2f71739a0313290810b9cf66b2263f5d97e815fcce598d4d + guard-boondoc (0.1.1) sha256=57bb57a8e81dd3f62518158df5a07af6c245e6460e0d91cf2cf7cf38cf1a83c2 + guard-compat (1.2.1) sha256=3ad21ab0070107f92edfd82610b5cdc2fb8e368851e72362ada9703443d646fe + guard-rspec (4.7.3) sha256=a47ba03cbd1e3c71e6ae8645cea97e203098a248aede507461a43e906e2f75ca + guard-rubocop (1.5.0) sha256=3041d796dcb5ee31e352de74732250826f5f235b4ff48df9dbf424a6dc736251 + guard-shell (0.7.2) sha256=c9cd104c597c51465f7bed623753f7f4cfb87cfbd0ff28bdda9a02f6605e436d + guard-status (0.1.0) sha256=4242b35622b04cd22e2df06777d5b05d295d6eddb712692d7bf4330c94be99ed + io-console (0.9.5) sha256=83dd9bbf4c4680416629245b39cd4212eb5f4d9a4ea91804870c1f6b846414d5 + irb (1.18.0) sha256=de9454a0703a54704b9811a5ef31a60c86949fbf4013fcf244fabc7c775248e3 + json (3.1.0) sha256=3ce7ccf8fde58960177a3a756c31f292a4c1e1dedee884547096a5b0604e3bd8 + language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0 + lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87 + listen (3.10.1) sha256=4c3f32855657f348747da4dd2372ff3790d90a6b1d3e4f21a3706adb2d04783c + logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203 + lumberjack (1.4.2) sha256=40de5ae46321380c835031bcc1370f13bba304d29f2b5f5bb152061a5a191b95 + method_source (1.1.0) sha256=181301c9c45b731b4769bc81e8860e72f9161ad7d66dd99103c9ab84f560f5c5 + nenv (0.3.0) sha256=d9de6d8fb7072228463bf61843159419c969edb34b3cef51832b516ae7972765 + notiffany (0.1.3) sha256=d37669605b7f8dcb04e004e6373e2a780b98c776f8eb503ac9578557d7808738 + ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912 + parallel (2.3.0) sha256=f75a3e904101ce6a1ccb6b8dc800cbafd42166d83330192438dcf29395167a6f + parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca + pp (0.6.4) sha256=dfcb0fce700c41456265922884f9fe195d7fbb0674a3578e6c0f69588e82b570 + prettyprint (0.2.1) sha256=d555d57ea6d7cf001d4774b693546a6bd88bec1e778ddffb5c60b68ceec4b7d4 + prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85 + pry (0.16.1) sha256=d92d82c86e7f2dc34beec6d2892d5cf5ea517eea22eed3ceabb38ee62a98accc + racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f + rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a + rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701 + rb-fsevent (0.11.2) sha256=43900b972e7301d6570f64b850a5aa67833ee7d87b458ee92805d56b7318aefe + rb-inotify (0.11.1) sha256=a0a700441239b0ff18eb65e3866236cd78613d6b9f78fea1f9ac47a85e47be6e + rbs (4.2.0) sha256=51f7b886dcc05bc09e10b901daa6a81829f6adc03101d6ca9ea4aac6103e0674 + rdoc (8.1.0) sha256=befc37880a5cfd36dcb176c748b5fd5ee39dd3536a75e81824e4972ce230f60a + regexp_parser (2.13.1) sha256=5aedb6b7c35688f51e86eef17a15374dfd287c83f4ca644b2c5f3ec50a33b44b + reline (0.7.0) sha256=5b012d8e55dbf9d450f12bde2cf7d15ff546ae80b3f8f3b30e570d431815583d + rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587 + rspec-core (3.13.7) sha256=c64ff16d9788119805cbc6a3f4b6d87cc22417c2d25976a3330de136896b5469 + rspec-expectations (3.13.6) sha256=bf538ca6d367ff5b0d85a87fdb5c0c6038f2cd1308a0eeba07476caa02d22f35 + rspec-mocks (3.13.9) sha256=ea5c7ff3054a6098ca6039aacef80aec69d5a8a3ef80be8fc88337359b672a30 + rspec-support (3.13.8) sha256=e4160030309b7e3e9d9863dfe720a35dfcc577162f5402cd68fc8c5258a0ffd6 + rubocop (1.92.2) sha256=14fb31eed143a0dc6f78186c550c1715e81cd38f59d05d1165050e445c1f5382 + rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db + rubocop-performance (1.27.0) sha256=eeeb1374d062a368ee1c787b70eb0b0cc4b184cb1f8565f424760946146d61ce + rubocop-rspec (3.10.2) sha256=0b3e2ecc592cd10ecbf0095bb58d1e357905276e069643523cc19eb7495f65e2 + ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33 + rubygems-generate_index (1.2.0) sha256=cb33b935545a38dba4ac1f20d391090d2bb017d0956537a8bbadc1fff23f0aab + securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1 + sharp_style_rubocop (0.1.5) sha256=bb75ff29653e6d64324df2c4ff794d5458c69183ef4c47ecf1c621a27862df11 + shellany (0.0.1) sha256=0e127a9132698766d7e752e82cdac8250b6adbd09e6c0a7fbbb6f61964fedee7 + simplecov (1.3.2) sha256=2a22ec1759f1b8db60881a2e038758956fc69ef93ed669be0bde0678de6d0e85 + steep (2.1.0) sha256=1daf53573283b0e6d404a7273cca41fe5c15b14ece05167934f5ffc179e171d9 + strscan (3.1.8) sha256=aae2db611a225559f21ffbb71765c9a4e60fd262534a9ea84f4f11c7f32f679e + terminal-table (4.0.0) sha256=f504793203f8251b2ea7c7068333053f0beeea26093ec9962e62ea79f94301d2 + thor (1.5.0) sha256=e3a9e55fe857e44859ce104a84675ab6e8cd59c650a49106a05f55f136425e73 + tsort (0.2.0) sha256=9650a793f6859a43b6641671278f79cfead60ac714148aabe4e3f0060480089f + unicode-display_width (3.3.0) sha256=4b7aa66a4b11db50f6f7e98411215cd0dd10eaecb665d36eec5bcacb3fa0b613 + unicode-emoji (4.3.0) sha256=11c02fa73290378c066bb0562cd4c87d8e1b706fbbe0059ca12746a4244de8ce + uri (1.1.2) sha256=89292e98eabbef6248b0a0ed5e7e398e152f83f17560023a6d94a7f492b169fb + version_gem (1.1.17) sha256=70420b9675fbb77a7e7e7eb8ed6580dd4dd25be53b1454c1a79cc18f3c5a8dfd + +BUNDLED WITH + 4.0.24 diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..5df2367 --- /dev/null +++ b/Guardfile @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +# More info at https://github.com/guard/guard#readme + +# Prevent informational messages from Guard +logger level: :warn + +guard(:rubocop, + cli: "--format progress --format json --out tmp/rubocop.json ", + all_on_start: true, + all_after_pass: true) do + watch(/.+\.rb$/) + watch(/bin/) + watch(/Guardfile/) + watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) } +end + +guard(:rspec, + cmd: "bundle exec rspec --exclude-pattern 'spec/e2e/**/*_spec.rb' " \ + "--format progress --format json --out tmp/rspec.json", + all_on_start: true, + all_after_pass: true) do + require "guard/rspec/dsl" + dsl = Guard::RSpec::Dsl.new(self) + + # RSpec files + rspec = dsl.rspec + watch(rspec.spec_helper) { rspec.spec_dir } + watch(rspec.spec_support) { rspec.spec_dir } + watch(%r{^spec/(?!e2e/).+_spec\.rb$}) + + # Ruby files + ruby = dsl.ruby + dsl.watch_spec_files_for(ruby.lib_files) + + # Golden files are compared by the generator spec + watch(%r{^spec/fixtures/}) { rspec.spec_dir } +end + +# Regenerates the Boondoc docs when the files they are built from change +guard :boondoc, status_file: "tmp/docs_status.txt" + +# One status line for the guards above, last so it runs after them +guard(:status, + segments: { + "RuboCop" => { rubocop: "tmp/rubocop.json" }, + "Tests" => { rspec: "tmp/rspec.json" }, + "Docs" => { file: "tmp/docs_status.txt" } + }) diff --git a/LICENSE b/LICENSE.txt similarity index 87% rename from LICENSE rename to LICENSE.txt index 1b37b3f..f4b21a6 100644 --- a/LICENSE +++ b/LICENSE.txt @@ -1,6 +1,6 @@ -MIT License +The MIT License (MIT) -Copyright (c) 2026 Joe Sharp +Copyright (c) 2026 joe-sharp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a41862 --- /dev/null +++ b/README.md @@ -0,0 +1,149 @@ +# BundleMatrix + +A Bundler plugin that repeats `bundle install`, `bundle update` and `bundle add` against every other gemfile in your project, so each lockfile stays in step with the main `Gemfile`. + +Projects that test against several dependency sets keep extra gemfiles (`ci.gemfile`, `gemfiles/rails7.gemfile`, engine Gemfiles, and so on). Without help, every one of them needs its own `BUNDLE_GEMFILE=… bundle install`. With bundle_matrix, running the command on the main `Gemfile` does that for you. + +![BundleMatrix](https://raw.githubusercontent.com/joe-sharp/bundle_matrix/main/BundleMatrix.webp) + +## Installation + +Install the plugin once for your user: + +```bash +bundle plugin install bundle_matrix +``` + +Or declare it in the project's `Gemfile`: + +```ruby +plugin "bundle_matrix" +``` + +## Usage + +Run the command on the main `Gemfile` as usual: + +```bash +bundle install +bundle update rails --conservative +bundle update --all +bundle add rack --group development +``` + +After the main run succeeds, each discovered gemfile is run with the same command and flags. Output is headed by the gemfile it applies to: + +``` +== bundle_matrix: gemfiles/rails7.gemfile -> bundle update rails --conservative +== bundle_matrix: engines/admin/Gemfile -> bundle install +``` + +Only `install`, `update` and `add` are fanned out. `bundle check`, `bundle exec` and similar commands are not, because Bundler does not run plugin hooks for them. + +Bundler's short forms work too: `bundle i`, `bundle ins`, `bundle a`, `bundle u`, and any other unambiguous prefix of a command name. + +A plain `bundle`, or `bundle` with only flags such as `bundle --jobs=4`, runs as `install` and fans out as `install`, but only while `default_cli_command` is unset or `install`. With it set to `cli_help`, those commands print help and nothing fans out. Bundler's documentation already names `cli_help` as the default, while its code still runs `install` and prints a notice that this will change. To keep fanning out whatever Bundler does, set the setting explicitly, or run `bundle install`: + +```bash +bundle config set default_cli_command install +``` + +## Which gemfiles are found + +The main gemfile is `Gemfile` in the project root, or the file named by `matrix.default_gemfile`. It is never treated as a secondary gemfile. Secondary gemfiles are found in: + +- `*.gemfile` in the project root, e.g. `ci.gemfile` with `ci.gemfile.lock` +- `*.gemfile` in `gemfiles/` +- any directories listed in `matrix.paths`, each contributing its `gems.rb`, or its `Gemfile` when it has no `gems.rb`, the order Bundler uses + +## Configuration + +Settings use `bundle config` or `BUNDLE_MATRIX__*` environment variables: + +| Setting | Default | Meaning | +| --- | --- | --- | +| `matrix.default_gemfile` | `Gemfile` | File name of the main gemfile in the project root. Fan-out starts only when the command runs on this file, e.g. `gems.rb` | +| `matrix.paths` | none | Extra directories or globs to search for a `gems.rb` or `Gemfile`, separated by commas, e.g. `engines/*,apps/admin` | +| `matrix.disabled` | `false` | Turn fan-out off | +| `matrix.fail_fast` | `false` | Stop at the first failing gemfile | +| `matrix.bundler_warnings` | `true` | Warn when a gemfile runs a different Bundler from the main Gemfile. Set it to `false` for projects that pin different Bundlers on purpose | + +```bash +bundle config set matrix.paths "engines/*,apps/admin" +``` + +For a project whose main file is `gems.rb`, set the main gemfile name once, and run the command on `gems.rb`: + +```bash +bundle config set matrix.default_gemfile gems.rb +bundle install +``` + +To skip the matrix for a single run, set `BUNDLE_MATRIX__DISABLED` on that command: + +```bash +BUNDLE_MATRIX__DISABLED=1 bundle install +``` + +`bundle config set matrix.disabled true` is the persistent version, and stays in effect until you change it back. + +## How each command is applied + +Flags are forwarded as typed, and Bundler's own option table decides what each flag means. The exceptions below are where a flag can't be applied to another gemfile as-is. + +- **`install`**: `--lockfile` and `--standalone` only apply to the main Gemfile, so they are not forwarded. +- **`update `**: only the named gems that appear in that gemfile's lockfile are updated. A gemfile with none of them is skipped. `--group` is limited to groups it declares. If it has no lockfile yet, `install` is run to create one. +- **`update --all`**, `--conservative`, `--patch`, `--minor`, `--major`, `--pre`, `--strict` and the rest: forwarded unchanged. +- **`add `**: gems the gemfile already declares, including through `eval_gemfile`, are not added again. If every gem is already declared, `install` is run to re-lock the gemfile. A relative `--path` is rewritten so it resolves from that gemfile's directory. +- `--gemfile` is never forwarded. The target is set through `BUNDLE_GEMFILE` instead. + +## Settings and Bundler versions + +Each gemfile is run the way `bundle` would run it on its own, with the project's settings: + +- The project's `.bundle/config` and the `BUNDLE_*` environment variables you set are passed on, so `frozen`, `without`, `jobs` and the rest apply to every gemfile. A relative `path`, `bin` or `cache_path` still resolves from the project root, so every gemfile shares the same `vendor/bundle`. A `.bundle/config` next to a secondary gemfile takes priority for that gemfile. +- `gemfile` and `lockfile` are not passed on, because they name the main Gemfile's own files. +- Each gemfile runs with the Bundler version in its own `BUNDLED WITH` line, and `install` installs that version first if it is missing. A `gem "bundler"` line in a gemfile that has no lockfile yet is not installed for you, so install that version yourself first. Bundler's `version` setting controls this: `bundle config set version system` keeps every gemfile on the running version, and `bundle config set version 4.0.23` pins one. `BUNDLER_VERSION` is not passed on, because it would hold every gemfile to one version. +- When a gemfile runs with a different Bundler from the main Gemfile, a one-line warning names both versions, and the run ends by suggesting either `bundle update --bundler`, which updates Bundler in every lockfile, or `bundle config set matrix.bundler_warnings false`, which turns these warnings off when each gemfile should keep its own Bundler. + +## How it uses Bundler + +bundle_matrix needs Bundler 2.6.9 or newer, below 4.2 and its prereleases, and is tested against 2.6.9, 2.7.2, 4.0.24 and 4.1.0.beta3. The upper bound moves up once a new Bundler minor has been tested. Under any other Bundler the plugin still runs, but prints a one-line warning that it is untested there, with a link to the issue tracker. + +Fan-out starts from Bundler's documented plugin hook, `after-install-all`, and each gemfile runs in its own `bundle` process. Everything else uses Bundler classes that work today but are not a plugin API, so a new Bundler release could change them: + +- Thor's command and option tables (`Bundler::CLI`), which decide what each flag means and which command was run (`ArgvParser`, `Invocation`). Command names such as `ins` are resolved by calling Thor's protected `normalize_command_name` with `send` +- `Bundler::Dsl` and `Bundler::LockfileParser`, which read each secondary gemfile and lockfile (`Planner`). `Bundler.definition` and `Bundler.locked_gems` only describe the main Gemfile, so they can't be used for the others. +- The settings' storage levels, through `Bundler.settings` (`ProjectSettings`) + +## Limitations + +- `bundle check` is not supported. Bundler only calls plugin hooks around installation. +- `bundle add --skip-install` never runs the hook, because no installation happens. +- Flags that Bundler does not know about are forwarded as-is, and any value after them is treated as a gem name. + +## Development + +Run `bin/setup`, then `rake`. The default task runs the unit specs, the end-to-end specs, RuboCop, `rake rbs`, which runs `rbs validate` on the signatures in `sig/` with the Bundler stubs in `sig_stubs/`, and `rake steep`, which type checks `lib/` against those signatures. Run `rake e2e` on its own for the end-to-end specs, which run real `bundle` commands and take longer. Run `COVERAGE=true bundle exec rake spec` to also write a coverage report to `coverage/index.html`. That run fails below 90% line coverage or 80% branch coverage, and CI runs it this way. + +Run `bundle exec guard` to re-run the unit specs and RuboCop on every change. It also regenerates the docs when the files they are built from change. + +`docs/` is generated by Boondoc, so edit `README.md` and the code comments rather than `docs/`. To regenerate it by hand, run `bundle exec boondoc` + +To try a change against a real project, install from a local checkout with `bundle plugin install bundle_matrix --path /path/to/bundle_matrix` (the directory must contain `plugins.rb`). + +## Contributing + +Bug reports and pull requests are welcome. Send issues and pull requests to https://github.com/joe-sharp/bundle_matrix. + +Run `bundle exec rake` before you open a pull request. The Development section above explains what it runs. + +A bug report should include: + +- the Bundler version, from `bundle -v` +- the command you ran, e.g. `bundle update rails --conservative` +- your gemfile layout: which gemfiles exist, where they are, and any `matrix.*` settings + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a39c35d --- /dev/null +++ b/Rakefile @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rspec/core/rake_task" +require "rubocop/rake_task" + +RSpec::Core::RakeTask.new(:spec) { |t| t.exclude_pattern = "spec/e2e/**/*_spec.rb" } + +desc "Run the end-to-end specs, which run real bundle commands" +RSpec::Core::RakeTask.new(:e2e) { |t| t.pattern = "spec/e2e/**/*_spec.rb" } + +RuboCop::RakeTask.new + +desc "Validate the RBS signatures in sig/, with stubs for the Bundler types they name" +task :rbs do + sh "rbs", "-I", "sig", "-I", "sig_stubs", "validate" +end + +desc "Type check lib/ against the signatures in sig/ with Steep" +task :steep do + sh "steep", "check" +end + +# Replaces Bundler's release task. +Rake::Task["release"].clear +desc "Tag, push, build and publish the gemspec version, asking for a description if not given" +task(:release, [:description]) { |_, args| exec "bin/release", *[args.to_a.join(", ")].reject(&:empty?) } + +task default: %i[spec e2e rubocop rbs steep] diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..7a686de --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,12 @@ +# Security Policy + +## Supported Versions + +Only the latest release of bundle_matrix is supported with security fixes. + +## Reporting a Vulnerability + +Please don't report vulnerabilities in a public issue. Report them privately in either of these ways: + +- With GitHub's private vulnerability reporting, from the Security tab of https://github.com/joe-sharp/bundle_matrix. +- By email to joesharp13@gmail.com. diff --git a/Steepfile b/Steepfile new file mode 100644 index 0000000..2cd2078 --- /dev/null +++ b/Steepfile @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +D = Steep::Diagnostic + +# Type checks lib/ against sig/. Bundler publishes no signatures, so sig_stubs/ declares the parts of its API we call. +target :lib do + signature "sig", "sig_stubs" + check "lib" + + library "pathname" + + # Annotating every empty [] and {} adds noise; the method signatures still check what they end up holding + configure_code_diagnostics(D::Ruby.default) do |hash| + hash[D::Ruby::UnannotatedEmptyCollection] = nil + end +end diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..42d18a8 --- /dev/null +++ b/bin/console @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "bundle_matrix" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +require "irb" +IRB.start(__FILE__) diff --git a/bin/release b/bin/release new file mode 100755 index 0000000..2fd1890 --- /dev/null +++ b/bin/release @@ -0,0 +1,79 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# Usage: bin/release [description] +# Releases v; the description is asked for if not given. + +require "bundler/gem_helper" +require "net/http" + +def ask(question) + print "#{question}: " + answer = $stdin.gets.to_s.strip + answer unless answer.empty? +end + +def run(*cmd) + puts "$ #{cmd.join(" ")}" + system(*cmd) || abort("Failed: #{cmd.join(" ")}") +end + +# Compact index lists every published version of the gem as the first word of each line. +def published?(host, name, version) + response = Net::HTTP.get_response(URI("#{host}/info/#{name}")) + case response + when Net::HTTPSuccess then response.body.each_line.any? { |line| line.split(" ", 2).first == version } + when Net::HTTPNotFound then false + else abort "Could not check #{host} for #{name} #{version}: #{response.code}" + end +end + +abort "GEM_HOST_API_KEY is not set" if ENV["GEM_HOST_API_KEY"].to_s.empty? +# Checked before switching branches, so a dirty tree is never carried over to main. +abort "Working tree is not clean" unless `git status --porcelain`.empty? + +run("git", "checkout", "main") +run("git", "pull", "--ff-only", "origin", "main") +abort "main does not match origin/main" unless `git rev-parse main`.strip == `git rev-parse origin/main`.strip + +# Loaded after checking out main, so the gemspec is main's. +helper = Bundler::GemHelper.new(File.expand_path("..", __dir__)) +name = helper.gemspec.name +version = helper.gemspec.version.to_s +tag = "v#{version}" + +description = ARGV.first || ask("Release description") or abort "A description is required" + +tag_exists = system("git", "rev-parse", "-q", "--verify", "refs/tags/#{tag}", out: File::NULL) +tag_commit = `git rev-parse "#{tag}^{commit}"`.strip if tag_exists +abort "#{tag} already exists but does not point at HEAD" if tag_exists && tag_commit != `git rev-parse HEAD`.strip + +puts "\nTag #{tag}: #{description}" +exit unless ask("Tag, push, build and publish? (y/N)").to_s.downcase.start_with?("y") + +# Built first, so a build failure stops the release before anything is tagged or published +puts "$ gem build #{File.basename(helper.spec_path)}" +gem_file = helper.build_gem + +if tag_exists + puts "#{tag} already exists at HEAD; not creating it again" +else + run "git", "tag", "-a", tag, "-m", description +end + +if published?(Gem.host, name, version) + puts "#{name} #{version} is already on #{Gem.host}; skipping" +else + run("gem", "push", gem_file) +end + +coop = "https://gem.coop/@joe-sharp" +if published?(coop, name, version) + puts "#{name} #{version} is already on #{coop}; skipping" +else + run("gem", "push", "--host", coop, gem_file) +end + +# Last, so the tag only goes public once the gem is out. +# Pushing a tag the remote already has at the same commit is a no-op. +run "git", "push", "origin", tag diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/bundle_matrix.gemspec b/bundle_matrix.gemspec new file mode 100644 index 0000000..9286274 --- /dev/null +++ b/bundle_matrix.gemspec @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require_relative "lib/bundle_matrix/version" + +Gem::Specification.new do |spec| + spec.name = "bundle_matrix" + spec.version = BundleMatrix::VERSION + spec.authors = ["Joe Sharp"] + spec.email = ["joesharp13@gmail.com"] + + spec.summary = "Runs bundle install, update and add against every secondary gemfile too." + spec.description = "A Bundler plugin. After install, update or add on the main Gemfile, it replays the same " \ + "command, with the same flags, against *.gemfile files in the project root and gemfiles/, " \ + "and any extra directories configured with matrix.paths." + spec.homepage = "https://github.com/joe-sharp/bundle_matrix" + spec.license = "MIT" + spec.required_ruby_version = ">= 3.3.0" + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = "#{spec.homepage}/tree/v#{BundleMatrix::VERSION}" + spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues" + spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md" + spec.metadata["rubygems_mfa_required"] = "true" + + # Specify which files should be added to the gem when it is released. + # Only the listed paths that are tracked in git go into the gem, so untracked files never get in. + spec.files = IO.popen( + %w[git ls-files -z -- lib sig plugins.rb README.md LICENSE.txt CHANGELOG.md], + chdir: __dir__, + err: IO::NULL + ) do |ls| + ls.readlines("\x0", chomp: true) + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + # The plugin relies on Bundler internals that aren't a plugin API, so it needs the oldest Bundler it runs under. + # Newer Bundlers are allowed, and the hook warns when it runs under one that has not been tested. + spec.add_dependency "bundler", ">= #{BundleMatrix::MIN_BUNDLER}" + + # For more information and examples about making a new gem, check out our + # guide at: https://guides.rubygems.org/make-your-own-gem/ +end diff --git a/docs/.boondoc-manifest b/docs/.boondoc-manifest new file mode 100644 index 0000000..c1cf46e --- /dev/null +++ b/docs/.boondoc-manifest @@ -0,0 +1,15 @@ +BundleMatrix.md +BundleMatrix/ArgvParser.md +BundleMatrix/ArgvParser/Switch.md +BundleMatrix/Config.md +BundleMatrix/Discovery.md +BundleMatrix/Error.md +BundleMatrix/Hook.md +BundleMatrix/Invocation.md +BundleMatrix/Invocation/Option.md +BundleMatrix/Planner.md +BundleMatrix/Planner/Plan.md +BundleMatrix/ProjectSettings.md +BundleMatrix/Runner.md +BundleMatrix/SecondaryGemfile.md +README.md diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md new file mode 100644 index 0000000..8a25e53 --- /dev/null +++ b/docs/ARCHITECTURE.md @@ -0,0 +1,126 @@ +# How bundle_matrix works + +After `bundle install`, `bundle update` or `bundle add` succeeds on the main Gemfile, bundle_matrix runs the same command against every secondary gemfile in the project. Each one runs in its own `bundle` process. + +## Overview + +```mermaid +flowchart LR + cmd["bundle install,
update or add"] --> main["Bundler runs it on
the main Gemfile"] + main -->|"success"| hook["Hook
should it fan out?"] + hook --> parse["Invocation and ArgvParser
parse the command line"] + parse --> discover["Discovery
find the secondary
gemfiles"] + discover --> plan["Planner
one plan per gemfile"] + plan --> run["Runner
one bundle process
per gemfile"] +``` + +| Step | Files | +|------|-------| +| Hook: should it fan out? | `hook.rb` | +| Parse the command line | `invocation.rb`, `argv_parser.rb` | +| Read the `matrix.*` settings | `config.rb` | +| Find the secondary gemfiles | `discovery.rb` | +| Plan each gemfile | `planner.rb`, `secondary_gemfile.rb` | +| Run each gemfile | `runner.rb`, `project_settings.rb` | + +The hook (`hook.rb`) stops without doing anything when: + +- the command isn't `install`, `update` or `add` +- `BUNDLE_MATRIX_ACTIVE` is set, so this is already a child run +- `matrix.disabled` is set +- the command ran on a gemfile other than `matrix.default_gemfile` +- no secondary gemfiles were found + +Discovery (`discovery.rb`) looks for `*.gemfile` in the project root, `gemfiles/*.gemfile`, and each `matrix.paths` entry. A directory in `matrix.paths` contributes its `gems.rb`, or else its `Gemfile`. The main gemfile is never included. + +Every gemfile is planned before any of them runs. Planning starts by dropping `--gemfile`, then follows one of the three paths below. + +## Install + +```mermaid +flowchart LR + install["bundle install"] --> drop["drop --lockfile,
--standalone and --path"] + drop --> run["bundle install
with the other flags"] +``` + +Those three flags only make sense for the main Gemfile, or are ones Bundler no longer accepts (`--path` on Bundler 4). Each one dropped is noted in the output. + +## Update + +```mermaid +flowchart TD + update["bundle update"] --> full{"--all, or nothing named?"} + full -->|"no"| lock{"does this gemfile
have a lockfile?"} + full -->|"yes"| runAll["bundle update
with the same flags"] + lock -->|"no"| install["bundle install
to create one"] + lock -->|"yes"| match{"are any of the named gems
or groups in this gemfile?"} + match -->|"no"| skip["skip this gemfile"] + match -->|"yes"| runSome["bundle update
with only those"] +``` + +"Named" means gems, `--group`, `--source`, `--ruby` or `--bundler`. Gems are matched against this gemfile's lockfile (`gems.locked` for a `gems.rb`, otherwise `.lock`). Groups are matched against the groups this gemfile declares. An update with `--ruby` or `--bundler` is never skipped. + +## Add + +```mermaid +flowchart TD + add["bundle add gems"] --> declared{"does this gemfile
already declare them?"} + declared -->|"some are new"| addNew["bundle add
only the new gems"] + declared -->|"all of them"| install["bundle install
to re-lock"] +``` + +Declared gems include those pulled in by `eval_gemfile`. A relative `--path` is rewritten to be relative to the secondary gemfile's directory. + +## Running each gemfile + +```mermaid +flowchart TD + plan{"plan"} -->|"skip"| pass["pass"] + plan -->|"command"| child["child bundle process
in the gemfile's directory"] + plan -->|"could not plan"| fail["failure"] + child -->|"exit 0"| pass + child -->|"non-zero"| fail + fail -->|"matrix.fail_fast"| stop["stop here"] +``` + +Gemfiles run in sorted order. A gemfile that could not be planned is printed as an error and counts as a failure, and the others still run. If any failed, the run ends with `BundleMatrix::Error`, which names them and exits 1. + +The child is a separate process because Bundler keeps global state. It gets: + +- the project's `.bundle/config` and the user's `BUNDLE_*` variables, with relative paths made absolute +- `BUNDLE_GEMFILE` set to its gemfile +- `BUNDLE_MATRIX_ACTIVE=1`, so its own hook doesn't fan out again +- no `BUNDLER_VERSION`, so it runs the Bundler named in its own lockfile's `BUNDLED WITH` + +## Bundler version warnings + +- Before planning, the hook warns when the running Bundler is outside `TESTED_BUNDLER` in `version.rb`. +- Each plan that runs a command carries the Bundler named in its gemfile's lockfile. The runner warns for each one that differs from the main Bundler, when `matrix.bundler_warnings` is on and the `version` setting is `lockfile` or unset. With `system` or a pinned version, every gemfile runs the main Bundler, so nothing is warned. At the end it suggests `bundle update --bundler`, or `bundle config set matrix.bundler_warnings false` to turn these warnings off. + +## Development and CI + +```mermaid +flowchart LR + appraisals["Appraisals"] --> gemfiles["gemfiles/
one per Bundler version,
plus gem_coop"] + gemfiles --> install["CI install job
per gemfile"] + install --> checks["RuboCop, RBS,
Steep and RSpec"] + install --> appraisal["RSpec per
appraisal gemfile"] +``` + +- `rake` runs `spec`, `e2e`, `rubocop`, `rbs` and `steep`. Steep type checks `lib/` against `sig/`, and `sig_stubs/` declares the parts of Bundler's API the code calls, since Bundler publishes no signatures. +- `MIN_BUNDLER` in `version.rb` sets the gemspec's Bundler dependency, a lower bound only, so a newer Bundler can still install the plugin. `TESTED_BUNDLER` in the same file sets the untested-Bundler warning. `spec/bundle_matrix_spec.rb` checks `TESTED_BUNDLER` against the `gemfiles/bundler_*.gemfile` files the Appraisals generate: every one is in range, and the range stops before the minor after the highest tested one. A new Bundler minor therefore needs the Appraisal and the range changed together. +- The E2E jobs in CI are disabled until the project moves to the public repo. + +## Bundler internals we rely on + +Parts of the plugin use Bundler code that is not a plugin API, so a Bundler release can change them. The Appraisals guard these: each `gemfiles/bundler_*.gemfile` installs one Bundler version and runs the RSpec suite against it, so a change that breaks one of these fails there. + +| Bundler internal | Used in | +|------------------|---------| +| Thor option tables: `Bundler::CLI.commands`, `Bundler::CLI.class_options`, and each option's `type`, `aliases` and `lazy_default` | `argv_parser.rb` (`table_for`, `switch_table`, `values_for`) | +| `Bundler::CLI.map`, the protected `normalize_command_name`, and the `default_cli_command` setting | `argv_parser.rb` (`split_command`, `default_command_used?`, `resolve_command`) | +| `Bundler::LockfileParser`, for `specs` and `bundler_version` | `secondary_gemfile.rb` | +| `Bundler::Dsl`, for `eval_gemfile` and `dependencies` | `secondary_gemfile.rb` | +| `Bundler.settings` `#all`, `#key_for` and `#locations` | `project_settings.rb` | +| `Bundler::SharedHelpers.relative_path_to` | `planner.rb` (`rewrite_path`) | +| The `version` setting, which decides whether each child uses its lockfile's Bundler | `runner.rb` (`other_bundler_for`) | diff --git a/docs/BundleMatrix.md b/docs/BundleMatrix.md new file mode 100644 index 0000000..2494b1e --- /dev/null +++ b/docs/BundleMatrix.md @@ -0,0 +1,44 @@ +# Module: BundleMatrix + +- **Defined in:** [`lib/bundle_matrix.rb`](../lib/bundle_matrix.rb), [`lib/bundle_matrix/argv_parser.rb`](../lib/bundle_matrix/argv_parser.rb), [`lib/bundle_matrix/config.rb`](../lib/bundle_matrix/config.rb), [`lib/bundle_matrix/discovery.rb`](../lib/bundle_matrix/discovery.rb), [`lib/bundle_matrix/hook.rb`](../lib/bundle_matrix/hook.rb), [`lib/bundle_matrix/invocation.rb`](../lib/bundle_matrix/invocation.rb), [`lib/bundle_matrix/planner.rb`](../lib/bundle_matrix/planner.rb), [`lib/bundle_matrix/project_settings.rb`](../lib/bundle_matrix/project_settings.rb), [`lib/bundle_matrix/runner.rb`](../lib/bundle_matrix/runner.rb), [`lib/bundle_matrix/secondary_gemfile.rb`](../lib/bundle_matrix/secondary_gemfile.rb), [`lib/bundle_matrix/version.rb`](../lib/bundle_matrix/version.rb) + +A Bundler plugin that repeats `install`, `update` and `add` against every secondary gemfile in a project. + +## Namespaces + +- [`BundleMatrix::ArgvParser`](BundleMatrix/ArgvParser.md) — Finds the command in argv the way Bundler does, then splits the tokens after it into options, gems and unknown switches, using the switch table Bundler itself uses for that command. +- [`BundleMatrix::Config`](BundleMatrix/Config.md) — Reads the matrix.* settings, from `bundle config` or BUNDLE_MATRIX__* env vars. +- [`BundleMatrix::Discovery`](BundleMatrix/Discovery.md) — Finds the secondary gemfiles that follow the main Gemfile. +- [`BundleMatrix::Error`](BundleMatrix/Error.md) — Raised after the matrix run, when one or more secondary gemfiles failed +- [`BundleMatrix::Hook`](BundleMatrix/Hook.md) — Called by Bundler after `install`, `update` or `add` has finished. +- [`BundleMatrix::Invocation`](BundleMatrix/Invocation.md) — A parsed `bundle install\|update\|add` command line. +- [`BundleMatrix::Planner`](BundleMatrix/Planner.md) — Decides what to run against one secondary gemfile, given the command the user ran on the main Gemfile. +- [`BundleMatrix::ProjectSettings`](BundleMatrix/ProjectSettings.md) — The project's settings that a child run would otherwise lose: its .bundle/config, which a child doesn't read because it looks in its own gemfile's directory, and the BUNDLE_* variables the user set, which Bundler.with_unbundled_env strips. +- [`BundleMatrix::Runner`](BundleMatrix/Runner.md) — Runs each planned command in a separate `bundle` process. +- [`BundleMatrix::SecondaryGemfile`](BundleMatrix/SecondaryGemfile.md) — A secondary gemfile, and what its lockfile and declarations say about it. + +## Constants + +### `MIN_BUNDLER` + +```ruby +MIN_BUNDLER = "2.6.9" +``` + +The oldest Bundler the plugin runs under. The gemspec requires it, and nothing newer is refused. +It is where Bundler::SharedHelpers.relative_path_to, which Planner uses, first appeared. + +### `TESTED_BUNDLER` + +```ruby +TESTED_BUNDLER = Gem::Requirement.new(">= #{MIN_BUNDLER}", "< 4.2.a") +``` + +The Bundler versions the Appraisals test. The hook warns outside this range, not the gemspec. +The `.a` keeps 4.2 prereleases out too; raise it once a new Bundler minor has been tested. + +### `VERSION` + +```ruby +VERSION = "0.1.0" +``` diff --git a/docs/BundleMatrix/ArgvParser.md b/docs/BundleMatrix/ArgvParser.md new file mode 100644 index 0000000..668a428 --- /dev/null +++ b/docs/BundleMatrix/ArgvParser.md @@ -0,0 +1,75 @@ +# Class: BundleMatrix::ArgvParser + +- **Defined in:** [`lib/bundle_matrix/argv_parser.rb`](../../lib/bundle_matrix/argv_parser.rb) + +Finds the command in argv the way Bundler does, then splits the tokens after it into +options, gems and unknown switches, using the switch table Bundler itself uses for that command. +Every touch of Bundler::CLI is in this class, so the rest of the plugin only sees command names and tokens. + +## Namespaces + +- [`BundleMatrix::ArgvParser::Switch`](ArgvParser/Switch.md) — One switch Bundler accepts for a command; negated is true for its `--no-name` form + +## Constants + +### `INSTALL_ALIASES` + +```ruby +INSTALL_ALIASES = %w[install install_or_cli_help].freeze +``` + +## Class methods + +| Method | Summary | +| --- | --- | +| [`.call(command, tokens)`](#method-c-call) | | +| [`.split_command(argv)`](#method-c-split_command) | Returns the command Bundler would run (nil when unknown) and the tokens after it | +| [`.table_for(command)`](#method-c-table_for) | Bundler's global options (--retry, --verbose, --no-color) apply to every command, so they are added to its own. | + +### `.call` + +```ruby +def self.call(command, tokens) +``` + +**Returns:** + +- (`Array(Array<`[`Invocation::Option`](Invocation/Option.md)`>, Array, Array)`) + +### `.split_command` + +```ruby +def self.split_command(argv) +``` + +**Returns:** + +- (`Array(String, Array)`) — the command Bundler would run (nil when unknown) and the tokens after it + +### `.table_for` + +```ruby +def self.table_for(command) +``` + +Bundler's global options (--retry, --verbose, --no-color) apply to every command, so they are added to its own. +Thor's tables are not a plugin API, so a Bundler release can change them; the Appraisals cover each version. + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(table)`](#method-i-initialize) | | +| [`#call(tokens)`](#method-i-call) | | + +### `#initialize` + +```ruby +def initialize(table) +``` + +### `#call` + +```ruby +def call(tokens) +``` diff --git a/docs/BundleMatrix/ArgvParser/Switch.md b/docs/BundleMatrix/ArgvParser/Switch.md new file mode 100644 index 0000000..4ef71f3 --- /dev/null +++ b/docs/BundleMatrix/ArgvParser/Switch.md @@ -0,0 +1,38 @@ +# Class: BundleMatrix::ArgvParser::Switch + +- **Inherits:** `Data` +- **Defined in:** [`lib/bundle_matrix/argv_parser.rb`](../../../lib/bundle_matrix/argv_parser.rb) + +One switch Bundler accepts for a command; negated is true for its `--no-name` form + +## Attributes + +### `#name` + +```ruby +attr_reader :name +``` + +### `#negated` + +```ruby +attr_reader :negated +``` + +### `#option` + +```ruby +attr_reader :option +``` + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(name:, option:, negated: false)`](#method-i-initialize) | | + +### `#initialize` + +```ruby +def initialize(name:, option:, negated: false) +``` diff --git a/docs/BundleMatrix/Config.md b/docs/BundleMatrix/Config.md new file mode 100644 index 0000000..99b3e71 --- /dev/null +++ b/docs/BundleMatrix/Config.md @@ -0,0 +1,71 @@ +# Class: BundleMatrix::Config + +- **Defined in:** [`lib/bundle_matrix/config.rb`](../../lib/bundle_matrix/config.rb) + +Reads the matrix.* settings, from `bundle config` or BUNDLE_MATRIX__* env vars. + +## Constants + +### `DEFAULT_GEMFILE` + +```ruby +DEFAULT_GEMFILE = "Gemfile" +``` + +### `FALSE_VALUES` + +```ruby +FALSE_VALUES = %w[false 0 no].freeze +``` + +### `TRUE_VALUES` + +```ruby +TRUE_VALUES = %w[true 1 yes].freeze +``` + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#bundler_warnings?`](#method-i-bundler_warnings-3F) | Warn when a gemfile runs a different Bundler from the main one; on unless set to false, 0 or no | +| [`#default_gemfile`](#method-i-default_gemfile) | The file name of the main gemfile in the project root, e.g. | +| [`#disabled?`](#method-i-disabled-3F) | | +| [`#fail_fast?`](#method-i-fail_fast-3F) | | +| [`#paths`](#method-i-paths) | Extra directories (or globs) to search for a Gemfile, e.g. | + +### `#bundler_warnings?` + +```ruby +def bundler_warnings? +``` + +Warn when a gemfile runs a different Bundler from the main one; on unless set to false, 0 or no + +### `#default_gemfile` + +```ruby +def default_gemfile +``` + +The file name of the main gemfile in the project root, e.g. "gems.rb". Fan-out starts from it. + +### `#disabled?` + +```ruby +def disabled? +``` + +### `#fail_fast?` + +```ruby +def fail_fast? +``` + +### `#paths` + +```ruby +def paths +``` + +Extra directories (or globs) to search for a Gemfile, e.g. "engines/*,apps/admin". diff --git a/docs/BundleMatrix/Discovery.md b/docs/BundleMatrix/Discovery.md new file mode 100644 index 0000000..e96c9b4 --- /dev/null +++ b/docs/BundleMatrix/Discovery.md @@ -0,0 +1,40 @@ +# Class: BundleMatrix::Discovery + +- **Defined in:** [`lib/bundle_matrix/discovery.rb`](../../lib/bundle_matrix/discovery.rb) + +Finds the secondary gemfiles that follow the main Gemfile. + +## Constants + +### `DIRECTORY_GEMFILES` + +```ruby +DIRECTORY_GEMFILES = %w[gems.rb Gemfile].freeze +``` + +The gemfile a matrix.paths directory contributes, in Bundler's order + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(root, paths, default_gemfile:)`](#method-i-initialize) | default_gemfile is the main gemfile's file name in root; it is never returned, whatever its name | +| [`#gemfiles`](#method-i-gemfiles) | Returns sorted, unique, excluding the main gemfile | + +### `#initialize` + +```ruby +def initialize(root, paths, default_gemfile:) +``` + +default_gemfile is the main gemfile's file name in root; it is never returned, whatever its name + +### `#gemfiles` + +```ruby +def gemfiles +``` + +**Returns:** + +- (`Array`) — sorted, unique, excluding the main gemfile diff --git a/docs/BundleMatrix/Error.md b/docs/BundleMatrix/Error.md new file mode 100644 index 0000000..348024c --- /dev/null +++ b/docs/BundleMatrix/Error.md @@ -0,0 +1,6 @@ +# Class: BundleMatrix::Error + +- **Inherits:** `Bundler::BundlerError` +- **Defined in:** [`lib/bundle_matrix.rb`](../../lib/bundle_matrix.rb) + +Raised after the matrix run, when one or more secondary gemfiles failed diff --git a/docs/BundleMatrix/Hook.md b/docs/BundleMatrix/Hook.md new file mode 100644 index 0000000..a180594 --- /dev/null +++ b/docs/BundleMatrix/Hook.md @@ -0,0 +1,19 @@ +# Module: BundleMatrix::Hook + +- **Defined in:** [`lib/bundle_matrix/hook.rb`](../../lib/bundle_matrix/hook.rb) + +Called by Bundler after `install`, `update` or `add` has finished. + +## Class methods + +| Method | Summary | +| --- | --- | +| [`.call(argv = ARGV)`](#method-c-call) | Checked before parsing, so a disabled plugin or a child run never depends on how Bundler's CLI parses | + +### `.call` + +```ruby +def self.call(argv = ARGV) +``` + +Checked before parsing, so a disabled plugin or a child run never depends on how Bundler's CLI parses diff --git a/docs/BundleMatrix/Invocation.md b/docs/BundleMatrix/Invocation.md new file mode 100644 index 0000000..d1ca09d --- /dev/null +++ b/docs/BundleMatrix/Invocation.md @@ -0,0 +1,108 @@ +# Class: BundleMatrix::Invocation + +- **Inherits:** `Data.define(:command, :options, :gems, :unknown)` +- **Defined in:** [`lib/bundle_matrix/invocation.rb`](../../lib/bundle_matrix/invocation.rb) + +A parsed `bundle install|update|add` command line. + +Option names, aliases and value types come from Bundler's own Thor tables, +so every flag Bundler accepts is understood, and the raw tokens are kept +so they can be forwarded exactly as the user typed them. + +## Namespaces + +- [`BundleMatrix::Invocation::Option`](Invocation/Option.md) — negated is true for a `--no-name` switch, which unsets the option rather than setting it + +## Constants + +### `COMMANDS` + +```ruby +COMMANDS = %w[install update add].freeze +``` + +## Class methods + +| Method | Summary | +| --- | --- | +| [`.parse(argv)`](#method-c-parse) | Returns nil when the command is not one we fan out | + +### `.parse` + +```ruby +def self.parse(argv) +``` + +**Returns:** + +- ([`Invocation`](Invocation.md), `nil`) — nil when the command is not one we fan out + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#as(target, gems: [])`](#method-i-as) | The same flags, re-targeted at another command. | +| [`#option(name)`](#method-i-option) | Bundler keeps the last copy of a repeated option, so that is the one that counts | +| [`#option?(name)`](#method-i-option-3F) | `--no-name` does not count as setting the option | +| [`#replace_values(name, new_values)`](#method-i-replace_values) | Replaces an option's values, dropping the option when none are left. | +| [`#to_argv`](#method-i-to_argv) | | +| [`#values(name)`](#method-i-values) | | +| [`#with_gems(new_gems)`](#method-i-with_gems) | | +| [`#without(*names)`](#method-i-without) | | + +### `#as` + +```ruby +def as(target, gems: []) +``` + +The same flags, re-targeted at another command. Only options that command +understands are kept, and the gems are replaced. + +### `#option` + +```ruby +def option(name) +``` + +Bundler keeps the last copy of a repeated option, so that is the one that counts + +### `#option?` + +```ruby +def option?(name) +``` + +`--no-name` does not count as setting the option + +### `#replace_values` + +```ruby +def replace_values(name, new_values) +``` + +Replaces an option's values, dropping the option when none are left. + +### `#to_argv` + +```ruby +def to_argv +``` + +### `#values` + +```ruby +def values(name) +``` + +### `#with_gems` + +```ruby +def with_gems(new_gems) +``` + +### `#without` + +```ruby +def without(*names) +``` diff --git a/docs/BundleMatrix/Invocation/Option.md b/docs/BundleMatrix/Invocation/Option.md new file mode 100644 index 0000000..0369554 --- /dev/null +++ b/docs/BundleMatrix/Invocation/Option.md @@ -0,0 +1,44 @@ +# Class: BundleMatrix::Invocation::Option + +- **Inherits:** `Data` +- **Defined in:** [`lib/bundle_matrix/invocation.rb`](../../../lib/bundle_matrix/invocation.rb) + +negated is true for a `--no-name` switch, which unsets the option rather than setting it + +## Attributes + +### `#name` + +```ruby +attr_reader :name +``` + +### `#negated` + +```ruby +attr_reader :negated +``` + +### `#tokens` + +```ruby +attr_reader :tokens +``` + +### `#values` + +```ruby +attr_reader :values +``` + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(name:, tokens:, values:, negated: false)`](#method-i-initialize) | | + +### `#initialize` + +```ruby +def initialize(name:, tokens:, values:, negated: false) +``` diff --git a/docs/BundleMatrix/Planner.md b/docs/BundleMatrix/Planner.md new file mode 100644 index 0000000..f1c6aa6 --- /dev/null +++ b/docs/BundleMatrix/Planner.md @@ -0,0 +1,48 @@ +# Class: BundleMatrix::Planner + +- **Defined in:** [`lib/bundle_matrix/planner.rb`](../../lib/bundle_matrix/planner.rb) + +Decides what to run against one secondary gemfile, given the command the +user ran on the main Gemfile. + +## Namespaces + +- [`BundleMatrix::Planner::Plan`](Planner/Plan.md) — invocation is nil when the gemfile should be skipped; notes explain why; error is set when planning failed; locked_bundler is the Bundler named in this gemfile's lockfile, set on every plan that runs a command + +## Constants + +### `MAIN_ONLY_INSTALL_OPTIONS` + +```ruby +MAIN_ONLY_INSTALL_OPTIONS = %w[lockfile standalone path].freeze +``` + +Install flags that only make sense for the main Gemfile, or that Bundler no longer accepts + +### `PATH_OPTIONS` + +```ruby +PATH_OPTIONS = %w[binstubs path target-rbconfig].freeze +``` + +Options whose values are paths. A child resolves a relative one from its own gemfile's directory, +so each is rewritten to be relative to that directory instead + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(invocation, gemfile, root)`](#method-i-initialize) | | +| [`#plan`](#method-i-plan) | | + +### `#initialize` + +```ruby +def initialize(invocation, gemfile, root) +``` + +### `#plan` + +```ruby +def plan +``` diff --git a/docs/BundleMatrix/Planner/Plan.md b/docs/BundleMatrix/Planner/Plan.md new file mode 100644 index 0000000..0275b2e --- /dev/null +++ b/docs/BundleMatrix/Planner/Plan.md @@ -0,0 +1,59 @@ +# Class: BundleMatrix::Planner::Plan + +- **Inherits:** `Data` +- **Defined in:** [`lib/bundle_matrix/planner.rb`](../../../lib/bundle_matrix/planner.rb) + +invocation is nil when the gemfile should be skipped; notes explain why; error is set when planning failed; +locked_bundler is the Bundler named in this gemfile's lockfile, set on every plan that runs a command + +## Attributes + +### `#error` + +```ruby +attr_reader :error +``` + +### `#invocation` + +```ruby +attr_reader :invocation +``` + +### `#locked_bundler` + +```ruby +attr_reader :locked_bundler +``` + +### `#notes` + +```ruby +attr_reader :notes +``` + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(invocation:, notes:, error: nil, locked_bundler: nil)`](#method-i-initialize) | | +| [`#failed?`](#method-i-failed-3F) | | +| [`#skipped?`](#method-i-skipped-3F) | | + +### `#initialize` + +```ruby +def initialize(invocation:, notes:, error: nil, locked_bundler: nil) +``` + +### `#failed?` + +```ruby +def failed? +``` + +### `#skipped?` + +```ruby +def skipped? +``` diff --git a/docs/BundleMatrix/ProjectSettings.md b/docs/BundleMatrix/ProjectSettings.md new file mode 100644 index 0000000..86c7579 --- /dev/null +++ b/docs/BundleMatrix/ProjectSettings.md @@ -0,0 +1,44 @@ +# Module: BundleMatrix::ProjectSettings + +- **Defined in:** [`lib/bundle_matrix/project_settings.rb`](../../lib/bundle_matrix/project_settings.rb) + +The project's settings that a child run would otherwise lose: its .bundle/config, +which a child doesn't read because it looks in its own gemfile's directory, and +the BUNDLE_* variables the user set, which Bundler.with_unbundled_env strips. +They are passed on as BUNDLE_* variables, so a gemfile's own .bundle/config still wins. +Settings#all and #locations are not a plugin API, so a Bundler release can change them. + +## Constants + +### `MAIN_ONLY` + +```ruby +MAIN_ONLY = %w[gemfile lockfile app_config bin_path].freeze +``` + +Settings that point at the main Gemfile's own files; each child sets these itself + +### `PATHS` + +```ruby +PATHS = %w[path bin cache_path].freeze +``` + +Settings holding a path relative to the project root, which a child would +otherwise resolve from its own directory + +## Class methods + +| Method | Summary | +| --- | --- | +| [`.env(root)`](#method-c-env) | Returns BUNDLE_* variables for a child run | + +### `.env` + +```ruby +def self.env(root) +``` + +**Returns:** + +- (`Hash{String => String}`) — BUNDLE_* variables for a child run diff --git a/docs/BundleMatrix/Runner.md b/docs/BundleMatrix/Runner.md new file mode 100644 index 0000000..7866feb --- /dev/null +++ b/docs/BundleMatrix/Runner.md @@ -0,0 +1,42 @@ +# Class: BundleMatrix::Runner + +- **Defined in:** [`lib/bundle_matrix/runner.rb`](../../lib/bundle_matrix/runner.rb) + +Runs each planned command in a separate `bundle` process. Bundler keeps +global state, so the secondary gemfiles can't be run in this process. + +## Constants + +### `BUNDLE` + +```ruby +BUNDLE = File.expand_path("bundle", __dir__) +``` + +Launched like a plain `bundle`, so RubyGems and Bundler pick each gemfile's Bundler version. +__dir__ is only nil inside eval, which Steep can't rule out. + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(root, fail_fast: false, bundler_warnings: true)`](#method-i-initialize) | bundler_warnings is false when the matrix.bundler_warnings setting turns off the Bundler version warnings | +| [`#run(plans)`](#method-i-run) | | + +### `#initialize` + +```ruby +def initialize(root, fail_fast: false, bundler_warnings: true) +``` + +bundler_warnings is false when the matrix.bundler_warnings setting turns off the Bundler version warnings + +### `#run` + +```ruby +def run(plans) +``` + +**Parameters:** + +- `plans` (`Hash{Pathname => `[`Planner::Plan`](Planner/Plan.md)`}`) diff --git a/docs/BundleMatrix/SecondaryGemfile.md b/docs/BundleMatrix/SecondaryGemfile.md new file mode 100644 index 0000000..b440005 --- /dev/null +++ b/docs/BundleMatrix/SecondaryGemfile.md @@ -0,0 +1,73 @@ +# Class: BundleMatrix::SecondaryGemfile + +- **Defined in:** [`lib/bundle_matrix/secondary_gemfile.rb`](../../lib/bundle_matrix/secondary_gemfile.rb) + +A secondary gemfile, and what its lockfile and declarations say about it. + +## Attributes + +### `#path` + +```ruby +attr_reader :path +``` + +## Instance methods + +| Method | Summary | +| --- | --- | +| [`#initialize(path)`](#method-i-initialize) | | +| [`#bundler_version`](#method-i-bundler_version) | The Bundler this gemfile's lockfile names in BUNDLED WITH, nil without a lockfile | +| [`#declared_groups`](#method-i-declared_groups) | | +| [`#declared_names`](#method-i-declared_names) | | +| [`#lock_names`](#method-i-lock_names) | Empty without a lockfile | +| [`#locked?`](#method-i-locked-3F) | | +| [`#lockfile`](#method-i-lockfile) | Bundler locks a gems.rb in gems.locked, and every other gemfile in .lock | + +### `#initialize` + +```ruby +def initialize(path) +``` + +### `#bundler_version` + +```ruby +def bundler_version +``` + +The Bundler this gemfile's lockfile names in BUNDLED WITH, nil without a lockfile + +### `#declared_groups` + +```ruby +def declared_groups +``` + +### `#declared_names` + +```ruby +def declared_names +``` + +### `#lock_names` + +```ruby +def lock_names +``` + +Empty without a lockfile + +### `#locked?` + +```ruby +def locked? +``` + +### `#lockfile` + +```ruby +def lockfile +``` + +Bundler locks a gems.rb in gems.locked, and every other gemfile in .lock diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..c055056 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,166 @@ +# BundleMatrix + +A Bundler plugin that repeats `bundle install`, `bundle update` and `bundle add` against every other gemfile in your project, so each lockfile stays in step with the main `Gemfile`. + +Projects that test against several dependency sets keep extra gemfiles (`ci.gemfile`, `gemfiles/rails7.gemfile`, engine Gemfiles, and so on). Without help, every one of them needs its own `BUNDLE_GEMFILE=… bundle install`. With bundle_matrix, running the command on the main `Gemfile` does that for you. + +![BundleMatrix](https://raw.githubusercontent.com/joe-sharp/bundle_matrix/main/BundleMatrix.webp) + +## Installation + +Install the plugin once for your user: + +```bash +bundle plugin install bundle_matrix +``` + +Or declare it in the project's `Gemfile`: + +```ruby +plugin "bundle_matrix" +``` + +## Usage + +Run the command on the main `Gemfile` as usual: + +```bash +bundle install +bundle update rails --conservative +bundle update --all +bundle add rack --group development +``` + +After the main run succeeds, each discovered gemfile is run with the same command and flags. Output is headed by the gemfile it applies to: + +``` +== bundle_matrix: gemfiles/rails7.gemfile -> bundle update rails --conservative +== bundle_matrix: engines/admin/Gemfile -> bundle install +``` + +Only `install`, `update` and `add` are fanned out. `bundle check`, `bundle exec` and similar commands are not, because Bundler does not run plugin hooks for them. + +Bundler's short forms work too: `bundle i`, `bundle ins`, `bundle a`, `bundle u`, and any other unambiguous prefix of a command name. + +A plain `bundle`, or `bundle` with only flags such as `bundle --jobs=4`, runs as `install` and fans out as `install`, but only while `default_cli_command` is unset or `install`. With it set to `cli_help`, those commands print help and nothing fans out. Bundler's documentation already names `cli_help` as the default, while its code still runs `install` and prints a notice that this will change. To keep fanning out whatever Bundler does, set the setting explicitly, or run `bundle install`: + +```bash +bundle config set default_cli_command install +``` + +## Which gemfiles are found + +The main gemfile is `Gemfile` in the project root, or the file named by `matrix.default_gemfile`. It is never treated as a secondary gemfile. Secondary gemfiles are found in: + +- `*.gemfile` in the project root, e.g. `ci.gemfile` with `ci.gemfile.lock` +- `*.gemfile` in `gemfiles/` +- any directories listed in `matrix.paths`, each contributing its `gems.rb`, or its `Gemfile` when it has no `gems.rb`, the order Bundler uses + +## Configuration + +Settings use `bundle config` or `BUNDLE_MATRIX__*` environment variables: + +| Setting | Default | Meaning | +| --- | --- | --- | +| `matrix.default_gemfile` | `Gemfile` | File name of the main gemfile in the project root. Fan-out starts only when the command runs on this file, e.g. `gems.rb` | +| `matrix.paths` | none | Extra directories or globs to search for a `gems.rb` or `Gemfile`, separated by commas, e.g. `engines/*,apps/admin` | +| `matrix.disabled` | `false` | Turn fan-out off | +| `matrix.fail_fast` | `false` | Stop at the first failing gemfile | +| `matrix.bundler_warnings` | `true` | Warn when a gemfile runs a different Bundler from the main Gemfile. Set it to `false` for projects that pin different Bundlers on purpose | + +```bash +bundle config set matrix.paths "engines/*,apps/admin" +``` + +For a project whose main file is `gems.rb`, set the main gemfile name once, and run the command on `gems.rb`: + +```bash +bundle config set matrix.default_gemfile gems.rb +bundle install +``` + +To skip the matrix for a single run, set `BUNDLE_MATRIX__DISABLED` on that command: + +```bash +BUNDLE_MATRIX__DISABLED=1 bundle install +``` + +`bundle config set matrix.disabled true` is the persistent version, and stays in effect until you change it back. + +## How each command is applied + +Flags are forwarded as typed, and Bundler's own option table decides what each flag means. The exceptions below are where a flag can't be applied to another gemfile as-is. + +- **`install`**: `--lockfile` and `--standalone` only apply to the main Gemfile, so they are not forwarded. +- **`update `**: only the named gems that appear in that gemfile's lockfile are updated. A gemfile with none of them is skipped. `--group` is limited to groups it declares. If it has no lockfile yet, `install` is run to create one. +- **`update --all`**, `--conservative`, `--patch`, `--minor`, `--major`, `--pre`, `--strict` and the rest: forwarded unchanged. +- **`add `**: gems the gemfile already declares, including through `eval_gemfile`, are not added again. If every gem is already declared, `install` is run to re-lock the gemfile. A relative `--path` is rewritten so it resolves from that gemfile's directory. +- `--gemfile` is never forwarded. The target is set through `BUNDLE_GEMFILE` instead. + +## Settings and Bundler versions + +Each gemfile is run the way `bundle` would run it on its own, with the project's settings: + +- The project's `.bundle/config` and the `BUNDLE_*` environment variables you set are passed on, so `frozen`, `without`, `jobs` and the rest apply to every gemfile. A relative `path`, `bin` or `cache_path` still resolves from the project root, so every gemfile shares the same `vendor/bundle`. A `.bundle/config` next to a secondary gemfile takes priority for that gemfile. +- `gemfile` and `lockfile` are not passed on, because they name the main Gemfile's own files. +- Each gemfile runs with the Bundler version in its own `BUNDLED WITH` line, and `install` installs that version first if it is missing. A `gem "bundler"` line in a gemfile that has no lockfile yet is not installed for you, so install that version yourself first. Bundler's `version` setting controls this: `bundle config set version system` keeps every gemfile on the running version, and `bundle config set version 4.0.23` pins one. `BUNDLER_VERSION` is not passed on, because it would hold every gemfile to one version. +- When a gemfile runs with a different Bundler from the main Gemfile, a one-line warning names both versions, and the run ends by suggesting either `bundle update --bundler`, which updates Bundler in every lockfile, or `bundle config set matrix.bundler_warnings false`, which turns these warnings off when each gemfile should keep its own Bundler. + +## How it uses Bundler + +bundle_matrix needs Bundler 2.6.9 or newer, below 4.2 and its prereleases, and is tested against 2.6.9, 2.7.2, 4.0.24 and 4.1.0.beta3. The upper bound moves up once a new Bundler minor has been tested. Under any other Bundler the plugin still runs, but prints a one-line warning that it is untested there, with a link to the issue tracker. + +Fan-out starts from Bundler's documented plugin hook, `after-install-all`, and each gemfile runs in its own `bundle` process. Everything else uses Bundler classes that work today but are not a plugin API, so a new Bundler release could change them: + +- Thor's command and option tables (`Bundler::CLI`), which decide what each flag means and which command was run (`ArgvParser`, `Invocation`). Command names such as `ins` are resolved by calling Thor's protected `normalize_command_name` with `send` +- `Bundler::Dsl` and `Bundler::LockfileParser`, which read each secondary gemfile and lockfile (`Planner`). `Bundler.definition` and `Bundler.locked_gems` only describe the main Gemfile, so they can't be used for the others. +- The settings' storage levels, through `Bundler.settings` (`ProjectSettings`) + +## Limitations + +- `bundle check` is not supported. Bundler only calls plugin hooks around installation. +- `bundle add --skip-install` never runs the hook, because no installation happens. +- Flags that Bundler does not know about are forwarded as-is, and any value after them is treated as a gem name. + +## Development + +Run `bin/setup`, then `rake`. The default task runs the unit specs, the end-to-end specs, RuboCop, `rake rbs`, which runs `rbs validate` on the signatures in `sig/` with the Bundler stubs in `sig_stubs/`, and `rake steep`, which type checks `lib/` against those signatures. Run `rake e2e` on its own for the end-to-end specs, which run real `bundle` commands and take longer. Run `COVERAGE=true bundle exec rake spec` to also write a coverage report to `coverage/index.html`. That run fails below 90% line coverage or 80% branch coverage, and CI runs it this way. + +Run `bundle exec guard` to re-run the unit specs and RuboCop on every change. It also regenerates the docs when the files they are built from change. + +`docs/` is generated by Boondoc, so edit `README.md` and the code comments rather than `docs/`. To regenerate it by hand, run `bundle exec boondoc` + +To try a change against a real project, install from a local checkout with `bundle plugin install bundle_matrix --path /path/to/bundle_matrix` (the directory must contain `plugins.rb`). + +## Contributing + +Bug reports and pull requests are welcome. Send issues and pull requests to https://github.com/joe-sharp/bundle_matrix. + +Run `bundle exec rake` before you open a pull request. The Development section above explains what it runs. + +A bug report should include: + +- the Bundler version, from `bundle -v` +- the command you ran, e.g. `bundle update rails --conservative` +- your gemfile layout: which gemfiles exist, where they are, and any `matrix.*` settings + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). + +## API reference + +- [`BundleMatrix`](BundleMatrix.md) — A Bundler plugin that repeats `install`, `update` and `add` against every secondary gemfile in a project. + - [`BundleMatrix::ArgvParser`](BundleMatrix/ArgvParser.md) — Finds the command in argv the way Bundler does, then splits the tokens after it into options, gems and unknown switches, using the switch table Bundler itself uses for that command. + - [`BundleMatrix::ArgvParser::Switch`](BundleMatrix/ArgvParser/Switch.md) — One switch Bundler accepts for a command; negated is true for its `--no-name` form + - [`BundleMatrix::Config`](BundleMatrix/Config.md) — Reads the matrix.* settings, from `bundle config` or BUNDLE_MATRIX__* env vars. + - [`BundleMatrix::Discovery`](BundleMatrix/Discovery.md) — Finds the secondary gemfiles that follow the main Gemfile. + - [`BundleMatrix::Error`](BundleMatrix/Error.md) — Raised after the matrix run, when one or more secondary gemfiles failed + - [`BundleMatrix::Hook`](BundleMatrix/Hook.md) — Called by Bundler after `install`, `update` or `add` has finished. + - [`BundleMatrix::Invocation`](BundleMatrix/Invocation.md) — A parsed `bundle install\|update\|add` command line. + - [`BundleMatrix::Invocation::Option`](BundleMatrix/Invocation/Option.md) — negated is true for a `--no-name` switch, which unsets the option rather than setting it + - [`BundleMatrix::Planner`](BundleMatrix/Planner.md) — Decides what to run against one secondary gemfile, given the command the user ran on the main Gemfile. + - [`BundleMatrix::Planner::Plan`](BundleMatrix/Planner/Plan.md) — invocation is nil when the gemfile should be skipped; notes explain why; error is set when planning failed; locked_bundler is the Bundler named in this gemfile's lockfile, set on every plan that runs a command + - [`BundleMatrix::ProjectSettings`](BundleMatrix/ProjectSettings.md) — The project's settings that a child run would otherwise lose: its .bundle/config, which a child doesn't read because it looks in its own gemfile's directory, and the BUNDLE_* variables the user set, which Bundler.with_unbundled_env strips. + - [`BundleMatrix::Runner`](BundleMatrix/Runner.md) — Runs each planned command in a separate `bundle` process. + - [`BundleMatrix::SecondaryGemfile`](BundleMatrix/SecondaryGemfile.md) — A secondary gemfile, and what its lockfile and declarations say about it. diff --git a/gemfiles/bundler_2.6.9.gemfile b/gemfiles/bundler_2.6.9.gemfile new file mode 100644 index 0000000..272bfc3 --- /dev/null +++ b/gemfiles/bundler_2.6.9.gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# This file was generated by Appraisal2 + +source "https://rubygems.org/" + +gem "bundler", "2.6.9" +gem "rake", "~> 13.0" +gem "rspec", "~> 3.0" +gem "rubygems-generate_index", require: false + +gemspec path: "../" diff --git a/gemfiles/bundler_2.6.9.gemfile.lock b/gemfiles/bundler_2.6.9.gemfile.lock new file mode 100644 index 0000000..8f90b19 --- /dev/null +++ b/gemfiles/bundler_2.6.9.gemfile.lock @@ -0,0 +1,48 @@ +PATH + remote: .. + specs: + bundle_matrix (0.1.0) + bundler (>= 2.6.9) + +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.6.2) + rake (13.4.2) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.7) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.9) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.8) + rubygems-generate_index (1.2.0) + +PLATFORMS + aarch64-linux-gnu + aarch64-linux-musl + arm-linux-gnu + arm-linux-musl + arm64-darwin + ruby + x86-linux-gnu + x86-linux-musl + x86_64-darwin + x86_64-linux-gnu + x86_64-linux-musl + +DEPENDENCIES + bundle_matrix! + bundler (= 2.6.9) + rake (~> 13.0) + rspec (~> 3.0) + rubygems-generate_index + +BUNDLED WITH + 2.6.9 diff --git a/gemfiles/bundler_2.7.2.gemfile b/gemfiles/bundler_2.7.2.gemfile new file mode 100644 index 0000000..3c5b105 --- /dev/null +++ b/gemfiles/bundler_2.7.2.gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# This file was generated by Appraisal2 + +source "https://rubygems.org/" + +gem "bundler", "2.7.2" +gem "rake", "~> 13.0" +gem "rspec", "~> 3.0" +gem "rubygems-generate_index", require: false + +gemspec path: "../" diff --git a/gemfiles/bundler_2.7.2.gemfile.lock b/gemfiles/bundler_2.7.2.gemfile.lock new file mode 100644 index 0000000..31a4778 --- /dev/null +++ b/gemfiles/bundler_2.7.2.gemfile.lock @@ -0,0 +1,48 @@ +PATH + remote: .. + specs: + bundle_matrix (0.1.0) + bundler (>= 2.6.9) + +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.6.2) + rake (13.4.2) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.7) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.9) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.8) + rubygems-generate_index (1.2.0) + +PLATFORMS + aarch64-linux-gnu + aarch64-linux-musl + arm-linux-gnu + arm-linux-musl + arm64-darwin + ruby + x86-linux-gnu + x86-linux-musl + x86_64-darwin + x86_64-linux-gnu + x86_64-linux-musl + +DEPENDENCIES + bundle_matrix! + bundler (= 2.7.2) + rake (~> 13.0) + rspec (~> 3.0) + rubygems-generate_index + +BUNDLED WITH + 2.7.2 diff --git a/gemfiles/bundler_4.0.24.gemfile b/gemfiles/bundler_4.0.24.gemfile new file mode 100644 index 0000000..fc91577 --- /dev/null +++ b/gemfiles/bundler_4.0.24.gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# This file was generated by Appraisal2 + +source "https://rubygems.org/" + +gem "bundler", "4.0.24" +gem "rake", "~> 13.0" +gem "rspec", "~> 3.0" +gem "rubygems-generate_index", require: false + +gemspec path: "../" diff --git a/gemfiles/bundler_4.0.24.gemfile.lock b/gemfiles/bundler_4.0.24.gemfile.lock new file mode 100644 index 0000000..74ec2d1 --- /dev/null +++ b/gemfiles/bundler_4.0.24.gemfile.lock @@ -0,0 +1,60 @@ +PATH + remote: .. + specs: + bundle_matrix (0.1.0) + bundler (>= 2.6.9) + +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.6.2) + rake (13.4.2) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.6) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.8) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.7) + rubygems-generate_index (1.2.0) + +PLATFORMS + aarch64-linux-gnu + aarch64-linux-musl + arm-linux-gnu + arm-linux-musl + arm64-darwin + ruby + x86-linux-gnu + x86-linux-musl + x86_64-darwin + x86_64-linux-gnu + x86_64-linux-musl + +DEPENDENCIES + bundle_matrix! + bundler (= 4.0.24) + rake (~> 13.0) + rspec (~> 3.0) + rubygems-generate_index + +CHECKSUMS + bundle_matrix (0.1.0) + bundler (4.0.24) sha256=16ca64e20816a0cf151a948fb141d788602996d2b4d73577b74388fe84fa98bc + diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962 + rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701 + rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587 + rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d + rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836 + rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47 + rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c + rubygems-generate_index (1.2.0) sha256=cb33b935545a38dba4ac1f20d391090d2bb017d0956537a8bbadc1fff23f0aab + +BUNDLED WITH + 4.0.24 diff --git a/gemfiles/bundler_4.1.0.beta3.gemfile b/gemfiles/bundler_4.1.0.beta3.gemfile new file mode 100644 index 0000000..0c34728 --- /dev/null +++ b/gemfiles/bundler_4.1.0.beta3.gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# This file was generated by Appraisal2 + +source "https://rubygems.org/" + +gem "bundler", "4.1.0.beta3" +gem "rake", "~> 13.0" +gem "rspec", "~> 3.0" +gem "rubygems-generate_index", require: false + +gemspec path: "../" diff --git a/gemfiles/bundler_4.1.0.beta3.gemfile.lock b/gemfiles/bundler_4.1.0.beta3.gemfile.lock new file mode 100644 index 0000000..63e944f --- /dev/null +++ b/gemfiles/bundler_4.1.0.beta3.gemfile.lock @@ -0,0 +1,60 @@ +PATH + remote: .. + specs: + bundle_matrix (0.1.0) + bundler (>= 2.6.9) + +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.6.2) + rake (13.4.2) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.7) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.9) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.8) + rubygems-generate_index (1.2.0) + +PLATFORMS + aarch64-linux-gnu + aarch64-linux-musl + arm-linux-gnu + arm-linux-musl + arm64-darwin + ruby + x86-linux-gnu + x86-linux-musl + x86_64-darwin + x86_64-linux-gnu + x86_64-linux-musl + +DEPENDENCIES + bundle_matrix! + bundler (= 4.1.0.beta3) + rake (~> 13.0) + rspec (~> 3.0) + rubygems-generate_index + +CHECKSUMS + bundle_matrix (0.1.0) + bundler (4.1.0.beta3) sha256=3a0dbbbb7c86af269517a9de67903472e219d3ae9edb3b6b4da7706cdc993d12 + diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962 + rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701 + rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587 + rspec-core (3.13.7) sha256=c64ff16d9788119805cbc6a3f4b6d87cc22417c2d25976a3330de136896b5469 + rspec-expectations (3.13.6) sha256=bf538ca6d367ff5b0d85a87fdb5c0c6038f2cd1308a0eeba07476caa02d22f35 + rspec-mocks (3.13.9) sha256=ea5c7ff3054a6098ca6039aacef80aec69d5a8a3ef80be8fc88337359b672a30 + rspec-support (3.13.8) sha256=e4160030309b7e3e9d9863dfe720a35dfcc577162f5402cd68fc8c5258a0ffd6 + rubygems-generate_index (1.2.0) sha256=cb33b935545a38dba4ac1f20d391090d2bb017d0956537a8bbadc1fff23f0aab + +BUNDLED WITH + 4.1.0.beta3 diff --git a/gemfiles/gem_coop.gemfile b/gemfiles/gem_coop.gemfile new file mode 100644 index 0000000..d646eb3 --- /dev/null +++ b/gemfiles/gem_coop.gemfile @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# This file was generated by Appraisal2 + +source "https://gem.coop/" + +gem "rake", "~> 13.0" +gem "rspec", "~> 3.0" +gem "rubygems-generate_index", require: false + +source "https://gem.coop/@joe-sharp" do + gem "boondoc" + gem "guard-boondoc" + gem "sharp_style_rubocop" +end + +gemspec path: "../" diff --git a/gemfiles/gem_coop.gemfile.lock b/gemfiles/gem_coop.gemfile.lock new file mode 100644 index 0000000..8ec3923 --- /dev/null +++ b/gemfiles/gem_coop.gemfile.lock @@ -0,0 +1,233 @@ +PATH + remote: .. + specs: + bundle_matrix (0.1.0) + bundler (>= 2.6.9) + +GEM + remote: https://gem.coop/ + specs: + ast (2.4.3) + coderay (1.1.3) + diff-lcs (1.6.2) + ffi (1.17.4) + ffi (1.17.4-aarch64-linux-gnu) + ffi (1.17.4-aarch64-linux-musl) + ffi (1.17.4-arm-linux-gnu) + ffi (1.17.4-arm-linux-musl) + ffi (1.17.4-arm64-darwin) + ffi (1.17.4-x86-linux-gnu) + ffi (1.17.4-x86-linux-musl) + ffi (1.17.4-x86_64-darwin) + ffi (1.17.4-x86_64-linux-gnu) + ffi (1.17.4-x86_64-linux-musl) + formatador (1.2.3) + reline + guard (2.20.2) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + logger (~> 1.6) + lumberjack (>= 1.0.12, < 2.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.13.0) + shellany (~> 0.0) + thor (>= 0.18.1) + guard-compat (1.2.1) + io-console (0.9.5) + json (3.1.0) + language_server-protocol (3.17.0.6) + lint_roller (1.1.0) + listen (3.10.1) + logger + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) + logger (1.7.0) + lumberjack (1.4.2) + method_source (1.1.0) + nenv (0.3.0) + notiffany (0.1.3) + nenv (~> 0.1) + shellany (~> 0.0) + parallel (2.3.0) + parser (3.3.13.0) + ast (~> 2.4.1) + racc + prism (1.9.0) + pry (0.16.1) + coderay (~> 1.1) + method_source (~> 1.0) + reline (>= 0.6.0) + racc (1.8.1) + rainbow (3.1.1) + rake (13.4.2) + rb-fsevent (0.11.2) + rb-inotify (0.11.1) + ffi (~> 1.0) + regexp_parser (2.13.1) + reline (0.7.0) + io-console (~> 0.5) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.6) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.8) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.7) + rubocop (1.92.2) + json (>= 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (>= 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.11.3, < 3.0) + rubocop-ast (>= 1.49.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.50.0) + parser (>= 3.3.7.2) + prism (~> 1.7) + rubocop-capybara (3.1.0) + lint_roller (~> 1.1) + rubocop (~> 1.89) + rubocop-minitest (0.41.0) + lint_roller (~> 1.1) + rubocop (>= 1.89.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-performance (1.27.0) + lint_roller (~> 1.1) + rubocop (>= 1.89.0, < 2.0) + rubocop-ast (>= 1.47.1, < 2.0) + rubocop-rake (0.7.1) + lint_roller (~> 1.1) + rubocop (>= 1.72.1) + rubocop-rspec (3.10.2) + lint_roller (~> 1.1) + regexp_parser (>= 2.0) + rubocop (~> 1.86, >= 1.86.2) + rubocop-thread_safety (0.8.0) + lint_roller (~> 1.1) + rubocop (~> 1.89) + rubocop-ast (>= 1.44.0, < 2.0) + ruby-progressbar (1.13.0) + rubygems-generate_index (1.2.0) + shellany (0.0.1) + thor (1.5.0) + unicode-display_width (3.3.0) + unicode-emoji (~> 4.3) + unicode-emoji (4.3.0) + +GEM + remote: https://gem.coop/@joe-sharp/ + specs: + boondoc (0.2.1) + prism (>= 1.0, < 3) + thor (~> 1.2) + guard-boondoc (0.1.1) + boondoc + guard (~> 2.20) + guard-compat (~> 1.2) + sharp_style_rubocop (0.1.6) + rubocop (~> 1.92) + rubocop-capybara (~> 3.1) + rubocop-minitest (~> 0.41) + rubocop-performance (~> 1.20) + rubocop-rake (~> 0.7) + rubocop-rspec (~> 3.0) + rubocop-thread_safety (~> 0.8) + +PLATFORMS + aarch64-linux-gnu + aarch64-linux-musl + arm-linux-gnu + arm-linux-musl + arm64-darwin + ruby + x86-linux-gnu + x86-linux-musl + x86_64-darwin + x86_64-linux-gnu + x86_64-linux-musl + +DEPENDENCIES + boondoc! + bundle_matrix! + guard-boondoc! + rake (~> 13.0) + rspec (~> 3.0) + rubygems-generate_index + sharp_style_rubocop! + +CHECKSUMS + ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383 + boondoc (0.2.1) sha256=3cfe5bd9b1ca5a2ab34a482d5b387778dad54eae575de5c90d850d1cb3599203 + bundle_matrix (0.1.0) + bundler (4.0.24) sha256=16ca64e20816a0cf151a948fb141d788602996d2b4d73577b74388fe84fa98bc + coderay (1.1.3) sha256=dc530018a4684512f8f38143cd2a096c9f02a1fc2459edcfe534787a7fc77d4b + diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962 + ffi (1.17.4) sha256=bcd1642e06f0d16fc9e09ac6d49c3a7298b9789bcb58127302f934e437d60acf + ffi (1.17.4-aarch64-linux-gnu) sha256=b208f06f91ffd8f5e1193da3cae3d2ccfc27fc36fba577baf698d26d91c080df + ffi (1.17.4-aarch64-linux-musl) sha256=9286b7a615f2676245283aef0a0a3b475ae3aae2bb5448baace630bb77b91f39 + ffi (1.17.4-arm-linux-gnu) sha256=d6dbddf7cb77bf955411af5f187a65b8cd378cb003c15c05697f5feee1cb1564 + ffi (1.17.4-arm-linux-musl) sha256=9d4838ded0465bef6e2426935f6bcc93134b6616785a84ffd2a3d82bc3cf6f95 + ffi (1.17.4-arm64-darwin) sha256=19071aaf1419251b0a46852abf960e77330a3b334d13a4ab51d58b31a937001b + ffi (1.17.4-x86-linux-gnu) sha256=38e150df5f4ca555e25beca4090823ae09657bceded154e3c52f8631c1ed72cf + ffi (1.17.4-x86-linux-musl) sha256=fbeec0fc7c795bcf86f623bb18d31ea1820f7bd580e1703a3d3740d527437809 + ffi (1.17.4-x86_64-darwin) sha256=aa70390523cf3235096cf64962b709b4cfbd5c082a2cb2ae714eb0fe2ccda496 + ffi (1.17.4-x86_64-linux-gnu) sha256=9d3db14c2eae074b382fa9c083fe95aec6e0a1451da249eab096c34002bc752d + ffi (1.17.4-x86_64-linux-musl) sha256=3fdf9888483de005f8ef8d1cf2d3b20d86626af206cbf780f6a6a12439a9c49e + formatador (1.2.3) sha256=19fa898133c2c26cdbb5d09f6998c1e137ad9427a046663e55adfe18b950d894 + guard (2.20.2) sha256=2d0574d35619669f2f71739a0313290810b9cf66b2263f5d97e815fcce598d4d + guard-boondoc (0.1.1) sha256=57bb57a8e81dd3f62518158df5a07af6c245e6460e0d91cf2cf7cf38cf1a83c2 + guard-compat (1.2.1) sha256=3ad21ab0070107f92edfd82610b5cdc2fb8e368851e72362ada9703443d646fe + io-console (0.9.5) sha256=83dd9bbf4c4680416629245b39cd4212eb5f4d9a4ea91804870c1f6b846414d5 + json (3.1.0) sha256=3ce7ccf8fde58960177a3a756c31f292a4c1e1dedee884547096a5b0604e3bd8 + language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0 + lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87 + listen (3.10.1) sha256=4c3f32855657f348747da4dd2372ff3790d90a6b1d3e4f21a3706adb2d04783c + logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203 + lumberjack (1.4.2) sha256=40de5ae46321380c835031bcc1370f13bba304d29f2b5f5bb152061a5a191b95 + method_source (1.1.0) sha256=181301c9c45b731b4769bc81e8860e72f9161ad7d66dd99103c9ab84f560f5c5 + nenv (0.3.0) sha256=d9de6d8fb7072228463bf61843159419c969edb34b3cef51832b516ae7972765 + notiffany (0.1.3) sha256=d37669605b7f8dcb04e004e6373e2a780b98c776f8eb503ac9578557d7808738 + parallel (2.3.0) sha256=f75a3e904101ce6a1ccb6b8dc800cbafd42166d83330192438dcf29395167a6f + parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca + prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85 + pry (0.16.1) sha256=d92d82c86e7f2dc34beec6d2892d5cf5ea517eea22eed3ceabb38ee62a98accc + racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f + rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a + rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701 + rb-fsevent (0.11.2) sha256=43900b972e7301d6570f64b850a5aa67833ee7d87b458ee92805d56b7318aefe + rb-inotify (0.11.1) sha256=a0a700441239b0ff18eb65e3866236cd78613d6b9f78fea1f9ac47a85e47be6e + regexp_parser (2.13.1) sha256=5aedb6b7c35688f51e86eef17a15374dfd287c83f4ca644b2c5f3ec50a33b44b + reline (0.7.0) sha256=5b012d8e55dbf9d450f12bde2cf7d15ff546ae80b3f8f3b30e570d431815583d + rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587 + rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d + rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836 + rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47 + rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c + rubocop (1.92.2) sha256=14fb31eed143a0dc6f78186c550c1715e81cd38f59d05d1165050e445c1f5382 + rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db + rubocop-capybara (3.1.0) sha256=1712900b04178272b643f7bffa385f7b1d157c4f83a0ad502ae10690e18f5096 + rubocop-minitest (0.41.0) sha256=c15bfe00134a30c8a00ce670b07ecc80440455ba277b8682cf0d515c98ee7426 + rubocop-performance (1.27.0) sha256=eeeb1374d062a368ee1c787b70eb0b0cc4b184cb1f8565f424760946146d61ce + rubocop-rake (0.7.1) sha256=3797f2b6810c3e9df7376c26d5f44f3475eda59eb1adc38e6f62ecf027cbae4d + rubocop-rspec (3.10.2) sha256=0b3e2ecc592cd10ecbf0095bb58d1e357905276e069643523cc19eb7495f65e2 + rubocop-thread_safety (0.8.0) sha256=71fe5e7894fb9acfbd4c2dcd394feabc35bd690f75b0bc846e1504d9e31f9034 + ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33 + rubygems-generate_index (1.2.0) sha256=cb33b935545a38dba4ac1f20d391090d2bb017d0956537a8bbadc1fff23f0aab + sharp_style_rubocop (0.1.6) sha256=624fa66fb1cea95d919092dd135f28ecf7d21319528f38e1486c84c2c64a4016 + shellany (0.0.1) sha256=0e127a9132698766d7e752e82cdac8250b6adbd09e6c0a7fbbb6f61964fedee7 + thor (1.5.0) sha256=e3a9e55fe857e44859ce104a84675ab6e8cd59c650a49106a05f55f136425e73 + unicode-display_width (3.3.0) sha256=4b7aa66a4b11db50f6f7e98411215cd0dd10eaecb665d36eec5bcacb3fa0b613 + unicode-emoji (4.3.0) sha256=11c02fa73290378c066bb0562cd4c87d8e1b706fbbe0059ca12746a4244de8ce + +BUNDLED WITH + 4.0.24 diff --git a/lib/bundle_matrix.rb b/lib/bundle_matrix.rb new file mode 100644 index 0000000..e2b3565 --- /dev/null +++ b/lib/bundle_matrix.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "bundler" +require_relative "bundle_matrix/version" +require_relative "bundle_matrix/config" +require_relative "bundle_matrix/project_settings" +require_relative "bundle_matrix/discovery" +require_relative "bundle_matrix/argv_parser" +require_relative "bundle_matrix/invocation" +require_relative "bundle_matrix/secondary_gemfile" +require_relative "bundle_matrix/planner" +require_relative "bundle_matrix/runner" +require_relative "bundle_matrix/hook" + +# A Bundler plugin that repeats `install`, `update` and `add` against every secondary gemfile in a project. +module BundleMatrix + # Raised after the matrix run, when one or more secondary gemfiles failed + class Error < Bundler::BundlerError + status_code(1) + end +end diff --git a/lib/bundle_matrix/argv_parser.rb b/lib/bundle_matrix/argv_parser.rb new file mode 100644 index 0000000..4deee15 --- /dev/null +++ b/lib/bundle_matrix/argv_parser.rb @@ -0,0 +1,160 @@ +# frozen_string_literal: true + +require "bundler/cli" + +module BundleMatrix + # Finds the command in argv the way Bundler does, then splits the tokens after it into + # options, gems and unknown switches, using the switch table Bundler itself uses for that command. + # Every touch of Bundler::CLI is in this class, so the rest of the plugin only sees command names and tokens. + class ArgvParser + Switch = Data.define(:name, :option, :negated) + + # One switch Bundler accepts for a command; negated is true for its `--no-name` form + class Switch + def initialize(name:, option:, negated: false) + super + end + end + + INSTALL_ALIASES = %w[install install_or_cli_help].freeze + + # @return [Array(String, Array)] the command Bundler would run (nil when unknown) and the tokens after it + def self.split_command(argv) + first, *rest = argv + return [resolve_command(first), rest] unless default_command_used?(first) + + command = resolve_command(Bundler.settings[:default_cli_command] || "install") + # Bundler runs install for its deprecated install_or_cli_help alias too + [INSTALL_ALIASES.include?(command) ? "install" : command, argv] + end + + # Bundler runs its default command when argv is empty or starts with a switch + # (`bundle`, `bundle --jobs=4`), and then every token is an argument. A mapped + # switch such as `-v` is still a command of its own. With default_cli_command unset, + # Bundler 4 still runs install here, with a notice that this will change. Its docs + # already name cli_help as the default, but the code does not, so this follows the code. + # The setting is read directly, because Bundler::CLI.default_command prints that notice again. + def self.default_command_used?(token) + token.nil? || (token.start_with?("-") && !Bundler::CLI.map.key?(token)) + end + private_class_method :default_command_used? + + # Thor has no public method for prefix lookup (`i`, `ins`, `a`, `u`), so this calls its protected + # normalize_command_name. The code already relies on Thor's tables, and calling the method keeps + # the result identical to Bundler's, where a copy could drift without anyone noticing. + def self.resolve_command(token) + return unless token + + Bundler::CLI.send(:normalize_command_name, token) + rescue Bundler::Thor::AmbiguousTaskError + # Bundler rejects an ambiguous name before installing, so the hook never sees one. This is only a safeguard. + nil + end + private_class_method :resolve_command + + # @return [Array(Array, Array, Array)] + def self.call(command, tokens) + new(table_for(command)).call(tokens) + end + + # Bundler's global options (--retry, --verbose, --no-color) apply to every command, so they are added to its own. + # Thor's tables are not a plugin API, so a Bundler release can change them; the Appraisals cover each version. + def self.table_for(command) + Bundler::CLI.class_options.merge(Bundler::CLI.commands.fetch(command).options) + end + + def initialize(table) + @switches = switch_table(table) + end + + def call(tokens) + switch_tokens, literal_gems = split_terminator(tokens) + options, gems, unknown = collect(switch_tokens.dup) + [options, gems + literal_gems, unknown] + end + + private + + def collect(rest) + options = [] + gems = [] + unknown = [] + + until rest.empty? + token = rest.shift + next gems << token unless switch?(token) + + option = read_option(token, rest) + option ? options << option : unknown << token + end + + [options, gems, unknown] + end + + def switch_table(table) + switches = table.each_with_object({}) do |(name, option), lookup| + lookup["--#{name}"] = Switch.new(name: name, option: option) + option.aliases.each { |alias_name| lookup[alias_name] = Switch.new(name: name, option: option) } + end + + table.each do |name, option| + next unless option.type == :boolean + + switches["--no-#{name}"] ||= Switch.new(name: name, option: option, negated: true) + end + + switches + end + + def switch?(token) + token.start_with?("-") && token != "-" + end + + # Everything after a bare `--` is a gem name, whatever it looks like + def split_terminator(tokens) + index = tokens.index("--") + return [tokens, []] unless index + + [tokens.take(index), tokens.drop(index + 1)] + end + + # Consumes the values that belong to a switch from the front of rest + def read_option(token, rest) + key, inline = split_switch(token) + switch = @switches[key] + return unless switch + + values, used = values_for(switch.option, inline, rest) + Invocation::Option.new( + name: switch.name, tokens: [token, *rest.shift(used)], values: values, negated: switch.negated + ) + end + + # `--name=value`, and `-jvalue` for single-dash switches + def split_switch(token) + key, separator, value = token.partition("=") + value = nil if separator.empty? + return [key, value] if @switches.key?(key) || !token.match?(/\A-[^-]/) + + short = token[0, 2].to_s + @switches.key?(short) ? [short, token[2..]] : [key, value] + end + + # @return [Array(Array, Integer)] the values and how many following tokens they used + def values_for(option, inline, rest) + return array_values(rest, [inline]) if option.type == :array && inline + return [[inline], 0] if inline + return [[], 0] if option.type == :boolean + return array_values(rest, []) if option.type == :array + return [[], 0] if rest.empty? || (option.lazy_default && switch?(rest.first)) + + [[rest.first], 1] + end + + # An array option takes every value up to the next switch, after any value given inline with `=` + def array_values(rest, leading) + values = rest.take_while { |token| !switch?(token) } + [leading + values, values.size] + end + end +end diff --git a/lib/bundle_matrix/bundle b/lib/bundle_matrix/bundle new file mode 100644 index 0000000..181e041 --- /dev/null +++ b/lib/bundle_matrix/bundle @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# The same as RubyGems' `bundle` binstub. Loading Bundler through RubyGems picks +# the Bundler version for each gemfile from BUNDLER_VERSION, the `version` +# setting or its lockfile's BUNDLED WITH line, as running `bundle` would. +# Kept in lib/ so it is not installed as an executable of this gem. +require "rubygems" + +if Gem.respond_to?(:activate_and_load_bin_path) + Gem.activate_and_load_bin_path("bundler", "bundle", ">= 0.a") +else + load Gem.activate_bin_path("bundler", "bundle", ">= 0.a") +end diff --git a/lib/bundle_matrix/config.rb b/lib/bundle_matrix/config.rb new file mode 100644 index 0000000..7b65784 --- /dev/null +++ b/lib/bundle_matrix/config.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module BundleMatrix + # Reads the matrix.* settings, from `bundle config` or BUNDLE_MATRIX__* env vars. + class Config + TRUE_VALUES = %w[true 1 yes].freeze + FALSE_VALUES = %w[false 0 no].freeze + DEFAULT_GEMFILE = "Gemfile" + + # Extra directories (or globs) to search for a Gemfile, e.g. "engines/*,apps/admin". + def paths + Bundler.settings["matrix.paths"].to_s.split(",").map(&:strip).reject(&:empty?) + end + + # The file name of the main gemfile in the project root, e.g. "gems.rb". Fan-out starts from it. + def default_gemfile + value = Bundler.settings["matrix.default_gemfile"].to_s.strip + value.empty? ? DEFAULT_GEMFILE : value + end + + def disabled? + truthy?("matrix.disabled") + end + + def fail_fast? + truthy?("matrix.fail_fast") + end + + # Warn when a gemfile runs a different Bundler from the main one; on unless set to false, 0 or no + def bundler_warnings? + !FALSE_VALUES.include?(Bundler.settings["matrix.bundler_warnings"].to_s.downcase) + end + + private + + def truthy?(key) + TRUE_VALUES.include?(Bundler.settings[key].to_s.downcase) + end + end +end diff --git a/lib/bundle_matrix/discovery.rb b/lib/bundle_matrix/discovery.rb new file mode 100644 index 0000000..6b02032 --- /dev/null +++ b/lib/bundle_matrix/discovery.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module BundleMatrix + # Finds the secondary gemfiles that follow the main Gemfile. + class Discovery + # The gemfile a matrix.paths directory contributes, in Bundler's order + DIRECTORY_GEMFILES = %w[gems.rb Gemfile].freeze + + # default_gemfile is the main gemfile's file name in root; it is never returned, whatever its name + def initialize(root, paths, default_gemfile:) + @root = Pathname(root).expand_path + @paths = paths + @default_gemfile = default_gemfile + end + + # @return [Array] sorted, unique, excluding the main gemfile + def gemfiles + found = glob("*.gemfile") + glob("gemfiles/*.gemfile") + configured_gemfiles + + found.map(&:expand_path).uniq.sort.reject { |path| main_gemfile?(path) } + end + + private + + def default_gemfile_path + @root.join(@default_gemfile) + end + + # Compares files, not paths, so a symlink to the main gemfile is excluded too + def main_gemfile?(path) + File.identical?(path, default_gemfile_path) + end + + # Patterns are relative to the root, so glob characters in the project's own path are not special + def glob(pattern) + Dir.glob(pattern, base: @root.to_s).map { |match| @root.join(match) } + end + + def configured_gemfiles + @paths.flat_map do |entry| + found = glob(entry).flat_map { |match| gemfiles_at(match) } + Bundler.ui.warn("bundle_matrix: matrix.paths entry #{entry.inspect} matched no Gemfile") if found.empty? + found + end + end + + def gemfiles_at(path) + if path.directory? + gemfile = DIRECTORY_GEMFILES.map { |name| path.join(name) }.find(&:file?) + gemfile ? [gemfile] : [] + elsif path.file? && path.basename.to_s.match?(/(gemfile|\Agems\.rb)\z/i) + [path] + else + [] + end + end + end +end diff --git a/lib/bundle_matrix/hook.rb b/lib/bundle_matrix/hook.rb new file mode 100644 index 0000000..89200e3 --- /dev/null +++ b/lib/bundle_matrix/hook.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module BundleMatrix + # Called by Bundler after `install`, `update` or `add` has finished. + module Hook + ISSUES_URL = "https://github.com/joe-sharp/bundle_matrix/issues" + + # Checked before parsing, so a disabled plugin or a child run never depends on how Bundler's CLI parses + def self.call(argv = ARGV) + return if secondary_run? + + config = Config.new + return if config.disabled? + + invocation = Invocation.parse(argv) + return unless invocation && default_gemfile?(config) + + root = Bundler.root + gemfiles = Discovery.new(root, config.paths, default_gemfile: config.default_gemfile).gemfiles + return if gemfiles.empty? + + warn_untested_bundler + + runner = Runner.new(root, fail_fast: config.fail_fast?, bundler_warnings: config.bundler_warnings?) + runner.run(plans_for(invocation, gemfiles, root)) + end + + def self.plans_for(invocation, gemfiles, root) + gemfiles.to_h do |gemfile| + [gemfile, Planner.new(invocation, gemfile, root).plan] + end + end + + # Fan-out still runs under an untested Bundler, but asks to hear about anything that breaks + def self.warn_untested_bundler + return if TESTED_BUNDLER.satisfied_by?(Gem::Version.new(Bundler::VERSION)) + + Bundler.ui.warn( + "bundle_matrix: not tested with Bundler #{Bundler::VERSION}; " \ + "if anything goes wrong, please open an issue at #{ISSUES_URL}" + ) + end + + def self.default_gemfile?(config) + Bundler.default_gemfile.basename.to_s == config.default_gemfile + end + + # Child runs set this, so they never fan out again + def self.secondary_run? + !ENV["BUNDLE_MATRIX_ACTIVE"].to_s.empty? + end + private_class_method :default_gemfile?, :secondary_run?, :plans_for, :warn_untested_bundler + private_constant :ISSUES_URL + end +end diff --git a/lib/bundle_matrix/invocation.rb b/lib/bundle_matrix/invocation.rb new file mode 100644 index 0000000..17e6f91 --- /dev/null +++ b/lib/bundle_matrix/invocation.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +module BundleMatrix + # A parsed `bundle install|update|add` command line. + # + # Option names, aliases and value types come from Bundler's own Thor tables, + # so every flag Bundler accepts is understood, and the raw tokens are kept + # so they can be forwarded exactly as the user typed them. + class Invocation < Data.define(:command, :options, :gems, :unknown) + COMMANDS = %w[install update add].freeze + + Option = Data.define(:name, :tokens, :values, :negated) + + # negated is true for a `--no-name` switch, which unsets the option rather than setting it + class Option + def initialize(name:, tokens:, values:, negated: false) + super + end + end + + # @return [Invocation, nil] nil when the command is not one we fan out + def self.parse(argv) + command, tokens = ArgvParser.split_command(argv) + return unless command && COMMANDS.include?(command) + + options, gems, unknown = ArgvParser.call(command, tokens) + new(command: command, options: options, gems: gems, unknown: unknown) + end + + # `--no-name` does not count as setting the option + def option?(name) + opt = option(name) + !opt.nil? && !opt.negated + end + + # Bundler keeps the last copy of a repeated option, so that is the one that counts + def option(name) + options.reverse_each.find { |opt| opt.name == name } + end + + def values(name) + option(name)&.values || [] + end + + def with_gems(new_gems) + with(gems: new_gems) + end + + def without(*names) + with(options: options.reject { |opt| names.include?(opt.name) }) + end + + # Replaces an option's values, dropping the option when none are left. + def replace_values(name, new_values) + rest = without(name) + return rest if new_values.empty? + + added = Option.new(name: name, tokens: ["--#{name}", *new_values], values: new_values) + rest.with(options: rest.options + [added]) + end + + # The same flags, re-targeted at another command. Only options that command + # understands are kept, and the gems are replaced. + def as(target, gems: []) + table = ArgvParser.table_for(target) + with(command: target, options: options.select { |opt| table.key?(opt.name) }, gems: gems, unknown: []) + end + + def to_argv + flags = [*options.flat_map(&:tokens), *unknown] + return [command, *gems, *flags] unless gems.any? { |gem| gem.start_with?("-") } + + # A gem that starts with a dash would be read as a switch, so the gems go after a `--` that ends the switches + [command, *flags, "--", *gems] + end + end +end diff --git a/lib/bundle_matrix/planner.rb b/lib/bundle_matrix/planner.rb new file mode 100644 index 0000000..b010acd --- /dev/null +++ b/lib/bundle_matrix/planner.rb @@ -0,0 +1,152 @@ +# frozen_string_literal: true + +module BundleMatrix + # Decides what to run against one secondary gemfile, given the command the + # user ran on the main Gemfile. + class Planner + Plan = Data.define(:invocation, :notes, :error, :locked_bundler) + + # invocation is nil when the gemfile should be skipped; notes explain why; error is set when planning failed; + # locked_bundler is the Bundler named in this gemfile's lockfile, set on every plan that runs a command + class Plan + def initialize(invocation:, notes:, error: nil, locked_bundler: nil) + super + end + + def failed? + !error.nil? + end + + def skipped? + invocation.nil? && error.nil? + end + end + + # Install flags that only make sense for the main Gemfile, or that Bundler no longer accepts + MAIN_ONLY_INSTALL_OPTIONS = %w[lockfile standalone path].freeze + + # Options whose values are paths. A child resolves a relative one from its own gemfile's directory, + # so each is rewritten to be relative to that directory instead + PATH_OPTIONS = %w[binstubs path target-rbconfig].freeze + + def initialize(invocation, gemfile, root) + @invocation = invocation.without("gemfile") + @gemfile = SecondaryGemfile.new(gemfile) + @root = Pathname(root).expand_path + end + + def plan + case @invocation.command + when "install" then install_plan(@invocation) + when "update" then update_plan + when "add" then add_plan + else raise ArgumentError, "unknown command #{@invocation.command}" + end + rescue => e + # One gemfile that can't be read must not stop the others, so its error is reported as its own failure + Plan.new(invocation: nil, notes: [], error: e.message) + end + + private + + def install_plan(invocation, notes = []) + dropped = MAIN_ONLY_INSTALL_OPTIONS.select { |name| invocation.option?(name) } + notes += dropped.map { |name| dropped_note(name) } + run(invocation.without(*MAIN_ONLY_INSTALL_OPTIONS), notes) + end + + # Bundler 2 saves --path to .bundle/config, which ProjectSettings passes on, so it still reaches other gemfiles + def dropped_note(name) + return "--#{name} is not forwarded to other gemfiles" unless name == "path" + + "--path is not forwarded to other gemfiles, but a path Bundler saved in .bundle/config " \ + "reaches them as BUNDLE_PATH" + end + + def update_plan + return run(@invocation) if full_update?(@invocation) + return install_instead("no lockfile yet") unless @gemfile.locked? + + narrow_update(@invocation) + end + + # Keeps only the named gems and groups that this gemfile's lockfile or dependencies know about + def narrow_update(invocation) + gems = invocation.gems & @gemfile.lock_names + # --source names gems too, so it is narrowed to the lockfile the same way + sources = invocation.values("source") & @gemfile.lock_names + groups = narrow_groups(invocation) + if unmatched?(invocation, [*gems, *sources, *groups]) + return skip("none of #{requested(invocation).join(", ")} are in this gemfile") + end + + run(invocation.with_gems(gems).replace_values("source", sources).replace_values("group", groups)) + end + + # Declared groups are only read when --group was given, because reading them evaluates the whole gemfile + def narrow_groups(invocation) + requested_groups = invocation.values("group") + return [] if requested_groups.empty? + + requested_groups & @gemfile.declared_groups + end + + # Named gems or groups were given, but none of them apply to this gemfile + def unmatched?(invocation, matched) + requested(invocation).any? && matched.empty? && + !invocation.option?("ruby") && !invocation.option?("bundler") + end + + def requested(invocation) + invocation.gems + invocation.values("source") + invocation.values("group") + end + + def add_plan + declared = @gemfile.declared_names + already = @invocation.gems & declared + new_gems = @invocation.gems - declared + notes = already.map { |name| "#{name} is already declared, not added again" } + + if new_gems.empty? + return install_plan(@invocation.as("install"), notes + ["gems already declared, re-locking with install"]) + end + + run(@invocation.with_gems(new_gems), notes) + end + + def full_update?(invocation) + return true if invocation.option?("all") + + invocation.gems.empty? && invocation.values("source").empty? && invocation.values("group").empty? && + !invocation.option?("ruby") && !invocation.option?("bundler") + end + + def install_instead(reason) + install_plan(@invocation.as("install"), ["#{reason}, running install to create it"]) + end + + # A relative path is relative to the root in the main run, so it has to be rewritten to be relative to + # this gemfile's directory, where the child runs. + def rewrite_paths(invocation) + PATH_OPTIONS.reduce(invocation) do |rewritten, name| + values = rewritten.values(name) + next rewritten if values.empty? + + rewritten.replace_values(name, values.map { |value| relative_to_gemfile(value) }) + end + end + + def relative_to_gemfile(value) + absolute = File.expand_path(value, @root) + Bundler::SharedHelpers.relative_path_to(absolute, from: @gemfile.path.dirname) + end + + def run(invocation, notes = []) + Plan.new(invocation: rewrite_paths(invocation), notes: notes, locked_bundler: @gemfile.bundler_version) + end + + def skip(reason) + Plan.new(invocation: nil, notes: ["skipped, #{reason}"]) + end + end +end diff --git a/lib/bundle_matrix/project_settings.rb b/lib/bundle_matrix/project_settings.rb new file mode 100644 index 0000000..99899c2 --- /dev/null +++ b/lib/bundle_matrix/project_settings.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module BundleMatrix + # The project's settings that a child run would otherwise lose: its .bundle/config, + # which a child doesn't read because it looks in its own gemfile's directory, and + # the BUNDLE_* variables the user set, which Bundler.with_unbundled_env strips. + # They are passed on as BUNDLE_* variables, so a gemfile's own .bundle/config still wins. + # Settings#all and #locations are not a plugin API, so a Bundler release can change them. + module ProjectSettings + # Settings that point at the main Gemfile's own files; each child sets these itself + MAIN_ONLY = %w[gemfile lockfile app_config bin_path].freeze + + # Settings holding a path relative to the project root, which a child would + # otherwise resolve from its own directory + PATHS = %w[path bin cache_path].freeze + + # @return [Hash{String => String}] BUNDLE_* variables for a child run + def self.env(root) + settings = Bundler.settings + user_env = Bundler.original_env + + (settings.all - MAIN_ONLY).each_with_object({}) do |name, env| + key = settings.key_for(name) + # Bundler gives .bundle/config priority over the environment + value = settings.locations(name)[:local] || user_env[key] + next if value.nil? + + env[key] = PATHS.include?(name) ? File.expand_path(value.to_s, root) : value.to_s + end + end + end +end diff --git a/lib/bundle_matrix/runner.rb b/lib/bundle_matrix/runner.rb new file mode 100644 index 0000000..23b2a84 --- /dev/null +++ b/lib/bundle_matrix/runner.rb @@ -0,0 +1,111 @@ +# frozen_string_literal: true + +module BundleMatrix + # Runs each planned command in a separate `bundle` process. Bundler keeps + # global state, so the secondary gemfiles can't be run in this process. + class Runner + # Launched like a plain `bundle`, so RubyGems and Bundler pick each gemfile's Bundler version. + # __dir__ is only nil inside eval, which Steep can't rule out. + BUNDLE = File.expand_path("bundle", __dir__) # steep:ignore + + # bundler_warnings is false when the matrix.bundler_warnings setting turns off the Bundler version warnings + def initialize(root, fail_fast: false, bundler_warnings: true) + @root = Pathname(root).expand_path + @fail_fast = fail_fast + @bundler_warnings = bundler_warnings + end + + # @param plans [Hash{Pathname => Planner::Plan}] + def run(plans) + failures = [] + other_bundler = false + + plans.each do |gemfile, plan| + label = gemfile.relative_path_from(@root) + # Only a plan that runs carries a locked_bundler, so a skipped or unplannable one never sets this + version = other_bundler_for(plan) + other_bundler ||= version + next if plan_passed?(gemfile, label, plan, version) + + failures << label + break if @fail_fast + end + + suggest_bundler_update if other_bundler + return if failures.empty? + + raise Error, "bundle_matrix: failed for #{failures.join(", ")}" + end + + private + + # @return [Boolean] true when the plan was skipped or its command succeeded + def plan_passed?(gemfile, label, plan, other_bundler) + if plan.failed? + report_unplanned(label, plan.error) + return false + end + + plan.notes.each { |note| say(label, note) } + # A plan that neither failed nor has a command was skipped + invocation = plan.invocation + return true unless invocation + + Bundler.ui.info("== bundle_matrix: #{label} -> bundle #{invocation.to_argv.join(" ")}") + warn_other_bundler(label, other_bundler) + success?(gemfile, invocation) + end + + def say(label, message) + Bundler.ui.info("== bundle_matrix: #{label}: #{message}") + end + + # A gemfile that could not be planned is a failure, and the other gemfiles still run + def report_unplanned(label, error) + Bundler.ui.error("== bundle_matrix: #{label}: could not plan: #{error}") + end + + # The Bundler a plan's lockfile switches to, when that is worth a warning; nil otherwise. + # A child switches only when the `version` setting is lockfile, the default. With `system` or a pinned + # version, every gemfile runs the same Bundler as the main one. + def other_bundler_for(plan) + version = plan.locked_bundler + return unless version && @bundler_warnings && [nil, "lockfile"].include?(Bundler.settings[:version]) + + version unless version == Gem::Version.new(Bundler::VERSION) + end + + def warn_other_bundler(label, version) + return unless version + + Bundler.ui.warn("bundle_matrix: #{label} uses Bundler #{version}, the main Gemfile uses #{Bundler::VERSION}") + end + + def suggest_bundler_update + Bundler.ui.warn( + "bundle_matrix: run `bundle update --bundler` to update Bundler in every lockfile, " \ + "or `bundle config set matrix.bundler_warnings false` to silence these warnings" + ) + end + + def success?(gemfile, invocation) + Bundler.unbundled_system( + child_env(gemfile), Gem.ruby, BUNDLE, *invocation.to_argv, chdir: gemfile.dirname.to_s + ) == true + end + + # BUNDLER_VERSION would hold every gemfile to one version, and it is left set in this + # process when Bundler restarted itself for the main lockfile, so it is cleared. + def child_env(gemfile) + project_env.merge( + "BUNDLE_GEMFILE" => gemfile.to_s, + "BUNDLE_MATRIX_ACTIVE" => "1", + "BUNDLER_VERSION" => nil + ) + end + + def project_env + @project_env ||= ProjectSettings.env(@root) + end + end +end diff --git a/lib/bundle_matrix/secondary_gemfile.rb b/lib/bundle_matrix/secondary_gemfile.rb new file mode 100644 index 0000000..c43f67e --- /dev/null +++ b/lib/bundle_matrix/secondary_gemfile.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +module BundleMatrix + # A secondary gemfile, and what its lockfile and declarations say about it. + class SecondaryGemfile + attr_reader :path + + def initialize(path) + @path = Pathname(path).expand_path + end + + # Bundler locks a gems.rb in gems.locked, and every other gemfile in .lock + def lockfile + return path.sub_ext(".locked") if path.basename.to_s == "gems.rb" + + Pathname("#{path}.lock") + end + + def locked? + lockfile.file? + end + + # The Bundler this gemfile's lockfile names in BUNDLED WITH, nil without a lockfile + def bundler_version + locked&.bundler_version + end + + # Empty without a lockfile + def lock_names + @lock_names ||= (locked&.specs || []).map(&:name).uniq + end + + def declared_names + declared.map(&:name) + end + + def declared_groups + declared.flat_map(&:groups).map(&:to_s).uniq + end + + private + + # nil when this gemfile has no lockfile yet. Bundler.locked_gems only reads the main Gemfile's lockfile, + # so LockfileParser, which is not a plugin API, reads this one. + def locked + return @locked if defined?(@locked) + + @locked = locked? ? Bundler::LockfileParser.new(lockfile.read) : nil + end + + # Dependencies declared by this gemfile, including those pulled in by eval_gemfile. + # Bundler.definition only describes the main Gemfile, so Dsl, which is not a plugin API, evaluates this one. + def declared + @declared ||= Bundler::Dsl.new.tap { |dsl| dsl.eval_gemfile(path) }.dependencies + end + end +end diff --git a/lib/bundle_matrix/version.rb b/lib/bundle_matrix/version.rb new file mode 100644 index 0000000..d39e8f8 --- /dev/null +++ b/lib/bundle_matrix/version.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module BundleMatrix + VERSION = "0.1.0" + + # The oldest Bundler the plugin runs under. The gemspec requires it, and nothing newer is refused. + # It is where Bundler::SharedHelpers.relative_path_to, which Planner uses, first appeared. + MIN_BUNDLER = "2.6.9" + + # The Bundler versions the Appraisals test. The hook warns outside this range, not the gemspec. + # The `.a` keeps 4.2 prereleases out too; raise it once a new Bundler minor has been tested. + TESTED_BUNDLER = Gem::Requirement.new(">= #{MIN_BUNDLER}", "< 4.2.a") +end diff --git a/plugins.rb b/plugins.rb new file mode 100644 index 0000000..d17a498 --- /dev/null +++ b/plugins.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require_relative "lib/bundle_matrix" + +Bundler::Plugin::API.hook(Bundler::Plugin::Events::GEM_AFTER_INSTALL_ALL) do |_dependencies| + BundleMatrix::Hook.call +end diff --git a/sig/bundle_matrix.rbs b/sig/bundle_matrix.rbs new file mode 100644 index 0000000..5cb4a6f --- /dev/null +++ b/sig/bundle_matrix.rbs @@ -0,0 +1,214 @@ +module BundleMatrix + VERSION: String + MIN_BUNDLER: String + TESTED_BUNDLER: Gem::Requirement + + class Error < Bundler::BundlerError + def status_code: () -> Integer + end + + class ArgvParser + class Switch < Data + attr_reader name: String + attr_reader option: Bundler::Thor::Option + attr_reader negated: bool + + def self.new: (name: String, option: Bundler::Thor::Option, ?negated: bool) -> instance + def initialize: (name: String, option: Bundler::Thor::Option, ?negated: bool) -> void + end + + INSTALL_ALIASES: Array[String] + + @switches: Hash[String, Switch] + + def self.split_command: (Array[String] argv) -> [String?, Array[String]] + private def self.default_command_used?: (String? token) -> bool + private def self.resolve_command: (String? token) -> String? + def self.call: (String command, Array[String] tokens) -> [Array[Invocation::Option], Array[String], Array[String]] + def self.table_for: (String command) -> Hash[String, Bundler::Thor::Option] + + def initialize: (Hash[String, Bundler::Thor::Option] table) -> void + def call: (Array[String] tokens) -> [Array[Invocation::Option], Array[String], Array[String]] + + private + + def collect: (Array[String] rest) -> [Array[Invocation::Option], Array[String], Array[String]] + def switch_table: (Hash[String, Bundler::Thor::Option] table) -> Hash[String, Switch] + def switch?: (String token) -> bool + def split_terminator: (Array[String] tokens) -> [Array[String], Array[String]] + def read_option: (String token, Array[String] rest) -> Invocation::Option? + def split_switch: (String token) -> [String, String?] + def values_for: (Bundler::Thor::Option option, String? inline, Array[String] rest) -> [Array[String], Integer] + def array_values: (Array[String] rest, Array[String] leading) -> [Array[String], Integer] + end + + class Config + TRUE_VALUES: Array[String] + FALSE_VALUES: Array[String] + DEFAULT_GEMFILE: String + + def paths: () -> Array[String] + def default_gemfile: () -> String + def disabled?: () -> bool + def fail_fast?: () -> bool + def bundler_warnings?: () -> bool + + private + + def truthy?: (String key) -> bool + end + + class Discovery + DIRECTORY_GEMFILES: Array[String] + + @root: Pathname + @paths: Array[String] + @default_gemfile: String + + def initialize: (Pathname | String root, Array[String] paths, default_gemfile: String) -> void + def gemfiles: () -> Array[Pathname] + + private + + def default_gemfile_path: () -> Pathname + def main_gemfile?: (Pathname path) -> bool + def glob: (String pattern) -> Array[Pathname] + def configured_gemfiles: () -> Array[Pathname] + def gemfiles_at: (Pathname path) -> Array[Pathname] + end + + class Invocation < Data + COMMANDS: Array[String] + + class Option < Data + attr_reader name: String + attr_reader tokens: Array[String] + attr_reader values: Array[String] + attr_reader negated: bool + + def self.new: (name: String, tokens: Array[String], values: Array[String], ?negated: bool) -> instance + def initialize: (name: String, tokens: Array[String], values: Array[String], ?negated: bool) -> void + end + + attr_reader command: String + attr_reader options: Array[Option] + attr_reader gems: Array[String] + attr_reader unknown: Array[String] + + def self.new: (command: String, options: Array[Option], gems: Array[String], unknown: Array[String]) -> instance + def self.parse: (Array[String] argv) -> Invocation? + + def with: (?command: String, ?options: Array[Option], ?gems: Array[String], ?unknown: Array[String]) -> Invocation + def option?: (String name) -> bool + def option: (String name) -> Option? + def values: (String name) -> Array[String] + def with_gems: (Array[String] new_gems) -> Invocation + def without: (*String names) -> Invocation + def replace_values: (String name, Array[String] new_values) -> Invocation + def as: (String target, ?gems: Array[String]) -> Invocation + def to_argv: () -> Array[String] + end + + class SecondaryGemfile + @locked: Bundler::LockfileParser? + @lock_names: Array[String]? + @declared: Array[Bundler::Dependency]? + + attr_reader path: Pathname + + def initialize: (Pathname | String path) -> void + def lockfile: () -> Pathname + def locked?: () -> bool + def bundler_version: () -> Gem::Version? + def lock_names: () -> Array[String] + def declared_names: () -> Array[String] + def declared_groups: () -> Array[String] + + private + + def locked: () -> Bundler::LockfileParser? + def declared: () -> Array[Bundler::Dependency] + end + + class Planner + class Plan < Data + attr_reader invocation: Invocation? + attr_reader notes: Array[String] + attr_reader error: String? + attr_reader locked_bundler: Gem::Version? + + def self.new: (invocation: Invocation?, notes: Array[String], ?error: String?, ?locked_bundler: Gem::Version?) -> instance + def initialize: (invocation: Invocation?, notes: Array[String], ?error: String?, ?locked_bundler: Gem::Version?) -> void + def failed?: () -> bool + def skipped?: () -> bool + end + + MAIN_ONLY_INSTALL_OPTIONS: Array[String] + PATH_OPTIONS: Array[String] + + @invocation: Invocation + @gemfile: SecondaryGemfile + @root: Pathname + + def initialize: (Invocation invocation, Pathname | String gemfile, Pathname | String root) -> void + def plan: () -> Plan + + private + + def install_plan: (Invocation invocation, ?Array[String] notes) -> Plan + def dropped_note: (String name) -> String + def update_plan: () -> Plan + def narrow_update: (Invocation invocation) -> Plan + def narrow_groups: (Invocation invocation) -> Array[String] + def unmatched?: (Invocation invocation, Array[String] matched) -> bool + def requested: (Invocation invocation) -> Array[String] + def add_plan: () -> Plan + def full_update?: (Invocation invocation) -> bool + def install_instead: (String reason) -> Plan + def rewrite_paths: (Invocation invocation) -> Invocation + def relative_to_gemfile: (String value) -> String + def run: (Invocation invocation, ?Array[String] notes) -> Plan + def skip: (String reason) -> Plan + end + + module ProjectSettings + MAIN_ONLY: Array[String] + PATHS: Array[String] + + def self.env: (Pathname root) -> Hash[String, String] + end + + class Runner + BUNDLE: String + + @root: Pathname + @fail_fast: bool + @bundler_warnings: bool + @project_env: Hash[String, String]? + + def initialize: (Pathname | String root, ?fail_fast: bool, ?bundler_warnings: bool) -> void + def run: (Hash[Pathname, Planner::Plan] plans) -> void + + private + + def plan_passed?: (Pathname gemfile, Pathname label, Planner::Plan plan, Gem::Version? other_bundler) -> bool + def say: (Pathname label, String message) -> void + def report_unplanned: (Pathname label, String? error) -> void + def other_bundler_for: (Planner::Plan plan) -> Gem::Version? + def warn_other_bundler: (Pathname label, Gem::Version? version) -> void + def suggest_bundler_update: () -> void + def success?: (Pathname gemfile, Invocation invocation) -> bool + def child_env: (Pathname gemfile) -> Hash[String, String?] + def project_env: () -> Hash[String, String] + end + + module Hook + ISSUES_URL: String + + def self.call: (?Array[String] argv) -> void + private def self.plans_for: (Invocation invocation, Array[Pathname] gemfiles, Pathname root) -> Hash[Pathname, Planner::Plan] + private def self.warn_untested_bundler: () -> void + private def self.default_gemfile?: (Config config) -> bool + private def self.secondary_run?: () -> bool + end +end diff --git a/sig_stubs/bundler.rbs b/sig_stubs/bundler.rbs new file mode 100644 index 0000000..e2e4026 --- /dev/null +++ b/sig_stubs/bundler.rbs @@ -0,0 +1,78 @@ +# Stand-ins for the parts of Bundler that lib/ calls, so rbs and Steep can check against them. +# Bundler publishes no signatures. These cover only what the plugin uses, and are not a model of its API. +module Bundler + VERSION: String + + def self.settings: () -> Settings + def self.ui: () -> UI::Shell + def self.root: () -> Pathname + def self.default_gemfile: () -> Pathname + def self.original_env: () -> Hash[String, String] + def self.unbundled_system: (Hash[String, String?] env, *String command, chdir: String) -> bool? + + class BundlerError < StandardError + def self.status_code: (Integer code) -> void + end + + class Settings + # Values are strings, booleans or numbers, depending on the setting + def []: (String | Symbol name) -> untyped + def all: () -> Array[String] + def key_for: (String name) -> String + def locations: (String name) -> Hash[Symbol, untyped] + end + + module UI + class Shell + def info: (String message) -> void + def warn: (String message) -> void + def error: (String message) -> void + end + end + + class Dependency + def name: () -> String + def groups: () -> Array[Symbol] + end + + class LazySpecification + def name: () -> String + end + + class LockfileParser + def initialize: (String lockfile) -> void + def specs: () -> Array[LazySpecification] + def bundler_version: () -> Gem::Version? + end + + class Dsl + def eval_gemfile: (Pathname gemfile) -> void + def dependencies: () -> Array[Dependency] + end + + module SharedHelpers + def self.relative_path_to: (String destination, from: Pathname) -> String + end + + class Thor + class Option + def type: () -> Symbol + def aliases: () -> Array[String] + def lazy_default: () -> untyped + end + + class Command + def options: () -> Hash[String, Option] + end + + class AmbiguousTaskError < StandardError + end + + def self.class_options: () -> Hash[String, Option] + def self.commands: () -> Hash[String, Command] + def self.map: () -> Hash[String, String] + end + + class CLI < Thor + end +end diff --git a/spec/bundle_matrix/config_spec.rb b/spec/bundle_matrix/config_spec.rb new file mode 100644 index 0000000..b2a76c0 --- /dev/null +++ b/spec/bundle_matrix/config_spec.rb @@ -0,0 +1,129 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::Config) do + let(:root) { Pathname(Dir.mktmpdir("bundle_matrix_root")).realpath } + let(:config) { described_class.new } + + after { FileUtils.remove_entry(root) } + + def settings(local_config: {}, env: {}) + root.join(".bundle").mkpath + root.join(".bundle", "config").write(local_config.map { |key, value| "#{key}: \"#{value}\"\n" }.join) + stub_const("ENV", isolated_env(env)) + allow(Bundler).to receive(:settings).and_return(Bundler::Settings.new(root.join(".bundle"))) + end + + # Keeps the developer's own BUNDLE_MATRIX__* vars and global Bundler config out of the spec. + # Bundler checks BUNDLE_CONFIG before BUNDLE_USER_CONFIG, so BUNDLE_CONFIG has to go too. + def isolated_env(env) + ENV.to_h + .reject { |key, _| key == "BUNDLE_CONFIG" || key.start_with?("BUNDLE_MATRIX__") } + .merge("BUNDLE_USER_CONFIG" => root.join("global_config").to_s, **env) + end + + it "splits matrix.paths on commas only, so a colon stays in the path" do + settings(local_config: { "BUNDLE_MATRIX__PATHS" => "engines/*, apps/admin,C:/engines" }) + + expect(config.paths).to eq(["engines/*", "apps/admin", "C:/engines"]) + end + + it "has no paths when matrix.paths is not set" do + settings + + expect(config.paths).to eq([]) + end + + it "drops empty entries and surrounding whitespace from matrix.paths" do + settings(local_config: { "BUNDLE_MATRIX__PATHS" => " a ,, b ," }) + + expect(config.paths).to eq(%w[a b]) + end + + it "uses Gemfile as the default gemfile when matrix.default_gemfile is not set" do + settings + + expect(config.default_gemfile).to eq("Gemfile") + end + + it "reads the default gemfile from bundle config" do + settings(local_config: { "BUNDLE_MATRIX__DEFAULT_GEMFILE" => "gems.rb" }) + + expect(config.default_gemfile).to eq("gems.rb") + end + + it "reads the default gemfile from BUNDLE_MATRIX__DEFAULT_GEMFILE" do + settings(env: { "BUNDLE_MATRIX__DEFAULT_GEMFILE" => "gems.rb" }) + + expect(config.default_gemfile).to eq("gems.rb") + end + + it "is neither disabled nor fail fast by default", :aggregate_failures do + settings + + expect(config).not_to be_disabled + expect(config).not_to be_fail_fast + end + + it "prefers bundle config over BUNDLE_MATRIX__* env vars" do + settings(local_config: { "BUNDLE_MATRIX__DISABLED" => "true" }, env: { "BUNDLE_MATRIX__DISABLED" => "no" }) + + expect(config).to be_disabled + end + + it "is disabled with bundle config" do + settings(local_config: { "BUNDLE_MATRIX__DISABLED" => "true" }) + + expect(config).to be_disabled + end + + it "is disabled with BUNDLE_MATRIX__DISABLED" do + settings(env: { "BUNDLE_MATRIX__DISABLED" => "1" }) + + expect(config).to be_disabled + end + + it "treats true and yes as on, regardless of case", :aggregate_failures do + settings(local_config: { "BUNDLE_MATRIX__DISABLED" => "TRUE", "BUNDLE_MATRIX__FAIL_FAST" => "YES" }) + + expect(config).to be_disabled + expect(config).to be_fail_fast + end + + it "treats false as off" do + settings(local_config: { "BUNDLE_MATRIX__DISABLED" => "false" }) + + expect(config).not_to be_disabled + end + + it "treats an empty value as off" do + settings(local_config: { "BUNDLE_MATRIX__DISABLED" => "" }) + + expect(config).not_to be_disabled + end + + it "warns about other Bundler versions by default" do + settings + + expect(config).to be_bundler_warnings + end + + it "turns off Bundler version warnings when matrix.bundler_warnings is false, 0 or no", :aggregate_failures do + %w[false 0 NO].each do |value| + settings(local_config: { "BUNDLE_MATRIX__BUNDLER_WARNINGS" => value }) + expect(config).not_to be_bundler_warnings + end + end + + it "turns off Bundler version warnings with BUNDLE_MATRIX__BUNDLER_WARNINGS" do + settings(env: { "BUNDLE_MATRIX__BUNDLER_WARNINGS" => "false" }) + + expect(config).not_to be_bundler_warnings + end + + it "treats anything other than true, 1 or yes as off", :aggregate_failures do + settings(local_config: { "BUNDLE_MATRIX__DISABLED" => "no", "BUNDLE_MATRIX__FAIL_FAST" => "off" }) + + expect(config).not_to be_disabled + expect(config).not_to be_fail_fast + end +end diff --git a/spec/bundle_matrix/discovery_spec.rb b/spec/bundle_matrix/discovery_spec.rb new file mode 100644 index 0000000..d0857cf --- /dev/null +++ b/spec/bundle_matrix/discovery_spec.rb @@ -0,0 +1,131 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::Discovery) do + let(:root) { Pathname(Dir.mktmpdir("bundle_matrix_root")).realpath } + + after { FileUtils.remove_entry(root) } + + def touch(*parts) + path = root.join(*parts) + path.dirname.mkpath + path.write("") + path + end + + def relative(gemfiles) + gemfiles.map { |path| path.relative_path_from(root).to_s } + end + + before do + touch("Gemfile") + touch("Gemfile.lock") + touch("ci.gemfile") + touch("ci.gemfile.lock") + touch("gemfiles", "rails7.gemfile") + touch("gemfiles", "rails7.gemfile.lock") + touch("engines", "admin", "Gemfile") + touch("engines", "api", "Gemfile") + touch("engines", "api.txt") + end + + it "finds *.gemfile in the root and in gemfiles/, but not lockfiles or the main Gemfile" do + found = described_class.new(root, [], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile gemfiles/rails7.gemfile]) + end + + it "finds gemfiles when the project path contains glob characters" do + touch("app[v2]", "ci.gemfile") + touch("app[v2]", "gemfiles", "rails.gemfile") + + found = described_class.new(root.join("app[v2]"), [], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[app[v2]/ci.gemfile app[v2]/gemfiles/rails.gemfile]) + end + + it "lists a gemfile once when matrix.paths entries overlap" do + found = described_class.new(root, ["engines/*", "engines/admin"], default_gemfile: "Gemfile").gemfiles + + expect(relative(found).count("engines/admin/Gemfile")).to eq(1) + end + + it "finds Gemfiles in configured directories, with globs" do + found = described_class.new(root, ["engines/*"], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile engines/admin/Gemfile engines/api/Gemfile gemfiles/rails7.gemfile]) + end + + it "accepts a gemfile path directly" do + touch("apps", "web.gemfile") + + found = described_class.new(root, ["apps/web.gemfile"], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to include("apps/web.gemfile") + end + + it "never returns the main gemfile, even when its name ends in .gemfile" do + touch("foo.gemfile") + touch("foo.gemfile.lock") + + found = described_class.new(root, [], default_gemfile: "foo.gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile gemfiles/rails7.gemfile]) + end + + it "never returns a symlink to the main gemfile, which is the same file" do + File.symlink(root.join("Gemfile"), root.join("linked.gemfile")) + + found = described_class.new(root, [], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile gemfiles/rails7.gemfile]) + end + + it "keeps Gemfile as a secondary when the main gemfile is named differently" do + touch("gems.rb") + + found = described_class.new(root, [], default_gemfile: "gems.rb").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile gemfiles/rails7.gemfile]) + end + + it "uses gems.rb in a configured directory that has no Gemfile" do + touch("engines", "web", "gems.rb") + + found = described_class.new(root, ["engines/web"], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile engines/web/gems.rb gemfiles/rails7.gemfile]) + end + + it "uses a gems.rb named directly in matrix.paths" do + touch("engines", "web", "gems.rb") + + found = described_class.new(root, ["engines/web/gems.rb"], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile engines/web/gems.rb gemfiles/rails7.gemfile]) + end + + it "prefers gems.rb over Gemfile in a configured directory, as Bundler does" do + touch("engines", "api", "gems.rb") + + found = described_class.new(root, ["engines/api"], default_gemfile: "Gemfile").gemfiles + + expect(relative(found)).to eq(%w[ci.gemfile engines/api/gems.rb gemfiles/rails7.gemfile]) + end + + it "skips the main gems.rb when matrix.paths points at the project root" do + touch("gems.rb") + root.join("Gemfile").delete + + found = described_class.new(root, ["."], default_gemfile: "gems.rb").gemfiles + + expect(relative(found)).not_to include("gems.rb") + end + + it "warns when a configured path matches nothing" do + allow(Bundler.ui).to receive(:warn) + + described_class.new(root, ["nowhere/*"], default_gemfile: "Gemfile").gemfiles + + expect(Bundler.ui).to have_received(:warn).with(%r{nowhere/\*}) + end +end diff --git a/spec/bundle_matrix/hook_spec.rb b/spec/bundle_matrix/hook_spec.rb new file mode 100644 index 0000000..a07a8cc --- /dev/null +++ b/spec/bundle_matrix/hook_spec.rb @@ -0,0 +1,183 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::Hook) do + let(:root) { Pathname("/project") } + let(:discovered) { [root.join("ci.gemfile"), root.join("gemfiles/rails7.gemfile")] } + + def config_double(**overrides) + defaults = { disabled?: false, paths: [], fail_fast?: false, bundler_warnings?: true, default_gemfile: "Gemfile" } + instance_double(BundleMatrix::Config, **defaults, **overrides) + end + + before do + allow(Bundler).to receive_messages(default_gemfile: root.join("Gemfile"), root: root) + allow(Bundler.ui).to receive(:warn) + allow(BundleMatrix::Runner).to receive(:new).and_return(instance_double(BundleMatrix::Runner, run: nil)) + allow(BundleMatrix::Discovery).to receive(:new).and_return( + instance_double(BundleMatrix::Discovery, gemfiles: [root.join("ci.gemfile")]) + ) + allow(BundleMatrix::Planner).to receive(:new).and_return(instance_double(BundleMatrix::Planner, plan: :ci_plan)) + allow(BundleMatrix::Config).to receive(:new).and_return(config_double) + stub_const("ENV", ENV.to_h.merge("BUNDLE_MATRIX_ACTIVE" => nil)) + end + + # The untested-Bundler warning, checked directly, since the hook only reaches it after finding gemfiles + describe "the untested Bundler warning" do + def warn_untested_bundler + described_class.send(:warn_untested_bundler) + end + + it "warns that a Bundler outside the tested range is untested, linking to the issue tracker" do + stub_const("Bundler::VERSION", "4.2.0") + + warn_untested_bundler + + expect(Bundler.ui).to have_received(:warn).with( + "bundle_matrix: not tested with Bundler 4.2.0; " \ + "if anything goes wrong, please open an issue at https://github.com/joe-sharp/bundle_matrix/issues" + ) + end + + it "treats a 4.2 prerelease as untested" do + stub_const("Bundler::VERSION", "4.2.0.beta1") + + warn_untested_bundler + + expect(Bundler.ui).to have_received(:warn) + end + + it "does not warn under a tested Bundler" do + %w[2.6.9 4.1.0.beta3].each do |version| + stub_const("Bundler::VERSION", version) + + warn_untested_bundler + end + + expect(Bundler.ui).not_to have_received(:warn) + end + + it "warns before fanning out, when the running Bundler is untested", :aggregate_failures do + stub_const("Bundler::VERSION", "4.2.0") + + described_class.call(%w[install]) + + expect(Bundler.ui).to have_received(:warn).with(a_string_including("not tested with Bundler 4.2.0")) + expect(BundleMatrix::Runner).to have_received(:new) + end + end + + it "does nothing when running inside a secondary run" do + stub_const("ENV", ENV.to_h.merge("BUNDLE_MATRIX_ACTIVE" => "1")) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).not_to have_received(:new) + end + + it "does nothing when the main Gemfile is not the default gemfile" do + allow(Bundler).to receive(:default_gemfile).and_return(root.join("ci.gemfile")) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).not_to have_received(:new) + end + + it "fans out from an overridden default gemfile", :aggregate_failures do + allow(Bundler).to receive(:default_gemfile).and_return(root.join("gems.rb")) + allow(BundleMatrix::Config).to receive(:new).and_return(config_double(default_gemfile: "gems.rb")) + + described_class.call(%w[install]) + + expect(BundleMatrix::Discovery).to have_received(:new).with(root, [], default_gemfile: "gems.rb") + expect(BundleMatrix::Runner).to have_received(:new) + end + + it "does nothing from Gemfile when the default gemfile is overridden" do + allow(BundleMatrix::Config).to receive(:new).and_return(config_double(default_gemfile: "gems.rb")) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).not_to have_received(:new) + end + + it "does nothing when disabled" do + allow(BundleMatrix::Config).to receive(:new).and_return(instance_double(BundleMatrix::Config, disabled?: true)) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).not_to have_received(:new) + end + + # Thor's tables can change in a Bundler release, so a parse that fails must not break installs that never fan out + it "does not parse the command when disabled" do + allow(BundleMatrix::Invocation).to receive(:parse).and_raise(NoMethodError) + allow(BundleMatrix::Config).to receive(:new).and_return(instance_double(BundleMatrix::Config, disabled?: true)) + + expect { described_class.call(%w[install]) }.not_to raise_error + end + + it "does not parse the command in a secondary run" do + allow(BundleMatrix::Invocation).to receive(:parse).and_raise(NoMethodError) + stub_const("ENV", ENV.to_h.merge("BUNDLE_MATRIX_ACTIVE" => "1")) + + expect { described_class.call(%w[install]) }.not_to raise_error + end + + it "does nothing for commands other than install, update and add" do + described_class.call(%w[exec rspec]) + + expect(BundleMatrix::Runner).not_to have_received(:new) + end + + it "does nothing when there are no secondary gemfiles" do + allow(BundleMatrix::Discovery).to receive(:new).and_return(instance_double(BundleMatrix::Discovery, gemfiles: [])) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).not_to have_received(:new) + end + + { + "install" => %w[install], + "update" => %w[update --all], + "add" => %w[add rack] + }.each do |command, argv| + it "plans every discovered gemfile for #{command}", :aggregate_failures do + allow(BundleMatrix::Discovery).to receive(:new) + .and_return(instance_double(BundleMatrix::Discovery, gemfiles: discovered)) + + described_class.call(argv) + + discovered.each do |gemfile| + expect(BundleMatrix::Planner).to have_received(:new) + .with(having_attributes(command: command), gemfile, root) + end + end + end + + it "hands every gemfile's plan to the runner, with fail_fast from the config", :aggregate_failures do + runner = instance_double(BundleMatrix::Runner, run: nil) + allow(BundleMatrix::Runner).to receive(:new).and_return(runner) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).to have_received(:new).with(root, fail_fast: false, bundler_warnings: true) + expect(runner).to have_received(:run).with(root.join("ci.gemfile") => :ci_plan) + end + + it "passes fail_fast through to the runner when the config enables it" do + allow(BundleMatrix::Config).to receive(:new).and_return(config_double(fail_fast?: true)) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).to have_received(:new).with(root, fail_fast: true, bundler_warnings: true) + end + + it "passes bundler_warnings through to the runner when the config turns it off" do + allow(BundleMatrix::Config).to receive(:new).and_return(config_double(bundler_warnings?: false)) + + described_class.call(%w[install]) + + expect(BundleMatrix::Runner).to have_received(:new).with(root, fail_fast: false, bundler_warnings: false) + end +end diff --git a/spec/bundle_matrix/invocation_spec.rb b/spec/bundle_matrix/invocation_spec.rb new file mode 100644 index 0000000..a751c20 --- /dev/null +++ b/spec/bundle_matrix/invocation_spec.rb @@ -0,0 +1,266 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::Invocation) do + def parse(*argv) + described_class.parse(argv) + end + + def thor_table(command) + Bundler::CLI.class_options.merge(Bundler::CLI.commands.fetch(command).options) + end + + # resolve_command is private to ArgvParser, and parse hides the name it resolved to + def resolve(token) + BundleMatrix::ArgvParser.send(:resolve_command, token) + end + + describe "command name resolution" do + it "returns exact command names", :aggregate_failures do + expect(resolve("install")).to eq("install") + expect(resolve("cache")).to eq("cache") + end + + it "resolves unambiguous prefixes", :aggregate_failures do + expect(resolve("ins")).to eq("install") + expect(resolve("a")).to eq("add") + expect(resolve("u")).to eq("update") + end + + it "resolves aliases to their target", :aggregate_failures do + expect(resolve("i")).to eq("install") + expect(resolve("ls")).to eq("list") + end + + it "returns nil for an ambiguous prefix", :aggregate_failures do + expect(resolve("in")).to be_nil + expect(parse("in")).to be_nil + end + + # Bundler 2.6.9 matches hidden commands by prefix, and 4.x does not, so only the parse result is the same + it "does not fan out for a hidden command reached by prefix" do + expect(parse("install_or")).to be_nil + end + + it "returns unknown names unchanged, for COMMANDS to filter out" do + expect(resolve("zz")).to eq("zz") + end + + it "returns nil when there is no token" do + expect(resolve(nil)).to be_nil + end + end + + describe ".parse" do + it "returns nil for commands bundle_matrix does not fan out", :aggregate_failures do + expect(parse("exec", "rspec")).to be_nil + expect(parse("check")).to be_nil + expect(parse("zz")).to be_nil + end + + it "treats `i` as install" do + expect(parse("i").command).to eq("install") + end + + it "accepts the unambiguous prefixes Bundler accepts", :aggregate_failures do + expect(parse("ins").command).to eq("install") + expect(parse("a", "rack").command).to eq("add") + expect(parse("u").command).to eq("update") + expect(parse("up").command).to eq("update") + end + + it "splits gems from options", :aggregate_failures do + invocation = parse("update", "rails", "--conservative", "rack") + + expect(invocation.gems).to eq(%w[rails rack]) + expect(invocation.option?("conservative")).to be(true) + end + + it "forwards a dash-leading gem after -- so it is not read as a switch", :aggregate_failures do + expect(parse("add", "--", "-weird").to_argv).to eq(%w[add -- -weird]) + expect(parse("add", "rack", "--", "-weird").to_argv).to eq(%w[add -- rack -weird]) + end + + it "keeps gems before the options when none starts with a dash" do + expect(parse("update", "rails", "--conservative").to_argv).to eq(%w[update rails --conservative]) + end + + it "resolves aliases to the canonical option name", :aggregate_failures do + invocation = parse("update", "-j", "4", "-g", "dev") + + expect(invocation.option?("jobs")).to be(true) + expect(invocation.values("group")).to eq(%w[dev]) + end + + it "handles --name=value and short values attached to the flag", :aggregate_failures do + expect(parse("install", "--jobs=4").values("jobs")).to eq(%w[4]) + expect(parse("install", "-j4").values("jobs")).to eq(%w[4]) + end + + it "lets array options consume values up to the next switch", :aggregate_failures do + invocation = parse("update", "--group", "test", "dev", "--conservative", "rails") + + expect(invocation.values("group")).to eq(%w[test dev]) + expect(invocation.gems).to eq(%w[rails]) + end + + it "reads the values after an array option given inline with =", :aggregate_failures do + invocation = parse("update", "--group=test", "dev", "--conservative", "rails") + + expect(invocation.values("group")).to eq(%w[test dev]) + expect(invocation.gems).to eq(%w[rails]) + end + + it "reads a repeated option the way Bundler does, from its last copy", :aggregate_failures do + expect(parse("update", "--group", "dev", "--group", "test").values("group")).to eq(%w[test]) + expect(parse("add", "foo", "--path", "a", "--path", "b").values("path")).to eq(%w[b]) + expect(parse("update", "--all", "--no-all").option?("all")).to be(false) + end + + it "does not count --no-name as setting the option, but still forwards it", :aggregate_failures do + invocation = parse("update", "--no-all", "rails") + + expect(invocation.option?("all")).to be(false) + expect(invocation.to_argv).to eq(%w[update rails --no-all]) + end + + it "still counts --no-color as setting the option, because it is a real option" do + expect(parse("install", "--no-color").option?("no-color")).to be(true) + end + + it "only consumes a value for a lazy-default option when one is present", :aggregate_failures do + expect(parse("update", "--bundler", "--all").values("bundler")).to eq([]) + expect(parse("update", "--bundler", "4.0.1").values("bundler")).to eq(%w[4.0.1]) + end + + it "keeps unknown switches for forwarding" do + expect(parse("install", "--some-future-flag").unknown).to eq(%w[--some-future-flag]) + end + + it "understands Bundler's global options, so their values are not read as gems", :aggregate_failures do + expect(parse("install", "--retry", "3").values("retry")).to eq(%w[3]) + expect(parse("update", "-r", "3").values("retry")).to eq(%w[3]) + expect(parse("update", "--retry", "3").gems).to eq([]) + expect(parse("update", "--verbose", "--all").option?("verbose")).to be(true) + end + + it "keeps global options when it is re-targeted at install", :aggregate_failures do + forwarded = parse("update", "--retry", "3", "--all").as("install") + + expect(forwarded.to_argv).to eq(%w[install --retry 3]) + end + + it "treats everything after -- as a gem name" do + expect(parse("add", "--strict", "--", "rails").gems).to eq(%w[rails]) + end + + # rubocop:disable-next RSpec/ExampleLength + it "matches Thor's own parsing for the gems and options it recognises", :aggregate_failures do + samples = [ + %w[update rails --conservative], + %w[update --group test dev rails], + %w[update --all --patch --jobs 2], + %w[install --local --jobs=2 --standalone dev test], + %w[add rails -v 1.0 -g dev --strict], + %w[add rails --skip-install], + %w[install --retry 3 --jobs 2], + %w[update --verbose --all], + %w[update --group=test dev --conservative rails] + ] + + no_values = [true, false, nil] + + samples.each do |argv| + command, *tokens = argv + thor = Bundler::Thor::Options.new(thor_table(command)) + thor_options = thor.parse(tokens) + invocation = parse(*argv) + + expect(invocation.gems).to eq(Array(thor.remaining)), "gems differ for #{argv.join(" ")}" + expect(invocation.options.map(&:name)).to( + match_array(thor_options.keys.map(&:to_s)), "options differ for #{argv.join(" ")}" + ) + thor_options.each do |name, value| + next if no_values.include?(value) + + expect(invocation.values(name.to_s)).to eq(Array(value).map(&:to_s)), + "values differ for --#{name} in #{argv.join(" ")}" + end + end + end + end + + describe "when no command is given" do + before do + allow(Bundler.settings).to receive(:[]).and_call_original + allow(Bundler.settings).to receive(:[]).with(:default_cli_command).and_return(nil) + end + + def default_command_is(name) + allow(Bundler.settings).to receive(:[]).with(:default_cli_command).and_return(name) + end + + it "fans out a bare bundle as install" do + expect(parse).to have_attributes(command: "install", gems: []) + end + + it "fans out switches as install options", :aggregate_failures do + invocation = parse("--jobs=4") + + expect(invocation.command).to eq("install") + expect(invocation.values("jobs")).to eq(%w[4]) + expect(invocation.to_argv).to eq(%w[install --jobs=4]) + end + + it "fans out when default_cli_command is install" do + default_command_is("install") + + expect(parse("--jobs=4").command).to eq("install") + end + + it "fans out when default_cli_command is install_or_cli_help, which Bundler runs as install" do + skip "this Bundler has no install_or_cli_help" unless Bundler::CLI.all_commands.key?("install_or_cli_help") + + default_command_is("install_or_cli_help") + + expect(parse("--jobs=4").command).to eq("install") + end + + it "does not fan out when default_cli_command is cli_help, which only prints help", :aggregate_failures do + default_command_is("cli_help") + + expect(parse).to be_nil + expect(parse("--jobs=4")).to be_nil + end + + it "does not treat a mapped switch such as -v as the default command" do + expect(parse("-v")).to be_nil + end + + # Bundler rejects this form before any hook runs, so it never fans out. Parsing it the same way keeps us honest. + it "reads a command after leading switches as an argument, as Bundler does" do + expect(parse("--verbose", "install").to_argv).to eq(%w[install install --verbose]) + end + end + + describe "#to_argv" do + it "re-emits every token the user supplied" do + argv = %w[update rails --conservative --group test dev --bundler 4.0.1 --unknown-thing] + + expect(parse(*argv).to_argv).to eq(argv) + end + end + + describe "#as" do + # rubocop:disable-next RSpec/ExampleLength + it "keeps only the options the target command understands", :aggregate_failures do + invocation = parse("update", "rails", "--conservative", "--group", "dev", "--jobs", "2") + install = invocation.as("install") + + expect(install.command).to eq("install") + expect(install.gems).to eq([]) + expect(install.option?("conservative")).to be(false) + expect(install.option?("group")).to be(false) + expect(install.option?("jobs")).to be(true) + end + end +end diff --git a/spec/bundle_matrix/planner_spec.rb b/spec/bundle_matrix/planner_spec.rb new file mode 100644 index 0000000..4d6a68e --- /dev/null +++ b/spec/bundle_matrix/planner_spec.rb @@ -0,0 +1,335 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::Planner) do + let(:root) { Pathname(Dir.mktmpdir("bundle_matrix_root")).realpath } + let(:gemfile) { root.join("gemfiles", "old.gemfile") } + + after { FileUtils.remove_entry(root) } + + def write(path, contents) + path.dirname.mkpath + path.write(contents) + end + + # Bundler added install --lockfile after 2.6 + def bundler_has_install_option?(name) + Bundler::CLI.commands.fetch("install").options.key?(name) + end + + def lock_for(*gems) + specs = gems.map { |name| " #{name} (1.0.0)" }.join("\n") + deps = gems.map { |name| " #{name}" }.join("\n") + + <<~LOCK + GEM + remote: https://rubygems.org/ + specs: + #{specs} + + PLATFORMS + ruby + + DEPENDENCIES + #{deps} + LOCK + end + + def plan_for(argv, gemfile_contents: "gem \"rake\"\n", lock: nil) + write(root.join("Gemfile"), "source \"https://rubygems.org\"\ngem \"rake\"\n") + write(gemfile, gemfile_contents) + write(Pathname("#{gemfile}.lock"), lock) if lock + + invocation = BundleMatrix::Invocation.parse(argv) + described_class.new(invocation, gemfile, root).plan + end + + def locked_by(version) + "#{lock_for("rake")}\nBUNDLED WITH\n #{version}\n" + end + + context "when the secondary is a gems.rb" do + let(:gemfile) { root.join("engines", "web", "gems.rb") } + + it "reads its lockfile from gems.locked" do + write(root.join("engines", "web", "gems.locked"), lock_for("rake")) + + plan = plan_for(%w[update rake --conservative]) + + expect(plan.invocation.to_argv).to eq(%w[update rake --conservative]) + end + end + + it "reports a gemfile it cannot evaluate as an error instead of raising", :aggregate_failures do + plan = plan_for(%w[add rack], gemfile_contents: "raise \"boom\"\n") + + expect(plan.invocation).to be_nil + expect(plan.error).to include("boom") + end + + it "reports a command it does not plan as an error instead of returning nil", :aggregate_failures do + invocation = BundleMatrix::Invocation.new(command: "exec", options: [], gems: [], unknown: []) + plan = described_class.new(invocation, gemfile, root).plan + + expect(plan.error).to eq("unknown command exec") + expect(plan).to be_failed + end + + context "when the plan's outcome is checked" do + it "is neither failed nor skipped when it runs a command", :aggregate_failures do + plan = plan_for(%w[install]) + + expect(plan).not_to be_failed + expect(plan).not_to be_skipped + end + + it "is skipped when there is nothing to run", :aggregate_failures do + plan = plan_for(%w[update rack], lock: lock_for("rake")) + + expect(plan).to be_skipped + expect(plan).not_to be_failed + end + + it "is failed when planning raised, and not skipped", :aggregate_failures do + plan = plan_for(%w[add rack], gemfile_contents: "raise \"boom\"\n") + + expect(plan).to be_failed + expect(plan).not_to be_skipped + end + end + + context "when the plan runs a command" do + it "records the Bundler the lockfile names, even when it is the running one", :aggregate_failures do + expect(plan_for(%w[install], lock: locked_by("2.5.23")).locked_bundler).to eq(Gem::Version.new("2.5.23")) + expect(plan_for(%w[install], lock: locked_by(Bundler::VERSION)).locked_bundler) + .to eq(Gem::Version.new(Bundler::VERSION)) + end + + it "records no Bundler for a gemfile without a lockfile" do + expect(plan_for(%w[install]).locked_bundler).to be_nil + end + + it "records no Bundler for a gemfile that is skipped" do + expect(plan_for(%w[update rack], lock: locked_by("2.5.23")).locked_bundler).to be_nil + end + end + + context "when the command is install" do + it "removes --gemfile, which is set through BUNDLE_GEMFILE instead" do + plan = plan_for(%w[install --gemfile Gemfile --jobs 4]) + + expect(plan.invocation.to_argv).to eq(%w[install --jobs 4]) + end + + it "drops --lockfile given inline with =", :aggregate_failures do + skip "Bundler #{Bundler::VERSION} has no install --lockfile" unless bundler_has_install_option?("lockfile") + + plan = plan_for(%w[install --lockfile=other.lock]) + + expect(plan.invocation.to_argv).to eq(%w[install]) + expect(plan.notes).to eq(["--lockfile is not forwarded to other gemfiles"]) + end + + it "drops --standalone, which only makes sense for the main Gemfile", :aggregate_failures do + plan = plan_for(%w[install --standalone --jobs 2]) + + expect(plan.invocation.to_argv).to eq(%w[install --jobs 2]) + expect(plan.notes).to eq(["--standalone is not forwarded to other gemfiles"]) + end + + it "drops --lockfile and --standalone, which only make sense for the main Gemfile", :aggregate_failures do + skip "Bundler #{Bundler::VERSION} has no install --lockfile" unless bundler_has_install_option?("lockfile") + + plan = plan_for(%w[install --lockfile other.lock --standalone --jobs 2]) + + expect(plan.invocation.to_argv).to eq(%w[install --jobs 2]) + expect(plan.notes).to eq([ + "--lockfile is not forwarded to other gemfiles", + "--standalone is not forwarded to other gemfiles" + ]) + end + end + + context "when the command is update" do + it "runs a bare update in full, even without a lockfile" do + expect(plan_for(%w[update]).invocation.to_argv).to eq(%w[update]) + end + + it "forwards --all and --conservative untouched" do + plan = plan_for(%w[update --all --conservative]) + + expect(plan.invocation.to_argv).to eq(%w[update --all --conservative]) + end + + it "forwards --patch and --conservative with named gems" do + plan = plan_for(%w[update rake --patch --conservative], lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[update rake --patch --conservative]) + end + + it "keeps only the named gems that appear in the secondary lockfile" do + plan = plan_for(%w[update rake rack --conservative], lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[update rake --conservative]) + end + + it "narrows the update for --no-all instead of running a full update" do + plan = plan_for(%w[update --no-all rake --conservative], lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[update rake --no-all --conservative]) + end + + it "skips the gemfile when none of the named gems are locked" do + plan = plan_for(%w[update rack], lock: lock_for("rake")) + + expect(plan.invocation).to be_nil + end + + it "runs install instead of a targeted update when there is no lockfile yet", :aggregate_failures do + plan = plan_for(%w[update rake --jobs 3]) + + expect(plan.invocation.to_argv).to eq(%w[install --jobs 3]) + expect(plan.notes).to eq(["no lockfile yet, running install to create it"]) + end + + it "removes --gemfile from the install it runs instead" do + plan = plan_for(%w[update rake --gemfile Gemfile --jobs 3]) + + expect(plan.invocation.to_argv).to eq(%w[install --jobs 3]) + end + + it "keeps only declared groups" do + gemfile_contents = "group :dev do\n gem \"rake\"\nend\n" + plan = plan_for(%w[update --group dev test], gemfile_contents: gemfile_contents, lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[update --group dev]) + end + + it "does not evaluate the gemfile when no group was given", :aggregate_failures do + gemfile_contents = "gem ENV.fetch(\"BUNDLE_MATRIX_SPEC_UNSET\")\n" + plan = plan_for(%w[update rake], gemfile_contents: gemfile_contents, lock: lock_for("rake")) + + expect(plan).not_to be_failed + expect(plan.invocation.to_argv).to eq(%w[update rake]) + end + + it "keeps only the --source gems that this gemfile's lockfile has" do + plan = plan_for(%w[update --source rake mygem], lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[update --source rake]) + end + + it "skips a gemfile whose lockfile has none of the --source gems" do + plan = plan_for(%w[update --source mygem], lock: lock_for("rake")) + + expect(plan).to be_skipped + end + + it "removes --gemfile, which is set through BUNDLE_GEMFILE instead" do + plan = plan_for(%w[update --gemfile Gemfile --all]) + + expect(plan.invocation.to_argv).to eq(%w[update --all]) + end + + it "forwards --bundler, so every lockfile's Bundler is updated" do + plan = plan_for(%w[update --bundler], lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[update --bundler]) + end + end + + context "when the command is add" do + it "adds gems the secondary does not declare" do + plan = plan_for(%w[add rack --group dev], lock: lock_for("rake")) + + expect(plan.invocation.to_argv).to eq(%w[add rack --group dev]) + end + + it "forwards --version" do + plan = plan_for(["add", "rack", "--version", "~> 1.0"]) + + expect(plan.invocation.to_argv).to eq(["add", "rack", "--version", "~> 1.0"]) + end + + it "runs install instead when the secondary declares the gem itself", :aggregate_failures do + plan = plan_for(%w[add rake --version 13.0]) + + expect(plan.invocation.to_argv).to eq(%w[install]) + expect(plan.notes).to include("rake is already declared, not added again") + end + + it "runs install instead when every gem is already declared, e.g. via eval_gemfile" do + plan = plan_for(%w[add rake --group dev --quiet], gemfile_contents: "eval_gemfile \"../Gemfile\"\n") + + expect(plan.invocation.to_argv).to eq(%w[install --quiet]) + end + + it "notes the main-only flags it drops from that install", :aggregate_failures do + plan = plan_for(%w[add rake --path vendor/rake --quiet], gemfile_contents: "eval_gemfile \"../Gemfile\"\n") + + expect(plan.invocation.to_argv).to eq(%w[install --quiet]) + expect(plan.notes).to eq([ + "rake is already declared, not added again", + "gems already declared, re-locking with install", + "--path is not forwarded to other gemfiles, but a path Bundler saved in .bundle/config " \ + "reaches them as BUNDLE_PATH" + ]) + end + + it "keeps an absolute --path pointing at the same place from the secondary gemfile's directory" do + plan = plan_for(%w[add rack --path /opt/rack], lock: lock_for("rake")) + + expect(File.expand_path(plan.invocation.values("path").first, gemfile.dirname)).to eq("/opt/rack") + end + + it "rewrites a relative --path so it resolves from the secondary gemfile's directory" do + plan = plan_for(%w[add local_gem --path vendor/local_gem]) + + expect(plan.invocation.values("path")).to eq(["../vendor/local_gem"]) + end + + it "keeps the absolute --path when no relative path exists, e.g. across Windows drives" do + allow_any_instance_of(Pathname).to receive(:relative_path_from).and_raise(ArgumentError) # rubocop:disable RSpec/AnyInstance + plan = plan_for(%w[add local_gem --path vendor/local_gem]) + + expect(plan.invocation.values("path")).to eq([root.join("vendor", "local_gem").to_s]) + end + + context "when the gemfile is in the project root" do + let(:gemfile) { root.join("ci.gemfile") } + + it "forwards a relative --path unchanged" do + plan = plan_for(%w[add local_gem --path vendor/local_gem]) + + expect(plan.invocation.values("path")).to eq(["vendor/local_gem"]) + end + + it "forwards relative install paths unchanged", :aggregate_failures do + plan = plan_for(%w[install --binstubs bin --target-rbconfig cfg/rbconfig.rb]) + + expect(plan.invocation.values("binstubs")).to eq(["bin"]) + expect(plan.invocation.values("target-rbconfig")).to eq(["cfg/rbconfig.rb"]) + end + end + end + + context "when a flag takes a path relative to the project root" do + it "rewrites --binstubs so it resolves from the secondary gemfile's directory" do + plan = plan_for(%w[install --binstubs bin]) + + expect(plan.invocation.values("binstubs")).to eq(["../bin"]) + end + + it "rewrites --target-rbconfig the same way", :aggregate_failures do + plan = plan_for(%w[install --target-rbconfig cfg/rbconfig.rb --jobs 2]) + + expect(plan.invocation.values("target-rbconfig")).to eq(["../cfg/rbconfig.rb"]) + expect(plan.invocation.option?("jobs")).to be(true) + end + + it "leaves a --binstubs given without a directory alone" do + plan = plan_for(%w[install --binstubs]) + + expect(plan.invocation.to_argv).to eq(%w[install --binstubs]) + end + end +end diff --git a/spec/bundle_matrix/project_settings_spec.rb b/spec/bundle_matrix/project_settings_spec.rb new file mode 100644 index 0000000..5fc0701 --- /dev/null +++ b/spec/bundle_matrix/project_settings_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::ProjectSettings) do + let(:root) { Pathname(Dir.mktmpdir("bundle_matrix_root")).realpath } + let(:user_env) { {} } + + after { FileUtils.remove_entry(root) } + + # Keeps the developer's own BUNDLE_* variables and global Bundler config out of the spec + # rubocop:disable-next Metrics/AbcSize + def env_with(local_config) + root.join(".bundle").mkpath + root.join(".bundle", "config").write(local_config.map { |key, value| "#{key}: \"#{value}\"\n" }.join) + stub_const("ENV", isolated_env.merge(user_env)) + allow(Bundler).to receive_messages(settings: Bundler::Settings.new(root.join(".bundle")), original_env: user_env) + described_class.env(root) + end + + def isolated_env + ENV.to_h + .reject { |key, _| key == "BUNDLE_CONFIG" || key.start_with?("BUNDLE_") } + .merge("BUNDLE_USER_CONFIG" => root.join("global_config").to_s) + end + + it "passes on the project's .bundle/config" do + env = env_with("BUNDLE_FROZEN" => "true", "BUNDLE_WITHOUT" => "development:test") + + expect(env).to include("BUNDLE_FROZEN" => "true", "BUNDLE_WITHOUT" => "development:test") + end + + # Bundler.settings lists the keys from ENV, and the values the user set come from Bundler.original_env + it "passes on BUNDLE_* variables the user set in the original environment" do + user_env["BUNDLE_JOBS"] = "4" + + expect(env_with({})).to include("BUNDLE_JOBS" => "4") + end + + it "prefers .bundle/config over the user's BUNDLE_* variables" do + user_env["BUNDLE_FROZEN"] = "false" + + expect(env_with("BUNDLE_FROZEN" => "true")).to include("BUNDLE_FROZEN" => "true") + end + + it "expands relative paths from the project root" do + env = env_with("BUNDLE_PATH" => "vendor/bundle", "BUNDLE_BIN" => "/usr/local/bin") + + expect(env).to include("BUNDLE_PATH" => root.join("vendor/bundle").to_s, "BUNDLE_BIN" => "/usr/local/bin") + end + + it "leaves out settings that point at the main Gemfile's own files" do + env = env_with("BUNDLE_GEMFILE" => "Gemfile", "BUNDLE_LOCKFILE" => "Gemfile.next.lock", "BUNDLE_JOBS" => "4") + + expect(env.keys).to eq(["BUNDLE_JOBS"]) + end +end diff --git a/spec/bundle_matrix/runner_spec.rb b/spec/bundle_matrix/runner_spec.rb new file mode 100644 index 0000000..867815d --- /dev/null +++ b/spec/bundle_matrix/runner_spec.rb @@ -0,0 +1,165 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::Runner) do + let(:root) { Pathname("/project") } + let(:ci) { root.join("ci.gemfile") } + let(:rails7) { root.join("gemfiles", "rails7.gemfile") } + let(:runner) { described_class.new(root) } + let(:child_envs) { [] } + let(:broken) { BundleMatrix::Planner::Plan.new(invocation: nil, notes: [], error: "Gemfile syntax error") } + + def plan(argv, notes = [], locked_bundler: nil) + BundleMatrix::Planner::Plan.new( + invocation: argv && BundleMatrix::Invocation.parse(argv), notes: notes, locked_bundler: locked_bundler + ) + end + + before do + allow(Bundler.ui).to receive(:info) + allow(Bundler.ui).to receive(:warn) + allow(Bundler.ui).to receive(:error) + allow(Bundler.settings).to receive(:[]).and_call_original + allow(Bundler.settings).to receive(:[]).with(:version).and_return(nil) + allow(BundleMatrix::ProjectSettings).to receive(:env).and_return({}) + allow(Bundler).to receive(:unbundled_system) { |env, *| child_envs << env }.and_return(true) + end + + it "runs each gemfile through the bundled launcher, from that gemfile's directory" do + runner.run(rails7 => plan(%w[install --jobs 4])) + + env = hash_including("BUNDLE_GEMFILE" => rails7.to_s, "BUNDLE_MATRIX_ACTIVE" => "1") + expect(Bundler).to have_received(:unbundled_system) + .with(env, Gem.ruby, described_class::BUNDLE, "install", "--jobs", "4", chdir: rails7.dirname.to_s) + end + + it "prints a header naming each gemfile and the command run against it" do + runner.run(rails7 => plan(%w[install --jobs 4])) + + expect(Bundler.ui).to have_received(:info) + .with("== bundle_matrix: gemfiles/rails7.gemfile -> bundle install --jobs 4") + end + + it "launches through RubyGems, so each gemfile gets its lockfile's Bundler version" do + expect(File.read(described_class::BUNDLE)).to include('activate_and_load_bin_path("bundler", "bundle"') + end + + it "clears BUNDLER_VERSION so it does not hold every gemfile to one version" do + runner.run(ci => plan(%w[install])) + + expect(child_envs.first).to include("BUNDLER_VERSION" => nil) + end + + it "passes on the project's settings, but sets the gemfile itself" do + allow(BundleMatrix::ProjectSettings).to receive(:env).with(root) + .and_return("BUNDLE_FROZEN" => "true", "BUNDLE_GEMFILE" => root.join("Gemfile").to_s) + + runner.run(ci => plan(%w[install])) + + expect(child_envs.first).to include("BUNDLE_FROZEN" => "true", "BUNDLE_GEMFILE" => ci.to_s) + end + + it "prints a plan's notes and does not run a skipped plan", :aggregate_failures do + runner.run(ci => plan(nil, ["skipped, no rails here"])) + + expect(Bundler.ui).to have_received(:info).with("== bundle_matrix: ci.gemfile: skipped, no rails here") + expect(Bundler).not_to have_received(:unbundled_system) + end + + it "runs every gemfile and then raises naming each that failed", :aggregate_failures do + allow(Bundler).to receive(:unbundled_system).and_return(false, false) + + expect { runner.run(ci => plan(%w[install]), rails7 => plan(%w[install])) } + .to raise_error(BundleMatrix::Error, "bundle_matrix: failed for ci.gemfile, gemfiles/rails7.gemfile") + expect(Bundler).to have_received(:unbundled_system).twice + end + + it "reports a gemfile that could not be planned as an error, and still runs the others", :aggregate_failures do + expect { runner.run(ci => broken, rails7 => plan(%w[install])) } + .to raise_error(BundleMatrix::Error, "bundle_matrix: failed for ci.gemfile") + message = "== bundle_matrix: ci.gemfile: could not plan: Gemfile syntax error" + expect(Bundler.ui).to have_received(:error).with(message) + expect(Bundler.ui).not_to have_received(:info).with(a_string_including("could not plan")) + expect(Bundler).to have_received(:unbundled_system).once + end + + it "stops at a gemfile that could not be planned with fail_fast", :aggregate_failures do + runner = described_class.new(root, fail_fast: true) + allow(Bundler).to receive(:unbundled_system).and_return(true) + + expect { runner.run(ci => broken, rails7 => plan(%w[install])) }.to raise_error(BundleMatrix::Error) + expect(Bundler).not_to have_received(:unbundled_system) + end + + it "stops at the first failure with fail_fast", :aggregate_failures do + runner = described_class.new(root, fail_fast: true) + allow(Bundler).to receive(:unbundled_system).and_return(false) + + expect { runner.run(ci => plan(%w[install]), rails7 => plan(%w[install])) } + .to raise_error(BundleMatrix::Error, "bundle_matrix: failed for ci.gemfile") + expect(Bundler).to have_received(:unbundled_system).once + end + + context "when a gemfile's lockfile names a different Bundler" do + let(:suggestion) do + "bundle_matrix: run `bundle update --bundler` to update Bundler in every lockfile, " \ + "or `bundle config set matrix.bundler_warnings false` to silence these warnings" + end + + def other_bundler_plan(version) + plan(%w[install], locked_bundler: Gem::Version.new(version)) + end + + it "warns with both versions for each such gemfile, then suggests updating once", :aggregate_failures do + runner.run(ci => other_bundler_plan("2.5.23"), rails7 => other_bundler_plan("2.4.22")) + + expect(Bundler.ui).to have_received(:warn) + .with("bundle_matrix: ci.gemfile uses Bundler 2.5.23, the main Gemfile uses #{Bundler::VERSION}") + expect(Bundler.ui).to have_received(:warn) + .with("bundle_matrix: gemfiles/rails7.gemfile uses Bundler 2.4.22, the main Gemfile uses #{Bundler::VERSION}") + expect(Bundler.ui).to have_received(:warn).with(suggestion).once + end + + it "still suggests updating when a gemfile fails", :aggregate_failures do + allow(Bundler).to receive(:unbundled_system).and_return(false) + + expect { runner.run(ci => other_bundler_plan("2.5.23")) }.to raise_error(BundleMatrix::Error) + expect(Bundler.ui).to have_received(:warn).with(suggestion) + end + + it "does not warn when the lockfile names the running Bundler" do + runner.run(ci => plan(%w[install], locked_bundler: Gem::Version.new(Bundler::VERSION))) + + expect(Bundler.ui).not_to have_received(:warn) + end + + it "does not warn or suggest updating for a plan without a lockfile" do + runner.run(ci => plan(%w[install])) + + expect(Bundler.ui).not_to have_received(:warn) + end + + it "does not warn when bundler_warnings is off" do + described_class.new(root, bundler_warnings: false).run(ci => other_bundler_plan("2.5.23")) + + expect(Bundler.ui).not_to have_received(:warn) + end + + it "warns when the version setting is lockfile, as well as when it is unset", :aggregate_failures do + allow(Bundler.settings).to receive(:[]).with(:version).and_return("lockfile") + + runner.run(ci => other_bundler_plan("2.5.23")) + + expect(Bundler.ui).to have_received(:warn).with(a_string_including("uses Bundler 2.5.23")) + end + + it "does not warn when the version setting keeps every gemfile on the main Bundler", :aggregate_failures do + %w[system 4.0.24].each do |setting| + allow(Bundler.settings).to receive(:[]).with(:version).and_return(setting) + + runner.run(ci => other_bundler_plan("2.5.23")) + end + + expect(Bundler.ui).not_to have_received(:warn) + end + end +end diff --git a/spec/bundle_matrix/secondary_gemfile_spec.rb b/spec/bundle_matrix/secondary_gemfile_spec.rb new file mode 100644 index 0000000..df8d61c --- /dev/null +++ b/spec/bundle_matrix/secondary_gemfile_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix::SecondaryGemfile) do + let(:root) { Pathname(Dir.mktmpdir("bundle_matrix_root")).realpath } + let(:gemfile) { root.join("gemfiles", "old.gemfile") } + + after { FileUtils.remove_entry(root) } + + def write(path, contents) + path.dirname.mkpath + path.write(contents) + end + + def lock_for(*gems) + specs = gems.map { |name| " #{name} (1.0.0)" }.join("\n") + deps = gems.map { |name| " #{name}" }.join("\n") + + <<~LOCK + GEM + remote: https://rubygems.org/ + specs: + #{specs} + + PLATFORMS + ruby + + DEPENDENCIES + #{deps} + LOCK + end + + describe "#lockfile" do + it "is .lock for a Gemfile-style gemfile" do + expect(described_class.new(gemfile).lockfile).to eq(root.join("gemfiles", "old.gemfile.lock")) + end + + it "is gems.locked for a gems.rb" do + expect(described_class.new(root.join("engines", "web", "gems.rb")).lockfile) + .to eq(root.join("engines", "web", "gems.locked")) + end + end + + describe "#locked?" do + it "is false until the lockfile exists", :aggregate_failures do + secondary = described_class.new(gemfile) + + expect(secondary).not_to be_locked + write(Pathname("#{gemfile}.lock"), lock_for("rake")) + expect(secondary).to be_locked + end + end + + describe "#bundler_version" do + it "reads the BUNDLED WITH line of the lockfile" do + write(Pathname("#{gemfile}.lock"), "#{lock_for("rake")}\nBUNDLED WITH\n 2.5.23\n") + + expect(described_class.new(gemfile).bundler_version).to eq(Gem::Version.new("2.5.23")) + end + + it "is nil without a lockfile" do + expect(described_class.new(gemfile).bundler_version).to be_nil + end + end + + describe "#lock_names" do + it "lists the gems the lockfile specifies" do + write(Pathname("#{gemfile}.lock"), lock_for("rake", "rack")) + + expect(described_class.new(gemfile).lock_names).to contain_exactly("rake", "rack") + end + end + + describe "declarations" do + it "lists the gems and groups this gemfile declares", :aggregate_failures do + write(gemfile, "gem \"rake\"\ngroup :dev do\n gem \"rack\"\nend\n") + + secondary = described_class.new(gemfile) + + expect(secondary.declared_names).to eq(%w[rake rack]) + expect(secondary.declared_groups).to eq(%w[default dev]) + end + + it "includes the gems pulled in by eval_gemfile" do + write(root.join("Gemfile"), "gem \"rake\"\n") + write(gemfile, "eval_gemfile \"../Gemfile\"\ngem \"rack\"\n") + + expect(described_class.new(gemfile).declared_names).to eq(%w[rake rack]) + end + end +end diff --git a/spec/bundle_matrix_spec.rb b/spec/bundle_matrix_spec.rb new file mode 100644 index 0000000..0febe5e --- /dev/null +++ b/spec/bundle_matrix_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +RSpec.describe(BundleMatrix) do + # The Bundler versions the Appraisals generate gemfiles for, from the file names + let(:tested) do + Dir.glob(File.join(__dir__, "..", "gemfiles", "bundler_*.gemfile")).map do |path| + Gem::Version.new(File.basename(path, ".gemfile").delete_prefix("bundler_")) + end + end + + it "accepts every Bundler the Appraisals test", :aggregate_failures do + expect(tested).not_to be_empty + expect(tested).to all(satisfy { |version| BundleMatrix::TESTED_BUNDLER.satisfied_by?(version) }) + end + + it "excludes the minor after the highest tested one", :aggregate_failures do + highest = tested.max + next_minor = Gem::Version.new("#{highest.segments[0]}.#{highest.segments[1] + 1}.a") + + expect(BundleMatrix::TESTED_BUNDLER.satisfied_by?(highest)).to be(true) + expect(BundleMatrix::TESTED_BUNDLER.satisfied_by?(next_minor)).to be(false) + end + + it "lets the gemspec install under a Bundler newer than the tested range", :aggregate_failures do + gemspec = Gem::Specification.load(File.join(__dir__, "..", "bundle_matrix.gemspec")) + requirement = gemspec.dependencies.find { |dependency| dependency.name == "bundler" }.requirement + + expect(requirement.satisfied_by?(Gem::Version.new("4.2.0"))).to be(true) + expect(requirement.satisfied_by?(Gem::Version.new(BundleMatrix::MIN_BUNDLER))).to be(true) + expect(requirement.satisfied_by?(Gem::Version.new("2.6.8"))).to be(false) + end +end diff --git a/spec/e2e/add_spec.rb b/spec/e2e/add_spec.rb new file mode 100644 index 0000000..c6f82a3 --- /dev/null +++ b/spec/e2e/add_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require_relative "support/e2e_helper" + +RSpec.describe("bundle add", :e2e) do + before do + project.gemfile("Gemfile", %(gem "hermoine", "1.0.0"\n)) + project.gemfile("ci.gemfile", %(gem "hermoine", "1.0.0"\n)) + project.gemfile("gemfiles/rails.gemfile", %(gem "harry"\n)) + project.bundle!("install") + end + + def declarations(gemfile, name) + project.read(gemfile).lines.grep(/^\s*gem "#{name}"/) + end + + # rubocop:disable-next RSpec/ExampleLength + it "adds the gem to every gemfile and locks it", :aggregate_failures do + project.bundle!("add", "ron") + + expect(declarations("Gemfile", "ron").size).to eq(1) + expect(declarations("ci.gemfile", "ron").size).to eq(1) + expect(declarations("gemfiles/rails.gemfile", "ron").size).to eq(1) + expect(project.locked("ci.gemfile")).to include("ron" => "1.2.0") + expect(project.locked("gemfiles/rails.gemfile")).to include("ron" => "1.2.0", "harry" => "2.0.0") + end + + # rubocop:disable-next RSpec/ExampleLength + it "fails naming a secondary gemfile that fails, and keeps the gem in the main Gemfile", :aggregate_failures do + project.gemfile("gemfiles/rails.gemfile", %(gem "harry", "~> 1.0"\n)) + + result = project.bundle("add", "ron", "--version", "1.2.0") + + expect(result).not_to be_success + expect(result.stderr).to include("bundle_matrix: failed for gemfiles/rails.gemfile") + expect(declarations("Gemfile", "ron").size).to eq(1) + expect(project.locked("ci.gemfile")).to include("ron" => "1.2.0") + end + + context "when the run does not fan out" do + let(:secondaries) { %w[ci.gemfile gemfiles/rails.gemfile] } + + def snapshot + secondaries.flat_map { |gemfile| [project.read(gemfile), project.read("#{gemfile}.lock")] } + end + + it "leaves every secondary gemfile untouched when the main add fails", :aggregate_failures do + before = snapshot + + result = project.bundle("add", "missing_gem") + + expect(result).not_to be_success + expect(result.output).not_to include("== bundle_matrix") + expect(snapshot).to eq(before) + end + + # Bundler fires no after-install hook when nothing is installed + it "does nothing with --skip-install", :aggregate_failures do + before = snapshot + + output = project.bundle!("add", "ron", "--skip-install").output + + expect(output).not_to include("== bundle_matrix") + expect(declarations("Gemfile", "ron").size).to eq(1) + expect(snapshot).to eq(before) + end + end +end diff --git a/spec/e2e/gems_rb_spec.rb b/spec/e2e/gems_rb_spec.rb new file mode 100644 index 0000000..7ec6c61 --- /dev/null +++ b/spec/e2e/gems_rb_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative "support/e2e_helper" + +RSpec.describe("bundle install with a gems.rb main file", :e2e) do + before do + project.config("matrix.default_gemfile", "gems.rb") + project.gemfile("gems.rb", %(gem "harry", "~> 1.0.0"\n)) + project.gemfile("ci.gemfile", %(gem "harry", "~> 1.0"\ngem "hermoine", "1.0.0"\n)) + project.gemfile("engines/web/gems.rb", %(gem "ron", "1.0.0"\n)) + project.config("matrix.paths", "engines/*") + end + + it "fans out from gems.rb and locks each file in its own lockfile", :aggregate_failures do + output = project.bundle!("install").output + + expect(output).to include(header("ci.gemfile", "install"), header("engines/web/gems.rb", "install")) + expect(project).to exist("gems.locked") + expect(project.read("ci.gemfile.lock")).to include("hermoine") + expect(project.read("engines/web/gems.locked")).to include("ron") + end +end diff --git a/spec/e2e/install_spec.rb b/spec/e2e/install_spec.rb new file mode 100644 index 0000000..8defdc3 --- /dev/null +++ b/spec/e2e/install_spec.rb @@ -0,0 +1,171 @@ +# frozen_string_literal: true + +require_relative "support/e2e_helper" + +RSpec.describe("bundle install", :e2e) do + before do + project.gemfile("Gemfile", %(gem "harry", "~> 1.0.0"\n)) + project.gemfile("ci.gemfile", %(gem "harry", "~> 1.0"\ngem "hermoine", "1.0.0"\n)) + project.gemfile("gemfiles/rails.gemfile", %(gem "ron", "1.0.0"\n)) + end + + context "when every gemfile resolves" do + it "locks the main Gemfile and every secondary gemfile with its own gems", :aggregate_failures do + project.bundle!("install") + + expect(project.locked("Gemfile")).to include("harry" => "1.0.1") + expect(project.locked("ci.gemfile")).to eq("harry" => "1.1.0", "hermoine" => "1.0.0") + expect(project.locked("gemfiles/rails.gemfile")).to eq("ron" => "1.0.0") + end + + it "treats `bundle i` as install", :aggregate_failures do + output = project.bundle!("i").output + + expect(output).to include(header("ci.gemfile", "install")) + expect(project).to be_lockfile("ci.gemfile") + end + + it "is idempotent" do + project.bundle!("install") + before = %w[Gemfile ci.gemfile gemfiles/rails.gemfile].map { |gemfile| project.read("#{gemfile}.lock") } + + project.bundle!("install") + + expect(%w[Gemfile ci.gemfile gemfiles/rails.gemfile].map { |gemfile| project.read("#{gemfile}.lock") }) + .to eq(before) + end + end + + # Bundler's own default_cli_command decides what a bare `bundle` runs, so these pin the plugin to it + context "when bundle runs without a command" do + it "fans out as install when default_cli_command is not set", :aggregate_failures do + output = project.bundle!.output + + expect(output).to include(header("ci.gemfile", "install")) + expect(project).to be_lockfile("ci.gemfile") + end + + it "fans out as install for switches only, such as --jobs=4", :aggregate_failures do + output = project.bundle!("--jobs=4").output + + expect(output).to include(header("ci.gemfile", "install --jobs=4")) + expect(project).to be_lockfile("ci.gemfile") + end + + it "fans out when default_cli_command is install" do + project.config("default_cli_command", "install") + + project.bundle! + + expect(project).to be_lockfile("ci.gemfile") + end + + it "does not fan out when default_cli_command is cli_help", :aggregate_failures do + project.config("default_cli_command", "cli_help") + + project.bundle! + + expect(project).not_to be_lockfile("ci.gemfile") + expect(project).not_to be_lockfile("gemfiles/rails.gemfile") + end + end + + it "also installs each Gemfile in the directories set by matrix.paths", :aggregate_failures do + project.gemfile("engines/admin/Gemfile", %(gem "hermoine", "1.0.0"\n)) + project.config("matrix.paths", "engines/*") + + output = project.bundle!("install").output + + expect(output).to include(header("engines/admin/Gemfile", "install")) + expect(project.locked("engines/admin/Gemfile")).to eq("hermoine" => "1.0.0") + end + + # Each child runs from its own gemfile's directory, so Bundler would otherwise resolve these there + context "with project settings" do + it "resolves a relative path from the project root for every gemfile", :aggregate_failures do + project.bundle!("install") + + expect(Dir.glob(project.root.join("vendor/bundle/ruby/*/gems/ron-1.0.0"))).not_to be_empty + expect(project).not_to exist("gemfiles/vendor") + end + + it "applies without to every gemfile", :aggregate_failures do + project.gemfile("ci.gemfile", %(gem "harry", "~> 1.0"\ngroup :test do\n gem "hermoine", "1.0.0"\nend\n)) + project.config("without", "test") + + project.bundle!("install") + + expect(project.locked("ci.gemfile")).to include("hermoine" => "1.0.0") + expect(Dir.glob(project.root.join("vendor/bundle/ruby/*/gems/hermoine-*"))).to be_empty + end + end + + # rubocop:disable-next RSpec/ExampleLength + it "runs the other gemfiles when a secondary gemfile fails, then fails naming it", :aggregate_failures do + project.gemfile("ci.gemfile", %(gem "harry", "~> 9.0"\n)) + + result = project.bundle("install") + + expect(result).not_to be_success + expect(result.stderr).to include("bundle_matrix: failed for ci.gemfile") + expect(project).to be_lockfile("Gemfile") + expect(project).to be_lockfile("gemfiles/rails.gemfile") + end + + # rubocop:disable-next RSpec/ExampleLength + it "fails an out of date frozen secondary gemfile, and still runs the others", :aggregate_failures do + project.bundle!("install") + locked_before = project.read("ci.gemfile.lock") + project.gemfile("ci.gemfile", %(gem "harry", "~> 1.0"\ngem "hermoine", "1.0.0"\ngem "ron", "1.0.0"\n)) + project.config("frozen", true) + + result = project.bundle("install") + + expect(result).not_to be_success + expect(result.stderr).to include("bundle_matrix: failed for ci.gemfile") + expect(result.output).to include(header("gemfiles/rails.gemfile", "install")) + expect(project.read("ci.gemfile.lock")).to eq(locked_before) + end + + context "when the run does not fan out" do + it "does nothing when the main install fails", :aggregate_failures do + project.gemfile("Gemfile", %(gem "harry", "~> 9.0"\n)) + + result = project.bundle("install") + + expect(result).not_to be_success + expect(result.output).not_to include("== bundle_matrix") + expect(project).not_to be_lockfile("ci.gemfile") + end + + it "does nothing when disabled", :aggregate_failures do + project.config("matrix.disabled", true) + + output = project.bundle!("install").output + + expect(output).not_to include("== bundle_matrix") + expect(project).not_to be_lockfile("ci.gemfile") + end + + it "does nothing when BUNDLE_MATRIX__DISABLED is set", :aggregate_failures do + output = project.bundle!("install", env: { "BUNDLE_MATRIX__DISABLED" => "1" }).output + + expect(output).not_to include("== bundle_matrix") + expect(project).not_to be_lockfile("ci.gemfile") + end + + # rubocop:disable-next RSpec/ExampleLength + it "does nothing when BUNDLE_GEMFILE names a secondary gemfile", :aggregate_failures do + project.bundle!("install") + project.gemfile("gemfiles/rack.gemfile", %(gem "harry"\n)) + # Control run: the same command without BUNDLE_GEMFILE does fan out to rack.gemfile + expect(project.bundle!("install").output).to include(header("gemfiles/rack.gemfile", "install")) + project.root.join("gemfiles/rack.gemfile.lock").delete + + output = project.bundle!("install", env: { "BUNDLE_GEMFILE" => project.root.join("ci.gemfile").to_s }).output + + expect(output).not_to include("== bundle_matrix") + expect(project).not_to be_lockfile("gemfiles/rack.gemfile") + end + end +end diff --git a/spec/e2e/support/e2e_helper.rb b/spec/e2e/support/e2e_helper.rb new file mode 100644 index 0000000..6d708ad --- /dev/null +++ b/spec/e2e/support/e2e_helper.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require_relative "gem_repo" +require_relative "project" + +module E2E + # Shared setup for the E2E specs: a fresh project per example. + module Helpers + def project + @project ||= Project.new + end + + # The line Runner prints before running a command against a secondary gemfile + def header(gemfile, command) + "== bundle_matrix: #{gemfile} -> bundle #{command}" + end + end +end + +RSpec.configure do |config| + config.include(E2E::Helpers, :e2e) + config.after(:each, :e2e) { @project&.cleanup } + config.after(:suite) { E2E::GemRepo.cleanup } +end diff --git a/spec/e2e/support/gem_repo.rb b/spec/e2e/support/gem_repo.rb new file mode 100644 index 0000000..13a0862 --- /dev/null +++ b/spec/e2e/support/gem_repo.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require "rubygems/indexer" +require "rubygems/package" + +module E2E + # A local gem server on disk, so the suite resolves real versions without the network. + module GemRepo + # name => { version => runtime dependencies } + GEMS = { + "harry" => { "1.0.0" => {}, "1.0.1" => {}, "1.1.0" => {}, "2.0.0" => {} }, + "hermoine" => { "1.0.0" => {}, "1.1.0" => { "harry" => ">= 1.1" } }, + "ron" => { "1.0.0" => {}, "1.2.0" => { "harry" => ">= 2.0" } } + }.freeze + + def self.uri + "file://#{dir}" + end + + def self.dir + @dir ||= build + end + + def self.cleanup + FileUtils.rm_rf(@dir) if @dir + @dir = nil + end + + def self.build + dir = Dir.mktmpdir("bundle_matrix-gems") + gems_dir = File.join(dir, "gems") + FileUtils.mkdir_p(gems_dir) + + Gem::DefaultUserInteraction.use_ui(Gem::SilentUI.new) do + Dir.chdir(gems_dir) do + GEMS.each do |name, versions| + versions.each { |version, deps| Gem::Package.build(spec(name, version, deps)) } + end + end + Gem::Indexer.new(dir).generate_index + end + dir + end + + def self.spec(name, version, deps) + Gem::Specification.new do |s| + s.name = name + s.version = version + s.summary = "Fixture gem for the bundle_matrix E2E suite" + s.authors = ["bundle_matrix"] + s.homepage = "https://example.com/#{name}" + s.license = "MIT" + s.required_ruby_version = ">= 3.2" + s.files = [] + deps.each { |dep, requirement| s.add_dependency(dep, requirement) } + end + end + private_class_method :build, :spec + end +end diff --git a/spec/e2e/support/project.rb b/spec/e2e/support/project.rb new file mode 100644 index 0000000..68bbd77 --- /dev/null +++ b/spec/e2e/support/project.rb @@ -0,0 +1,118 @@ +# frozen_string_literal: true + +require "open3" +require "yaml" + +module E2E + # A throwaway project with the plugin declared in its main Gemfile. Commands run + # in a real `bundle` process, isolated from the developer's own Bundler setup. + class Project + PLUGIN_ROOT = File.expand_path("../../..", __dir__) + + Result = Data.define(:stdout, :stderr, :status) do + def success? + status.success? + end + + def output + stdout + stderr + end + end + + attr_reader :root + + def initialize + @root = Pathname(Dir.mktmpdir("bundle_matrix-project")).realpath + @home = Pathname(Dir.mktmpdir("bundle_matrix-home")) + # Every gemfile installs into the project's own vendor/bundle, never the developer's gems + config("path", "vendor/bundle") + end + + def cleanup + FileUtils.rm_rf([@root, @home]) + end + + # Writes a gemfile using the fixture gem repo. The main Gemfile also declares the plugin. + def gemfile(path, body = "") + plugin = %w[Gemfile gems.rb].include?(path) ? %(plugin "bundle_matrix", path: "#{PLUGIN_ROOT}"\n) : "" + write(path, %(source "#{GemRepo.uri}"\n#{plugin}#{body})) + end + + # A gem on disk, for `path:` sources + def path_gem(dir, name, version = "1.0.0") + write(File.join(dir, "#{name}.gemspec"), <<~RUBY) + Gem::Specification.new do |s| + s.name = #{name.inspect} + s.version = #{version.inspect} + s.summary = "Path fixture gem" + s.authors = ["bundle_matrix"] + s.files = [] + end + RUBY + end + + def write(path, content) + file = @root.join(path) + file.dirname.mkpath + file.write(content) + end + + def read(path) + @root.join(path).read + end + + def exist?(path) + @root.join(path).exist? + end + + # Sets a value in .bundle/config, in the project root or next to a secondary gemfile + def config(key, value, dir: ".") + file = @root.join(dir, ".bundle", "config") + settings = file.file? ? YAML.safe_load(file.read) : {} + settings["BUNDLE_#{key.upcase.gsub(".", "__")}"] = value.to_s + file.dirname.mkpath + file.write(YAML.dump(settings)) + end + + # @return [Hash{String => String}] gem name => locked version, empty without a lockfile + def locked(gemfile) + lockfile = @root.join("#{gemfile}.lock") + return {} unless lockfile.file? + + Bundler::LockfileParser.new(lockfile.read).specs.to_h { |spec| [spec.name, spec.version.to_s] } + end + + def lockfile?(gemfile) + @root.join("#{gemfile}.lock").file? + end + + def bundle(*args, env: {}) + Bundler.with_unbundled_env do + stdout, stderr, status = Open3.capture3( + isolated_env.merge(env), Gem.ruby, BundleMatrix::Runner::BUNDLE, *args, chdir: @root.to_s + ) + Result.new(stdout: stdout, stderr: stderr, status: status) + end + end + + # Like #bundle, but fails the example with Bundler's output when the command fails + def bundle!(*args, env: {}) + bundle(*args, env: env).tap do |result| + raise "bundle #{args.join(" ")} failed:\n#{result.output}" unless result.success? + end + end + + private + + def isolated_env + { + "HOME" => @home.to_s, + "BUNDLE_USER_HOME" => @home.join(".bundle").to_s, + "BUNDLE_GEMFILE" => nil, + "BUNDLE_APP_CONFIG" => nil, + "BUNDLER_VERSION" => nil, + "BUNDLE_MATRIX_ACTIVE" => nil + } + end + end +end diff --git a/spec/e2e/update_spec.rb b/spec/e2e/update_spec.rb new file mode 100644 index 0000000..3390335 --- /dev/null +++ b/spec/e2e/update_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require_relative "support/e2e_helper" + +RSpec.describe("bundle update", :e2e) do + # Locks every gem at 1.0.0, then loosens the requirements so there is something to update + before do + write_gemfiles("1.0.0") + project.bundle!("install") + write_gemfiles(">= 1.0") + end + + def write_gemfiles(requirement) + project.gemfile("Gemfile", %(gem "harry", "#{requirement}"\n)) + project.gemfile("ci.gemfile", %(gem "harry", "#{requirement}"\ngem "hermoine", "#{requirement}"\n)) + project.gemfile("gemfiles/rails.gemfile", %(gem "ron", "#{requirement}"\n)) + end + + it "updates a named gem in every lockfile that has it, and skips the rest", :aggregate_failures do + output = project.bundle!("update", "harry").output + + expect(output).to include("gemfiles/rails.gemfile: skipped, none of harry are in this gemfile") + expect(project.locked("Gemfile")).to include("harry" => "2.0.0") + expect(project.locked("ci.gemfile")).to eq("harry" => "2.0.0", "hermoine" => "1.0.0") + expect(project.locked("gemfiles/rails.gemfile")).to eq("ron" => "1.0.0") + end + + it "updates every gem in every gemfile with --all", :aggregate_failures do + project.bundle!("update", "--all") + + expect(project.locked("ci.gemfile")).to eq("harry" => "2.0.0", "hermoine" => "1.1.0") + expect(project.locked("gemfiles/rails.gemfile")).to eq("harry" => "2.0.0", "ron" => "1.2.0") + end + + # A gemfile's own .bundle/config wins over the project's, so only this gemfile is frozen + # rubocop:disable-next RSpec/ExampleLength + it "updates the other gemfiles when a secondary gemfile fails, then fails naming it", :aggregate_failures do + project.gemfile("engines/admin/Gemfile", %(gem "harry", "1.0.0"\n)) + project.config("matrix.paths", "engines/*") + project.bundle!("install") + project.gemfile("engines/admin/Gemfile", %(gem "harry"\n)) + project.config("frozen", true, dir: "engines/admin") + + result = project.bundle("update", "--all") + + expect(result).not_to be_success + expect(result.stderr).to include("bundle_matrix: failed for engines/admin/Gemfile") + expect(project.locked("Gemfile")).to include("harry" => "2.0.0") + expect(project.locked("gemfiles/rails.gemfile")).to include("ron" => "1.2.0") + end + + it "does nothing when the main update fails", :aggregate_failures do + before = project.read("ci.gemfile.lock") + + result = project.bundle("update", "nonexistent") + + expect(result).not_to be_success + expect(result.output).not_to include("== bundle_matrix") + expect(project.read("ci.gemfile.lock")).to eq(before) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d0b23e9 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +if ENV["COVERAGE"] == "true" + require "simplecov" + + SimpleCov.start do + enable_coverage :branch # track branches as well as lines + track_files "lib/**/*.rb" # report on these files, even if never loaded + add_filter "/spec/" + minimum_coverage line: 90, branch: 80 # SimpleCov 0.22 spelling of coverage :line / :branch minimums + maximum_coverage_drop 1 # fail if coverage drops more than 1 point from last run + end +end + +require "bundle_matrix" +require "fileutils" +require "tmpdir" + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with(:rspec) do |c| + c.syntax = :expect + end + + # Stubs must name methods that exist, and examples must not depend on the order they run in + config.mock_with(:rspec) { |mocks| mocks.verify_partial_doubles = true } + config.order = :random + Kernel.srand(config.seed) +end