fail_fast 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.txt CHANGED
@@ -1,3 +1,6 @@
1
+ 0.3.0
2
+ - added value_of()
3
+ - added checks not linked to a config file (FailFast().check do .. end)
1
4
  0.2.1
2
5
  - improve + colour the report template
3
6
  0.2.0
data/README.markdown CHANGED
@@ -7,7 +7,30 @@
7
7
  ## How to use :
8
8
 
9
9
 
10
- ### Case 1 : don't start the application if the DB cannot be reached.
10
+ ### Example 0 : don't start the application if '_why' cannot be found (on the PATH).
11
+
12
+ Insert this code early in your program starting sequence :
13
+
14
+ require 'fail_fast'
15
+ FailFast().check do
16
+ fail '_why could not be found on the path' unless `which _why` =~ /_why$/
17
+ end
18
+
19
+
20
+ If _\_why_ can't be found :
21
+ * the application will exit immediately and
22
+ * you will see this report :
23
+
24
+
25
+ +------------------------------------------------------------------------------------------
26
+ | FAIL_FAST error : precondition(s) not met.
27
+ +------------------------------------------------------------------------------------------
28
+ | _why could not be found on the path
29
+ +------------------------------------------------------------------------------------------
30
+
31
+
32
+
33
+ ### Example 1 : don't start the application if the DB cannot be reached.
11
34
 
12
35
  Early in your project boot sequence insert this code :
13
36
 
@@ -19,14 +42,13 @@ Early in your project boot sequence insert this code :
19
42
 
20
43
  If the DB connection fails,
21
44
  * the application will exit immediately and
22
- * you'll see this report :
45
+ * you will see this report :
23
46
 
24
47
 
25
48
  +------------------------------------------------------------------------------------------
26
49
  | FAIL_FAST error : precondition(s) not met in
27
50
  | -----------------
28
51
  | file : "path/to/database.yml"
29
- | keys prefix : (none)
30
52
  +------------------------------------------------------------------------------------------
31
53
  | error key value
32
54
  +------------------------------------------------------------------------------------------
@@ -34,9 +56,9 @@ If the DB connection fails,
34
56
  +------------------------------------------------------------------------------------------
35
57
 
36
58
  Remark : `check` will call `exit(1)` at the end of the first block with an error.
37
- If you want to collect and report all the errors before exiting, use `check_now.but_fail_later` (see case 2 below).
59
+ If you want to collect and report all the errors before exiting, use `check_now.but_fail_later` (see Example 2 below).
38
60
 
39
- ### Case 2 : collect errors in multiple blocks.
61
+ ### Example 2 : collect errors in multiple blocks.
40
62
 
41
63
 
42
64
  require 'fail_fast'
@@ -109,7 +131,7 @@ If it fails, you'll get a report like this :
109
131
  | * fail a custom failure message
110
132
  +------------------------------------------------------------------------------------------
111
133
 
112
- ### Case 3 : print an additional custom message if errors were detected
134
+ ### Example 3 : print an additional custom message if errors were detected
113
135
 
114
136
  ... # code like in the cases above.
115
137
 
@@ -123,8 +145,7 @@ If it fails, you'll get a report like this :
123
145
 
124
146
  ## Info :
125
147
 
126
- Failing fast is important.
127
- This gem DSL lets you write tests scripts that run early in the boot sequence of an application.
148
+ This gem DSL lets you write preconditions-scripts that you run early in the boot sequence of an application.
128
149
  An exception is raised if one or more tests fail, and you get a detailled report of all the problems encountered.
129
150
 
130
151
  Some rules are based on the contents of configuration files (database.yml, config.yml, etc...) :
@@ -169,6 +190,11 @@ You can also add custom rules, not related to any config files :
169
190
  has_url_for 'bug_tracker/url'
170
191
  has_email_for 'newsletter/to_address'
171
192
 
193
+ * customize it
194
+
195
+ nda_file = value_of(:nda_file)
196
+ fail 'NDA is too old' if (Time.now - File.mtime(nda_file)) > (24 * 60 * 60) * 365
197
+
172
198
  Test the file system :
173
199
 
174
200
  directory_exists_for 'assets-upload_dir'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/fail_fast.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fail_fast}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alain Ravet"]
12
- s.date = %q{2010-07-05}
12
+ s.date = %q{2010-07-17}
13
13
  s.description = %q{raises an error if the yaml contents of a config file does not pass a test script.}
14
14
  s.email = %q{alainravet@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -42,21 +42,22 @@ Gem::Specification.new do |s|
42
42
  "lib/fail_fast/support/error_details.rb",
43
43
  "lib/fail_fast/support/z_only_for_tests.rb",
44
44
  "show_all_errors.rb",
45
- "spec/base_commands_spec.rb",
46
- "spec/check_active_record_db_spec.rb",
47
- "spec/check_email_spec.rb",
48
- "spec/check_file_system_spec.rb",
49
- "spec/check_mongo_db_spec.rb",
50
- "spec/check_url_spec.rb",
51
- "spec/check_value_spec.rb",
52
- "spec/errors_storage_spec.rb",
53
- "spec/file_is_empty_spec.rb",
54
- "spec/file_is_missing_spec.rb",
45
+ "spec/base/base_commands_spec.rb",
46
+ "spec/base/errors_storage_spec.rb",
47
+ "spec/base/file_is_empty_spec.rb",
48
+ "spec/base/file_is_missing_spec.rb",
49
+ "spec/base/multiple_blocks_support_spec.rb",
50
+ "spec/base/not_linked_to_a_file_spec.rb",
51
+ "spec/base/report_printing_spec.rb",
52
+ "spec/extensions/check_active_record_db_spec.rb",
53
+ "spec/extensions/check_email_spec.rb",
54
+ "spec/extensions/check_file_system_spec.rb",
55
+ "spec/extensions/check_mongo_db_spec.rb",
56
+ "spec/extensions/check_url_spec.rb",
57
+ "spec/extensions/check_value_spec.rb",
55
58
  "spec/fixtures/empty.yml",
56
59
  "spec/fixtures/simple.yml",
57
60
  "spec/how_to_use_spec.rb",
58
- "spec/multiple_blocks_support_spec.rb",
59
- "spec/report_printing_spec.rb",
60
61
  "spec/spec.opts",
61
62
  "spec/spec_helper.rb"
62
63
  ]
@@ -66,19 +67,20 @@ Gem::Specification.new do |s|
66
67
  s.rubygems_version = %q{1.3.7}
67
68
  s.summary = %q{raises an error if the yaml contents of a config file does pass a test script.}
68
69
  s.test_files = [
69
- "spec/base_commands_spec.rb",
70
- "spec/check_active_record_db_spec.rb",
71
- "spec/check_email_spec.rb",
72
- "spec/check_file_system_spec.rb",
73
- "spec/check_mongo_db_spec.rb",
74
- "spec/check_url_spec.rb",
75
- "spec/check_value_spec.rb",
76
- "spec/errors_storage_spec.rb",
77
- "spec/file_is_empty_spec.rb",
78
- "spec/file_is_missing_spec.rb",
70
+ "spec/base/base_commands_spec.rb",
71
+ "spec/base/errors_storage_spec.rb",
72
+ "spec/base/file_is_empty_spec.rb",
73
+ "spec/base/file_is_missing_spec.rb",
74
+ "spec/base/multiple_blocks_support_spec.rb",
75
+ "spec/base/not_linked_to_a_file_spec.rb",
76
+ "spec/base/report_printing_spec.rb",
77
+ "spec/extensions/check_active_record_db_spec.rb",
78
+ "spec/extensions/check_email_spec.rb",
79
+ "spec/extensions/check_file_system_spec.rb",
80
+ "spec/extensions/check_mongo_db_spec.rb",
81
+ "spec/extensions/check_url_spec.rb",
82
+ "spec/extensions/check_value_spec.rb",
79
83
  "spec/how_to_use_spec.rb",
80
- "spec/multiple_blocks_support_spec.rb",
81
- "spec/report_printing_spec.rb",
82
84
  "spec/spec_helper.rb"
83
85
  ]
84
86
 
@@ -5,7 +5,7 @@ class FailFast
5
5
  def check(&block)
6
6
  fail_now_mode = block_given? # false in the case of *.check_now.but_fail_now do .. end
7
7
 
8
- @config_file_not_found = !File.exist?(@config_file_path)
8
+ @config_file_not_found = @config_file_path && !File.exist?(@config_file_path)
9
9
  if @config_file_not_found
10
10
  add_error ErrorDetails.new(nil, :config_file_not_found, @config_file_path)
11
11
  else
@@ -30,7 +30,7 @@ class FailFast
30
30
  private
31
31
 
32
32
  def check_all_rules(&block)
33
- @yaml_config_as_hash = YAML.load(ERB.new(File.read(@config_file_path)).result) || {}
33
+ @yaml_config_as_hash = (@config_file_path && YAML.load(ERB.new(File.read(@config_file_path)).result)) || {}
34
34
  self.instance_eval(&block)
35
35
  end
36
36
 
@@ -1,6 +1,14 @@
1
1
  class FailFast
2
2
  module CheckValue
3
3
 
4
+ # Usage
5
+ # nda_file = value_of(:nda_file)
6
+ #
7
+ def value_of(key)
8
+ key_value_regexp_options(key, []).value
9
+ end
10
+
11
+
4
12
  # Usage
5
13
  # has_values_for :sym_key, 'str_key'
6
14
  #
@@ -1,10 +1,15 @@
1
+ <% if @config_file_path %>
1
2
  +------------------------------------------------------------------------------------------
2
3
  | FAIL_FAST error : precondition(s) not met in
3
4
  | -----------------
4
5
  | file : "<%= @config_file_path %>"
5
- | keys prefix : <%= @keys_prefix ? @keys_prefix.inspect : 'none' %>
6
+ <% if @keys_prefix%>| keys prefix : <%= @keys_prefix ? @keys_prefix.inspect : 'none' %>
7
+ <% end %>+------------------------------------------------------------------------------------------
8
+ <% else %>
6
9
  +------------------------------------------------------------------------------------------
7
- <% @errors.each do |e|
10
+ | FAIL_FAST error : precondition(s) not met.
11
+ +------------------------------------------------------------------------------------------
12
+ <% end %><% @errors.each do |e|
8
13
  @msg = case e.kind
9
14
  when :config_file_not_found
10
15
  red("The config file could not be found") + " : '#{yellow(lred(e.value))}'."
data/lib/fail_fast.rb CHANGED
@@ -5,7 +5,7 @@ Dir.glob(File.dirname(__FILE__) + '/fail_fast/extensions/*.rb') {|file| require
5
5
 
6
6
 
7
7
  # alternative syntax
8
- def FailFast(config_file_path, keys_prefix=nil)
8
+ def FailFast(config_file_path=nil, keys_prefix=nil)
9
9
  FailFast.new(config_file_path, keys_prefix)
10
10
  end
11
11
 
data/show_all_errors.rb CHANGED
@@ -29,6 +29,9 @@ require 'fail_fast'
29
29
  FailFast('unknown-file').check_now.but_fail_later do
30
30
  end
31
31
 
32
+ FailFast().check_now.but_fail_later do
33
+ fail '_why could not be found on the path' unless `which _why` =~ /_why$/
34
+ end
32
35
 
33
36
  FailFast(SPEC_DIR + '/fixtures/simple.yml').check_now.but_fail_later do
34
37
 
@@ -44,6 +47,9 @@ FailFast(SPEC_DIR + '/fixtures/simple.yml').check_now.but_fail_later do
44
47
  fake_http_server
45
48
  has_url_for 'test/host' # value is not a url
46
49
 
50
+ nda_file = value_of(:nda_file)
51
+ fail 'NDA is too old' if (Time.now - File.mtime(nda_file)) > (24 * 60 * 60) * 365
52
+
47
53
  #test http server :
48
54
  has_url_for 'test/url_not_reachable', :reachable => true #server is not reachable
49
55
 
@@ -69,6 +75,13 @@ FailFast(SPEC_DIR + '/fixtures/simple.yml').check_now.but_fail_later do
69
75
 
70
76
  #misc
71
77
  fail 'a custom failure message'
78
+
79
+ #handmade :
80
+ fail 'zruby is not on the path' unless `which zruby` =~ /zruby$/
81
+
82
+ nda_file = value_of(:nda_file)
83
+ fail 'NDA is too old' if (Time.now - File.mtime(nda_file)) > (24 * 60 * 60) * 365
84
+
72
85
  end
73
86
 
74
87
  if FailFast.failed?
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "fail()" do
4
4
  it_should_raise_a_direct_error('message-123', :fail, 'when fail() is called') {
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "errors" do
4
4
  before(:each) { capture_stdout }
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "ConfigCheck on an empty file" do
4
4
  before(:each) { capture_stdout }
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "ConfigCheck on an unknown file" do
4
4
  before(:all) { capture_stdout }
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "FailFast.check_now.but_fail_later" do
4
4
  before(:all) { capture_stdout }
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "check not linked to a config file" do
4
+ before(:each) { capture_stdout }
5
+ after( :each) { restore_stdout }
6
+
7
+
8
+ it "should not raise an error when there are no checks" do
9
+ lambda {
10
+ FailFast().check do end
11
+ }.should_not raise_error
12
+ FailFast.failed?.should be_false
13
+ end
14
+
15
+ it "should raise an error when there is a failing check" do
16
+ lambda {
17
+ FailFast().check do
18
+ fail 'error'
19
+ end
20
+ }.should raise_error(ExitTriggered)
21
+ FailFast.failed?.should be_true
22
+ end
23
+
24
+ it "should raise a delayed error when there is a failing check" do
25
+ lambda {
26
+ FailFast().check_now.but_fail_later do
27
+ fail 'error'
28
+ end
29
+ }.should_not raise_error
30
+ lambda {
31
+ FailFast.fail_now
32
+ }.should raise_error(ExitTriggered)
33
+ FailFast.global_errors.collect(&:kind).should == [:fail]
34
+ end
35
+
36
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "the printed error report" do
4
4
  before(:each) { capture_stdout }
@@ -9,13 +9,13 @@ describe "the printed error report" do
9
9
  FailFast(SIMPLE_FILE_PATH).check { has_value_for :anykey }
10
10
  rescue
11
11
  end
12
- $stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*missing_value/mi)
12
+ $stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*missing value/mi)
13
13
  end
14
14
 
15
15
 
16
16
  it "contains an error details, even when we delay the failing" do
17
17
  FailFast(SIMPLE_FILE_PATH).check_now.but_fail_later { has_value_for :unknown_key }
18
- $stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*missing_value/mi)
18
+ $stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*missing value/mi)
19
19
  $stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*unknown_key/mi)
20
20
  end
21
21
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe 'has_ar_db' do
4
4
  context '' do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe 'has_email_for()' do
4
4
  it_should_not_raise_an_error('when the value is an email') {
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  a_dir = SPEC_DIR + '/fixtures'
4
4
  a_file = SPEC_DIR + '/fixtures/simple.yml'
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe 'has_mongoDB' do
4
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe 'has_url_for()' do
4
4
  before(:all) do
@@ -1,4 +1,31 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'value_of()' do
4
+ it 'should return nil when the value is blank' do
5
+ FailFast(SIMPLE_FILE_PATH).check { @@val = value_of(:key_with_blank_value) }
6
+ @@val.should be_nil
7
+ end
8
+
9
+ it 'should return nil when the key is unknown' do
10
+ FailFast(EMPTY_FILE_PATH).check { @@val = value_of(:anykey) }
11
+ @@val.should be_nil
12
+ end
13
+ it 'should return the value when it is a leaf'do
14
+ FailFast(SIMPLE_FILE_PATH).check { @@val = value_of(:first_key) }
15
+ @@val.should == 'premier'
16
+ end
17
+
18
+ it 'should return the values in a hash when the value is a tree' do
19
+ FailFast(SIMPLE_FILE_PATH).check { @@val = value_of(:test) }
20
+ @@val.should be_a(Hash)
21
+ @@val['mongoDB']['host'].should == 'localhost'
22
+ end
23
+
24
+ it 'should preprend the prefix to the key' do
25
+ FailFast(SIMPLE_FILE_PATH, 'test').check { @@val = value_of(:mongoDB) }
26
+ @@val['host'].should == 'localhost'
27
+ end
28
+ end
2
29
 
3
30
  describe 'has_value_for()' do
4
31
 
@@ -30,3 +30,4 @@ db_connection:
30
30
 
31
31
  number_six: 6
32
32
  letter_x: x
33
+ nda_file: <%= SPEC_DIR + '/fixtures/simple.yml' %>
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fail_fast
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alain Ravet
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-05 00:00:00 +02:00
18
+ date: 2010-07-17 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -97,21 +97,22 @@ files:
97
97
  - lib/fail_fast/support/error_details.rb
98
98
  - lib/fail_fast/support/z_only_for_tests.rb
99
99
  - show_all_errors.rb
100
- - spec/base_commands_spec.rb
101
- - spec/check_active_record_db_spec.rb
102
- - spec/check_email_spec.rb
103
- - spec/check_file_system_spec.rb
104
- - spec/check_mongo_db_spec.rb
105
- - spec/check_url_spec.rb
106
- - spec/check_value_spec.rb
107
- - spec/errors_storage_spec.rb
108
- - spec/file_is_empty_spec.rb
109
- - spec/file_is_missing_spec.rb
100
+ - spec/base/base_commands_spec.rb
101
+ - spec/base/errors_storage_spec.rb
102
+ - spec/base/file_is_empty_spec.rb
103
+ - spec/base/file_is_missing_spec.rb
104
+ - spec/base/multiple_blocks_support_spec.rb
105
+ - spec/base/not_linked_to_a_file_spec.rb
106
+ - spec/base/report_printing_spec.rb
107
+ - spec/extensions/check_active_record_db_spec.rb
108
+ - spec/extensions/check_email_spec.rb
109
+ - spec/extensions/check_file_system_spec.rb
110
+ - spec/extensions/check_mongo_db_spec.rb
111
+ - spec/extensions/check_url_spec.rb
112
+ - spec/extensions/check_value_spec.rb
110
113
  - spec/fixtures/empty.yml
111
114
  - spec/fixtures/simple.yml
112
115
  - spec/how_to_use_spec.rb
113
- - spec/multiple_blocks_support_spec.rb
114
- - spec/report_printing_spec.rb
115
116
  - spec/spec.opts
116
117
  - spec/spec_helper.rb
117
118
  has_rdoc: true
@@ -149,17 +150,18 @@ signing_key:
149
150
  specification_version: 3
150
151
  summary: raises an error if the yaml contents of a config file does pass a test script.
151
152
  test_files:
152
- - spec/base_commands_spec.rb
153
- - spec/check_active_record_db_spec.rb
154
- - spec/check_email_spec.rb
155
- - spec/check_file_system_spec.rb
156
- - spec/check_mongo_db_spec.rb
157
- - spec/check_url_spec.rb
158
- - spec/check_value_spec.rb
159
- - spec/errors_storage_spec.rb
160
- - spec/file_is_empty_spec.rb
161
- - spec/file_is_missing_spec.rb
153
+ - spec/base/base_commands_spec.rb
154
+ - spec/base/errors_storage_spec.rb
155
+ - spec/base/file_is_empty_spec.rb
156
+ - spec/base/file_is_missing_spec.rb
157
+ - spec/base/multiple_blocks_support_spec.rb
158
+ - spec/base/not_linked_to_a_file_spec.rb
159
+ - spec/base/report_printing_spec.rb
160
+ - spec/extensions/check_active_record_db_spec.rb
161
+ - spec/extensions/check_email_spec.rb
162
+ - spec/extensions/check_file_system_spec.rb
163
+ - spec/extensions/check_mongo_db_spec.rb
164
+ - spec/extensions/check_url_spec.rb
165
+ - spec/extensions/check_value_spec.rb
162
166
  - spec/how_to_use_spec.rb
163
- - spec/multiple_blocks_support_spec.rb
164
- - spec/report_printing_spec.rb
165
167
  - spec/spec_helper.rb