/
spec_helper.rb
175 lines (147 loc) · 4.51 KB
/
spec_helper.rb
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal: true
$LOAD_PATH << File.expand_path(__dir__)
require 'spec_const'
require 'spec_condition'
require 'cli_helper'
require 'hide_pending_formatter'
require 'processing_for_ci'
require 'ridgepole'
require 'ridgepole/cli/config'
require 'active_support/core_ext'
require 'open3'
require 'tempfile'
require 'json'
require 'rspec/match_fuzzy'
require 'rspec/match_ruby'
require 'erbh'
require 'hash_modern_inspect'
require 'hash_order_helper'
require 'erb_helper'
RSpec.configure do |config|
config.before(:all) do
if condition(:debug) || condition(:verbose)
ActiveRecord::Migration.verbose = true
logger = Ridgepole::Logger.instance
logger.level = ::Logger::DEBUG
logger.verbose = condition(:verbose)
ActiveRecord::Base.logger = logger
else
ActiveRecord::Migration.verbose = false
Ridgepole::Logger.instance.level = ::Logger::ERROR
end
end
config.before(:each) do |example|
conds = example.metadata[:condition]
if conds
skip unless Array(conds).any? { |c| condition(c) }
end
case example.metadata[:file_path]
when /mysql/
skip if condition(:postgresql)
when /postgresql/
skip unless condition(:postgresql)
end
restore_database
end
end
module SpecHelper
def restore_database
if condition(:postgresql)
restore_database_postgresql
else
restore_database_mysql
end
end
def system_raise_on_fail(*args)
raise "Failed to run: #{args}" unless system(*args)
end
def restore_database_mysql
sql_file = File.expand_path('mysql/ridgepole_test_database.sql', __dir__)
system_raise_on_fail("#{MYSQL_CLI} < #{sql_file}")
end
def restore_database_postgresql
sql_file = File.expand_path('postgresql/ridgepole_test_database.sql', __dir__)
system("#{PG_CREATEDB} ridgepole_test 2>/dev/null")
system_raise_on_fail("#{PG_PSQL} ridgepole_test --set ON_ERROR_STOP=off -q -f #{sql_file} 2>/dev/null")
end
def restore_tables
if condition(:postgresql)
restore_tables_postgresql
else
restore_tables_mysql
end
end
def restore_tables_mysql
sql_file = File.expand_path('mysql/ridgepole_test_tables.sql', __dir__)
system_raise_on_fail("#{MYSQL_CLI} < #{sql_file}")
end
def restore_tables_postgresql
sql_file = File.expand_path('postgresql/ridgepole_test_tables.sql', __dir__)
system_raise_on_fail("#{PG_PSQL} ridgepole_test -q -f #{sql_file} 2>/dev/null")
end
def restore_tables_mysql_unknown_column_type
sql_file = File.expand_path('mysql/ridgepole_test_tables_unknown_column_type.sql', __dir__)
system_raise_on_fail("#{MYSQL_CLI} < #{sql_file}")
end
def client(options = {}, config = {})
config = conn_spec(config)
default_options = { debug: condition(:debug) }
default_options[:dump_without_table_options] = true
options = default_options.merge(options)
Ridgepole::Client.new(config, options)
end
def conn_spec(config = {})
if condition(:postgresql)
{
adapter: 'postgresql',
database: TEST_SCHEMA,
host: TEST_PG_HOST,
port: TEST_PG_PORT,
username: TEST_PG_USER,
password: TEST_PG_PASS,
}.merge(config)
else
{
adapter: 'mysql2',
database: TEST_SCHEMA,
host: TEST_MYSQL_HOST,
port: TEST_MYSQL_PORT,
username: TEST_MYSQL_USER,
password: TEST_MYSQL_PASS,
ssl_mode: 'DISABLED',
}.merge(config)
end
end
def show_create_table(table_name)
if condition(:postgresql)
show_create_table_postgresql(table_name)
else
normalize_mysql_ddl(show_create_table_mysql(table_name))
end
end
def show_create_table_mysql(table_name)
raw_conn = ActiveRecord::Base.connection.raw_connection
raw_conn.query("SHOW CREATE TABLE `#{table_name}`").first[1]
end
def show_create_table_postgresql(table_name)
`#{PG_DUMP} --schema-only #{TEST_SCHEMA} --table=#{table_name} | awk '/^CREATE TABLE/,/);/{print} /^CREATE INDEX/{print}'`.strip
end
def normalize_mysql_ddl(ddl)
ddl.gsub(/\(\d+\)/, '').gsub(/CHARSET=utf8$/, 'CHARSET=utf8mb3')
end
def tempfile(basename, content = '')
path = `mktemp /tmp/#{basename}.XXXXXX`
File.open(path, 'wb') { |f| f << content }
FileUtils.chmod(0o777, path)
yield(path)
ensure
FileUtils.rm_f(path) if path
end
def run_ridgepole(*args)
Dir.chdir(File.expand_path('..', __dir__)) do
cmd = [:bundle, :exec, './bin/ridgepole'] + args
Open3.capture2e(cmd.join(' '))
end
end
end
include SpecHelper