Skip to content

Commit cc3ee41

Browse files
committed
Start typesense locally automatically for integration tests
1 parent 468e3d5 commit cc3ee41

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
ruby-version: ['3.0', '3.2', '3.3']
1414
services:
1515
typesense:
16-
image: typesense/typesense:28.0
16+
image: typesense/typesense:29.0
1717
ports:
1818
- 8108:8108
1919
volumes:

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
services:
2+
typesense:
3+
image: typesense/typesense:29.0
4+
ports:
5+
- "127.0.0.1:8108:8108"
6+
volumes:
7+
- data:/data
8+
- analytics:/analytics
9+
environment:
10+
TYPESENSE_API_KEY: xyz
11+
TYPESENSE_DATA_DIR: /data
12+
TYPESENSE_ENABLE_CORS: true
13+
TYPESENSE_ANALYTICS_DIR: /analytics
14+
TYPESENSE_ENABLE_SEARCH_ANALYTICS: true
15+
16+
volumes:
17+
data:
18+
analytics:

spec/spec_helper.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,62 @@
1010
require 'bundler/setup'
1111
require 'webmock/rspec'
1212
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
1369

1470
RSpec.configure do |config|
1571
# Enable flags like --only-failures and --next-failure
@@ -31,4 +87,13 @@
3187
# It causes the host group and examples to inherit metadata
3288
# from the shared context.
3389
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
3499
end

0 commit comments

Comments
 (0)