diddy 0.2.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/diddy.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "diddy"
7
- gem.version = '0.2.0'
7
+ gem.version = '0.5.0'
8
8
  gem.authors = ["Diederick Lawson", "Marcel de Graaf"]
9
9
  gem.email = ["diederick@altovista.nl", "mail@marceldegraaf.net"]
10
10
  gem.description = %q{Diddy script runner}
data/lib/diddy/script.rb CHANGED
@@ -1,6 +1,11 @@
1
+ # encoding: utf-8
1
2
  module Diddy
2
3
  class Script
3
- attr_accessor :steps, :scenario
4
+ STATE_OK = 1
5
+ STATE_FAILED = 2
6
+ STATE_EXCEPTION = 3
7
+
8
+ attr_accessor :steps, :scenario, :log
4
9
 
5
10
  #
6
11
  # Creates a script
@@ -17,6 +22,13 @@ module Diddy
17
22
  @steps_instances << klass.new(shared_scope)
18
23
  end
19
24
 
25
+ #
26
+ # Full log
27
+ #
28
+ def log
29
+ @log ||= ""
30
+ end
31
+
20
32
  #
21
33
  # Describes which step should be run
22
34
  #
@@ -43,7 +55,7 @@ module Diddy
43
55
  #
44
56
  def run
45
57
  begin
46
- puts scenario
58
+ self.class.write_log(scenario)
47
59
 
48
60
  @steps.each do |step|
49
61
  run_step(step)
@@ -51,12 +63,19 @@ module Diddy
51
63
  return true
52
64
 
53
65
  rescue ScriptAborted
54
- puts "Aborted"
66
+ self.class.write_log("Aborted")
55
67
  return false
56
68
 
57
69
  end
58
70
  end
59
71
 
72
+ #
73
+ # Returns the log of the last run
74
+ #
75
+ def self.last_log
76
+ @last_log ||= ""
77
+ end
78
+
60
79
  #
61
80
  # Defines a script
62
81
  #
@@ -82,16 +101,27 @@ module Diddy
82
101
  # scripts returned true. Otherwise returns false.
83
102
  #
84
103
  def self.run_all
85
- puts "[#{Time.now}] Diddy starting to run #{@scripts.size} scripts"
104
+ # empty log
105
+ @last_log = ""
106
+
107
+ write_log("[#{Time.now}] Diddy starting to run #{@scripts.size} scripts")
86
108
 
87
109
  # Run all scripts and remember their return status
88
- script_statuses = @scripts.map { |script| script.run }
110
+ status = @scripts.map do |script|
111
+ # run the script
112
+ result = script.run
113
+
114
+ # concat log of script to general log
115
+ write_log(script.log)
89
116
 
90
- puts "[#{Time.now}] Diddy finished running #{@scripts.size} scripts"
117
+ result
118
+ end
119
+
120
+ write_log("[#{Time.now}] Diddy finished running #{@scripts.size} scripts")
91
121
 
92
122
  # If one of the scripts returned with "false"; make the entire run
93
123
  # return false as well
94
- script_statuses.include?(false) ? false : true
124
+ status.include?(false) ? false : true
95
125
  end
96
126
 
97
127
  #
@@ -107,6 +137,11 @@ module Diddy
107
137
 
108
138
  private
109
139
 
140
+ def self.write_log(message, print = true)
141
+ puts(message) if print
142
+ self.last_log << "#{message}\n"
143
+ end
144
+
110
145
  #
111
146
  # Runs one step
112
147
  #
@@ -116,27 +151,46 @@ module Diddy
116
151
  result = step.run
117
152
 
118
153
  rescue Exception => exception
119
- step.log(Diddy::Step::STATE_EXCEPTION)
154
+ log_step(step, STATE_EXCEPTION)
120
155
  print_exception(step, exception)
121
156
  raise ScriptAborted.new
122
157
  end
123
158
 
124
159
  if result
125
- step.log(Diddy::Step::STATE_OK)
160
+ log_step(step, STATE_OK)
126
161
  else
127
- step.log(Diddy::Step::STATE_EXCEPTION)
162
+ log_step(step, STATE_EXCEPTION)
128
163
  raise ScriptAborted.new
129
164
  end
130
165
  end
131
166
 
167
+ #
168
+ # Logs step to log
169
+ #
170
+ def log_step(step, state)
171
+ if state == STATE_FAILED || state == STATE_EXCEPTION
172
+ print(red(bold("✕ #{step.description}")))
173
+ self.class.write_log("✕ #{step.description}", false)
174
+
175
+ if state == STATE_EXCEPTION
176
+ print(" [EXCEPTION]")
177
+ print("\n\n")
178
+ self.class.write_log("[EXCEPTION]", false)
179
+ end
180
+ else
181
+ print(green("✓ #{step.description}"), "\n")
182
+ self.class.write_log("✓ #{step.description}", false)
183
+ end
184
+ end
185
+
132
186
  #
133
187
  # Prints exception thrown by step, on screen
134
188
  #
135
189
  def print_exception(current_step, exception)
136
190
  # print backtrace
137
- puts "- #{exception.message}"
138
- puts " #{exception.backtrace.join("\n ")}"
139
- puts "\n"
191
+ self.class.write_log("- #{exception.message}")
192
+ self.class.write_log(" #{exception.backtrace.join("\n ")}")
193
+ self.class.write_log("\n")
140
194
  end
141
195
 
142
196
  #
data/lib/diddy/step.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Diddy
3
3
  class Step
4
- STATE_OK = 1
5
- STATE_FAILED = 2
6
- STATE_EXCEPTION = 3
7
-
8
4
  attr_accessor :description, :definition, :steps_instance
9
5
 
10
6
  #
@@ -20,18 +16,5 @@ module Diddy
20
16
  def run
21
17
  steps_instance.instance_eval(&definition)
22
18
  end
23
-
24
- #
25
- # Logs state of step to screen
26
- #
27
- def log(state)
28
- if state == STATE_FAILED || state == STATE_EXCEPTION
29
- print red(bold("✕ #{description}"))
30
- print " [EXCEPTION]" if state == STATE_EXCEPTION
31
- print "\n\n"
32
- else
33
- print green("✓ #{description}"), "\n"
34
- end
35
- end
36
19
  end
37
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diddy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-23 00:00:00.000000000 Z
13
+ date: 2012-11-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: term/aniscolor
@@ -36,6 +36,7 @@ executables: []
36
36
  extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
+ - .gitignore
39
40
  - .rbenv-version
40
41
  - .rvmrc
41
42
  - README.md