|
10 | 10 | require 'bundler/setup' |
11 | 11 | require 'webmock/rspec' |
12 | 12 | require 'typesense' |
| 13 | +require 'faraday' |
| 14 | + |
| 15 | +WebMock.disable_net_connect!(allow_localhost: true) |
| 16 | + |
| 17 | +def typesense_healthy?(host = 'localhost', port = 8108) |
| 18 | + conn = Faraday.new("http://#{host}:#{port}") |
| 19 | + response = conn.get('/health') |
| 20 | + response.status == 200 && response.body.include?('ok') |
| 21 | +rescue StandardError |
| 22 | + false |
| 23 | +end |
| 24 | + |
| 25 | +def start_typesense_if_needed |
| 26 | + if typesense_healthy? |
| 27 | + puts '✅ Typesense is already running and healthy, ready for use in integration tests' |
| 28 | + return false |
| 29 | + end |
| 30 | + |
| 31 | + # Check if Docker is running |
| 32 | + unless system('docker info > /dev/null 2>&1') |
| 33 | + raise 'Docker daemon is not running. Please start Docker and try again.' |
| 34 | + end |
| 35 | + |
| 36 | + puts 'Starting Typesense with docker-compose...' |
| 37 | + unless system('docker-compose up -d') |
| 38 | + raise 'Failed to start docker-compose' |
| 39 | + end |
| 40 | + |
| 41 | + # Wait for Typesense to be ready |
| 42 | + print 'Waiting for Typesense to start' |
| 43 | + 20.times do |
| 44 | + break if typesense_healthy? |
| 45 | + |
| 46 | + print '.' |
| 47 | + sleep 1 |
| 48 | + end |
| 49 | + puts |
| 50 | + |
| 51 | + unless typesense_healthy? |
| 52 | + raise 'Failed to start Typesense - health endpoint did not return OK' |
| 53 | + end |
| 54 | + |
| 55 | + puts 'Typesense is ready!' |
| 56 | + $typesense_started_by_tests = true |
| 57 | + true |
| 58 | +end |
| 59 | + |
| 60 | +def stop_typesense_if_started |
| 61 | + unless $typesense_started_by_tests |
| 62 | + puts "\e[33m\nTest suite did not shut down Typesense automatically, because it was already running when tests started\e[0m" |
| 63 | + return |
| 64 | + end |
| 65 | + |
| 66 | + puts 'Stopping Typesense...' |
| 67 | + system('docker-compose down') |
| 68 | +end |
13 | 69 |
|
14 | 70 | RSpec.configure do |config| |
15 | 71 | # Enable flags like --only-failures and --next-failure |
|
31 | 87 | # It causes the host group and examples to inherit metadata |
32 | 88 | # from the shared context. |
33 | 89 | config.shared_context_metadata_behavior = :apply_to_host_groups |
| 90 | + |
| 91 | + config.before(:suite) do |
| 92 | + $typesense_started_by_tests = start_typesense_if_needed |
| 93 | + WebMock.disable_net_connect! |
| 94 | + end |
| 95 | + |
| 96 | + config.after(:suite) do |
| 97 | + stop_typesense_if_started |
| 98 | + end |
34 | 99 | end |
0 commit comments