#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

core_spec_path = File.expand_path("../spec", __dir__)
plugin_spec_requested =
  ARGV.any? do |pattern|
    next false if pattern.start_with?("-")
    next true if pattern.include?("plugins/")
    next false if !ENV.key?("DISCOURSE_REPO_BASE_DIRECTORY")

    requested_path = File.expand_path(pattern.sub(/:\d+(?::\d+)?\z/, ""))
    File.exist?(requested_path) && requested_path != core_spec_path &&
      !requested_path.start_with?("#{core_spec_path}/")
  end

if ENV["LOAD_PLUGINS"].nil? && plugin_spec_requested
  STDERR.puts "Detected plugin spec path, setting LOAD_PLUGINS to 1"
  ENV["LOAD_PLUGINS"] = "1"
end

ENV["PLAYWRIGHT_HEADLESS"] = "0" if ARGV.delete("--headful")
ENV["CAPYBARA_PLAYWRIGHT_DEBUG_CLIENT_SETTLED"] = "1" if ARGV.delete("--debug-client-settled")

if ARGV.any? { |arg| arg.include?("RspecPerformanceFormatter") }
  ENV["DISCOURSE_RSPEC_PERFORMANCE_FORMATTER"] = "1"
end

load Gem.bin_path("rspec-core", "rspec")
