spectre-core 1.12.2 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Spectre
2
- module Logger
2
+ module Logging
3
3
  class File
4
4
  def initialize config
5
5
  raise 'No log format section in config for console logger' unless config.key? 'log_format' and config['log_format'].key? 'file'
@@ -9,87 +9,93 @@ module Spectre
9
9
  @fmt_end_group = @config['end_group']
10
10
  @fmt_sep = @config['separator']
11
11
 
12
- @file_log = ::Logger.new config['log_file'], progname: 'spectre'
12
+ @file_log = ::Logger.new(config['log_file'], progname: 'spectre')
13
13
  @file_log.level = config['debug'] ? 'DEBUG' : 'INFO'
14
14
  end
15
15
 
16
16
  def start_subject subject
17
- @file_log.debug "start running subject '#{subject.desc}'"
17
+ @file_log.debug("start running subject '#{subject.desc}'")
18
18
  end
19
19
 
20
20
  def end_subject subject
21
- @file_log.debug "subject '#{subject.desc}' finished"
21
+ @file_log.debug("subject '#{subject.desc}' finished")
22
22
  end
23
23
 
24
24
  def start_context context
25
25
  if context and context.__desc
26
- @file_log.debug "start running context '#{context.__desc}'"
26
+ @file_log.debug("start running context '#{context.__desc}'")
27
27
  else
28
- @file_log.debug "start running main context of #{context.__subject.desc}"
28
+ @file_log.debug("start running main context of #{context.__subject.desc}")
29
29
  end
30
30
  end
31
31
 
32
32
  def end_context context
33
33
  if context and context.__desc
34
- @file_log.debug "context '#{context.__desc}' finished"
34
+ @file_log.debug("context '#{context.__desc}' finished")
35
35
  else
36
- @file_log.debug "main context finished of #{context.__subject.desc}"
36
+ @file_log.debug("main context finished of #{context.__subject.desc}")
37
37
  end
38
38
  end
39
39
 
40
40
  def start_spec spec, data=nil
41
41
  log_msg = "start running spec [#{spec.name}] '#{spec.desc}'"
42
42
  log_msg += " with data #{data}" if data
43
- @file_log.debug log_msg
43
+ @file_log.debug(log_msg)
44
44
  end
45
45
 
46
46
  def end_spec spec, data=nil
47
47
  log_msg = "running spec [#{spec.name}] '#{spec.desc}'"
48
48
  log_msg += " with data #{data}" if data
49
49
  log_msg += " finished"
50
- @file_log.debug log_msg
50
+ @file_log.debug(log_msg)
51
51
  end
52
52
 
53
53
  def log_separator desc
54
54
  desc = @fmt_sep.gsub('<desc>', desc) if @fmt_sep
55
- @file_log.info desc
55
+ @file_log.info(desc)
56
56
  end
57
57
 
58
58
  def start_group desc
59
59
  desc = @fmt_start_group.gsub('<desc>', desc) if @fmt_start_group
60
- @file_log.info desc
60
+ @file_log.info(desc)
61
61
  end
62
62
 
63
63
  def end_group desc
64
64
  desc = @fmt_end_group.gsub('<desc>', desc) if @fmt_end_group
65
- @file_log.info desc
65
+ @file_log.info(desc)
66
66
  end
67
67
 
68
68
  def log_process desc
69
- @file_log.debug desc
69
+ @file_log.debug(desc)
70
70
  end
71
71
 
72
72
  def log_info message
73
- @file_log.info "#{Status::INFO} #{message}"
73
+ @file_log.info("#{Status::INFO} #{message}")
74
74
  end
75
75
 
76
76
  def log_debug message
77
- @file_log.debug "#{Status::DEBUG} #{message}"
77
+ @file_log.debug("#{Status::DEBUG} #{message}")
78
78
  end
79
79
 
80
80
  def log_error spec, exception
81
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"}"
82
+ @file_log.error("An unexpected error occurred at '#{file}:#{line}' while running spec '#{spec.name}': [#{exception.class}] #{exception.message}\n#{exception.backtrace.join "\n"}")
83
83
  end
84
84
 
85
- def log_skipped spec
86
- @file_log.warn "spec '#{spec.desc}' canceled by user"
85
+ def log_skipped spec, message=nil
86
+ txt = "spec '#{spec.desc}' skipped"
87
+
88
+ unless message.nil?
89
+ txt += ': ' + message
90
+ end
91
+
92
+ @file_log.warn(txt)
87
93
  end
88
94
 
89
95
  def log_status desc, status, annotation=nil
90
96
  msg = "expected #{desc}...#{status.upcase}"
91
97
  msg += " - #{annotation}" if annotation
92
- @file_log.debug msg
98
+ @file_log.debug(msg)
93
99
  end
94
100
  end
95
101
  end
@@ -2,7 +2,7 @@ require_relative '../spectre'
2
2
  require 'date'
3
3
 
4
4
  module Spectre
5
- module Logger
5
+ module Logging
6
6
  module Status
7
7
  OK = '[ok]'
8
8
  FAILED = '[failed]'
@@ -12,6 +12,31 @@ module Spectre
12
12
  DEBUG = '[debug]'
13
13
  end
14
14
 
15
+ class ModuleLogger
16
+ def initialize config, name
17
+ @name = name
18
+ @debug = config['debug']
19
+ @logger = ::Logger.new(config['log_file'], progname: name)
20
+
21
+ @logger.level = @debug ? ::Logger::DEBUG : ::Logger::INFO
22
+ end
23
+
24
+ def info message
25
+ @logger.info(message)
26
+ Logging.add_log(message, :info, @name)
27
+ end
28
+
29
+ def debug message
30
+ @logger.debug(message)
31
+ Logging.add_log(message, :debug, @name) if @debug
32
+ end
33
+
34
+ def warn message
35
+ @logger.warn(message)
36
+ Logging.add_log(message, :warn, @name)
37
+ end
38
+ end
39
+
15
40
  class << self
16
41
  @@debug = false
17
42
  @@logger = []
@@ -93,24 +118,24 @@ module Spectre
93
118
  end
94
119
 
95
120
  def log_info message
96
- add_log(message)
121
+ add_log(message, :info)
97
122
  delegate(:log_info, message)
98
123
  end
99
124
 
100
125
  def log_debug message
101
126
  return unless @@debug
102
127
 
103
- add_log(message)
128
+ add_log(message, :debug)
104
129
  delegate(:log_debug, message)
105
130
  end
106
131
 
107
132
  def log_error spec, exception
108
- add_log(exception)
133
+ add_log(exception, :error)
109
134
  delegate(:log_error, spec, exception)
110
135
  end
111
136
 
112
- def log_skipped spec
113
- delegate(:log_skipped, spec)
137
+ def log_skipped spec, message=nil
138
+ delegate(:log_skipped, spec, message)
114
139
  end
115
140
 
116
141
  def log_status desc, status, annotation=nil
@@ -118,9 +143,15 @@ module Spectre
118
143
  end
119
144
 
120
145
  def group desc
121
- Logger.start_group desc
146
+ Logging.start_group desc
122
147
  yield
123
- Logger.end_group desc
148
+ Logging.end_group desc
149
+ end
150
+
151
+ def add_log message, level, logger_name='spectre'
152
+ return unless Spectre::Runner.current
153
+
154
+ Spectre::Runner.current.log.append([DateTime.now, message, level, logger_name])
124
155
  end
125
156
 
126
157
  alias_method :info, :log_info
@@ -135,10 +166,6 @@ module Spectre
135
166
  logger.send(method, *args) if logger.respond_to? method
136
167
  end
137
168
  end
138
-
139
- def add_log message
140
- Spectre::Runner.current.log.append([DateTime.now, message])
141
- end
142
169
  end
143
170
 
144
171
  Spectre.delegate :log, :info, :debug, :group, :separate, to: self
data/lib/spectre/mixin.rb CHANGED
@@ -12,12 +12,12 @@ module Spectre
12
12
 
13
13
  def required params, *keys
14
14
  missing_keys = keys.select { |x| !params.to_h.key? x }
15
- Spectre::Logger.log_debug("required parameters for '#{@__desc}': #{keys.join ', '}")
15
+ Spectre::Logging.log_debug("required parameters for '#{@__desc}': #{keys.join ', '}")
16
16
  raise ArgumentError, "mixin '#{@__desc}' requires #{keys.join ', '}, but only has #{missing_keys.join ', '} given" unless missing_keys.empty?
17
17
  end
18
18
 
19
19
  def optional params, *keys
20
- Spectre::Logger.log_debug("optional parameters for '#{@__desc}': #{keys.join ', '}")
20
+ Spectre::Logging.log_debug("optional parameters for '#{@__desc}': #{keys.join ', '}")
21
21
  params
22
22
  end
23
23
  end
@@ -32,7 +32,7 @@ module Spectre
32
32
  def run desc, with: []
33
33
  raise "no mixin with desc '#{desc}' defined" unless @@mixins.key? desc
34
34
 
35
- Spectre::Logger.log_debug "running mixin '#{desc}'"
35
+ Spectre::Logging.log_debug "running mixin '#{desc}'"
36
36
 
37
37
  params = with || {}
38
38
 
@@ -14,37 +14,34 @@ module Spectre::Reporter
14
14
  run_infos
15
15
  .select { |x| x.error != nil or x.failure != nil }
16
16
  .each_with_index do |run_info, index|
17
- spec = run_info.spec
17
+ report_str += "\n#{index+1}) #{format_title(run_info)}\n"
18
18
 
19
- report_str += "\n#{index+1}) #{format_title(run_info)}\n"
19
+ if run_info.failure
20
+ report_str += " Expected #{run_info.failure.expectation}"
21
+ report_str += " with #{run_info.data}" if run_info.data
20
22
 
21
- if run_info.failure
22
- report_str += " Expected #{run_info.failure.expectation}"
23
- report_str += " with #{run_info.data}" if run_info.data
24
- report_str += " during #{spec.context.__desc}" if spec.context.__desc
23
+ report_str += " but it failed"
25
24
 
26
- report_str += " but it failed"
25
+ if run_info.failure.cause
26
+ report_str += "\n with an unexpected error:\n"
27
+ report_str += format_exception(run_info.failure.cause)
27
28
 
28
- if run_info.failure.cause
29
- report_str += "\n with an unexpected error:\n"
30
- report_str += format_exception(run_info.failure.cause)
29
+ elsif run_info.failure.message and not run_info.failure.message.empty?
30
+ report_str += " with:\n #{run_info.failure.message}"
31
31
 
32
- elsif run_info.failure.message and not run_info.failure.message.empty?
33
- report_str += " with:\n #{run_info.failure.message}"
32
+ else
33
+ report_str += '.'
34
+ end
35
+
36
+ report_str += "\n"
37
+ failures += 1
34
38
 
35
39
  else
36
- report_str += '.'
40
+ report_str += " but an unexpected error occurred during run\n"
41
+ report_str += format_exception(run_info.error)
42
+ errors += 1
37
43
  end
38
-
39
- report_str += "\n"
40
- failures += 1
41
-
42
- else
43
- report_str += " but an unexpected error occured during run\n"
44
- report_str += format_exception(run_info.error)
45
- errors += 1
46
44
  end
47
- end
48
45
 
49
46
  if failures + errors > 0
50
47
  summary = ''
@@ -55,7 +52,6 @@ module Spectre::Reporter
55
52
  summary += "#{run_infos.length} total"
56
53
  print "\n#{summary}\n".red
57
54
  else
58
- summary = ''
59
55
  summary = "\nRun finished successfully"
60
56
  summary += " (#{skipped} skipped)" if skipped > 0
61
57
  print "#{summary}\n".green
@@ -68,6 +64,7 @@ module Spectre::Reporter
68
64
 
69
65
  def format_title run_info
70
66
  title = run_info.spec.subject.desc
67
+ title += " #{run_info.spec.context.__desc}" if run_info.spec.context.__desc
71
68
  title += ' ' + run_info.spec.desc
72
69
  title += " (#{'%.3f' % run_info.duration}s)"
73
70
  title += " [#{run_info.spec.name}]"