-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathspec_helper.rb
More file actions
140 lines (109 loc) · 3.24 KB
/
spec_helper.rb
File metadata and controls
140 lines (109 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# frozen_string_literal: true
require 'simplecov'
SimpleCov.start do
add_group 'lib', 'lib'
add_filter 'spec'
end
require 'bundler/setup'
require 'webmock/rspec'
require 'typesense'
require 'faraday'
WebMock.disable_net_connect!(allow_localhost: true)
module TypesenseTestState
@started_by_tests = false
def self.started_by_tests?
@started_by_tests
end
def self.mark_started
@started_by_tests = true
end
end
def typesense_healthy?(host = 'localhost', port = 8108)
conn = Faraday.new("http://#{host}:#{port}")
response = conn.get('/health')
response.status == 200 && response.body.include?('ok')
rescue StandardError
false
end
def typesense_version
WebMock.allow_net_connect!
conn = Faraday.new('http://localhost:8108')
response = conn.get('/debug') do |req|
req.headers['X-TYPESENSE-API-KEY'] = 'xyz'
end
if response.status == 200 && !response.body.empty?
debug_info = JSON.parse(response.body)
debug_info['version']
end
rescue StandardError
nil
ensure
WebMock.disable_net_connect!(allow_localhost: true)
end
def typesense_v30_or_above?
version = typesense_version
return false unless version
return true if version == 'nightly'
if version.match(/^v(\d+)/)
major_version = Regexp.last_match(1).to_i
return major_version >= 30
end
if version.match(/^(\d+)/)
major_version = Regexp.last_match(1).to_i
return major_version >= 30
end
false
end
def ensure_typesense_running
if typesense_healthy?
puts '✅ Typesense is already running and healthy, ready for use in integration tests'
return false
end
# Check if Docker is running
raise 'Docker daemon is not running. Please start Docker and try again.' unless system('docker info > /dev/null 2>&1')
puts 'Starting Typesense with docker-compose...'
raise 'Failed to start docker-compose' unless system('docker-compose up -d')
# Wait for Typesense to be ready
print 'Waiting for Typesense to start'
20.times do
break if typesense_healthy?
print '.'
sleep 1
end
puts
raise 'Failed to start Typesense - health endpoint did not return OK' unless typesense_healthy?
puts 'Typesense is ready!'
TypesenseTestState.mark_started
true
end
def stop_typesense_if_started
unless TypesenseTestState.started_by_tests?
puts "\e[33m\nTest suite did not shut down Typesense automatically, because it was already running when tests started\e[0m"
return
end
puts 'Stopping Typesense...'
system('docker-compose down')
end
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.expose_dsl_globally = true
# This config option will be enabled by default on RSpec 4,
# but for reasons of backwards compatibility, you have to
# set it on RSpec 3.
#
# It causes the host group and examples to inherit metadata
# from the shared context.
config.shared_context_metadata_behavior = :apply_to_host_groups
config.before(:suite) do
ensure_typesense_running
end
config.after(:suite) do
stop_typesense_if_started
end
end