test.rake revision b1ecbf5c75ae7e6f12e199064ef3c592518abd9b
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonnamespace :test do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster # We want to purge our database our own way, without deleting everything
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rake::Task['db:test:purge'].overwrite do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster # Taken from https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/railties/databases.rake#L512
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rake::Task['db:redis:clean'].invoke
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rake::Task['db:migrate:clean'].invoke
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HETS_API_OPTIONS = '/auto'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HETS_BASE_IRI = 'http://localhost:8000'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HETS_PATH = `which hets`.strip
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HETS_SERVER_ARGS = YAML.load(File.open('config/hets.yml'))['server_options']
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def all_files_beneath(dir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster globbed_files = Dir.glob(File.join(dir, '**/*'))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster globbed_files.select { |file| !File.directory?(file) }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def ontology_files
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster all_files_beneath('spec/fixtures/ontologies').select do |file|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def prove_files
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster all_files_beneath('spec/fixtures/ontologies/prove')
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def absolute_filepath(file)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def cassette_file(file)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster # remove spec/fixtures/ontologies/ for cassette name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cassette_filepath = file.split('/')[3..-1].join('/')
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def hets_cassette_dir(subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File.join('hets-out', subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def cassette_path_in_fixtures(subdir, file)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File.join(hets_cassette_dir(subdir), cassette_file(file))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def recorded_file(subdir, file)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster base = file.split('.')[0..-2].join('.')
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster old_extension = File.extname(file)[1..-1]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster file = "#{base}_#{old_extension}.yml"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File.join('spec', 'fixtures', 'vcr', cassette_path_in_fixtures(subdir, file))
365156d5428edc07eae024879829e586bbaf9d42Neil Madden def outdated_cassettes(files, subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cassette = recorded_file(subdir, file)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster !FileUtils.uptodate?(cassette, Array(HETS_PATH))
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington def on_outdated_cassettes(files, subdir, &block)
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington outdated_cassettes(files, subdir).each do |file|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def prover_output_target_file(node, prover)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = "spec/fixtures/prover_output/generated/#{node}/#{prover}"
365156d5428edc07eae024879829e586bbaf9d42Neil Madden File.open(filepath, 'w')
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def write_prover_output_fixture(node, prover, response_hash)
365156d5428edc07eae024879829e586bbaf9d42Neil Madden file = prover_output_target_file(node, prover)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster file.write(response_hash.first['goals'].first['proverOutput'])
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def http_request_with_get(uri, _header, _data)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def http_request_with_post(uri, header, data)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Net::HTTP.start(uri.hostname, uri.port) do |http|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def call_hets(file, subdir, command,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster method: :get,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hets_api_options: HETS_API_OPTIONS,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster query_string: '',
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster puts "Calling hets/#{command} on #{file.inspect}"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster escaped_iri = Rack::Utils.escape_path("file://#{absolute_filepath(file)}")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hets_iri = "#{HETS_BASE_IRI}/#{command}/#{escaped_iri}"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hets_iri << hets_api_options
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hets_iri << query_string
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FileUtils.rm_f(recorded_file(subdir, file))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster VCR.use_cassette(cassette_path_in_fixtures(subdir, file)) do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster send("http_request_with_#{method}", URI(hets_iri), header, data)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def call_hets_dg(file, subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hets_api_options = "#{HETS_API_OPTIONS}/full-signatures/full-theories"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets(file, subdir, 'dg', hets_api_options: hets_api_options)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def call_hets_provers(file, subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster query_string = '?format=json'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets(file, subdir, 'provers', query_string: query_string)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def call_hets_prove(file, subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster header = {'Content-Type' => 'application/json'}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster data = {format: 'json', include: 'true'}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets(file, subdir, 'prove', method: :post, header: header, data: data)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def call_hets_prover_output(file, subdir, nodes)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster header = {'Content-Type' => 'application/json'}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster data_template = {format: 'json', include: 'true'}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster provers = %w(SPASS darwin darwin-non-fd eprover)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster data = data_template.merge({prover: prover, node: node})
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster response = call_hets(file, subdir, 'prove',
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster method: :post, header: header, data: data)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster write_prover_output_fixture(node, prover, json)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def freshen_ontology_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster on_outdated_cassettes(ontology_files, 'dg') do |file, subdir|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets_dg(file, subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def freshen_provers_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster on_outdated_cassettes(ontology_files, 'provers') do |file, subdir|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets_provers(file, subdir)
365156d5428edc07eae024879829e586bbaf9d42Neil Madden def freshen_proof_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster on_outdated_cassettes(prove_files, 'prove') do |file, subdir|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets_prove(file, subdir)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def freshen_prover_output_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster on_outdated_cassettes(prove_files, 'prover_output') do |file, subdir|
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster call_hets_prover_output(file, subdir, %w(CounterSatisfiable Theorem))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def setup_vcr
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster require 'vcr'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def port_open?(ip, port, seconds=1)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster require 'socket'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster require 'timeout'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Timeout::timeout(seconds) do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster TCPSocket.new(ip, port).close
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rescue Timeout::Error
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster def with_running_hets(&block)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster need_to_start_hets = !port_open?('127.0.0.1', 8000)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if need_to_start_hets
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hets_pid = fork { exec("hets --server #{HETS_SERVER_ARGS.join(' ')}") }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster # hets server needs some startup time
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if need_to_start_hets
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster puts 'Stopping hets server.'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Process.kill('TERM', hets_pid)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'abort execution if elasticsearch is not running'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster task :abort_if_elasticsearch_is_not_running do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster elasticsearch_port = ENV['ELASTIC_TEST_PORT']
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster elasticsearch_port = '9250' unless elasticsearch_port.present?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster unless port_open?('127.0.0.1', elasticsearch_port)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster $stderr.puts 'Elasticsearch is not running. Please start it before running the tests.'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster $stderr.puts 'Aborting tests.'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'Update all fixtures'
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington task :freshen_fixtures do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster outdated_exist = outdated_cassettes(ontology_files, 'dg').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster outdated_exist ||= outdated_cassettes(ontology_files, 'provers').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster outdated_exist ||= outdated_cassettes(prove_files, 'prove').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster outdated_exist ||= outdated_cassettes(prove_files, 'prover_output').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if outdated_exist
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster with_running_hets do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster freshen_ontology_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster freshen_provers_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster freshen_proof_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster freshen_prover_output_fixtures
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'Update all ontology fixtures'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster task :freshen_ontology_fixtures do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if outdated_cassettes(ontology_files, 'dg').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster with_running_hets { freshen_ontology_fixtures }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'Update all provers fixtures'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster task :freshen_provers_fixtures do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if outdated_cassettes(ontology_files, 'provers').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster with_running_hets { freshen_provers_fixtures }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'Update all proof fixtures'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster task :freshen_proof_fixtures do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if outdated_cassettes(prove_files, 'prove').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster with_running_hets { freshen_proof_fixtures }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'Enable coverage report (only useful as prerequisite of other tasks)'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster task :enable_coverage do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ENV['COVERAGE'] = 'true'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster desc 'Update all prover output fixtures'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster task :freshen_prover_output_fixtures do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if outdated_cassettes(prove_files, 'prover_output').any?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster with_running_hets { freshen_prover_output_fixtures }