spectre-core 1.10.0 → 1.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,12 +24,14 @@ module Spectre
24
24
 
25
25
  def start_context context
26
26
  return unless context.__desc
27
+
27
28
  puts (' ' * indent) + context.__desc.magenta
28
29
  @level += 1
29
30
  end
30
31
 
31
32
  def end_context context
32
33
  return unless context.__desc
34
+
33
35
  @level -= 1
34
36
  puts (' ' * indent) + @fmt_end_context.gsub('<desc>', context.__desc).magenta if @fmt_end_context
35
37
  end
@@ -42,7 +44,7 @@ module Spectre
42
44
  @level += 1
43
45
  end
44
46
 
45
- def end_spec spec, data
47
+ def end_spec _spec, _data
46
48
  @level -= 1
47
49
  end
48
50
 
@@ -76,7 +78,7 @@ module Spectre
76
78
  @level += 1
77
79
  end
78
80
 
79
- def log_status desc, status, annotation=nil
81
+ def log_status _desc, status, annotation=nil
80
82
  status = status.green if status == Status::OK
81
83
  status = status.blue if status == Status::INFO
82
84
  status = status.grey if status == Status::DEBUG
@@ -106,12 +108,12 @@ module Spectre
106
108
  print_line(message, Status::DEBUG.grey)
107
109
  end
108
110
 
109
- def log_error spec, exception
111
+ def log_error _spec, exception
110
112
  txt = (Status::ERROR + ' - ' + exception.class.name).red
111
113
  print_line('', txt)
112
114
  end
113
115
 
114
- def log_skipped spec
116
+ def log_skipped _spec
115
117
  print_line('', Status::SKIPPED.grey)
116
118
  end
117
119
 
@@ -136,7 +138,6 @@ module Spectre
136
138
  @process = nil
137
139
  end
138
140
  end
139
-
140
141
  end
141
142
  end
142
- end
143
+ end
@@ -1,96 +1,96 @@
1
- module Spectre
2
- module Logger
3
- class File
4
- def initialize config
5
- raise 'No log format section in config for console logger' unless config.key? 'log_format' and config['log_format'].key? 'file'
6
-
7
- @config = config['log_format']['file']
8
- @fmt_start_group = @config['start_group']
9
- @fmt_end_group = @config['end_group']
10
- @fmt_sep = @config['separator']
11
-
12
- @file_log = ::Logger.new config['log_file'], progname: 'spectre'
13
- @file_log.level = config['debug'] ? 'DEBUG' : 'INFO'
14
- end
15
-
16
- def start_subject subject
17
- @file_log.debug "start running subject '#{subject.desc}'"
18
- end
19
-
20
- def end_subject subject
21
- @file_log.debug "subject '#{subject.desc}' finished"
22
- end
23
-
24
- def start_context context
25
- if context and context.__desc
26
- @file_log.debug "start running context '#{context.__desc}'"
27
- else
28
- @file_log.debug "start running main context of #{context.__subject.desc}"
29
- end
30
- end
31
-
32
- def end_context context
33
- if context and context.__desc
34
- @file_log.debug "context '#{context.__desc}' finished"
35
- else
36
- @file_log.debug "main context finished of #{context.__subject.desc}"
37
- end
38
- end
39
-
40
- def start_spec spec, data=nil
41
- log_msg = "start running spec [#{spec.name}] '#{spec.desc}'"
42
- log_msg += " with data #{data}" if data
43
- @file_log.debug log_msg
44
- end
45
-
46
- def end_spec spec, data=nil
47
- log_msg = "running spec [#{spec.name}] '#{spec.desc}'"
48
- log_msg += " with data #{data}" if data
49
- log_msg += " finished"
50
- @file_log.debug log_msg
51
- end
52
-
53
- def log_separator desc
54
- desc = @fmt_sep.gsub('<desc>', desc) if @fmt_sep
55
- @file_log.info desc
56
- end
57
-
58
- def start_group desc
59
- desc = @fmt_start_group.gsub('<desc>', desc) if @fmt_start_group
60
- @file_log.info desc
61
- end
62
-
63
- def end_group desc
64
- desc = @fmt_end_group.gsub('<desc>', desc) if @fmt_end_group
65
- @file_log.info desc
66
- end
67
-
68
- def log_process desc
69
- @file_log.debug desc
70
- end
71
-
72
- def log_info message
73
- @file_log.info "#{Status::INFO} #{message}"
74
- end
75
-
76
- def log_debug message
77
- @file_log.debug "#{Status::DEBUG} #{message}"
78
- end
79
-
80
- def log_error spec, exception
81
- file, line = exception.backtrace[0].match(/(.*\.rb):(\d+)/).captures
82
- @file_log.error "An unexpected error occured at '#{file}:#{line}' while running spec '#{spec.name}': [#{exception.class}] #{exception.message}\n#{exception.backtrace.join "\n"}"
83
- end
84
-
85
- def log_skipped spec
86
- @file_log.warn "spec '#{spec.desc}' canceled by user"
87
- end
88
-
89
- def log_status desc, status, annotation=nil
90
- msg = "expected #{desc}...#{status.upcase}"
91
- msg += " - #{annotation}" if annotation
92
- @file_log.debug msg
93
- end
94
- end
95
- end
96
- end
1
+ module Spectre
2
+ module Logger
3
+ class File
4
+ def initialize config
5
+ raise 'No log format section in config for console logger' unless config.key? 'log_format' and config['log_format'].key? 'file'
6
+
7
+ @config = config['log_format']['file']
8
+ @fmt_start_group = @config['start_group']
9
+ @fmt_end_group = @config['end_group']
10
+ @fmt_sep = @config['separator']
11
+
12
+ @file_log = ::Logger.new config['log_file'], progname: 'spectre'
13
+ @file_log.level = config['debug'] ? 'DEBUG' : 'INFO'
14
+ end
15
+
16
+ def start_subject subject
17
+ @file_log.debug "start running subject '#{subject.desc}'"
18
+ end
19
+
20
+ def end_subject subject
21
+ @file_log.debug "subject '#{subject.desc}' finished"
22
+ end
23
+
24
+ def start_context context
25
+ if context and context.__desc
26
+ @file_log.debug "start running context '#{context.__desc}'"
27
+ else
28
+ @file_log.debug "start running main context of #{context.__subject.desc}"
29
+ end
30
+ end
31
+
32
+ def end_context context
33
+ if context and context.__desc
34
+ @file_log.debug "context '#{context.__desc}' finished"
35
+ else
36
+ @file_log.debug "main context finished of #{context.__subject.desc}"
37
+ end
38
+ end
39
+
40
+ def start_spec spec, data=nil
41
+ log_msg = "start running spec [#{spec.name}] '#{spec.desc}'"
42
+ log_msg += " with data #{data}" if data
43
+ @file_log.debug log_msg
44
+ end
45
+
46
+ def end_spec spec, data=nil
47
+ log_msg = "running spec [#{spec.name}] '#{spec.desc}'"
48
+ log_msg += " with data #{data}" if data
49
+ log_msg += " finished"
50
+ @file_log.debug log_msg
51
+ end
52
+
53
+ def log_separator desc
54
+ desc = @fmt_sep.gsub('<desc>', desc) if @fmt_sep
55
+ @file_log.info desc
56
+ end
57
+
58
+ def start_group desc
59
+ desc = @fmt_start_group.gsub('<desc>', desc) if @fmt_start_group
60
+ @file_log.info desc
61
+ end
62
+
63
+ def end_group desc
64
+ desc = @fmt_end_group.gsub('<desc>', desc) if @fmt_end_group
65
+ @file_log.info desc
66
+ end
67
+
68
+ def log_process desc
69
+ @file_log.debug desc
70
+ end
71
+
72
+ def log_info message
73
+ @file_log.info "#{Status::INFO} #{message}"
74
+ end
75
+
76
+ def log_debug message
77
+ @file_log.debug "#{Status::DEBUG} #{message}"
78
+ end
79
+
80
+ def log_error spec, exception
81
+ file, line = exception.backtrace[0].match(/(.*\.rb):(\d+)/).captures
82
+ @file_log.error "An unexpected error occured at '#{file}:#{line}' while running spec '#{spec.name}': [#{exception.class}] #{exception.message}\n#{exception.backtrace.join "\n"}"
83
+ end
84
+
85
+ def log_skipped spec
86
+ @file_log.warn "spec '#{spec.desc}' canceled by user"
87
+ end
88
+
89
+ def log_status desc, status, annotation=nil
90
+ msg = "expected #{desc}...#{status.upcase}"
91
+ msg += " - #{annotation}" if annotation
92
+ @file_log.debug msg
93
+ end
94
+ end
95
+ end
96
+ end
@@ -1,144 +1,146 @@
1
- require 'date'
2
-
3
- module Spectre
4
- module Logger
5
- module Status
6
- OK = '[ok]'
7
- FAILED = '[failed]'
8
- ERROR = '[error]'
9
- INFO = '[info]'
10
- SKIPPED = '[skipped]'
11
- DEBUG = '[debug]'
12
- end
13
-
14
- class << self
15
- @@debug = false
16
- @@logger = []
17
-
18
- def debug!
19
- @@debug = true
20
- end
21
-
22
- def debug?
23
- @@debug
24
- end
25
-
26
- def add logger
27
- @@logger.append logger
28
- end
29
-
30
- def start_subject subject
31
- delegate(:start_subject, subject)
32
- end
33
-
34
- def end_subject subject
35
- delegate(:end_subject, subject)
36
- end
37
-
38
- def start_context context
39
- delegate(:start_context, context)
40
- end
41
-
42
- def end_context context
43
- delegate(:end_context, context)
44
- end
45
-
46
- def start_spec spec, data=nil
47
- delegate(:start_spec, spec, data)
48
- end
49
-
50
- def end_spec spec, data=nil
51
- delegate(:end_spec, spec, data)
52
- end
53
-
54
- def log_subject subject
55
- begin
56
- start_subject(subject)
57
- yield
58
- ensure
59
- end_subject(subject)
60
- end
61
- end
62
-
63
- def log_context context
64
- begin
65
- start_context(context)
66
- yield
67
- ensure
68
- end_context(context)
69
- end
70
- end
71
-
72
- def log_spec spec, data=nil
73
- start_spec(spec, data)
74
- yield
75
- end_spec(spec, data)
76
- end
77
-
78
- def log_separator desc
79
- delegate(:log_separator, desc)
80
- end
81
-
82
- def start_group desc
83
- delegate(:start_group, desc)
84
- end
85
-
86
- def end_group desc
87
- delegate(:end_group, desc)
88
- end
89
-
90
- def log_process desc
91
- delegate(:log_process, desc)
92
- end
93
-
94
- def log_info message
95
- add_log(message)
96
- delegate(:log_info, message)
97
- end
98
-
99
- def log_debug message
100
- return unless @@debug
101
- add_log(message)
102
- delegate(:log_debug, message)
103
- end
104
-
105
- def log_error spec, exception
106
- add_log(exception)
107
- delegate(:log_error, spec, exception)
108
- end
109
-
110
- def log_skipped spec
111
- delegate(:log_skipped, spec)
112
- end
113
-
114
- def log_status desc, status, annotation=nil
115
- delegate(:log_status, desc, status, annotation)
116
- end
117
-
118
- def group desc
119
- Logger.start_group desc
120
- yield
121
- Logger.end_group desc
122
- end
123
-
124
- alias_method :info, :log_info
125
- alias_method :log, :log_info
126
- alias_method :debug, :log_debug
127
- alias_method :separate, :log_separator
128
-
129
- private
130
-
131
- def delegate method, *args
132
- @@logger.each do |logger|
133
- logger.send(method, *args) if logger.respond_to? method
134
- end
135
- end
136
-
137
- def add_log message
138
- Spectre::Runner.current.log.append([DateTime.now, message])
139
- end
140
- end
141
-
142
- Spectre.delegate :log, :info, :debug, :group, :separate, to: self
143
- end
144
- end
1
+ require_relative '../spectre'
2
+ require 'date'
3
+
4
+ module Spectre
5
+ module Logger
6
+ module Status
7
+ OK = '[ok]'
8
+ FAILED = '[failed]'
9
+ ERROR = '[error]'
10
+ INFO = '[info]'
11
+ SKIPPED = '[skipped]'
12
+ DEBUG = '[debug]'
13
+ end
14
+
15
+ class << self
16
+ @@debug = false
17
+ @@logger = []
18
+
19
+ def debug!
20
+ @@debug = true
21
+ end
22
+
23
+ def debug?
24
+ @@debug
25
+ end
26
+
27
+ def add logger
28
+ @@logger.append logger
29
+ end
30
+
31
+ def start_subject subject
32
+ delegate(:start_subject, subject)
33
+ end
34
+
35
+ def end_subject subject
36
+ delegate(:end_subject, subject)
37
+ end
38
+
39
+ def start_context context
40
+ delegate(:start_context, context)
41
+ end
42
+
43
+ def end_context context
44
+ delegate(:end_context, context)
45
+ end
46
+
47
+ def start_spec spec, data=nil
48
+ delegate(:start_spec, spec, data)
49
+ end
50
+
51
+ def end_spec spec, data=nil
52
+ delegate(:end_spec, spec, data)
53
+ end
54
+
55
+ def log_subject subject
56
+ begin
57
+ start_subject(subject)
58
+ yield
59
+ ensure
60
+ end_subject(subject)
61
+ end
62
+ end
63
+
64
+ def log_context context
65
+ begin
66
+ start_context(context)
67
+ yield
68
+ ensure
69
+ end_context(context)
70
+ end
71
+ end
72
+
73
+ def log_spec spec, data=nil
74
+ start_spec(spec, data)
75
+ yield
76
+ end_spec(spec, data)
77
+ end
78
+
79
+ def log_separator desc
80
+ delegate(:log_separator, desc)
81
+ end
82
+
83
+ def start_group desc
84
+ delegate(:start_group, desc)
85
+ end
86
+
87
+ def end_group desc
88
+ delegate(:end_group, desc)
89
+ end
90
+
91
+ def log_process desc
92
+ delegate(:log_process, desc)
93
+ end
94
+
95
+ def log_info message
96
+ add_log(message)
97
+ delegate(:log_info, message)
98
+ end
99
+
100
+ def log_debug message
101
+ return unless @@debug
102
+
103
+ add_log(message)
104
+ delegate(:log_debug, message)
105
+ end
106
+
107
+ def log_error spec, exception
108
+ add_log(exception)
109
+ delegate(:log_error, spec, exception)
110
+ end
111
+
112
+ def log_skipped spec
113
+ delegate(:log_skipped, spec)
114
+ end
115
+
116
+ def log_status desc, status, annotation=nil
117
+ delegate(:log_status, desc, status, annotation)
118
+ end
119
+
120
+ def group desc
121
+ Logger.start_group desc
122
+ yield
123
+ Logger.end_group desc
124
+ end
125
+
126
+ alias_method :info, :log_info
127
+ alias_method :log, :log_info
128
+ alias_method :debug, :log_debug
129
+ alias_method :separate, :log_separator
130
+
131
+ private
132
+
133
+ def delegate method, *args
134
+ @@logger.each do |logger|
135
+ logger.send(method, *args) if logger.respond_to? method
136
+ end
137
+ end
138
+
139
+ def add_log message
140
+ Spectre::Runner.current.log.append([DateTime.now, message])
141
+ end
142
+ end
143
+
144
+ Spectre.delegate :log, :info, :debug, :group, :separate, to: self
145
+ end
146
+ end
data/lib/spectre/mixin.rb CHANGED
@@ -1,7 +1,27 @@
1
+ require_relative '../spectre'
2
+ require_relative 'logger'
3
+
1
4
  require 'ostruct'
2
5
 
3
6
  module Spectre
4
7
  module Mixin
8
+ class MixinContext < Spectre::DslClass
9
+ def initialize desc
10
+ @__desc = desc
11
+ end
12
+
13
+ def required params, *keys
14
+ missing_keys = keys.select { |x| !params.to_h.key? x }
15
+ Spectre::Logger.log_debug("required parameters for '#{@__desc}': #{keys.join ', '}")
16
+ raise ArgumentError, "mixin '#{@__desc}' requires #{keys.join ', '}, but only has #{missing_keys.join ', '} given" unless missing_keys.empty?
17
+ end
18
+
19
+ def optional params, *keys
20
+ Spectre::Logger.log_debug("optional parameters for '#{@__desc}': #{keys.join ', '}")
21
+ params
22
+ end
23
+ end
24
+
5
25
  class << self
6
26
  @@mixins = {}
7
27
 
@@ -11,31 +31,28 @@ module Spectre
11
31
 
12
32
  def run desc, with: []
13
33
  raise "no mixin with desc '#{desc}' defined" unless @@mixins.key? desc
14
- Logger.log_debug "running mixin '#{desc}'"
15
34
 
16
- if with.is_a? Array
17
- @@mixins[desc].call *with
35
+ Spectre::Logger.log_debug "running mixin '#{desc}'"
36
+
37
+ params = with || {}
38
+
39
+ ctx = MixinContext.new(desc)
40
+
41
+ if params.is_a? Array
42
+ return_val = ctx._execute(*params, &@@mixins[desc])
43
+ elsif params.is_a? Hash
44
+ return_val = ctx._execute(OpenStruct.new(params), &@@mixins[desc])
18
45
  else
19
- @@mixins[desc].call with
46
+ return_val = ctx._execute(params, &@@mixins[desc])
20
47
  end
48
+
49
+ return_val.is_a?(Hash) ? OpenStruct.new(return_val) : return_val
21
50
  end
22
51
 
23
52
  alias_method :also, :run
24
53
  alias_method :step, :run
25
54
  end
26
55
 
27
- Spectre.register do |config|
28
- if not config.key? 'mixin_patterns'
29
- return
30
- end
31
-
32
- config['mixin_patterns'].each do |pattern|
33
- Dir.glob(pattern).each do|f|
34
- require_relative File.join(Dir.pwd, f)
35
- end
36
- end
37
- end
38
-
39
- Spectre.delegate :mixin, :run, :also, :step, to: Mixin
56
+ Spectre.delegate :mixin, :run, :also, :step, to: self
40
57
  end
41
- end
58
+ end
@@ -5,7 +5,6 @@ module Spectre::Reporter
5
5
  end
6
6
 
7
7
  def report run_infos
8
-
9
8
  report_str = ''
10
9
 
11
10
  errors = 0
@@ -15,7 +14,6 @@ module Spectre::Reporter
15
14
  run_infos
16
15
  .select { |x| x.error != nil or x.failure != nil }
17
16
  .each_with_index do |run_info, index|
18
-
19
17
  spec = run_info.spec
20
18
 
21
19
  report_str += "\n#{index+1}) #{format_title(run_info)}\n"
@@ -93,12 +91,11 @@ module Spectre::Reporter
93
91
  file.slice!(Dir.pwd + '/')
94
92
 
95
93
  str = ''
96
- str += " file.....: #{file}\n"
97
- str += " line.....: #{line}\n"
94
+ str += " file.....: #{file}:#{line}\n"
98
95
  str += " type.....: #{error.class}\n"
99
96
  str += " message..: #{error.message}\n"
100
97
  str += " backtrace: \n #{error.backtrace.join("\n ")}\n" if @config['debug']
101
98
  str
102
99
  end
103
100
  end
104
- end
101
+ end
@@ -57,8 +57,9 @@ module Spectre::Reporter
57
57
 
58
58
  if run_info.log.count > 0 or run_info.properties.count > 0 or run_info.data
59
59
  xml_str += '<system-out>'
60
+ xml_str += '<![CDATA['
60
61
 
61
- if run_info.properties.count > 0
62
+ if run_info.properties.count > 0
62
63
  run_info.properties.each do |key, val|
63
64
  xml_str += "#{key}: #{val}\n"
64
65
  end
@@ -75,6 +76,7 @@ module Spectre::Reporter
75
76
  xml_str += messages.join("\n")
76
77
  end
77
78
 
79
+ xml_str += ']]>'
78
80
  xml_str += '</system-out>'
79
81
  end
80
82
 
@@ -86,7 +88,7 @@ module Spectre::Reporter
86
88
 
87
89
  xml_str += '</testsuites>'
88
90
 
89
- Dir.mkdir @config['out_path'] if not Dir.exist? @config['out_path']
91
+ Dir.mkdir @config['out_path'] unless Dir.exist? @config['out_path']
90
92
 
91
93
  file_path = File.join(@config['out_path'], "spectre-junit_#{timestamp}.xml")
92
94
 
@@ -95,4 +97,4 @@ module Spectre::Reporter
95
97
  end
96
98
  end
97
99
  end
98
- end
100
+ end