<commit_before>source_root = File.expand_path("../..", File.dirname(__FILE__))
$LOAD_PATH.unshift("#{source_root}/src/ruby_supportlib")
require 'phusion_passenger'
PhusionPassenger.locate_directories
PhusionPassenger.require_passenger_lib 'constants'
PhusionPassenger.require_passenger_lib 'platform_info/binary_compatibility'
require 'tmpdir'
require 'fileutils'
require 'webrick'
require 'thread'
require 'open-uri'

ENV['PATH'] = "#{PhusionPassenger.bin_dir}:#{ENV['PATH']}"
# This environment variable changes Passenger Standalone's behavior,
# so ensure that it's not set.
ENV.delete('PASSENGER_DEBUG')
ENV['PASSENGER_DOWNLOAD_NATIVE_SUPPORT_BINARY'] = '0'
ENV['PASSENGER_COMPILE_NATIVE_SUPPORT_BINARY']  = '0'

module PhusionPassenger

describe "Passenger Standalone" do
  let(:version) { PhusionPassenger::VERSION_STRING }
  let(:nginx_version) { PhusionPassenger::PREFERRED_NGINX_VERSION }
  let(:compat_id) { PhusionPassenger::PlatformInfo.cxx_binary_compatibility_id }

  def sh(*command)
    if !system(*command)
      abort "Command failed: #{command.join(' ')}"
    end
  end

  def capture_output(command)
    output = `#{command} 2>&1`.strip
    if $?.exitstatus == 0
      return output
    else
      abort "Command #{command} exited with status #{$?.exitstatus}; output:\n#{output}"
    end
  end

  def start_server(document_root)
    server = WEBrick::HTTPServer.new(:BindAddress => '127.0.0.1',
      :Port => 0,
      :DocumentRoot => document_root,
      :Logger => WEBrick::Log.new("/dev/null"),
      :AccessLog => [])
    Thread.new do
      Thread.current.abort_on_exception = true
      server.start
    end
    [server, "http://127.0.0.1:#{server.config[:Port]}"]
  end

  def create_tarball(filename, contents = nil)
    filename = File.expand_path(filename)
    Dir.mktmpdir("tarball-") do |tarball_dir|
      Dir.chdir(tarball_dir) do
        if block_given?
          yield
        else
          contents.each do |content_name|
            create_file(content_name)
          end
        end
        sh "tar", "-czf", filename, "."
      end
    end
  end

  def create_dummy_support_binaries
    Dir.mkdir("support-binaries") if !File.exist?("support-binaries")
    File.open("support-binaries/#{AGENT_EXE}", "w") do |f|
      f.puts "#!/bin/bash"
      f.puts "echo PASS"
    end
    File.chmod(0755, "support-binaries/#{AGENT_EXE}")
  end

  def create_dummy_nginx_binary
    File.open("PassengerWebHelper", "w") do |f|
      f.puts "#!/bin/bash"
      f.puts "echo nginx version: 1.0.0"
    end
    File.chmod(0755, "PassengerWebHelper")
  end

  def create_file(filename, contents = nil)
    File.open(filename, "wb") do |f|
      f.write(contents) if contents
    end
  end

  specify "invoking 'passenger' without an argument is equivalent to 'passenger help'" do
    output = capture_output("passenger")
    output.should == capture_output("passenger help")
  end

  specify "'passenger --help' is equivalent to 'passenger help'" do
    output = capture_output("passenger")
    output.should == capture_output("passenger help")
  end

  specify "'passenger --version' displays the version number" do
    output = capture_output("passenger --version")
    output.should include(" #{PhusionPassenger::VERSION_STRING}\n")
  end

  describe "start command" do
    AGENT_BINARY_DOWNLOAD_MESSAGE = "--> Downloading a #{PROGRAM_NAME} agent binary for your platform"
    AGENT_BINARY_COMPILE_MESSAGE  = "Compiling #{PROGRAM_NAME} agent"
    NGINX_BINARY_INSTALL_MESSAGE  = "Installing Nginx"

    def test_serving_application(passenger_command)
      Dir.mktmpdir do |tmpdir|
        Dir.chdir(tmpdir) do
          File.open("config.ru", "w") do |f|
            f.write(%Q{
              app = lambda do |env|<commit_msg>Fix Passenger Standalone version test<commit_after>source_root = File.expand_path("../..", File.dirname(__FILE__))
$LOAD_PATH.unshift("#{source_root}/src/ruby_supportlib")
require 'phusion_passenger'
PhusionPassenger.locate_directories
PhusionPassenger.require_passenger_lib 'constants'
PhusionPassenger.require_passenger_lib 'platform_info/binary_compatibility'
require 'tmpdir'
require 'fileutils'
require 'webrick'
require 'thread'
require 'open-uri'

ENV['PATH'] = "#{PhusionPassenger.bin_dir}:#{ENV['PATH']}"
# This environment variable changes Passenger Standalone's behavior,
# so ensure that it's not set.
ENV.delete('PASSENGER_DEBUG')
ENV['PASSENGER_DOWNLOAD_NATIVE_SUPPORT_BINARY'] = '0'
ENV['PASSENGER_COMPILE_NATIVE_SUPPORT_BINARY']  = '0'

module PhusionPassenger

describe "Passenger Standalone" do
  let(:version) { PhusionPassenger::VERSION_STRING }
  let(:nginx_version) { PhusionPassenger::PREFERRED_NGINX_VERSION }
  let(:compat_id) { PhusionPassenger::PlatformInfo.cxx_binary_compatibility_id }

  def sh(*command)
    if !system(*command)
      abort "Command failed: #{command.join(' ')}"
    end
  end

  def capture_output(command)
    output = `#{command} 2>&1`.strip
    if $?.exitstatus == 0
      return output
    else
      abort "Command #{command} exited with status #{$?.exitstatus}; output:\n#{output}"
    end
  end

  def start_server(document_root)
    server = WEBrick::HTTPServer.new(:BindAddress => '127.0.0.1',
      :Port => 0,
      :DocumentRoot => document_root,
      :Logger => WEBrick::Log.new("/dev/null"),
      :AccessLog => [])
    Thread.new do
      Thread.current.abort_on_exception = true
      server.start
    end
    [server, "http://127.0.0.1:#{server.config[:Port]}"]
  end

  def create_tarball(filename, contents = nil)
    filename = File.expand_path(filename)
    Dir.mktmpdir("tarball-") do |tarball_dir|
      Dir.chdir(tarball_dir) do
        if block_given?
          yield
        else
          contents.each do |content_name|
            create_file(content_name)
          end
        end
        sh "tar", "-czf", filename, "."
      end
    end
  end

  def create_dummy_support_binaries
    Dir.mkdir("support-binaries") if !File.exist?("support-binaries")
    File.open("support-binaries/#{AGENT_EXE}", "w") do |f|
      f.puts "#!/bin/bash"
      f.puts "echo PASS"
    end
    File.chmod(0755, "support-binaries/#{AGENT_EXE}")
  end

  def create_dummy_nginx_binary
    File.open("PassengerWebHelper", "w") do |f|
      f.puts "#!/bin/bash"
      f.puts "echo nginx version: 1.0.0"
    end
    File.chmod(0755, "PassengerWebHelper")
  end

  def create_file(filename, contents = nil)
    File.open(filename, "wb") do |f|
      f.write(contents) if contents
    end
  end

  specify "invoking 'passenger' without an argument is equivalent to 'passenger help'" do
    output = capture_output("passenger")
    output.should == capture_output("passenger help")
  end

  specify "'passenger --help' is equivalent to 'passenger help'" do
    output = capture_output("passenger")
    output.should == capture_output("passenger help")
  end

  specify "'passenger --version' displays the version number" do
    output = capture_output("passenger --version")
    output.should =~ /#{Regexp.escape PhusionPassenger::VERSION_STRING}$/
  end

  describe "start command" do
    AGENT_BINARY_DOWNLOAD_MESSAGE = "--> Downloading a #{PROGRAM_NAME} agent binary for your platform"
    AGENT_BINARY_COMPILE_MESSAGE  = "Compiling #{PROGRAM_NAME} agent"
    NGINX_BINARY_INSTALL_MESSAGE  = "Installing Nginx"

    def test_serving_application(passenger_command)
      Dir.mktmpdir do |tmpdir|
        Dir.chdir(tmpdir) do
          File.open("config.ru", "w") do |f|
            f.write(%Q{
              app = lambda do |env|
{'fraction_non_alphanumeric': 0.12586206896551724, 'fraction_numerical': 0.007692307692307693, 'mean_word_length': 3.666460396039604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3509204', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2474, 'n_words': 533}