<commit_before># frozen_string_literal: true

RSpec.describe "bundle binstubs <gem>" do
  context "when the gem exists in the lockfile" do
    it "sets up the binstub" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
      G

      bundle "binstubs rack"

      expect(bundled_app("bin/rackup")).to exist
    end

    it "does not install other binstubs" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
        gem "rails"
      G

      bundle "binstubs rails"

      expect(bundled_app("bin/rackup")).not_to exist
      expect(bundled_app("bin/rails")).to exist
    end

    it "does install multiple binstubs" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
        gem "rails"
      G

      bundle "binstubs rails rack"

      expect(bundled_app("bin/rackup")).to exist
      expect(bundled_app("bin/rails")).to exist
    end

    it "displays an error when used without any gem" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
      G

      bundle "binstubs"
      expect(exitstatus).to eq(1) if exitstatus
      expect(out).to include("`bundle binstubs` needs at least one gem to run.")
    end

    context "when generating bundle binstub outside bundler" do
      it "should abort" do
        install_gemfile <<-G
          source "file://#{gem_repo1}"
          gem "rack"
        G

        bundle "binstubs rack"

        File.open("bin/bundle", "wb") do |file|
          file.print "OMG"
        end

        sys_exec "bin/rackup"

        expect(last_command.stderr).to include("was not generated by Bundler")
      end
    end

    context "the bundle binstub" do
      before do
        if system_bundler_version == :bundler
          system_gems :bundler
        elsif system_bundler_version
          build_repo4 do
            build_gem "bundler", system_bundler_version do |s|
              s.executables = "bundle"
              s.bindir = "exe"
              s.write "exe/bundle", "puts %(system bundler #{system_bundler_version}\\n\#{ARGV.inspect})"
            end
          end
          system_gems "bundler-#{system_bundler_version}", :gem_repo => gem_repo4
        end
        build_repo2 do
          build_gem "prints_loaded_gems", "1.0" do |s|
            s.executables = "print_loaded_gems"
            s.write "bin/print_loaded_gems", <<-R
              specs = Gem.loaded_specs.values.reject {|s| Bundler.rubygems.spec_default_gem?(s) }
              puts specs.map(&:full_name).sort.inspect
            R
          end
        end
        install_gemfile! <<-G
          source "file://#{gem_repo2}"
          gem "rack"
          gem "prints_loaded_gems"
        G
        bundle! "binstubs bundler rack prints_loaded_gems"
      end

      let(:system_bundler_version) { Bundler::VERSION }

      it "runs bundler" do
        sys_exec! "#{bundled_app("bin/bundle")} install"<commit_msg>To use exe instead of default name. Because bin name is conflict with Ruby 2.5 or later<commit_after># frozen_string_literal: true

RSpec.describe "bundle binstubs <gem>" do
  context "when the gem exists in the lockfile" do
    it "sets up the binstub" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
      G

      bundle "binstubs rack"

      expect(bundled_app("bin/rackup")).to exist
    end

    it "does not install other binstubs" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
        gem "rails"
      G

      bundle "binstubs rails"

      expect(bundled_app("bin/rackup")).not_to exist
      expect(bundled_app("bin/rails")).to exist
    end

    it "does install multiple binstubs" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
        gem "rails"
      G

      bundle "binstubs rails rack"

      expect(bundled_app("bin/rackup")).to exist
      expect(bundled_app("bin/rails")).to exist
    end

    it "displays an error when used without any gem" do
      install_gemfile <<-G
        source "file://#{gem_repo1}"
        gem "rack"
      G

      bundle "binstubs"
      expect(exitstatus).to eq(1) if exitstatus
      expect(out).to include("`bundle binstubs` needs at least one gem to run.")
    end

    context "when generating bundle binstub outside bundler" do
      it "should abort" do
        install_gemfile <<-G
          source "file://#{gem_repo1}"
          gem "rack"
        G

        bundle "binstubs rack"

        File.open("bin/bundle", "wb") do |file|
          file.print "OMG"
        end

        sys_exec "bin/rackup"

        expect(last_command.stderr).to include("was not generated by Bundler")
      end
    end

    context "the bundle binstub" do
      before do
        if system_bundler_version == :bundler
          system_gems :bundler
        elsif system_bundler_version
          build_repo4 do
            build_gem "bundler", system_bundler_version do |s|
              s.executables = "bundle"
              s.bindir = "exe"
              s.write "exe/bundle", "puts %(system bundler #{system_bundler_version}\\n\#{ARGV.inspect})"
            end
          end
          system_gems "bundler-#{system_bundler_version}", :gem_repo => gem_repo4
        end
        build_repo2 do
          build_gem "prints_loaded_gems", "1.0" do |s|
            s.executables = "print_loaded_gems"
            s.bindir = "exe"
            s.write "exe/print_loaded_gems", <<-R
              specs = Gem.loaded_specs.values.reject {|s| Bundler.rubygems.spec_default_gem?(s) }
              puts specs.map(&:full_name).sort.inspect
            R
          end
        end
        install_gemfile! <<-G
          source "file://#{gem_repo2}"
          gem "rack"
          gem "prints_loaded_gems"
        G
        bundle! "binstubs bundler rack prints_loaded_gems"
      end

      let(:system_bundler_version) { Bundler::VERSION }

      it "runs bundler" do
        sys_exec! "#{bundled_app("bin/bundle")} install"
{'fraction_non_alphanumeric': 0.10375020650916901, 'fraction_numerical': 0.0042953907153477616, 'mean_word_length': 2.028514257128564, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2989296', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1888, 'n_words': 497}