trace_wrapper 0.1.0 → 0.2.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/CHANGELOG.md +42 -0
- data/README.md +1 -1
- data/Rakefile +7 -0
- data/lib/trace_wrapper.rb +35 -44
- data/lib/trace_wrapper/shell.rb +32 -0
- data/lib/trace_wrapper/version.rb +2 -1
- data/test/test_trace_wrapper.rb +1 -1
- data/trace_wrapper.gemspec +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a24b778f09263356a804557de301cd497ee947853e019021e1489119c9dbaebc
|
4
|
+
data.tar.gz: 3f30db2855adc23225ded0f7780f10b2ede9ff04909bc8ff1b26ff5a06340914
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc092be9bb2b3e468ff1084447bf4c76a174b506e8e958992512a3d82662830610dae3d977bf2ff2afc3e14029b209df086341fa67c01c1afedf6d7bf6b893f2
|
7
|
+
data.tar.gz: d92524251b1fc0a1d9dba6807e67fcd9854be77245e7a28eb8b8295d138da35a3722aaf4dcd20cd09448a4a6d5f22470c84fce71a902d21e4a53f86f9a320823
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
|
9
|
+
## [0.2.0] - 2020-04-17
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
- [BREAKING CHANGE] `:visibility` option to `wrap()` changed to just indicate the lowest visibility level wanted [commit](https://github.com/biinari/trace_wrapper/commit/84024964d846ed522192d4c1f0a8ecbb8f516323)
|
13
|
+
- Moved shell colour internals to separate, undocumented module. [commit](https://github.com/biinari/trace_wrapper/7d4a9cb04314e252c4bfd184ce815b300d1f7852)
|
14
|
+
|
15
|
+
### Documentation
|
16
|
+
- Fixed documentation to include `README.md`.
|
17
|
+
- Added description of `#wrap` behaviour with / without a block.
|
18
|
+
|
19
|
+
## [0.1.0] - 2020-04-17
|
20
|
+
|
21
|
+
Initial public release
|
22
|
+
|
23
|
+
### Changed
|
24
|
+
|
25
|
+
- Changed `:method_type` choices to `:instance` (instance methods), `:self`
|
26
|
+
(class / module methods, i.e. methods called directly on receiver), `:all`
|
27
|
+
(both instance and direct methods)
|
28
|
+
|
29
|
+
### Added
|
30
|
+
|
31
|
+
- Allow passing `receivers` as an array in first argument to `wrap()`. [commit](https://github.com/biinari/trace_wrapper/3d16a0fa823219705a1214114016d8f66e820609)
|
32
|
+
- Add `:visibility` option to `wrap()` [PR #1](https://github.com/biinari/trace_wrapper/pulls/1)
|
33
|
+
- Add `[process:thread]` identifier to trace output when calls are made on a
|
34
|
+
different process / thread to the instantiation of `TraceWrapper`.
|
35
|
+
|
36
|
+
## 0.0.1 (never released)
|
37
|
+
|
38
|
+
[Initial commit](https://github.com/biinari/tree/f71416e97ce3b7c1e76d0d6722ea64eb4d2a01ff)
|
39
|
+
|
40
|
+
[Unreleased]: https://github.com/biinari/trace_wrapper/compare/v0.2.0...HEAD
|
41
|
+
[0.2.0]: https://github.com/biinari/trace_wrapper/compare/v0.1.0...v0.2.0
|
42
|
+
[0.1.0]: https://github.com/biinari/trace_wrapper/releases/tag/v0.1.0
|
data/README.md
CHANGED
@@ -64,7 +64,7 @@ What is the answer to the ultimate question?
|
|
64
64
|
42
|
65
65
|
```
|
66
66
|
|
67
|
-
See [custom example](examples/custom.rb) for a more custom usage
|
67
|
+
See [custom example](https://github.com/biinari/trace_wrapper/blob/master/examples/custom.rb) for a more custom usage
|
68
68
|
|
69
69
|
### Options
|
70
70
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rake/testtask'
|
4
|
+
require 'rdoc/task'
|
4
5
|
|
5
6
|
Rake::TestTask.new(:test) do |t|
|
6
7
|
t.libs << 'test'
|
@@ -12,4 +13,10 @@ task :rubocop do
|
|
12
13
|
sh 'rubocop'
|
13
14
|
end
|
14
15
|
|
16
|
+
RDoc::Task.new do |rdoc|
|
17
|
+
rdoc.main = 'README.md'
|
18
|
+
rdoc.rdoc_files.include('README.md', 'lib/**/*.rb')
|
19
|
+
rdoc.rdoc_dir = 'doc'
|
20
|
+
end
|
21
|
+
|
15
22
|
task default: %i[rubocop test]
|
data/lib/trace_wrapper.rb
CHANGED
@@ -1,32 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'trace_wrapper/version'
|
4
|
+
require 'trace_wrapper/shell'
|
4
5
|
|
5
6
|
##
|
6
7
|
# Wraps methods on given classes or modules to output a call/return tree.
|
7
8
|
class TraceWrapper
|
8
|
-
|
9
|
-
red: '31m',
|
10
|
-
b_red: '1;31m',
|
11
|
-
green: '32m',
|
12
|
-
b_green: '1;32m',
|
13
|
-
orange: '33m',
|
14
|
-
yellow: '1;33m',
|
15
|
-
blue: '34m',
|
16
|
-
b_blue: '1;34m',
|
17
|
-
purple: '35m',
|
18
|
-
b_purple: '1;35m',
|
19
|
-
teal: '36m',
|
20
|
-
cyan: '1;36m'
|
21
|
-
}.freeze
|
22
|
-
|
23
|
-
ELLIPSIS = "\u2026"
|
9
|
+
include TraceWrapper::Shell
|
24
10
|
|
25
11
|
class << self
|
26
|
-
|
12
|
+
##
|
13
|
+
# Wraps methods on given +receivers+ with tracing
|
14
|
+
#
|
15
|
+
# options will be passed to .new and #wrap respectively
|
16
|
+
#
|
17
|
+
# If a block is given, it will be passed to #wrap
|
18
|
+
def wrap(*receivers, **options, &block) # :yields: a_trace_wrapper
|
27
19
|
init_keys = %i[output colour]
|
28
|
-
init_args =
|
29
|
-
wrap_args =
|
20
|
+
init_args = options.select { |k, _| init_keys.include?(k) }
|
21
|
+
wrap_args = options.reject { |k, _| init_keys.include?(k) }
|
30
22
|
|
31
23
|
new(**init_args).wrap(*receivers, **wrap_args, &block)
|
32
24
|
end
|
@@ -39,7 +31,7 @@ class TraceWrapper
|
|
39
31
|
#
|
40
32
|
# :output - +IO+ object to write trace output to (default +STDOUT+)
|
41
33
|
# :colour - True to use shell colours in output (default +nil+ will colour if
|
42
|
-
#
|
34
|
+
# output is a TTY)
|
43
35
|
def initialize(output: $stdout, colour: nil)
|
44
36
|
@output = output
|
45
37
|
@colour = colour
|
@@ -54,18 +46,23 @@ class TraceWrapper
|
|
54
46
|
#
|
55
47
|
# Options
|
56
48
|
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
49
|
+
# +:method_type+ - Types of methods to wrap (default: +:all+). Choices are:
|
50
|
+
# +:instance+ (for instance methods),
|
51
|
+
# +:self+ (for receiver methods, i.e. class/module functions),
|
52
|
+
# +:all+ for both
|
53
|
+
#
|
54
|
+
# +:visibility+ - Lowest method visibility level to wrap
|
55
|
+
# (default: +:protected+). Choices are: :public, :protected, :private.
|
64
56
|
#
|
65
|
-
# If a block is given, the wrappers will be created just around the block
|
57
|
+
# If a block is given, the wrappers will be created just around the block and
|
58
|
+
# the block's result will be returned.
|
59
|
+
# The TraceWrapper instance will be yielded to the block to allow further
|
60
|
+
# wraps to be added.
|
61
|
+
#
|
62
|
+
# If no block is given, you should call unwrap after use.
|
66
63
|
def wrap(*receivers,
|
67
64
|
method_type: :all,
|
68
|
-
visibility:
|
65
|
+
visibility: :protected) # :yields: a_trace_wrapper
|
69
66
|
unwrappers = []
|
70
67
|
Array(*receivers).each do |receiver|
|
71
68
|
if %i[all self].include?(method_type)
|
@@ -96,7 +93,7 @@ class TraceWrapper
|
|
96
93
|
private
|
97
94
|
|
98
95
|
# Wrap standard methods (methods on the object given) with tracing
|
99
|
-
def wrap_methods(*receivers, visibility:
|
96
|
+
def wrap_methods(*receivers, visibility: :protected)
|
100
97
|
unwrappers = []
|
101
98
|
Array(*receivers).each do |receiver|
|
102
99
|
mod, unwrapper = wrapping_module(receiver, :self, visibility)
|
@@ -108,7 +105,7 @@ class TraceWrapper
|
|
108
105
|
|
109
106
|
# Wrap instance methods (called on an instance of the class given) with
|
110
107
|
# tracing
|
111
|
-
def wrap_instance_methods(*receivers, visibility:
|
108
|
+
def wrap_instance_methods(*receivers, visibility: :protected)
|
112
109
|
unwrappers = []
|
113
110
|
Array(*receivers).each do |receiver|
|
114
111
|
mod, unwrapper = wrapping_module(receiver, :instance, visibility)
|
@@ -166,12 +163,16 @@ class TraceWrapper
|
|
166
163
|
protected: :protected_methods,
|
167
164
|
private: :private_methods
|
168
165
|
}
|
169
|
-
}.freeze
|
166
|
+
}.freeze # :nodoc:
|
170
167
|
|
171
168
|
def get_methods(receiver, method_type, visibility)
|
172
|
-
%i[public protected private]
|
173
|
-
|
169
|
+
visibilities = %i[public protected private]
|
170
|
+
unless visibilities.include?(visibility)
|
171
|
+
raise "visibility option not recognised: #{visibility.inspect}"
|
172
|
+
end
|
173
|
+
visibilities = visibilities[0..visibilities.find_index(visibility)]
|
174
174
|
|
175
|
+
visibilities.map do |vis|
|
175
176
|
lister = LIST_METHODS[method_type][vis]
|
176
177
|
receiver.public_send(lister, false) - Object.public_send(lister)
|
177
178
|
end.compact.flatten
|
@@ -208,16 +209,6 @@ class TraceWrapper
|
|
208
209
|
@output.write("#{indent}#{text}\n")
|
209
210
|
end
|
210
211
|
|
211
|
-
def colour(text, colour)
|
212
|
-
return text unless colour?
|
213
|
-
"\e[#{COLOURS[colour]}#{text}\e[0m"
|
214
|
-
end
|
215
|
-
|
216
|
-
def colour?
|
217
|
-
return @colour unless @colour.nil?
|
218
|
-
@output.respond_to?(:isatty) && @output.isatty
|
219
|
-
end
|
220
|
-
|
221
212
|
def incr_indent
|
222
213
|
process[:indent] += 1
|
223
214
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class TraceWrapper
|
4
|
+
module Shell # :nodoc: all
|
5
|
+
COLOURS = {
|
6
|
+
red: '31m',
|
7
|
+
b_red: '1;31m',
|
8
|
+
green: '32m',
|
9
|
+
b_green: '1;32m',
|
10
|
+
orange: '33m',
|
11
|
+
yellow: '1;33m',
|
12
|
+
blue: '34m',
|
13
|
+
b_blue: '1;34m',
|
14
|
+
purple: '35m',
|
15
|
+
b_purple: '1;35m',
|
16
|
+
teal: '36m',
|
17
|
+
cyan: '1;36m'
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
ELLIPSIS = "\u2026"
|
21
|
+
|
22
|
+
def colour?
|
23
|
+
return @colour unless @colour.nil?
|
24
|
+
@output.respond_to?(:isatty) && @output.isatty
|
25
|
+
end
|
26
|
+
|
27
|
+
def colour(text, colour)
|
28
|
+
return text unless colour?
|
29
|
+
"\e[#{COLOURS[colour]}#{text}\e[0m"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/test/test_trace_wrapper.rb
CHANGED
@@ -156,7 +156,7 @@ class TestTraceWrapper < Minitest::Test
|
|
156
156
|
.gsub!(/@([^@]*)@/, "#{PURPLE}\\1#{CLEAR}")
|
157
157
|
|
158
158
|
subject = lambda do |tracer|
|
159
|
-
tracer.wrap(PlayClass, visibility:
|
159
|
+
tracer.wrap(PlayClass, visibility: :private)
|
160
160
|
result = PlayClass.new.play_solitaire(2, 3)
|
161
161
|
|
162
162
|
assert_equal('solo: 2, 3', result)
|
data/trace_wrapper.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
'source_code_uri' => 'https://github.com/biinari/trace_wrapper'
|
23
23
|
}
|
24
24
|
s.rdoc_options << '--main' << 'README.md'
|
25
|
+
s.extra_rdoc_files = ['README.md']
|
25
26
|
|
26
27
|
s.add_development_dependency 'minitest', '~> 5.14'
|
27
28
|
s.add_development_dependency 'rake', '~> 13.0'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trace_wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bill Ruddock
|
@@ -60,11 +60,13 @@ description: |-
|
|
60
60
|
email:
|
61
61
|
executables: []
|
62
62
|
extensions: []
|
63
|
-
extra_rdoc_files:
|
63
|
+
extra_rdoc_files:
|
64
|
+
- README.md
|
64
65
|
files:
|
65
66
|
- ".gitignore"
|
66
67
|
- ".rubocop.yml"
|
67
68
|
- ".travis.yml"
|
69
|
+
- CHANGELOG.md
|
68
70
|
- Gemfile
|
69
71
|
- LICENSE.md
|
70
72
|
- README.md
|
@@ -72,6 +74,7 @@ files:
|
|
72
74
|
- examples/basic.rb
|
73
75
|
- examples/custom.rb
|
74
76
|
- lib/trace_wrapper.rb
|
77
|
+
- lib/trace_wrapper/shell.rb
|
75
78
|
- lib/trace_wrapper/version.rb
|
76
79
|
- test/playground.rb
|
77
80
|
- test/test_trace_wrapper.rb
|