zonebie 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ notifications:
11
+ email:
12
+ - alindeman@gmail.com
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zonebie.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andy Lindeman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Zonebie
2
+
3
+ [![Build Status](https://secure.travis-ci.org/highgroove/zonebie.png)](http://travis-ci.org/highgroove/zonebie)
4
+
5
+ Zonebie prevents bugs in code that deals with timezones by randomly assigning a
6
+ zone on every run.
7
+
8
+ ## Requirements
9
+
10
+ * MRI (1.8.7, 1.9.2, 1.9.3)
11
+ * JRuby (1.6)
12
+ * Rubinius (1.2, 2.0)
13
+
14
+ ***
15
+
16
+ And **either** of these gems which adds timezone support to Ruby:
17
+
18
+ * `activesupport` >= 2.3 (Rails 2.3, 3.0, 3.1, 3.2)
19
+ * `tzinfo` >= 0.3
20
+
21
+ ## Installation
22
+
23
+ If using Bundler (recommended), add to Gemfile:
24
+
25
+ ````ruby
26
+ gem 'zonebie'
27
+ ````
28
+
29
+ ## Usage with Rails & ActiveSupport
30
+
31
+ ActiveSupport allows setting a global timezone that will be used for many date
32
+ and time calculations throughout the application.
33
+
34
+ Zonebie can set this to a random timezone at the beginning of test runs.
35
+ Specifically for ActiveSupport, it sets `Time.zone`.
36
+
37
+ ### Test::Unit & Minitest
38
+
39
+ Add to `test/test_helper.rb`:
40
+
41
+ ```ruby
42
+ Zonebie.set_random_timezone
43
+ ```
44
+
45
+ ### RSpec
46
+
47
+ Add to `spec/spec_helper.rb`:
48
+
49
+ ```ruby
50
+ Zonebie.set_random_timezone
51
+ ```
52
+
53
+ ### Cucumber
54
+
55
+ Add a file `features/support/zonebie.rb` with the following contents:
56
+
57
+ ```ruby
58
+ Zonebie.set_random_timezone
59
+ ```
60
+
61
+ ## Usage with TZInfo
62
+
63
+ Zonebie can use the `tzinfo` gem, allowing it to work outside of ActiveSupport
64
+ (Rails).
65
+
66
+ However, `Zonebie.set_random_timezone` does not work outside of ActiveSupport
67
+ because there is not a concept of a global timezone setting. If you simply need
68
+ a random timezone for some other part of your tests, Zonebie can help.
69
+
70
+ ```ruby
71
+ zone = TZInfo::Timezone.get(Zonebie.random_timezone)
72
+ puts zone.now
73
+
74
+ # Also works in Rails/ActiveSupport
75
+ zone = ActiveSupport::TimeZone[Zonebie.random_timezone]
76
+ puts zone.now
77
+ ```
78
+
79
+ ## Reproducing Bugs
80
+
81
+ When `Zonebie.set_random_timezone` is called, Zonebie assigns a timezone and
82
+ prints a message to STDOUT:
83
+
84
+ ```
85
+ [Zonebie] Setting timezone to "Eastern Time (US & Canada)"
86
+ ```
87
+
88
+ If you would rather that Zonebie not print out this information during your tests,
89
+ put Zonebie in quiet mode before calling `set_random_timezone`:
90
+
91
+ ```ruby
92
+ Zonebie.quiet = true
93
+ ```
94
+
95
+ To rerun tests with a specific timezone (e.g., to reproduce a bug that only
96
+ seems present in one zone), set the `TZ` environment variable:
97
+
98
+ ```ruby
99
+ # Assuming tests run with simply `rake`
100
+ TZ="Eastern Time (US & Canada)" rake
101
+ ```
102
+
103
+ ## Contributing
104
+
105
+ 1. Fork it
106
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
107
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
108
+ 4. Push to the branch (`git push origin my-new-feature`)
109
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
data/lib/zonebie.rb ADDED
@@ -0,0 +1,53 @@
1
+ require File.expand_path("zonebie/version", File.dirname(__FILE__))
2
+
3
+ module Zonebie
4
+ class << self
5
+ attr_accessor :quiet
6
+
7
+ def backend
8
+ unless @backend
9
+ @backend = if @backends[:activesupport].usable?
10
+ @backends[:activesupport]
11
+ else
12
+ @backends.values.detect(&:usable?)
13
+ end
14
+ end
15
+
16
+ @backend
17
+ end
18
+
19
+ def backend=(backend)
20
+ case backend
21
+ when Symbol
22
+ @backend = @backends[backend]
23
+ else
24
+ @backend = backend
25
+ end
26
+
27
+ if !backend.nil? && @backend.nil?
28
+ raise ArgumentError, "Unsupported backend: #{backend}"
29
+ end
30
+
31
+ @backend
32
+ end
33
+
34
+ def add_backend(backend)
35
+ @backends ||= {}
36
+ @backends[backend.name] = backend
37
+ end
38
+
39
+ def set_random_timezone
40
+ zone = ENV['TZ'] || random_timezone
41
+
42
+ $stdout.puts("[Zonebie] Setting timezone to \"#{zone}\"") unless quiet
43
+ backend.zone = zone
44
+ end
45
+
46
+ def random_timezone
47
+ zones = backend.zones
48
+ zones[rand(zones.length)]
49
+ end
50
+ end
51
+ end
52
+
53
+ require File.expand_path("zonebie/backends", File.dirname(__FILE__))
@@ -0,0 +1,2 @@
1
+ require File.expand_path("backends/active_support", File.dirname(__FILE__))
2
+ require File.expand_path("backends/tzinfo", File.dirname(__FILE__))
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "active_support/time"
3
+ rescue LoadError
4
+ # not required; this backend will simply not be used
5
+ end
6
+
7
+ module Zonebie
8
+ module Backends
9
+ class ActiveSupport
10
+ class << self
11
+ def name
12
+ :activesupport
13
+ end
14
+
15
+ def zones
16
+ ::ActiveSupport::TimeZone.all.map(&:name)
17
+ end
18
+
19
+ def zone=(zone)
20
+ ::Time.zone = zone
21
+ end
22
+
23
+ def usable?
24
+ defined?(::ActiveSupport)
25
+ end
26
+ end
27
+
28
+ Zonebie.add_backend(self)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "tzinfo"
3
+ rescue LoadError
4
+ # not required; this backend will simply not be used
5
+ end
6
+
7
+ module Zonebie
8
+ module Backends
9
+ class TZInfo
10
+ class << self
11
+ def name
12
+ :tzinfo
13
+ end
14
+
15
+ def zones
16
+ ::TZInfo::Timezone.all.map(&:identifier)
17
+ end
18
+
19
+ def zone=(zone)
20
+ $stderr.puts("[Zonebie] It is not possible to set a global timezone with `tzinfo`")
21
+ end
22
+
23
+ def usable?
24
+ defined?(::TZInfo)
25
+ end
26
+ end
27
+
28
+ Zonebie.add_backend(self)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path("../zonebie", File.dirname(__FILE__))
2
+
3
+ RSpec.configure do |c|
4
+ c.before(:suite) do
5
+ Zonebie.set_random_timezone
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Zonebie
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+ require "active_support/time"
3
+
4
+ describe Zonebie do
5
+ before do
6
+ Zonebie.backend = :activesupport
7
+ end
8
+
9
+ describe "#set_random_timezone" do
10
+ it "assigns a random timezone" do
11
+ ::Time.expects(:zone=).with do |zone|
12
+ ActiveSupport::TimeZone.all.map(&:name).include? zone
13
+ end
14
+
15
+ $stdout.stubs(:puts)
16
+ Zonebie.set_random_timezone
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe Zonebie::Backends::ActiveSupport do
4
+ describe "#name" do
5
+ it "is :activesupport" do
6
+ described_class.name.should == :activesupport
7
+ end
8
+ end
9
+
10
+ describe "#zones" do
11
+ it "returns a list of zones provided by ActiveSupport" do
12
+ ::ActiveSupport::TimeZone.stubs(:all).
13
+ returns([
14
+ stub(:name => "Eastern Time (US & Canada)"),
15
+ stub(:name => "Central Time (US & Canada)")
16
+ ])
17
+
18
+ described_class.zones.should =~ ["Eastern Time (US & Canada)", "Central Time (US & Canada)"]
19
+ end
20
+ end
21
+
22
+ describe "#zone=" do
23
+ it "sets Time.zone provided by ActiveSupport" do
24
+ ::Time.expects(:zone=).with("Eastern Time (US & Canada)")
25
+
26
+ described_class.zone = "Eastern Time (US & Canada)"
27
+ end
28
+ end
29
+
30
+ describe "usable?" do
31
+ it "returns true if ActiveSupport is available" do
32
+ described_class.should be_usable
33
+ end
34
+
35
+ it "returns false if ActiveSupport is unavailable" do
36
+ old_active_support = ActiveSupport
37
+ Object.send(:remove_const, :ActiveSupport)
38
+ begin
39
+ described_class.should_not be_usable
40
+ ensure
41
+ ActiveSupport = old_active_support
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Zonebie::Backends::TZInfo do
4
+ describe "#name" do
5
+ it "is :tzinfo" do
6
+ described_class.name.should == :tzinfo
7
+ end
8
+ end
9
+
10
+ describe "#zones" do
11
+ it "returns a list of zones provided by TZInfo" do
12
+ ::TZInfo::Timezone.stubs(:all).
13
+ returns([
14
+ stub(:identifier => "America/Chicago"),
15
+ stub(:identifier => "America/New York")
16
+ ])
17
+
18
+ described_class.zones.should =~ ["America/Chicago", "America/New York"]
19
+ end
20
+ end
21
+
22
+ describe "#zone=" do
23
+ it "is a noop" do
24
+ $stderr.stubs(:puts)
25
+ described_class.zone = "America/Chicago"
26
+ end
27
+ end
28
+
29
+ describe "usable?" do
30
+ it "returns true if TZInfo is available" do
31
+ described_class.should be_usable
32
+ end
33
+
34
+ it "returns false if TZInfo is unavailable" do
35
+ old_tz_info = TZInfo
36
+ Object.send(:remove_const, :TZInfo)
37
+ begin
38
+ described_class.should_not be_usable
39
+ ensure
40
+ TZInfo = old_tz_info
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,97 @@
1
+ require "spec_helper"
2
+
3
+ describe Zonebie do
4
+ describe "#backend" do
5
+ it "defaults to the activesupport backend in the presence of activesupport" do
6
+ Zonebie.backend.name.should == :activesupport
7
+ end
8
+
9
+ it "allows setting the backend to tzinfo" do
10
+ Zonebie.backend = :tzinfo
11
+ Zonebie.backend.name.should == :tzinfo
12
+ end
13
+
14
+ it "defaults to tzinfo in the absense of activesupport" do
15
+ Zonebie::Backends::ActiveSupport.stubs(:usable?).returns(false)
16
+
17
+ Zonebie.backend.name.should == :tzinfo
18
+ end
19
+
20
+ it "does not allow setting the backend to an unsupported value" do
21
+ expect {
22
+ Zonebie.backend = :foobar
23
+ }.to raise_error(ArgumentError)
24
+ end
25
+ end
26
+
27
+ describe "#add_backend" do
28
+ it "allows registration of backends classes" do
29
+ expect {
30
+ Zonebie.backend = :my_new_backend
31
+ }.to raise_error(ArgumentError)
32
+
33
+ Zonebie.add_backend(stub(:name => :my_new_backend))
34
+ Zonebie.backend = :my_new_backend
35
+ end
36
+ end
37
+
38
+ describe "#set_random_timezone" do
39
+ let(:backend) {
40
+ stub_everything(:name => :my_awesome_backend,
41
+ :zones => ["Eastern Time (US & Canada)"])
42
+ }
43
+
44
+ before do
45
+ Zonebie.add_backend(backend)
46
+ Zonebie.backend = :my_awesome_backend
47
+ end
48
+
49
+ it "asks the backend to set the zone" do
50
+ $stdout.stubs(:puts)
51
+ backend.expects(:zone=).with("Eastern Time (US & Canada)")
52
+
53
+ Zonebie.set_random_timezone
54
+ end
55
+
56
+ it "outputs the timezone to STDOUT" do
57
+ $stdout.expects(:puts).with("[Zonebie] Setting timezone to \"Eastern Time (US & Canada)\"")
58
+ Zonebie.set_random_timezone
59
+ end
60
+
61
+ it "does not output the timezone to STDOUT if quiet mode is enabled" do
62
+ $stdout.expects(:puts).never
63
+
64
+ Zonebie.quiet = true
65
+ begin
66
+ Zonebie.set_random_timezone
67
+ ensure
68
+ Zonebie.quiet = false
69
+ end
70
+ end
71
+
72
+ it "sends the timezone as ENV[TZ] if present" do
73
+ $stdout.stubs(:puts)
74
+
75
+ backend.expects(:zone=).with("Pacific Time (US & Canada)")
76
+
77
+ ENV['TZ'] = "Pacific Time (US & Canada)"
78
+ Zonebie.set_random_timezone
79
+ end
80
+ end
81
+
82
+ describe "#random_timezone" do
83
+ let(:backend) {
84
+ stub_everything(:name => :my_awesome_backend,
85
+ :zones => ["Eastern Time (US & Canada)"])
86
+ }
87
+
88
+ before do
89
+ Zonebie.add_backend(backend)
90
+ Zonebie.backend = :my_awesome_backend
91
+ end
92
+
93
+ it "returns a random timezone" do
94
+ Zonebie.random_timezone.should == "Eastern Time (US & Canada)"
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path("../lib/zonebie", File.dirname(__FILE__))
2
+
3
+ require "mocha"
4
+ RSpec.configure do |c|
5
+ c.mock_with :mocha
6
+
7
+ c.before do
8
+ ENV.delete('TZ')
9
+ Zonebie.backend = nil # reset to default
10
+ end
11
+ end
data/zonebie.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/zonebie/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andy Lindeman"]
6
+ gem.email = ["alindeman@gmail.com"]
7
+ gem.description = %q{Runs your tests in a random timezone}
8
+ gem.summary = %q{Zonebie prevents bugs in code that deals with timezones by randomly assigning a zone on every run}
9
+ gem.homepage = "https://github.com/highgroove/zonebie"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "zonebie"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Zonebie::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec", "~>2.8"
20
+ gem.add_development_dependency "mocha", "~>0.10.0"
21
+
22
+ gem.add_development_dependency "activesupport", ">=2.3"
23
+ gem.add_development_dependency "tzinfo", ">=0.3"
24
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zonebie
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Andy Lindeman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ requirement: *id001
32
+ type: :development
33
+ name: rake
34
+ - !ruby/object:Gem::Dependency
35
+ prerelease: false
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 19
42
+ segments:
43
+ - 2
44
+ - 8
45
+ version: "2.8"
46
+ requirement: *id002
47
+ type: :development
48
+ name: rspec
49
+ - !ruby/object:Gem::Dependency
50
+ prerelease: false
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 55
57
+ segments:
58
+ - 0
59
+ - 10
60
+ - 0
61
+ version: 0.10.0
62
+ requirement: *id003
63
+ type: :development
64
+ name: mocha
65
+ - !ruby/object:Gem::Dependency
66
+ prerelease: false
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 5
73
+ segments:
74
+ - 2
75
+ - 3
76
+ version: "2.3"
77
+ requirement: *id004
78
+ type: :development
79
+ name: activesupport
80
+ - !ruby/object:Gem::Dependency
81
+ prerelease: false
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 13
88
+ segments:
89
+ - 0
90
+ - 3
91
+ version: "0.3"
92
+ requirement: *id005
93
+ type: :development
94
+ name: tzinfo
95
+ description: Runs your tests in a random timezone
96
+ email:
97
+ - alindeman@gmail.com
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files: []
103
+
104
+ files:
105
+ - .gitignore
106
+ - .travis.yml
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - lib/zonebie.rb
112
+ - lib/zonebie/backends.rb
113
+ - lib/zonebie/backends/active_support.rb
114
+ - lib/zonebie/backends/tzinfo.rb
115
+ - lib/zonebie/rspec.rb
116
+ - lib/zonebie/version.rb
117
+ - spec/integrations/activesupport_spec.rb
118
+ - spec/lib/zonebie/backends/active_support_spec.rb
119
+ - spec/lib/zonebie/backends/tzinfo_spec.rb
120
+ - spec/lib/zonebie_spec.rb
121
+ - spec/spec_helper.rb
122
+ - zonebie.gemspec
123
+ homepage: https://github.com/highgroove/zonebie
124
+ licenses: []
125
+
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ requirements: []
150
+
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.15
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: Zonebie prevents bugs in code that deals with timezones by randomly assigning a zone on every run
156
+ test_files:
157
+ - spec/integrations/activesupport_spec.rb
158
+ - spec/lib/zonebie/backends/active_support_spec.rb
159
+ - spec/lib/zonebie/backends/tzinfo_spec.rb
160
+ - spec/lib/zonebie_spec.rb
161
+ - spec/spec_helper.rb