fail_fast 0.1.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +5 -0
- data/README.markdown +109 -49
- data/VERSION +1 -1
- data/fail_fast.gemspec +36 -24
- data/lib/fail_fast/base/base.rb +44 -0
- data/lib/fail_fast/base/utils.rb +58 -0
- data/lib/fail_fast/base/z_only_for_tests.rb +10 -0
- data/lib/fail_fast/{misc.rb → extensions/base_commands.rb} +3 -3
- data/lib/fail_fast/{check_active_record_db.rb → extensions/check_active_record_db.rb} +17 -52
- data/lib/fail_fast/{check_email.rb → extensions/check_email.rb} +1 -1
- data/lib/fail_fast/{check_file_system.rb → extensions/check_file_system.rb} +4 -4
- data/lib/fail_fast/{check_mongo_db.rb → extensions/check_mongo_db.rb} +4 -4
- data/lib/fail_fast/{check_url.rb → extensions/check_url.rb} +2 -2
- data/lib/fail_fast/extensions/check_value.rb +40 -0
- data/lib/fail_fast/main.rb +16 -71
- data/lib/fail_fast/report.txt.erb +45 -6
- data/lib/fail_fast/support/error_db.rb +23 -0
- data/lib/fail_fast/support/error_details.rb +10 -0
- data/lib/fail_fast/support/z_only_for_tests.rb +6 -0
- data/lib/fail_fast.rb +6 -10
- data/show_all_errors.rb +12 -1
- data/spec/errors_storage_spec.rb +18 -0
- data/spec/file_is_empty_spec.rb +32 -4
- data/spec/file_is_missing_spec.rb +5 -3
- data/spec/multiple_blocks_support_spec.rb +37 -0
- data/spec/report_printing_spec.rb +29 -0
- data/spec/spec_helper.rb +44 -18
- metadata +37 -25
- data/lib/fail_fast/check_value.rb +0 -38
- /data/spec/{misc_spec.rb → base_commands_spec.rb} +0 -0
- /data/spec/{has_active_record_db_spec.rb → check_active_record_db_spec.rb} +0 -0
- /data/spec/{has_email_for_spec.rb → check_email_spec.rb} +0 -0
- /data/spec/{file_system_spec.rb → check_file_system_spec.rb} +0 -0
- /data/spec/{has_mongoDB_for_spec.rb → check_mongo_db_spec.rb} +0 -0
- /data/spec/{has_url_for_spec.rb → check_url_spec.rb} +0 -0
- /data/spec/{has_value_for_spec.rb → check_value_spec.rb} +0 -0
data/lib/fail_fast/main.rb
CHANGED
@@ -1,89 +1,34 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
require 'erb'
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/support/error_db')
|
3
4
|
|
4
5
|
class FailFast
|
5
|
-
ERB_TEMPLATE = File.dirname(__FILE__) + '/report.txt.erb'
|
6
6
|
|
7
|
-
|
8
|
-
class ErrorDetails < Struct.new(:key, :kind, :value) ;
|
9
|
-
def has_key_and_kind?(akey, akind)
|
10
|
-
(key.to_s == akey.to_s) && kind.to_sym == akind.to_sym
|
11
|
-
end
|
12
|
-
def has_value_and_kind?(avalue, akind)
|
13
|
-
(value.to_s == avalue.to_s) && kind.to_sym == akind.to_sym
|
14
|
-
end
|
15
|
-
end
|
16
|
-
class Params < Struct.new(:key, :value, :regexp, :options) ; end
|
7
|
+
@@_errors_db = FailFast::ErrorDb.new
|
17
8
|
|
18
|
-
def initialize(
|
19
|
-
@
|
20
|
-
@
|
21
|
-
|
9
|
+
def initialize(config_file_path, keys_prefix=nil)
|
10
|
+
@config_file_path = config_file_path
|
11
|
+
@keys_prefix = keys_prefix
|
12
|
+
@errors_key = ErrorDb.key_for(config_file_path, keys_prefix)
|
22
13
|
end
|
23
14
|
|
24
|
-
def self.
|
25
|
-
|
15
|
+
def self.fail_now
|
16
|
+
exit(1) unless errors_db.keys.empty?
|
26
17
|
end
|
27
18
|
|
28
|
-
def
|
29
|
-
|
30
|
-
FailFast.errors << ErrorDetails.new(nil, :config_file_not_found, @path)
|
31
|
-
else
|
32
|
-
@hash = YAML.load(ERB.new(File.read(@path)).result) || {}
|
33
|
-
self.instance_eval(&block)
|
34
|
-
end
|
35
|
-
raise_and_print_errors if errors?
|
19
|
+
def self.failed?
|
20
|
+
!global_errors.empty?
|
36
21
|
end
|
37
22
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
def blank?( value) value.nil? || value.is_a?(String) && ''==value.strip end
|
42
|
-
def range?( value) value.is_a?(Range ) end
|
43
|
-
def regexp?(value) value.is_a?(Regexp) end
|
44
|
-
def array?( value) value.is_a?(Array ) end
|
45
|
-
def hash?( value) value.is_a?(Hash) end
|
46
|
-
def missing_file?(path) !File.exist?(path) end
|
47
|
-
|
48
|
-
# Usage
|
49
|
-
# value_for_deep_key('one/two/three')
|
50
|
-
# returns
|
51
|
-
# @hash[:one][:two][:three]
|
52
|
-
#
|
53
|
-
def value_for_deep_key(key)
|
54
|
-
key.to_s.split('/').inject(@hash) { |h, k| h[k] } rescue nil
|
23
|
+
def self.errors_db
|
24
|
+
@@_errors_db
|
55
25
|
end
|
56
26
|
|
57
|
-
|
58
|
-
|
59
|
-
last = params.pop
|
60
|
-
if last.is_a?(Hash)
|
61
|
-
options = last
|
62
|
-
else
|
63
|
-
params << last
|
64
|
-
options = {}
|
65
|
-
end
|
66
|
-
|
67
|
-
last = params.pop
|
68
|
-
if last.is_a?(Regexp)
|
69
|
-
regexp = last
|
70
|
-
else
|
71
|
-
params << last
|
72
|
-
end
|
73
|
-
|
74
|
-
key = "#{@prefix}/#{key}" if @prefix
|
75
|
-
value = value_for_deep_key(key)
|
76
|
-
|
77
|
-
Params.new(key, value, regexp, options)
|
27
|
+
def add_error(value)
|
28
|
+
@@_errors_db.append(@errors_key, value)
|
78
29
|
end
|
79
30
|
|
80
|
-
def errors
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
|
85
|
-
def raise_and_print_errors
|
86
|
-
@errors = @@errors
|
87
|
-
raise "\n\n\n" + ERB.new(File.read(ERB_TEMPLATE)).result(binding) + "\n\n"
|
31
|
+
def errors
|
32
|
+
@@_errors_db.errors_for(@errors_key)
|
88
33
|
end
|
89
34
|
end
|
@@ -1,8 +1,47 @@
|
|
1
1
|
+------------------------------------------------------------------------------------------
|
2
|
-
| FAIL_FAST error :
|
3
|
-
|
|
2
|
+
| FAIL_FAST error : precondition(s) not met in
|
3
|
+
| -----------------
|
4
|
+
| file : "<%= @config_file_path %>"
|
5
|
+
| keys prefix : <%= @keys_prefix ? @keys_prefix.inspect : 'none' %>
|
4
6
|
+------------------------------------------------------------------------------------------
|
5
|
-
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
<% @errors.each do |e|
|
8
|
+
@msg = case e.kind
|
9
|
+
when :config_file_not_found
|
10
|
+
red("The config file could not be found") + " : '#{yellow(lred(e.value))}'."
|
11
|
+
when :missing_value
|
12
|
+
red("Missing value") +" for the key '#{yellow(e.key)}'."
|
13
|
+
when :value_does_not_match
|
14
|
+
red("Invalid value") +" '#{lred(e.value)}' for the key '#{yellow(e.key)}'."
|
15
|
+
when :not_an_email
|
16
|
+
red("Invalid email address") + " '#{lred(e.value)}' for the key '#{yellow(e.key)}'."
|
17
|
+
when :not_a_url
|
18
|
+
red("Invalid url") + " '#{lred(e.value)}' for the key '#{yellow(e.key)}'."
|
19
|
+
when :url_not_reachable
|
20
|
+
red("Could not reach the url") + " '#{lred(e.value)}' for the key '#{yellow(e.key)}'."
|
21
|
+
when :directory_not_found
|
22
|
+
e.key ?
|
23
|
+
red("Missing directory") + " '#{lred(e.value)}' for the key '#{yellow(e.key)}'." :
|
24
|
+
red("Missing directory") + " '#{lred(e.value)}'."
|
25
|
+
when :file_not_found
|
26
|
+
e.key ?
|
27
|
+
red("Missing file") + " '#{lred(e.value)}' for the key '#{yellow(e.key)}'." :
|
28
|
+
red("Missing file") + " '#{lred(e.value)}'."
|
29
|
+
when :mongoDB_server_not_found
|
30
|
+
e.key ?
|
31
|
+
red("Could not connect to the mongoDb server") + " '#{lred(e.value)} for the key '#{yellow(e.key)}'." :
|
32
|
+
red("Could not connect to the mongoDb server") + " '#{lred(e.value)}."
|
33
|
+
when :mongoDB_db_not_found
|
34
|
+
e.key ?
|
35
|
+
red("Could not open the mongoDb db") + " '#{lred(e.value)} for the key '#{yellow(e.key)}'." :
|
36
|
+
red("Could not open the mongoDb db") + " '#{lred(e.value)}."
|
37
|
+
when :active_record_db_connection_error
|
38
|
+
e.key ?
|
39
|
+
red("Could not connect to the DB server") + " '#{lred(e.value)} for the key '#{yellow(e.key)}'." :
|
40
|
+
red("Could not connect to the DB server") + " '#{lred(e.value)}."
|
41
|
+
when :fail
|
42
|
+
red(lred(e.value))
|
43
|
+
else "%-38s %-35s %-30s \n" % [ e.kind, e.key, lred(e.value)]
|
44
|
+
end
|
45
|
+
|
46
|
+
%>| <%= @msg + "\n"%><% end
|
47
|
+
%>+------------------------------------------------------------------------------------------
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class FailFast
|
2
|
+
class ErrorDb
|
3
|
+
def initialize
|
4
|
+
@@hash = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def errors_for(key)
|
8
|
+
@@hash[key] ||= []
|
9
|
+
end
|
10
|
+
|
11
|
+
def append(key, value)
|
12
|
+
errors_for(key) << value
|
13
|
+
end
|
14
|
+
|
15
|
+
def keys
|
16
|
+
@@hash.keys
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.key_for(config_file_path, keys_prefix=nil)
|
20
|
+
["<#{config_file_path}>", keys_prefix].compact.join
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class FailFast::ErrorDetails < Struct.new(:key, :kind, :value)
|
2
|
+
|
3
|
+
def has_key_and_kind?(akey, akind)
|
4
|
+
(key.to_s == akey.to_s) && kind.to_sym == akind.to_sym
|
5
|
+
end
|
6
|
+
|
7
|
+
def has_value_and_kind?(avalue, akind)
|
8
|
+
(value.to_s == avalue.to_s) && kind.to_sym == akind.to_sym
|
9
|
+
end
|
10
|
+
end
|
data/lib/fail_fast.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
1
|
require 'fail_fast/main'
|
2
|
+
Dir.glob(File.dirname(__FILE__) + '/fail_fast/support/*.rb' ) {|file| require file }
|
3
|
+
Dir.glob(File.dirname(__FILE__) + '/fail_fast/base/*.rb' ) {|file| require file }
|
4
|
+
Dir.glob(File.dirname(__FILE__) + '/fail_fast/extensions/*.rb') {|file| require file }
|
2
5
|
|
3
|
-
require 'fail_fast/check_value'
|
4
|
-
require 'fail_fast/check_file_system'
|
5
|
-
require 'fail_fast/check_mongo_db'
|
6
|
-
require 'fail_fast/check_active_record_db'
|
7
|
-
require 'fail_fast/check_url'
|
8
|
-
require 'fail_fast/check_email'
|
9
|
-
require 'fail_fast/misc'
|
10
6
|
|
11
|
-
|
12
|
-
def FailFast(
|
13
|
-
FailFast.new(
|
7
|
+
# alternative syntax
|
8
|
+
def FailFast(config_file_path, keys_prefix=nil)
|
9
|
+
FailFast.new(config_file_path, keys_prefix)
|
14
10
|
end
|
15
11
|
|
data/show_all_errors.rb
CHANGED
@@ -24,7 +24,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)+'/lib')
|
|
24
24
|
SPEC_DIR = File.dirname(__FILE__)+'/spec'
|
25
25
|
|
26
26
|
require 'fail_fast'
|
27
|
-
|
27
|
+
|
28
|
+
|
29
|
+
FailFast('unknown-file').check_now.but_fail_later do
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
FailFast(SPEC_DIR + '/fixtures/simple.yml').check_now.but_fail_later do
|
28
34
|
|
29
35
|
#test values :
|
30
36
|
has_value_for :first_keyNOT # single absent key
|
@@ -64,3 +70,8 @@ FailFast(SPEC_DIR + '/fixtures/simple.yml').check do
|
|
64
70
|
#misc
|
65
71
|
fail 'a custom failure message'
|
66
72
|
end
|
73
|
+
|
74
|
+
if FailFast.failed?
|
75
|
+
puts "cannot start the application due to the problems mentioned above"
|
76
|
+
FailFast.fail_now
|
77
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "errors" do
|
4
|
+
before(:each) { capture_stdout }
|
5
|
+
after( :each) { restore_stdout }
|
6
|
+
|
7
|
+
it "should use the filename and the prefix as key" do
|
8
|
+
begin
|
9
|
+
FailFast('invalid_file_path', 'a_prefix').check() do
|
10
|
+
has_value_for :unknown
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
end
|
14
|
+
|
15
|
+
FailFast.errors_db.should have(1).keys
|
16
|
+
FailFast.errors_db.keys.should == ["<invalid_file_path>a_prefix"]
|
17
|
+
end
|
18
|
+
end
|
data/spec/file_is_empty_spec.rb
CHANGED
@@ -1,8 +1,36 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "ConfigCheck on an empty file" do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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(EMPTY_FILE_PATH).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(EMPTY_FILE_PATH).check do
|
18
|
+
has_value_for :anykey
|
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(EMPTY_FILE_PATH).check_now.but_fail_later do
|
27
|
+
has_value_for :anykey
|
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 == [:missing_value]
|
34
|
+
end
|
35
|
+
|
8
36
|
end
|
@@ -1,17 +1,19 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "ConfigCheck on an unknown file" do
|
4
|
+
before(:all) { capture_stdout }
|
5
|
+
after( :all) { restore_stdout }
|
4
6
|
|
5
7
|
it "should not raise an error in new()" do
|
6
8
|
FailFast(UNKNOWN_FILE_PATH)
|
7
|
-
fail
|
9
|
+
fail if FailFast.failed?
|
8
10
|
end
|
9
11
|
|
10
12
|
it "should raise an error in fail_fast()" do
|
11
13
|
lambda {
|
12
14
|
FailFast(UNKNOWN_FILE_PATH).check do end
|
13
|
-
}.should raise_error
|
14
|
-
fail unless FailFast.
|
15
|
+
}.should raise_error(ExitTriggered)
|
16
|
+
fail unless FailFast.global_errors.collect(&:kind) == [:config_file_not_found]
|
15
17
|
end
|
16
18
|
|
17
19
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "FailFast.check_now.but_fail_later" do
|
4
|
+
before(:all) { capture_stdout }
|
5
|
+
after( :all) { restore_stdout }
|
6
|
+
|
7
|
+
it 'should not raise an error on the check(..) call' do
|
8
|
+
lambda {
|
9
|
+
FailFast(UNKNOWN_FILE_PATH).check_now.but_fail_later {}
|
10
|
+
}.should_not raise_error
|
11
|
+
FailFast.global_errors.collect(&:kind).should == [:config_file_not_found]
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when there are errors in the first 2 of 3 blocks' do
|
15
|
+
before(:each) do
|
16
|
+
FailFast(UNKNOWN_FILE_PATH).check_now.but_fail_later {}
|
17
|
+
FailFast(SIMPLE_FILE_PATH ).check_now.but_fail_later {
|
18
|
+
has_value_for 'AN-UNKNOWN-KEY'
|
19
|
+
has_value_for 'AN-UNKNOWN-KEY-2'
|
20
|
+
}
|
21
|
+
FailFast(EMPTY_FILE_PATH ).check_now.but_fail_later {} #no errors in the last block
|
22
|
+
end
|
23
|
+
|
24
|
+
it("should detect and collect all the errors") {
|
25
|
+
FailFast.errors_db.errors_for(FailFast::ErrorDb.key_for(UNKNOWN_FILE_PATH)).collect(&:kind).should == [:config_file_not_found]
|
26
|
+
FailFast.errors_db.errors_for(FailFast::ErrorDb.key_for(SIMPLE_FILE_PATH )).collect(&:kind).should == [:missing_value, :missing_value]
|
27
|
+
FailFast.errors_db.errors_for(FailFast::ErrorDb.key_for(EMPTY_FILE_PATH )).collect(&:kind).should == []
|
28
|
+
}
|
29
|
+
context "after FailFast.fail_now" do
|
30
|
+
it "should raise an error" do
|
31
|
+
lambda {
|
32
|
+
FailFast.fail_now
|
33
|
+
}.should raise_error(ExitTriggered)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "the printed error report" do
|
4
|
+
before(:each) { capture_stdout }
|
5
|
+
after( :each) { restore_stdout }
|
6
|
+
|
7
|
+
it "contains an error details" do
|
8
|
+
begin
|
9
|
+
FailFast(SIMPLE_FILE_PATH).check { has_value_for :anykey }
|
10
|
+
rescue
|
11
|
+
end
|
12
|
+
$stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*missing_value/mi)
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
it "contains an error details, even when we delay the failing" do
|
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)
|
19
|
+
$stdout.string.should match(/error.*#{SIMPLE_FILE_PATH}.*unknown_key/mi)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "contains all errors details, in the right order, when they appear in 2 separate blocks" do
|
23
|
+
FailFast(EMPTY_FILE_PATH ).check_now.but_fail_later { has_value_for :unknown_key_1 }
|
24
|
+
FailFast(SIMPLE_FILE_PATH).check_now.but_fail_later { has_value_for :unknown_key_2 }
|
25
|
+
|
26
|
+
$stdout.string.should match(/error.*#{EMPTY_FILE_PATH }.*unknown_key_1.*#{SIMPLE_FILE_PATH}.*unknown_key_2/m)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'rubygems'
|
1
2
|
require 'fail_fast'
|
2
3
|
require 'spec'
|
3
4
|
require 'spec/autorun'
|
@@ -9,69 +10,90 @@ UNKNOWN_FILE_PATH = 'an_unknown_file_path'
|
|
9
10
|
EMPTY_FILE_PATH = File.expand_path(File.dirname(__FILE__) + '/fixtures/empty.yml')
|
10
11
|
SIMPLE_FILE_PATH = File.expand_path(File.dirname(__FILE__) + '/fixtures/simple.yml')
|
11
12
|
|
13
|
+
class ExitTriggered < StandardError ; end
|
14
|
+
module Kernel
|
15
|
+
def exit(param=nil)
|
16
|
+
raise ExitTriggered.new('Kernel.exit was called')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
12
20
|
|
13
21
|
module DSLMacros
|
22
|
+
module InstanceMethods
|
23
|
+
def capture_stdout
|
24
|
+
@@original_stdout = STDOUT
|
25
|
+
$stdout = StringIO.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def restore_stdout
|
29
|
+
$stdout = @@original_stdout
|
30
|
+
end
|
31
|
+
end
|
14
32
|
module ClassMethods
|
33
|
+
|
15
34
|
def it_should_not_raise_an_error(msg, &block)
|
16
35
|
it "should not raise an error #{msg}" do
|
36
|
+
capture_stdout
|
17
37
|
begin
|
18
38
|
FailFast(SIMPLE_FILE_PATH).check do
|
19
|
-
raise "BUG : @@errorz should be empty \n#{
|
39
|
+
raise "BUG : @@errorz should be empty \n#{errors.inspect}" unless errors.empty?
|
20
40
|
self.instance_eval(&block)
|
21
41
|
end
|
22
42
|
rescue => e
|
23
43
|
raise e
|
24
44
|
ensure
|
25
|
-
if
|
26
|
-
fail "ZZshould not have raised an error, but it raised\n#{FailFast.
|
45
|
+
if FailFast.failed?
|
46
|
+
fail "ZZshould not have raised an error, but it raised\n#{FailFast.global_errors.join("\n")}"
|
27
47
|
end
|
28
48
|
end
|
29
|
-
|
49
|
+
restore_stdout
|
30
50
|
end
|
31
51
|
end
|
32
52
|
|
33
53
|
def it_should_raise_an_error(key, kind, msg, &block)
|
34
54
|
it "should raise an error #{kind}-#{key}-#{msg}" do
|
55
|
+
capture_stdout
|
35
56
|
begin
|
36
57
|
FailFast(SIMPLE_FILE_PATH).check do
|
37
|
-
raise "BUG : @@errorz should be empty \n#{
|
58
|
+
raise "BUG : @@errorz should be empty \n#{errors.inspect}" unless errors.empty?
|
38
59
|
self.instance_eval(&block)
|
39
60
|
end
|
40
61
|
rescue => e
|
41
62
|
# uncomment the next line after the refactoring/once error are no longer raise
|
42
63
|
# raise e
|
43
64
|
ensure
|
44
|
-
if FailFast.
|
65
|
+
if !FailFast.failed?
|
45
66
|
fail "\ne2d\nshould have raised a #{kind} error for #{key} \n==#{e}"
|
46
|
-
elsif FailFast.
|
47
|
-
fail "\ne2e\nshould have raised a #{kind.inspect} error for #{key.inspect}, but raised instead #{FailFast.
|
48
|
-
elsif 2 <= FailFast.
|
49
|
-
fail "\ne2f\nshould have raised only a #{kind} error for #{key}\n#{FailFast.
|
67
|
+
elsif FailFast.global_errors.length == 1 && !FailFast.global_errors.first.has_key_and_kind?(key, kind)
|
68
|
+
fail "\ne2e\nshould have raised a #{kind.inspect} error for #{key.inspect}, but raised instead #{FailFast.global_errors.inspect}"
|
69
|
+
elsif 2 <= FailFast.global_errors.length
|
70
|
+
fail "\ne2f\nshould have raised only a #{kind} error for #{key}\n#{FailFast.global_errors.join("\n")}"
|
50
71
|
end
|
51
72
|
end
|
52
|
-
|
73
|
+
restore_stdout
|
53
74
|
end
|
54
75
|
end
|
55
76
|
def it_should_raise_a_direct_error(value, kind, msg, &block)
|
56
77
|
it "should raise an error #{kind}-#{value}-#{msg}" do
|
78
|
+
capture_stdout
|
57
79
|
begin
|
58
80
|
FailFast(SIMPLE_FILE_PATH).check do
|
59
|
-
raise "BUG : @@errorz should be empty \n#{
|
81
|
+
raise "BUG : @@errorz should be empty \n#{errors.inspect}" unless errors.empty?
|
60
82
|
self.instance_eval(&block)
|
61
83
|
end
|
62
84
|
rescue => e
|
63
85
|
# uncomment the next line after the refactoring/once error are no longer raise
|
64
86
|
# raise e
|
65
87
|
ensure
|
66
|
-
if FailFast.
|
88
|
+
if !FailFast.failed?
|
67
89
|
fail "\ne2d\nshould have raised a #{kind} error for #{value} \n==#{e}"
|
68
|
-
elsif FailFast.
|
69
|
-
fail "\ne2e\nshould have raised a #{kind.inspect} error for #{value.inspect}\n, but raised instead\n#{FailFast.
|
70
|
-
elsif 2 <= FailFast.
|
71
|
-
fail "\ne2f\nshould have raised only 1 #{kind} error for #{value}\nbut raised instead\n#{FailFast.
|
90
|
+
elsif FailFast.global_errors.length == 1 && !FailFast.global_errors.first.has_value_and_kind?(value, kind)
|
91
|
+
fail "\ne2e\nshould have raised a #{kind.inspect} error for #{value.inspect}\n, but raised instead\n#{FailFast.global_errors.inspect}"
|
92
|
+
elsif 2 <= FailFast.global_errors.length
|
93
|
+
fail "\ne2f\nshould have raised only 1 #{kind} error for #{value}\nbut raised instead\n#{FailFast.global_errors.join("\n")}"
|
72
94
|
end
|
73
95
|
end
|
74
|
-
|
96
|
+
restore_stdout
|
75
97
|
end
|
76
98
|
end
|
77
99
|
|
@@ -79,8 +101,12 @@ module DSLMacros
|
|
79
101
|
|
80
102
|
def self.included(receiver)
|
81
103
|
receiver.extend(ClassMethods)
|
104
|
+
receiver.send :include, InstanceMethods
|
82
105
|
end
|
83
106
|
end
|
84
107
|
Spec::Runner.configure do |config|
|
85
108
|
config.include(DSLMacros)
|
109
|
+
config.before(:each) do
|
110
|
+
FailFast.reset_error_db!
|
111
|
+
end
|
86
112
|
end
|
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:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 1
|
9
8
|
- 2
|
10
|
-
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
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-
|
18
|
+
date: 2010-07-05 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -81,28 +81,37 @@ files:
|
|
81
81
|
- VERSION
|
82
82
|
- fail_fast.gemspec
|
83
83
|
- lib/fail_fast.rb
|
84
|
-
- lib/fail_fast/
|
85
|
-
- lib/fail_fast/
|
86
|
-
- lib/fail_fast/
|
87
|
-
- lib/fail_fast/
|
88
|
-
- lib/fail_fast/
|
89
|
-
- lib/fail_fast/
|
84
|
+
- lib/fail_fast/base/base.rb
|
85
|
+
- lib/fail_fast/base/utils.rb
|
86
|
+
- lib/fail_fast/base/z_only_for_tests.rb
|
87
|
+
- lib/fail_fast/extensions/base_commands.rb
|
88
|
+
- lib/fail_fast/extensions/check_active_record_db.rb
|
89
|
+
- lib/fail_fast/extensions/check_email.rb
|
90
|
+
- lib/fail_fast/extensions/check_file_system.rb
|
91
|
+
- lib/fail_fast/extensions/check_mongo_db.rb
|
92
|
+
- lib/fail_fast/extensions/check_url.rb
|
93
|
+
- lib/fail_fast/extensions/check_value.rb
|
90
94
|
- lib/fail_fast/main.rb
|
91
|
-
- lib/fail_fast/misc.rb
|
92
95
|
- lib/fail_fast/report.txt.erb
|
96
|
+
- lib/fail_fast/support/error_db.rb
|
97
|
+
- lib/fail_fast/support/error_details.rb
|
98
|
+
- lib/fail_fast/support/z_only_for_tests.rb
|
93
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
|
94
108
|
- spec/file_is_empty_spec.rb
|
95
109
|
- spec/file_is_missing_spec.rb
|
96
|
-
- spec/file_system_spec.rb
|
97
110
|
- spec/fixtures/empty.yml
|
98
111
|
- spec/fixtures/simple.yml
|
99
|
-
- spec/has_active_record_db_spec.rb
|
100
|
-
- spec/has_email_for_spec.rb
|
101
|
-
- spec/has_mongoDB_for_spec.rb
|
102
|
-
- spec/has_url_for_spec.rb
|
103
|
-
- spec/has_value_for_spec.rb
|
104
112
|
- spec/how_to_use_spec.rb
|
105
|
-
- spec/
|
113
|
+
- spec/multiple_blocks_support_spec.rb
|
114
|
+
- spec/report_printing_spec.rb
|
106
115
|
- spec/spec.opts
|
107
116
|
- spec/spec_helper.rb
|
108
117
|
has_rdoc: true
|
@@ -140,14 +149,17 @@ signing_key:
|
|
140
149
|
specification_version: 3
|
141
150
|
summary: raises an error if the yaml contents of a config file does pass a test script.
|
142
151
|
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
|
143
160
|
- spec/file_is_empty_spec.rb
|
144
161
|
- spec/file_is_missing_spec.rb
|
145
|
-
- spec/file_system_spec.rb
|
146
|
-
- spec/has_active_record_db_spec.rb
|
147
|
-
- spec/has_email_for_spec.rb
|
148
|
-
- spec/has_mongoDB_for_spec.rb
|
149
|
-
- spec/has_url_for_spec.rb
|
150
|
-
- spec/has_value_for_spec.rb
|
151
162
|
- spec/how_to_use_spec.rb
|
152
|
-
- spec/
|
163
|
+
- spec/multiple_blocks_support_spec.rb
|
164
|
+
- spec/report_printing_spec.rb
|
153
165
|
- spec/spec_helper.rb
|
@@ -1,38 +0,0 @@
|
|
1
|
-
class FailFast
|
2
|
-
module CheckValue
|
3
|
-
|
4
|
-
# Usage
|
5
|
-
# has_value_for 'str_key'
|
6
|
-
# has_value_for :sym_key, /localhost/
|
7
|
-
# returns
|
8
|
-
# true if succesful, false otherwise
|
9
|
-
def has_value_for(key, *params)
|
10
|
-
p = key_value_regexp_options(key, params)
|
11
|
-
key, options = p.key, p.options
|
12
|
-
|
13
|
-
nof_errors = FailFast.errors.size
|
14
|
-
if blank?(p.value)
|
15
|
-
FailFast.errors << ErrorDetails.new(key, :missing_value, nil)
|
16
|
-
|
17
|
-
elsif p.regexp
|
18
|
-
FailFast.errors << ErrorDetails.new(key, :value_does_not_match, p.value) unless p.value =~ p.regexp
|
19
|
-
|
20
|
-
elsif hash?(options) && range?(options[:in])
|
21
|
-
FailFast.errors << ErrorDetails.new(key, :value_not_in_range, p.value) unless options[:in].include?(p.value)
|
22
|
-
|
23
|
-
elsif hash?(options) && array?(options[:in])
|
24
|
-
FailFast.errors << ErrorDetails.new(key, :value_not_in_array, p.value) unless options[:in].include?(p.value)
|
25
|
-
end
|
26
|
-
no_new_error = nof_errors == FailFast.errors.size
|
27
|
-
end
|
28
|
-
|
29
|
-
# Usage
|
30
|
-
# has_values_for :sym_key, 'str_key'
|
31
|
-
#
|
32
|
-
def has_values_for(*keys)
|
33
|
-
keys.each{|key| has_value_for(key)}
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
38
|
-
FailFast.send :include, FailFast::CheckValue
|
File without changes
|
File without changes
|