stable 1.4.0 → 1.5.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0ceeb4f1149e27df669ab25482efd557e1235a1e55014322ada67ce1639c34f
4
- data.tar.gz: 2e181545071d2d064cea2da6ebe1c587e113af4751333a20af2260b413ce602e
3
+ metadata.gz: 04f5edbc2d167df3bc6a5057291e567562f2e07b6885140ec96f3cf87b601032
4
+ data.tar.gz: 6da1a393e4f367fe1fcb284198f4f0a31e70df9537a8d910c4901e6b863c60f3
5
5
  SHA512:
6
- metadata.gz: 79c8dd17e53a71b7b4ee2a045182c08aa8c85a08f5aca4374aca987ef7ab740e134776fd1b0f396462e5e9890713639c7581b332eab507ca295c71c2854e3985
7
- data.tar.gz: 0ecb7350223103b539f01a260219549b75c9f0602abb7369ac57cd92aba6f76cdc060371d7d0ef0f0037fdc8552501ffd54db5758626d5a8b2b55e1b0bf768e2
6
+ metadata.gz: 95eb8713130c459485d8024fb7c5cef3b80d5e973b81534ee1cc43c11d617e573b3dedec3256bc4aafd3510915baf77498d6f83c096a3fb8ce921e9fd733b78b
7
+ data.tar.gz: bb5156a92198985700e2624d47f9c34f84a2d608c4192a2672550a0111fceac1d12ba71fef6949787bb2cdcaf514581f313535a160f6664a6f2b17a989970fbd
@@ -0,0 +1,11 @@
1
+ # lib/stable/configuration.rb
2
+ module Stable
3
+ class Configuration
4
+ attr_accessor :storage_path, :enabled
5
+
6
+ def initialize
7
+ @storage_path = nil
8
+ @enabled = false
9
+ end
10
+ end
11
+ end
data/lib/stable/spec.rb CHANGED
@@ -54,11 +54,11 @@ module Stable
54
54
 
55
55
  case status
56
56
  when :passed
57
- "#{desc} P #{call}"
57
+ "#{desc} #{_green('P')} #{call}"
58
58
  when :passed_with_error
59
- "#{desc} P (error) #{call}"
59
+ "#{desc} #{_green('P')} (error) #{call}"
60
60
  when :failed
61
- lines = ["#{desc} F #{call}"]
61
+ lines = ["#{desc} #{_red('F')} #{call}"]
62
62
  if actual_error
63
63
  if error
64
64
  lines << " Expected error: #{error['class']}"
@@ -78,7 +78,7 @@ module Stable
78
78
  end
79
79
  lines.join("\n")
80
80
  else
81
- "#{desc} ? #{call}"
81
+ "#{desc} #{_yellow('?')} #{call}"
82
82
  end
83
83
  end
84
84
 
@@ -107,5 +107,17 @@ module Stable
107
107
  uuid: data['uuid']
108
108
  )
109
109
  end
110
+
111
+ def _green(text)
112
+ "\e[32m#{text}\e[0m"
113
+ end
114
+
115
+ def _red(text)
116
+ "\e[31m#{text}\e[0m"
117
+ end
118
+
119
+ def _yellow(text)
120
+ "\e[33m#{text}\e[0m"
121
+ end
110
122
  end
111
123
  end
data/lib/stable.rb CHANGED
@@ -1,9 +1,18 @@
1
1
  # `stable` is a library for recording and replaying method calls.
2
2
  # See README.md for detailed usage instructions.
3
3
  require_relative 'stable/spec'
4
+ require_relative 'stable/configuration'
4
5
 
5
6
  module Stable
6
7
  class << self
8
+ def configuration
9
+ @configuration ||= Configuration.new
10
+ end
11
+
12
+ def configure
13
+ yield(configuration)
14
+ end
15
+
7
16
  def enable!
8
17
  Thread.current[:stable_enabled] = true
9
18
  end
@@ -13,7 +22,7 @@ module Stable
13
22
  end
14
23
 
15
24
  def enabled?
16
- Thread.current[:stable_enabled] || false
25
+ Thread.current[:stable_enabled] || configuration.enabled || false
17
26
  end
18
27
 
19
28
  def storage=(io)
@@ -21,7 +30,7 @@ module Stable
21
30
  end
22
31
 
23
32
  def storage
24
- @storage || raise("Stable.storage must be set to an IO-like object")
33
+ @storage ||= (configuration.storage_path && File.open(configuration.storage_path, 'a+')) || raise("Stable.storage must be set to an IO-like object")
25
34
  end
26
35
 
27
36
  # this method is a block-based way to enable and disable recording of
@@ -39,6 +48,8 @@ module Stable
39
48
  yield if block_given?
40
49
  ensure
41
50
  disable!
51
+ storage.close if storage.respond_to?(:close)
52
+ @storage = nil
42
53
  end
43
54
 
44
55
  def watch(klass, method_name)
@@ -56,6 +67,7 @@ module Stable
56
67
  )
57
68
  unless Stable.send(:_spec_exists?, spec.signature)
58
69
  Stable.storage.puts(spec.to_jsonl)
70
+ Stable.storage.flush
59
71
  Stable.send(:_recorded_specs) << spec
60
72
  end
61
73
  result
@@ -72,6 +84,7 @@ module Stable
72
84
  )
73
85
  unless Stable.send(:_spec_exists?, spec.signature)
74
86
  Stable.storage.puts(spec.to_jsonl)
87
+ Stable.storage.flush
75
88
  Stable.send(:_recorded_specs) << spec
76
89
  end
77
90
  raise e
@@ -93,7 +106,10 @@ module Stable
93
106
  def _recorded_specs
94
107
  @_recorded_specs ||= begin
95
108
  return [] unless storage.respond_to?(:path) && File.exist?(storage.path)
96
- File.foreach(storage.path).map { |line| Spec.from_jsonl(line) }
109
+ storage.rewind
110
+ specs = storage.each_line.map { |line| Spec.from_jsonl(line) }
111
+ storage.seek(0, IO::SEEK_END)
112
+ specs
97
113
  end
98
114
  end
99
115
 
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.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/stable.rb
20
+ - lib/stable/configuration.rb
20
21
  - lib/stable/spec.rb
21
22
  homepage: https://github.com/jefflunt/stable
22
23
  licenses: