#!/usr/bin/env ruby
# Run the plugin's tests inside the Redmine 7 test image. Pass a test file to run just that one.
#   bin/test                                   all unit and integration tests
#   bin/test test/unit/errors_report_test.rb
Dir.chdir(File.expand_path("..", __dir__))

target = ARGV.first
runner = if target
  "bundle exec ruby -Itest plugins/redmine_rubylode_glitchtip/#{target}"
else
  "bundle exec rake redmine:plugins:test NAME=redmine_rubylode_glitchtip"
end

inside = <<~SH
  cd /usr/src/redmine &&
  printf 'test:\\n  adapter: sqlite3\\n  database: /tmp/redmine_test.sqlite3\\n' > config/database.yml &&
  bundle exec rake generate_secret_token db:migrate redmine:plugins:migrate >/dev/null &&
  #{runner}
SH

# The Status plugin, when its checkout sits next to this one, is mounted read-only beside this plugin, so the tests also
# run against the real registry. CI and a machine without that checkout run the tests without it.
status_checkout = File.expand_path("../redmine-rubylode-status", Dir.pwd)
mount = if File.directory?(status_checkout) && ENV["WITHOUT_STATUS_PLUGIN"].to_s.empty?
  ["--volume", "#{status_checkout}:/usr/src/redmine/plugins/redmine_rubylode_status:ro"]
else
  []
end

exec "docker", "compose", "--profile", "test", "run", "--rm", *mount, "test", "sh", "-c", inside
