<commit_before>#!/usr/bin/env ruby
# Run automated tests in Max
# Copyright (c) 2014, Cycling '74

puts "Max Automated Test Runner"

olddir = Dir.getwd

require 'rubygems'
gemlist = `gem list`.strip
if !/^sqlite3/.match gemlist
  puts
  puts "-> You are missing the 'sqlite3' rubygem, which you need for automated testing."
  puts "-> Please type 'gem install sqlite3' in the Terminal."
  puts
  exit
end
require 'fileutils'
require 'pathname'
require 'sqlite3'
require "#{olddir}/rosc/lib/osc"


###################################################################
# argument processing
###################################################################

if (ARGV.length < 1 || ARGV.length > 2)
  puts "usage: ruby test.rb <path-to-max> <no-exit>"
  puts "examples:"
  puts '  ruby test.rb "/Applications"'
  puts '  ruby test.rb "/Applications/Max 6.1"'
  puts '  ruby test.rb "C:\Program Files\Cycling \'74\Max 6.1"'
  puts
  exit;
end

@maxfolder = ARGV[0]
@noexit = false
@noexit = true if ARGV.length > 1

###################################################################
# initialization
###################################################################

@host = 'localhost'
@receivePort = 4792 # This must match the port-send number in testpackage-config.json
@sendPort = 4791    # This must match the port-listen number in testpackage-config.json
@passes = 0
@failures = 0

puts "  Starting the OSC Server..."
puts
@oscReceiver = OSC::UDPServer.new
@oscReceiver.bind @host, @receivePort
@oscSender = OSC::UDPSocket.new
@testdbPath = ""
@starttime = Time.now.iso8601.chop.chop.chop.chop.chop.chop


###################################################################
# sub routines
###################################################################

def establishCommunication
  @pingReturned = 0

  @oscReceiver.add_method('/testdb/path', 's') do |msg|
    @testdbPath = msg.args[0]
  end

  @oscReceiver.add_method('/ping/return', '') do |msg|
    puts "    Ping successfully returned."
    puts ""
    
    @oscSender.send(OSC::Message.new('/testdb/path?'), 0, @host, @sendPort)
    sleep 1 
    @pingReturned = 1
  end

  Thread.new do
    @oscReceiver.serve
  end
  sleep 5

  ping = OSC::Message.new('/ping');
  while @pingReturned == 0
    puts "    Sending ping to Max."
    @oscSender.send(ping, 0, @host, @sendPort)
    sleep 2
  end
end


def waitOnDatabase
  @dbReady = 0

  @oscReceiver.add_method('/db/ready', '') do |msg|
    @dbReady = 1
  end

  Thread.new do
    @oscReceiver.serve
  end
  sleep 10

  ping = OSC::Message.new('/db/ready?');
  while @dbReady == 0
    puts "    Sending query to Max."
    @oscSender.send(ping, 0, @host, @sendPort)
    sleep 10
  end
end


def waitForTestCompletion
  @testCompleted = 0

  @oscReceiver.add_method('/test/complete', '') do |msg|
    @testCompleted = 1
  end

  Thread.new do
    @oscReceiver.serve
  end

  while @testCompleted == 0
    sleep 1
  end
end


def waitForAllTestCompletion
  @testCompleted = 0

  @oscReceiver.add_method('/test/all/complete', '') do |msg|
    @testCompleted = 1
  end

  Thread.new do
    @oscReceiver.serve
  end

  while @testCompleted == 0
    sleep 1
  end
end


def launchMax
  if RUBY_PLATFORM.match(/darwin/)
    `open "#{@maxfolder}/Max.app/Contents/MacOS/Max"`
  else
    IO.popen("#{@maxfolder}/Max.exe")
  end
end


###################################################################<commit_msg>add possibility to give app name directly (in case it was renamed)<commit_after>#!/usr/bin/env ruby
# Run automated tests in Max
# Copyright (c) 2014, Cycling '74

puts "Max Automated Test Runner"

olddir = Dir.getwd

require 'rubygems'
gemlist = `gem list`.strip
if !/^sqlite3/.match gemlist
  puts
  puts "-> You are missing the 'sqlite3' rubygem, which you need for automated testing."
  puts "-> Please type 'gem install sqlite3' in the Terminal."
  puts
  exit
end
require 'fileutils'
require 'pathname'
require 'sqlite3'
require "#{olddir}/rosc/lib/osc"


###################################################################
# argument processing
###################################################################

if (ARGV.length < 1 || ARGV.length > 2)
  puts "usage: ruby test.rb <path-to-max> <no-exit>"
  puts "examples:"
  puts '  ruby test.rb "/Applications"'
  puts '  ruby test.rb "/Applications/Max 6.1"'
  puts '  ruby test.rb "/Applications/Max7.app"  --- you can give the path to the max application directly'
  puts '  ruby test.rb "C:\Program Files\Cycling \'74\Max 6.1"'
  puts
  exit;
end

@maxfolder = ARGV[0]
@noexit = false
@noexit = true if ARGV.length > 1

###################################################################
# initialization
###################################################################

@host = 'localhost'
@receivePort = 4792 # This must match the port-send number in testpackage-config.json
@sendPort = 4791    # This must match the port-listen number in testpackage-config.json
@passes = 0
@failures = 0

puts "  Starting the OSC Server..."
puts
@oscReceiver = OSC::UDPServer.new
@oscReceiver.bind @host, @receivePort
@oscSender = OSC::UDPSocket.new
@testdbPath = ""
@starttime = Time.now.iso8601.chop.chop.chop.chop.chop.chop


###################################################################
# sub routines
###################################################################

def establishCommunication
  @pingReturned = 0

  @oscReceiver.add_method('/testdb/path', 's') do |msg|
    @testdbPath = msg.args[0]
  end

  @oscReceiver.add_method('/ping/return', '') do |msg|
    puts "    Ping successfully returned."
    puts ""
    
    @oscSender.send(OSC::Message.new('/testdb/path?'), 0, @host, @sendPort)
    sleep 1 
    @pingReturned = 1
  end

  Thread.new do
    @oscReceiver.serve
  end
  sleep 5

  ping = OSC::Message.new('/ping');
  while @pingReturned == 0
    puts "    Sending ping to Max."
    @oscSender.send(ping, 0, @host, @sendPort)
    sleep 2
  end
end


def waitOnDatabase
  @dbReady = 0

  @oscReceiver.add_method('/db/ready', '') do |msg|
    @dbReady = 1
  end

  Thread.new do
    @oscReceiver.serve
  end
  sleep 10

  ping = OSC::Message.new('/db/ready?');
  while @dbReady == 0
    puts "    Sending query to Max."
    @oscSender.send(ping, 0, @host, @sendPort)
    sleep 10
  end
end


def waitForTestCompletion
  @testCompleted = 0

  @oscReceiver.add_method('/test/complete', '') do |msg|
    @testCompleted = 1
  end

  Thread.new do
    @oscReceiver.serve
  end

  while @testCompleted == 0
    sleep 1
  end
end


def waitForAllTestCompletion
  @testCompleted = 0

  @oscReceiver.add_method('/test/all/complete', '') do |msg|
    @testCompleted = 1
  end

  Thread.new do
    @oscReceiver.serve
  end

  while @testCompleted == 0
    sleep 1
  end
end


def launchMax
  if RUBY_PLATFORM.match(/darwin/)
    if @maxfolder.match(/\.app\/*$/) # check if app name given directly
      `open "#{@maxfolder}/Contents/MacOS/Max"`
    else # nope, just a folder name, so assume Max.app
    `open "#{@maxfolder}/Max.app/Contents/MacOS/Max"`
    end
  else
    IO.popen("#{@maxfolder}/Max.exe")
  end
end


###################################################################
{'fraction_non_alphanumeric': 0.2435182603847226, 'fraction_numerical': 0.016587677725118485, 'mean_word_length': 3.815436241610738, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '2072355', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2265, 'n_words': 619}