vanity 3.1.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/linting.yml +28 -0
  3. data/.github/workflows/test.yml +3 -6
  4. data/.rubocop.yml +114 -0
  5. data/.rubocop_todo.yml +67 -0
  6. data/Appraisals +9 -31
  7. data/CHANGELOG +5 -0
  8. data/Gemfile +7 -3
  9. data/Gemfile.lock +31 -3
  10. data/README.md +4 -9
  11. data/Rakefile +25 -24
  12. data/bin/vanity +1 -1
  13. data/doc/configuring.textile +1 -0
  14. data/gemfiles/rails52.gemfile +6 -3
  15. data/gemfiles/rails52.gemfile.lock +34 -9
  16. data/gemfiles/rails60.gemfile +6 -3
  17. data/gemfiles/rails60.gemfile.lock +34 -9
  18. data/gemfiles/rails61.gemfile +6 -3
  19. data/gemfiles/rails61.gemfile.lock +34 -9
  20. data/lib/generators/vanity/migration_generator.rb +5 -7
  21. data/lib/vanity/adapters/abstract_adapter.rb +43 -45
  22. data/lib/vanity/adapters/active_record_adapter.rb +30 -30
  23. data/lib/vanity/adapters/mock_adapter.rb +14 -18
  24. data/lib/vanity/adapters/mongodb_adapter.rb +73 -69
  25. data/lib/vanity/adapters/redis_adapter.rb +19 -27
  26. data/lib/vanity/adapters.rb +1 -1
  27. data/lib/vanity/autoconnect.rb +6 -7
  28. data/lib/vanity/commands/list.rb +7 -7
  29. data/lib/vanity/commands/report.rb +18 -22
  30. data/lib/vanity/configuration.rb +19 -19
  31. data/lib/vanity/connection.rb +12 -14
  32. data/lib/vanity/experiment/ab_test.rb +82 -70
  33. data/lib/vanity/experiment/alternative.rb +3 -5
  34. data/lib/vanity/experiment/base.rb +24 -19
  35. data/lib/vanity/experiment/bayesian_bandit_score.rb +7 -13
  36. data/lib/vanity/experiment/definition.rb +6 -6
  37. data/lib/vanity/frameworks/rails.rb +39 -39
  38. data/lib/vanity/frameworks.rb +2 -2
  39. data/lib/vanity/helpers.rb +1 -1
  40. data/lib/vanity/metric/active_record.rb +21 -19
  41. data/lib/vanity/metric/base.rb +22 -23
  42. data/lib/vanity/metric/google_analytics.rb +6 -9
  43. data/lib/vanity/metric/remote.rb +3 -5
  44. data/lib/vanity/playground.rb +3 -6
  45. data/lib/vanity/vanity.rb +8 -12
  46. data/lib/vanity/version.rb +1 -1
  47. data/test/adapters/active_record_adapter_test.rb +1 -5
  48. data/test/adapters/mock_adapter_test.rb +0 -2
  49. data/test/adapters/mongodb_adapter_test.rb +1 -5
  50. data/test/adapters/redis_adapter_test.rb +2 -3
  51. data/test/adapters/shared_tests.rb +9 -12
  52. data/test/autoconnect_test.rb +3 -3
  53. data/test/cli_test.rb +0 -1
  54. data/test/configuration_test.rb +18 -34
  55. data/test/connection_test.rb +3 -3
  56. data/test/dummy/Rakefile +1 -1
  57. data/test/dummy/app/controllers/use_vanity_controller.rb +12 -8
  58. data/test/dummy/app/mailers/vanity_mailer.rb +3 -3
  59. data/test/dummy/config/application.rb +1 -1
  60. data/test/dummy/config/boot.rb +3 -3
  61. data/test/dummy/config/environment.rb +1 -1
  62. data/test/dummy/config/environments/development.rb +0 -1
  63. data/test/dummy/config/environments/test.rb +1 -1
  64. data/test/dummy/config/initializers/session_store.rb +1 -1
  65. data/test/dummy/config.ru +1 -1
  66. data/test/dummy/script/rails +2 -2
  67. data/test/experiment/ab_test.rb +148 -154
  68. data/test/experiment/base_test.rb +48 -32
  69. data/test/frameworks/rails/action_controller_test.rb +25 -25
  70. data/test/frameworks/rails/action_mailer_test.rb +2 -2
  71. data/test/frameworks/rails/action_view_test.rb +5 -6
  72. data/test/frameworks/rails/rails_test.rb +147 -181
  73. data/test/helper_test.rb +2 -2
  74. data/test/metric/active_record_test.rb +174 -212
  75. data/test/metric/base_test.rb +21 -46
  76. data/test/metric/google_analytics_test.rb +17 -25
  77. data/test/metric/remote_test.rb +7 -10
  78. data/test/playground_test.rb +7 -14
  79. data/test/templates_test.rb +16 -20
  80. data/test/test_helper.rb +28 -29
  81. data/test/vanity_test.rb +4 -10
  82. data/test/web/rails/dashboard_test.rb +21 -21
  83. data/vanity.gemspec +8 -7
  84. metadata +28 -30
  85. data/gemfiles/rails42.gemfile +0 -33
  86. data/gemfiles/rails42.gemfile.lock +0 -265
  87. data/gemfiles/rails42_protected_attributes.gemfile +0 -34
  88. data/gemfiles/rails42_protected_attributes.gemfile.lock +0 -264
  89. data/gemfiles/rails51.gemfile +0 -33
  90. data/gemfiles/rails51.gemfile.lock +0 -285
@@ -1,259 +1,226 @@
1
1
  require "test_helper"
2
2
 
3
3
  describe "deprecated Rails load_path and deprecated connection configuration" do
4
-
5
4
  it "load_path" do
6
- assert_equal "./experiments", load_rails("", <<-RB)
7
- $stdout << Vanity.playground.load_path
5
+ assert_equal "./experiments", load_rails("", <<~RB)
6
+ $stdout << Vanity.playground.load_path
8
7
  RB
9
8
  end
10
9
 
11
10
  it "settable load_path" do
12
- assert_equal "predictions", load_rails(%Q{\nVanity.playground.load_path = "predictions"\n}, <<-RB)
13
- $stdout << Vanity.playground.load_path
11
+ assert_equal "predictions", load_rails(%{\nVanity.playground.load_path = "predictions"\n}, <<~RB)
12
+ $stdout << Vanity.playground.load_path
14
13
  RB
15
14
  end
16
15
 
17
16
  it "absolute load_path" do
18
17
  Dir.mktmpdir do |dir|
19
- assert_equal dir, load_rails(%Q{\nVanity.playground.load_path = "#{dir}"\n}, <<-RB)
20
- $stdout << Vanity.playground.load_path
18
+ assert_equal dir, load_rails(%{\nVanity.playground.load_path = "#{dir}"\n}, <<~RB)
19
+ $stdout << Vanity.playground.load_path
21
20
  RB
22
21
  end
23
22
  end
24
23
 
25
24
  if ENV['DB'] == 'redis'
26
25
  it "default connection" do
27
- assert_equal "redis://127.0.0.1:6379/0", load_rails("", <<-RB)
28
- $stdout << Vanity.playground.connection
26
+ assert_equal "redis://127.0.0.1:6379/0", load_rails("", <<~RB)
27
+ $stdout << Vanity.playground.connection
29
28
  RB
30
29
  end
31
30
 
32
31
  it "connection from string" do
33
- assert_equal "redis://192.168.1.1:6379/5", load_rails(%Q{\nVanity.playground.establish_connection "redis://192.168.1.1:6379/5"\n}, <<-RB)
34
- $stdout << Vanity.playground.connection
32
+ assert_equal "redis://192.168.1.1:6379/5", load_rails(%{\nVanity.playground.establish_connection "redis://192.168.1.1:6379/5"\n}, <<~RB)
33
+ $stdout << Vanity.playground.connection
35
34
  RB
36
35
  end
37
36
 
38
37
  it "connection from yaml" do
39
- begin
40
- FileUtils.mkpath "tmp/config"
41
- File.open("tmp/config/vanity.yml", "w") do |io|
42
- io.write <<-YML
43
- production:
44
- adapter: redis
45
- host: somehost
46
- database: 15
47
- YML
48
- end
49
- assert_equal "redis://somehost:6379/15", load_rails("", <<-RB, "production")
50
- $stdout << Vanity.playground.connection
51
- RB
52
- ensure
53
- File.unlink "tmp/config/vanity.yml"
54
- end
38
+ FileUtils.mkpath "tmp/config"
39
+ File.write("tmp/config/vanity.yml", <<~YML)
40
+ production:
41
+ adapter: redis
42
+ host: somehost
43
+ database: 15
44
+ YML
45
+ assert_equal "redis://somehost:6379/15", load_rails("", <<~RB, "production")
46
+ $stdout << Vanity.playground.connection
47
+ RB
48
+ ensure
49
+ File.unlink "tmp/config/vanity.yml"
55
50
  end
56
51
 
57
52
  it "connection from yaml url" do
58
- begin
59
- FileUtils.mkpath "tmp/config"
60
- File.open("tmp/config/vanity.yml", "w") do |io|
61
- io.write <<-YML
62
- production: redis://somehost/15
63
- YML
64
- end
65
- assert_equal "redis://somehost:6379/15", load_rails("", <<-RB, "production")
66
- $stdout << Vanity.playground.connection
67
- RB
68
- ensure
69
- File.unlink "tmp/config/vanity.yml"
70
- end
53
+ FileUtils.mkpath "tmp/config"
54
+ File.write("tmp/config/vanity.yml", <<~YML)
55
+ production: redis://somehost/15
56
+ YML
57
+ assert_equal "redis://somehost:6379/15", load_rails("", <<~RB, "production")
58
+ $stdout << Vanity.playground.connection
59
+ RB
60
+ ensure
61
+ File.unlink "tmp/config/vanity.yml"
71
62
  end
72
63
 
73
64
  it "connection from yaml with erb" do
74
- begin
75
- FileUtils.mkpath "tmp/config"
76
- # Pass storage URL through environment like heroku does
77
- @original_redis_url = ENV["REDIS_URL"]
78
- ENV["REDIS_URL"] = "redis://somehost:6379/15"
79
- File.open("tmp/config/vanity.yml", "w") do |io|
80
- io.write <<-YML
81
- production: <%= ENV['REDIS_URL'] %>
82
- YML
83
- end
84
- assert_equal "redis://somehost:6379/15", load_rails("", <<-RB, "production")
85
- $stdout << Vanity.playground.connection
86
- RB
87
- ensure
88
- ENV["REDIS_URL"] = @original_redis_url
89
- File.unlink "tmp/config/vanity.yml"
90
- end
65
+ FileUtils.mkpath "tmp/config"
66
+ # Pass storage URL through environment like heroku does
67
+ @original_redis_url = ENV["REDIS_URL"]
68
+ ENV["REDIS_URL"] = "redis://somehost:6379/15"
69
+ File.write("tmp/config/vanity.yml", <<~YML)
70
+ production: <%= ENV['REDIS_URL'] %>
71
+ YML
72
+ assert_equal "redis://somehost:6379/15", load_rails("", <<~RB, "production")
73
+ $stdout << Vanity.playground.connection
74
+ RB
75
+ ensure
76
+ ENV["REDIS_URL"] = @original_redis_url
77
+ File.unlink "tmp/config/vanity.yml"
91
78
  end
92
79
 
93
80
  it "connection from redis yml" do
94
- begin
95
- FileUtils.mkpath "tmp/config"
96
- yml = File.open("tmp/config/redis.yml", "w")
97
- yml << "production: internal.local:6379\n"
98
- yml.flush
99
- assert_match %r{redis://internal.local:6379/0\Z}, load_rails("", <<-RB)
100
- $stdout << Vanity.playground.connection
101
- RB
102
- ensure
103
- File.unlink yml.path
104
- end
81
+ FileUtils.mkpath "tmp/config"
82
+ yml = File.open("tmp/config/redis.yml", "w")
83
+ yml << "production: internal.local:6379\n"
84
+ yml.flush
85
+ assert_match %r{redis://internal.local:6379/0\Z}, load_rails("", <<~RB)
86
+ $stdout << Vanity.playground.connection
87
+ RB
88
+ ensure
89
+ File.unlink yml.path
105
90
  end
106
91
  end
107
92
 
108
93
  if ENV['DB'] == 'mongo'
109
94
  it "mongo connection from yaml" do
110
- begin
111
- FileUtils.mkpath "tmp/config"
112
- File.open("tmp/config/vanity.yml", "w") do |io|
113
- io.write <<-YML
114
- mongodb:
115
- adapter: mongodb
116
- host: localhost
117
- port: 27017
118
- database: vanity_test
119
- YML
120
- end
95
+ FileUtils.mkpath "tmp/config"
96
+ File.write("tmp/config/vanity.yml", <<~YML)
97
+ mongodb:
98
+ adapter: mongodb
99
+ host: localhost
100
+ port: 27017
101
+ database: vanity_test
102
+ YML
121
103
 
122
- assert_equal "mongodb://localhost:27017/vanity_test", load_rails("", <<-RB, "mongodb")
123
- $stdout << Vanity.playground.connection
124
- RB
125
- ensure
126
- File.unlink "tmp/config/vanity.yml"
127
- end
104
+ assert_equal "mongodb://localhost:27017/vanity_test", load_rails("", <<~RB, "mongodb")
105
+ $stdout << Vanity.playground.connection
106
+ RB
107
+ ensure
108
+ File.unlink "tmp/config/vanity.yml"
128
109
  end
129
110
 
130
111
  unless ENV['CI'] == 'true' # See http://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
131
112
  it "mongodb replica set connection" do
132
- begin
133
- FileUtils.mkpath "tmp/config"
134
- File.open("tmp/config/vanity.yml", "w") do |io|
135
- io.write <<-YML
136
- mongodb:
137
- adapter: mongodb
138
- hosts:
139
- - localhost
140
- port: 27017
141
- database: vanity_test
142
- YML
143
- end
113
+ FileUtils.mkpath "tmp/config"
114
+ File.write("tmp/config/vanity.yml", <<~YML)
115
+ mongodb:
116
+ adapter: mongodb
117
+ hosts:
118
+ - localhost
119
+ port: 27017
120
+ database: vanity_test
121
+ YML
144
122
 
145
- assert_equal "mongodb://localhost:27017/vanity_test", load_rails("", <<-RB, "mongodb")
146
- $stdout << Vanity.playground.connection
147
- RB
123
+ assert_equal "mongodb://localhost:27017/vanity_test", load_rails("", <<~RB, "mongodb")
124
+ $stdout << Vanity.playground.connection
125
+ RB
148
126
 
149
- assert_equal "Mongo::ReplSetConnection", load_rails("", <<-RB, "mongodb")
150
- $stdout << Vanity.playground.connection.mongo.class
151
- RB
152
- ensure
153
- File.unlink "tmp/config/vanity.yml"
154
- end
127
+ assert_equal "Mongo::ReplSetConnection", load_rails("", <<~RB, "mongodb")
128
+ $stdout << Vanity.playground.connection.mongo.class
129
+ RB
130
+ ensure
131
+ File.unlink "tmp/config/vanity.yml"
155
132
  end
156
133
  end
157
134
  end
158
135
 
159
136
  it "connection from yaml missing" do
160
- begin
161
- FileUtils.mkpath "tmp/config"
162
- File.open("tmp/config/vanity.yml", "w") do |io|
163
- io.write <<-YML
164
- production:
165
- adapter: redis
166
- YML
167
- end
137
+ FileUtils.mkpath "tmp/config"
138
+ File.write("tmp/config/vanity.yml", <<~YML)
139
+ production:
140
+ adapter: redis
141
+ YML
168
142
 
169
- assert_equal "No configuration for development", load_rails("\nbegin\n", <<-RB, "development")
170
- rescue => e
171
- $stdout << e.message
172
- end
173
- RB
174
- ensure
175
- File.unlink "tmp/config/vanity.yml"
176
- end
143
+ assert_equal "No configuration for development", load_rails("\nbegin\n", <<~RB, "development")
144
+ rescue => e
145
+ $stdout << e.message
146
+ end
147
+ RB
148
+ ensure
149
+ File.unlink "tmp/config/vanity.yml"
177
150
  end
178
151
 
179
152
  it "collection from vanity yaml" do
180
- begin
181
- FileUtils.mkpath "tmp/config"
182
- File.open("tmp/config/vanity.yml", "w") do |io|
183
- io.write <<-YML
184
- production:
185
- collecting: false
186
- adapter: mock
187
- YML
188
- end
189
- assert_equal "false", load_rails("", <<-RB)
190
- $stdout << Vanity.playground.collecting?
191
- RB
192
- ensure
193
- File.unlink "tmp/config/vanity.yml"
194
- end
153
+ FileUtils.mkpath "tmp/config"
154
+ File.write("tmp/config/vanity.yml", <<~YML)
155
+ production:
156
+ collecting: false
157
+ adapter: mock
158
+ YML
159
+ assert_equal "false", load_rails("", <<~RB)
160
+ $stdout << Vanity.playground.collecting?
161
+ RB
162
+ ensure
163
+ File.unlink "tmp/config/vanity.yml"
195
164
  end
196
165
 
197
166
  it "collection true in production by default" do
198
- assert_equal "true", load_rails("", <<-RB)
199
- $stdout << Vanity.playground.collecting?
167
+ assert_equal "true", load_rails("", <<~RB)
168
+ $stdout << Vanity.playground.collecting?
200
169
  RB
201
170
  end
202
171
 
203
172
  it "collection false in production when configured" do
204
- assert_equal "false", load_rails("\nVanity.playground.collecting = false\n", <<-RB)
205
- $stdout << Vanity.playground.collecting?
173
+ assert_equal "false", load_rails("\nVanity.playground.collecting = false\n", <<~RB)
174
+ $stdout << Vanity.playground.collecting?
206
175
  RB
207
176
  end
208
177
 
209
178
  it "collection true in development by default" do
210
- assert_equal "true", load_rails("", <<-RB, "development")
211
- $stdout << Vanity.playground.collecting?
179
+ assert_equal "true", load_rails("", <<~RB, "development")
180
+ $stdout << Vanity.playground.collecting?
212
181
  RB
213
182
  end
214
183
 
215
184
  it "collection true in development when configured" do
216
- assert_equal "true", load_rails("\nVanity.playground.collecting = true\n", <<-RB, "development")
217
- $stdout << Vanity.playground.collecting?
185
+ assert_equal "true", load_rails("\nVanity.playground.collecting = true\n", <<~RB, "development")
186
+ $stdout << Vanity.playground.collecting?
218
187
  RB
219
188
  end
220
189
 
221
190
  it "playground loads experiments if connected" do
222
- assert_equal "{}", load_rails("", <<-RB)
223
- $stdout << Vanity.playground.experiments.inspect
191
+ assert_equal "{}", load_rails("", <<~RB)
192
+ $stdout << Vanity.playground.experiments.inspect
224
193
  RB
225
194
  end
226
195
 
227
196
  it "playground does not instantiate connection if disabled" do
228
- begin
229
- ENV['VANITY_DISABLED'] = '1'
230
- assert_equal "false", load_rails("", <<-RB)
231
- Vanity.playground.experiments.inspect
232
- $stdout << !!Vanity.playground.connected?
233
- RB
234
- ensure
235
- ENV['VANITY_DISABLED'] = nil
236
- end
197
+ ENV['VANITY_DISABLED'] = '1'
198
+ assert_equal "false", load_rails("", <<~RB)
199
+ Vanity.playground.experiments.inspect
200
+ $stdout << !!Vanity.playground.connected?
201
+ RB
202
+ ensure
203
+ ENV['VANITY_DISABLED'] = nil
237
204
  end
238
205
 
239
- def load_rails(before_initialize, after_initialize, env="production")
206
+ def load_rails(before_initialize, after_initialize, env = "production")
240
207
  tmp = Tempfile.open("test.rb")
241
208
  begin
242
- code_setup = <<-RB
243
- $:.delete_if { |path| path[/gems\\/vanity-\\d/] }
244
- $:.unshift File.expand_path("../lib")
245
- RAILS_ROOT = File.expand_path(".")
209
+ code_setup = <<~RB
210
+ $:.delete_if { |path| path[/gems\\/vanity-\\d/] }
211
+ $:.unshift File.expand_path("../lib")
212
+ RAILS_ROOT = File.expand_path(".")
246
213
  RB
247
214
  code = code_setup
248
215
  code += load_rails_3_or_4(env)
249
- code += %Q{\nrequire "vanity"\n}
216
+ code += %{\nrequire "vanity"\n}
250
217
  code += before_initialize
251
218
  code += initialize_rails_3_or_4
252
219
  code += after_initialize
253
220
  tmp.write code
254
221
  tmp.flush
255
222
  Dir.chdir "tmp" do
256
- open("| ruby #{tmp.path}").read
223
+ open("| ruby #{tmp.path}").read # rubocop:todo Security/Open
257
224
  end
258
225
  ensure
259
226
  tmp.close!
@@ -261,30 +228,29 @@ RAILS_ROOT = File.expand_path(".")
261
228
  end
262
229
 
263
230
  def load_rails_3_or_4(env)
264
- <<-RB
265
- ENV['BUNDLE_GEMFILE'] ||= "#{ENV['BUNDLE_GEMFILE']}"
266
- require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
267
- ENV['RAILS_ENV'] = ENV['RACK_ENV'] = "#{env}"
268
- require "active_model/railtie"
269
- require "action_controller/railtie"
231
+ <<~RB
232
+ ENV['BUNDLE_GEMFILE'] ||= "#{ENV['BUNDLE_GEMFILE']}"
233
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
234
+ ENV['RAILS_ENV'] = ENV['RACK_ENV'] = "#{env}"
235
+ require "active_model/railtie"
236
+ require "action_controller/railtie"
270
237
 
271
- Bundler.require(:default)
238
+ Bundler.require(:default)
272
239
 
273
- module Foo
274
- class Application < Rails::Application
275
- config.active_support.deprecation = :notify
276
- config.log_level = :fatal
277
- config.eager_load = #{env == "production"} if Rails::Application.respond_to?(:eager_load!)
278
- ActiveSupport::Deprecation.silenced = true if ActiveSupport::Deprecation.respond_to?(:silenced) && ENV['CI']
279
- end
280
- end
240
+ module Foo
241
+ class Application < Rails::Application
242
+ config.active_support.deprecation = :notify
243
+ config.log_level = :fatal
244
+ config.eager_load = #{env == 'production'} if Rails::Application.respond_to?(:eager_load!)
245
+ ActiveSupport::Deprecation.silenced = true if ActiveSupport::Deprecation.respond_to?(:silenced) && ENV['CI']
246
+ end
247
+ end
281
248
  RB
282
249
  end
283
250
 
284
251
  def initialize_rails_3_or_4
285
- <<-RB
286
- Foo::Application.initialize!
252
+ <<~RB
253
+ Foo::Application.initialize!
287
254
  RB
288
255
  end
289
-
290
256
  end
data/test/helper_test.rb CHANGED
@@ -9,7 +9,7 @@ describe Vanity::Helpers do
9
9
  default "foo"
10
10
  metrics :coolness
11
11
  end
12
- Vanity.track!(:coolness, :identity=>'quux')
12
+ Vanity.track!(:coolness, identity: 'quux')
13
13
 
14
14
  assert_equal 1, experiment(:foobar).alternatives.sum(&:conversions)
15
15
  end
@@ -21,7 +21,7 @@ describe Vanity::Helpers do
21
21
  default "foo"
22
22
  metrics :coolness
23
23
  end
24
- Vanity.track!(:coolness, :identity=>'quux', :values=>[2])
24
+ Vanity.track!(:coolness, identity: 'quux', values: [2])
25
25
 
26
26
  assert_equal 2, experiment(:foobar).alternatives.sum(&:conversions)
27
27
  end