stable 1.10.0 → 1.12.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.
- checksums.yaml +4 -4
- data/lib/stable/configuration.rb +2 -1
- data/lib/stable/{spec.rb → fact.rb} +3 -71
- data/lib/stable/formatters/colors.rb +24 -0
- data/lib/stable/formatters/verbose.rb +84 -0
- data/lib/stable.rb +17 -17
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84ffc96e3befd0fb806003874c7b63dcc466b0c93de4c0490742542d0c22b98e
|
4
|
+
data.tar.gz: f7ed6f16c45d94a71f5042c91819617694d0ecb0a229dd15efe62c6b34463e59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 347c9d97b2e4f4a320bc43e62674de74b2c60e0001b5d05df852347d8307cd3be93332bbcb2c8b921ec60ebc51970f9e5cf180d8e92e4a14626d0b7c65d4493a
|
7
|
+
data.tar.gz: 4f9cb7f894efd116ce22dc200c11d5f2a1401cdadbbb4d9cc131961c4a612964196379c761db9d37f071cb5a682ece70a0cc983d30d46a8b495b61e782e56c6f
|
data/lib/stable/configuration.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
# lib/stable/configuration.rb
|
2
2
|
module Stable
|
3
3
|
class Configuration
|
4
|
-
attr_accessor :storage_path, :enabled
|
4
|
+
attr_accessor :storage_path, :enabled, :fact_paths
|
5
5
|
|
6
6
|
def initialize
|
7
7
|
@storage_path = nil
|
8
8
|
@enabled = false
|
9
|
+
@fact_paths = ['facts/**/*.fact']
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
# lib/stable/
|
1
|
+
# lib/stable/fact.rb
|
2
2
|
require 'json'
|
3
3
|
require 'securerandom'
|
4
4
|
require 'digest'
|
5
5
|
|
6
6
|
module Stable
|
7
|
-
# a
|
7
|
+
# a fact is a recording of a single method call, including the inputs and
|
8
8
|
# outputs. it's a self-contained, serializable representation of a method's
|
9
9
|
# behavior at a specific point in time.
|
10
|
-
class
|
10
|
+
class Fact
|
11
11
|
attr_reader :class_name, :method_name, :args, :result, :error, :actual_result, :actual_error, :status, :uuid, :signature, :name
|
12
12
|
|
13
13
|
def initialize(class_name:, method_name:, args:, result: nil, error: nil, uuid: SecureRandom.uuid, name: nil)
|
@@ -51,42 +51,7 @@ module Stable
|
|
51
51
|
self
|
52
52
|
end
|
53
53
|
|
54
|
-
def to_s
|
55
|
-
short_uuid = uuid.split('-').last
|
56
|
-
short_sig = signature[0..6]
|
57
|
-
desc = "#{short_uuid}/#{short_sig}"
|
58
|
-
name_str = name[..19].ljust(20)
|
59
|
-
call = "#{class_name}##{method_name}(#{args.join(', ')})"
|
60
|
-
status_code = _status_code
|
61
|
-
error_code = _error_code
|
62
54
|
|
63
|
-
case status
|
64
|
-
when :passed, :passed_with_error
|
65
|
-
"#{desc} #{name_str} #{status_code}#{error_code} #{call}"
|
66
|
-
when :failed
|
67
|
-
lines = ["#{desc} #{name_str} #{status_code}#{error_code} #{call}"]
|
68
|
-
if actual_error
|
69
|
-
if error
|
70
|
-
lines << " Expected error: #{error['class']}"
|
71
|
-
lines << " Actual error: #{actual_error.class.name}: #{actual_error.message}"
|
72
|
-
else
|
73
|
-
lines << " Expected result: #{result.inspect}"
|
74
|
-
lines << " Actual error: #{actual_error.class.name}: #{actual_error.message}"
|
75
|
-
end
|
76
|
-
else
|
77
|
-
if error
|
78
|
-
lines << " Expected error: #{error['class']}"
|
79
|
-
lines << " Actual result: #{actual_result.inspect}"
|
80
|
-
else
|
81
|
-
lines << " Expected: #{result.inspect}"
|
82
|
-
lines << " Actual: #{actual_result.inspect}"
|
83
|
-
end
|
84
|
-
end
|
85
|
-
lines.join("\n")
|
86
|
-
else
|
87
|
-
"#{desc} #{name_str} #{status_code}#{error_code} #{call}"
|
88
|
-
end
|
89
|
-
end
|
90
55
|
|
91
56
|
def to_jsonl
|
92
57
|
{
|
@@ -114,39 +79,6 @@ module Stable
|
|
114
79
|
)
|
115
80
|
end
|
116
81
|
|
117
|
-
def _green(text)
|
118
|
-
"\e[32m#{text}\e[0m"
|
119
|
-
end
|
120
|
-
|
121
|
-
def _red(text)
|
122
|
-
"\e[31m#{text}\e[0m"
|
123
|
-
end
|
124
|
-
|
125
|
-
def _yellow(text)
|
126
|
-
"\e[33m#{text}\e[0m"
|
127
|
-
end
|
128
|
-
|
129
|
-
def _light_blue(text)
|
130
|
-
"\e[94m#{text}\e[0m"
|
131
|
-
end
|
132
|
-
|
133
|
-
def _status_code
|
134
|
-
case status
|
135
|
-
when :passed, :passed_with_error
|
136
|
-
_green('P')
|
137
|
-
when :failed
|
138
|
-
_red('F')
|
139
|
-
else
|
140
|
-
_yellow('?')
|
141
|
-
end
|
142
|
-
end
|
143
82
|
|
144
|
-
def _error_code
|
145
|
-
if error || actual_error
|
146
|
-
_light_blue('E')
|
147
|
-
else
|
148
|
-
_green('N')
|
149
|
-
end
|
150
|
-
end
|
151
83
|
end
|
152
84
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# lib/stable/formatters/colors.rb
|
2
|
+
module Stable
|
3
|
+
module Formatters
|
4
|
+
module Colors
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def green(text)
|
8
|
+
"\e[32m#{text}\e[0m"
|
9
|
+
end
|
10
|
+
|
11
|
+
def red(text)
|
12
|
+
"\e[31m#{text}\e[0m"
|
13
|
+
end
|
14
|
+
|
15
|
+
def yellow(text)
|
16
|
+
"\e[33m#{text}\e[0m"
|
17
|
+
end
|
18
|
+
|
19
|
+
def light_blue(text)
|
20
|
+
"\e[94m#{text}\e[0m"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# lib/stable/formatters/verbose.rb
|
2
|
+
require_relative 'colors'
|
3
|
+
|
4
|
+
module Stable
|
5
|
+
module Formatters
|
6
|
+
class Verbose
|
7
|
+
|
8
|
+
def initialize(facts)
|
9
|
+
@facts = facts
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s(fact)
|
13
|
+
short_uuid = fact.uuid.split('-').last
|
14
|
+
short_sig = fact.signature[0..6]
|
15
|
+
desc = "#{short_uuid}/#{short_sig}"
|
16
|
+
name_str = fact.name[..19].ljust(20)
|
17
|
+
call = "#{fact.class_name}##{fact.method_name}(#{fact.args.join(', ')})"
|
18
|
+
status_code = _status_code(fact)
|
19
|
+
error_code = _error_code(fact)
|
20
|
+
|
21
|
+
case fact.status
|
22
|
+
when :passed, :passed_with_error
|
23
|
+
"#{desc} #{name_str} #{status_code}#{error_code} #{call}"
|
24
|
+
when :failed
|
25
|
+
lines = ["#{desc} #{name_str} #{status_code}#{error_code} #{call}"]
|
26
|
+
if fact.actual_error
|
27
|
+
if fact.error
|
28
|
+
lines << " Expected error: #{fact.error['class']}"
|
29
|
+
lines << " Actual error: #{fact.actual_error.class.name}: #{fact.actual_error.message}"
|
30
|
+
else
|
31
|
+
lines << " Expected result: #{fact.result.inspect}"
|
32
|
+
lines << " Actual error: #{fact.actual_error.class.name}: #{fact.actual_error.message}"
|
33
|
+
end
|
34
|
+
else
|
35
|
+
if fact.error
|
36
|
+
lines << " Expected error: #{fact.error['class']}"
|
37
|
+
lines << " Actual result: #{fact.actual_result.inspect}"
|
38
|
+
else
|
39
|
+
lines << " Expected: #{fact.result.inspect}"
|
40
|
+
lines << " Actual: #{fact.actual_result.inspect}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
lines.join("\n")
|
44
|
+
else
|
45
|
+
"#{desc} #{name_str} #{status_code}#{error_code} #{call}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def header
|
50
|
+
header = "#{'uuid / sig'.ljust(20)} #{'name'.ljust(20)} st call"
|
51
|
+
"#{header}\n#{'-' * 20} #{'-' * 20} -- #{'-' * 35}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def summary
|
55
|
+
passing = @facts.count { |f| f.status == :passed || f.status == :passed_with_error }
|
56
|
+
failing = @facts.count { |f| f.status == :failed }
|
57
|
+
pending = @facts.count { |f| f.status == :pending }
|
58
|
+
"\n#{@facts.count} facts, #{passing} passing, #{pending} pending, #{failing} failing"
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def _status_code(fact)
|
65
|
+
case fact.status
|
66
|
+
when :passed, :passed_with_error
|
67
|
+
Colors.green('P')
|
68
|
+
when :failed
|
69
|
+
Colors.red('F')
|
70
|
+
else
|
71
|
+
Colors.yellow('?')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def _error_code(fact)
|
76
|
+
if fact.error || fact.actual_error
|
77
|
+
Colors.light_blue('E')
|
78
|
+
else
|
79
|
+
Colors.green('N')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/stable.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# `stable` is a library for recording and replaying method calls.
|
2
2
|
# See README.md for detailed usage instructions.
|
3
|
-
require_relative 'stable/
|
3
|
+
require_relative 'stable/fact'
|
4
4
|
require_relative 'stable/configuration'
|
5
5
|
|
6
6
|
if defined?(Rake)
|
@@ -38,7 +38,7 @@ module Stable
|
|
38
38
|
end
|
39
39
|
|
40
40
|
# this method is a block-based way to enable and disable recording of
|
41
|
-
#
|
41
|
+
# facts. It ensures that recording is turned on for the duration of the
|
42
42
|
# block and is automatically turned off afterward, even if an error occurs.
|
43
43
|
#
|
44
44
|
# example:
|
@@ -63,20 +63,20 @@ module Stable
|
|
63
63
|
if Stable.enabled?
|
64
64
|
begin
|
65
65
|
result = original_method.bind(self).call(*args, &block)
|
66
|
-
|
66
|
+
fact = Fact.new(
|
67
67
|
class_name: klass.name,
|
68
68
|
method_name: method_name,
|
69
69
|
args: args,
|
70
70
|
result: result
|
71
71
|
)
|
72
|
-
unless Stable.send(:
|
73
|
-
Stable.storage.puts(
|
72
|
+
unless Stable.send(:_fact_exists?, fact.signature)
|
73
|
+
Stable.storage.puts(fact.to_jsonl)
|
74
74
|
Stable.storage.flush
|
75
|
-
Stable.send(:
|
75
|
+
Stable.send(:_recorded_facts) << fact
|
76
76
|
end
|
77
77
|
result
|
78
78
|
rescue => e
|
79
|
-
|
79
|
+
fact = Fact.new(
|
80
80
|
class_name: klass.name,
|
81
81
|
method_name: method_name,
|
82
82
|
args: args,
|
@@ -86,10 +86,10 @@ module Stable
|
|
86
86
|
backtrace: e.backtrace
|
87
87
|
}
|
88
88
|
)
|
89
|
-
unless Stable.send(:
|
90
|
-
Stable.storage.puts(
|
89
|
+
unless Stable.send(:_fact_exists?, fact.signature)
|
90
|
+
Stable.storage.puts(fact.to_jsonl)
|
91
91
|
Stable.storage.flush
|
92
|
-
Stable.send(:
|
92
|
+
Stable.send(:_recorded_facts) << fact
|
93
93
|
end
|
94
94
|
raise e
|
95
95
|
end
|
@@ -102,23 +102,23 @@ module Stable
|
|
102
102
|
end
|
103
103
|
|
104
104
|
def verify(record_hash)
|
105
|
-
|
105
|
+
Fact.from_jsonl(record_hash.to_json).run!
|
106
106
|
end
|
107
107
|
|
108
108
|
private
|
109
109
|
|
110
|
-
def
|
111
|
-
@
|
110
|
+
def _recorded_facts
|
111
|
+
@_recorded_facts ||= begin
|
112
112
|
return [] unless storage.respond_to?(:path) && File.exist?(storage.path)
|
113
113
|
storage.rewind
|
114
|
-
|
114
|
+
facts = storage.each_line.map { |line| Fact.from_jsonl(line) }
|
115
115
|
storage.seek(0, IO::SEEK_END)
|
116
|
-
|
116
|
+
facts
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
|
-
def
|
121
|
-
|
120
|
+
def _fact_exists?(signature)
|
121
|
+
_recorded_facts.any? { |fact| fact.signature == signature }
|
122
122
|
end
|
123
123
|
end
|
124
124
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Lunt
|
@@ -33,7 +33,9 @@ files:
|
|
33
33
|
- lib/example/calculator.rb
|
34
34
|
- lib/stable.rb
|
35
35
|
- lib/stable/configuration.rb
|
36
|
-
- lib/stable/
|
36
|
+
- lib/stable/fact.rb
|
37
|
+
- lib/stable/formatters/colors.rb
|
38
|
+
- lib/stable/formatters/verbose.rb
|
37
39
|
homepage: https://github.com/jefflunt/stable
|
38
40
|
licenses:
|
39
41
|
- MIT
|