trace_wrapper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +75 -0
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/LICENSE.md +33 -0
- data/README.md +110 -0
- data/Rakefile +15 -0
- data/examples/basic.rb +26 -0
- data/examples/custom.rb +30 -0
- data/lib/trace_wrapper/version.rb +5 -0
- data/lib/trace_wrapper.rb +274 -0
- data/test/playground.rb +79 -0
- data/test/test_trace_wrapper.rb +313 -0
- data/trace_wrapper.gemspec +29 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b0064e5f0a037df2776197d1bc4f037b2143bffed37c585dd2b32c3e00206eb9
|
4
|
+
data.tar.gz: 1fe3daabe757fae12b60deb33e28df9fc647c74c433b356bbbcce8085fef3e12
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 311db8eafe42199cb782128a12bf0271d40664a4a54de3f1aea57e0a60f144ed20225338b22e0517e9a99dab8f95dab8f8b7341a31f8b40c751ce0642a6c1954
|
7
|
+
data.tar.gz: ed522468367180ee1c63029a6fbb5d670547e54d839bb6e149670baed46d9e8e27d6517e76ce9f6f89e42cd785ce54f5aeb2c2f302be36d1a24ab8aa8b8ef970
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
Layout/EmptyLineAfterGuardClause:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Layout/LineLength:
|
8
|
+
Exclude:
|
9
|
+
- '**/Gemfile'
|
10
|
+
- '**/*.gemspec'
|
11
|
+
IgnoredPatterns:
|
12
|
+
- '^.{,89}$'
|
13
|
+
- '[\w:]{50}'
|
14
|
+
- '^\s*\b(background|context|it|describe|scenario|pending|skip|(include|shared)_(examples|context)|it_behaves_like)\b.*(do|\{)?$'
|
15
|
+
|
16
|
+
Metrics/AbcSize:
|
17
|
+
Max: 30
|
18
|
+
Severity: refactor
|
19
|
+
Exclude:
|
20
|
+
- '**/test/test_*.rb'
|
21
|
+
|
22
|
+
Metrics/BlockLength:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Metrics/ClassLength:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Metrics/MethodLength:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Metrics/ParameterLists:
|
32
|
+
Max: 6
|
33
|
+
|
34
|
+
Naming/MethodParameterName:
|
35
|
+
AllowedNames:
|
36
|
+
- a
|
37
|
+
- b
|
38
|
+
- m
|
39
|
+
- n
|
40
|
+
- x
|
41
|
+
- y
|
42
|
+
- io
|
43
|
+
- id
|
44
|
+
- to
|
45
|
+
- by
|
46
|
+
- 'on'
|
47
|
+
- in
|
48
|
+
- at
|
49
|
+
- ip
|
50
|
+
- db
|
51
|
+
- os
|
52
|
+
- pp
|
53
|
+
|
54
|
+
Style/Documentation:
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
Style/FrozenStringLiteralComment:
|
58
|
+
Exclude:
|
59
|
+
- '**/Gemfile'
|
60
|
+
- '**/*.gemspec'
|
61
|
+
|
62
|
+
Style/HashEachMethods:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
Style/HashTransformKeys:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
Style/HashTransformValues:
|
69
|
+
Enabled: false
|
70
|
+
|
71
|
+
Style/RescueModifier:
|
72
|
+
Enabled: false
|
73
|
+
|
74
|
+
Style/TrailingUnderscoreVariable:
|
75
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
TraceWrapper Copyright (2020) (Bill Ruddock)(“Licensor”)
|
2
|
+
|
3
|
+
Hippocratic License Version Number: 2.1.
|
4
|
+
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
|
6
|
+
|
7
|
+
Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
|
8
|
+
|
9
|
+
* Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
|
10
|
+
|
11
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
|
12
|
+
|
13
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
14
|
+
|
15
|
+
1. Human Rights Principles.
|
16
|
+
|
17
|
+
(a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
|
18
|
+
|
19
|
+
(b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
|
20
|
+
|
21
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
|
22
|
+
|
23
|
+
2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
|
24
|
+
|
25
|
+
3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
|
26
|
+
|
27
|
+
* Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
|
28
|
+
|
29
|
+
* Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
|
30
|
+
|
31
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
32
|
+
|
33
|
+
This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# TraceWrapper method call / return tracing
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/biinari/trace_wrapper.svg?branch=master)](https://travis-ci.org/biinari/trace_wrapper)
|
4
|
+
|
5
|
+
TraceWrapper outputs method call and return info for a class or module
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'trace_wrapper'
|
13
|
+
```
|
14
|
+
|
15
|
+
Or install it generally with:
|
16
|
+
|
17
|
+
```sh
|
18
|
+
$ gem install trace_wrapper
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
A simple example of a module and a class to be traced:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
module MyModule
|
27
|
+
module_function
|
28
|
+
|
29
|
+
def something_else
|
30
|
+
puts 'What else?'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class MyClass
|
35
|
+
def self.forty(two: 2)
|
36
|
+
40 + two
|
37
|
+
end
|
38
|
+
|
39
|
+
def plus_two(x)
|
40
|
+
x + 2
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
A basic example of tracing our sample class and module:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
TraceWrapper.wrap(MyClass, MyModule) do
|
49
|
+
puts MyModule.subject
|
50
|
+
puts MyClass.meaning(x: 40)
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Will output the following (with the tracing all indented by at least 2 spaces):
|
55
|
+
|
56
|
+
```
|
57
|
+
MyModule.subject()
|
58
|
+
MyModule.subject return "What is the answer …"
|
59
|
+
What is the answer to the ultimate question?
|
60
|
+
MyClass.meaning(x: 40)
|
61
|
+
MyClass#plus_two(40)
|
62
|
+
MyClass#plus_two return 42
|
63
|
+
MyClass.meaning return 42
|
64
|
+
42
|
65
|
+
```
|
66
|
+
|
67
|
+
See [custom example](examples/custom.rb) for a more custom usage
|
68
|
+
|
69
|
+
### Options
|
70
|
+
|
71
|
+
Options for `TraceWrapper.new`
|
72
|
+
|
73
|
+
* `:colour` - Enable coloured output (default: `nil` automatically enables colour if output is a TTY)
|
74
|
+
* `:output` - Specify output `IO` (default: `STDOUT`)
|
75
|
+
|
76
|
+
Options for `TraceWrapper#wrap`
|
77
|
+
|
78
|
+
* `:method_type` - Which type of methods to wrap
|
79
|
+
|
80
|
+
It is also supported to run `TraceWrapper.wrap` with options available to `.new` and the instance method `#wrap`.
|
81
|
+
|
82
|
+
Sample usage with all options:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
tracer = TraceWrapper.new(colour: true, output: STDERR)
|
86
|
+
tracer.wrap(MyClass, method_type: :instance_methods)
|
87
|
+
MyClass.new.plus_two(40)
|
88
|
+
tracer.unwrap
|
89
|
+
|
90
|
+
# or in a single call
|
91
|
+
TraceWrapper.wrap(MyClass, colour: false, method_type: :methods) do
|
92
|
+
MyClass.meaning(x: 40)
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
## Testing
|
97
|
+
|
98
|
+
Run the tests with `rake` or directly with:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
bundle exec ruby test/test_trace_wrapper.rb
|
102
|
+
```
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
1. Fork it
|
107
|
+
2. Create your feature branch
|
108
|
+
3. Commit your changes
|
109
|
+
4. Push to your branch
|
110
|
+
5. Create a Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << 'test'
|
7
|
+
t.libs << 'lib'
|
8
|
+
t.test_files = FileList['test/**/test_*.rb']
|
9
|
+
end
|
10
|
+
|
11
|
+
task :rubocop do
|
12
|
+
sh 'rubocop'
|
13
|
+
end
|
14
|
+
|
15
|
+
task default: %i[rubocop test]
|
data/examples/basic.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../lib/trace_wrapper'
|
4
|
+
|
5
|
+
module MyModule
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def subject
|
9
|
+
'What is the answer to the ultimate question?'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class MyClass
|
14
|
+
def self.meaning(x: 1)
|
15
|
+
new.plus_two(x)
|
16
|
+
end
|
17
|
+
|
18
|
+
def plus_two(x)
|
19
|
+
x + 2
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
TraceWrapper.wrap(MyModule, MyClass) do
|
24
|
+
puts MyModule.subject
|
25
|
+
puts MyClass.meaning(x: 40)
|
26
|
+
end
|
data/examples/custom.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../lib/trace_wrapper'
|
4
|
+
|
5
|
+
module MyModule
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def subject
|
9
|
+
'What is the answer to the ultimate question?'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class MyClass
|
14
|
+
def self.meaning(x: 1)
|
15
|
+
new.plus_two(x)
|
16
|
+
end
|
17
|
+
|
18
|
+
def plus_two(x)
|
19
|
+
x + 2
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
tracer = TraceWrapper.new(colour: false, output: STDERR)
|
24
|
+
tracer.wrap(MyModule, method_type: :methods)
|
25
|
+
tracer.wrap(MyClass, method_type: :all)
|
26
|
+
|
27
|
+
puts MyModule.subject
|
28
|
+
puts MyClass.meaning(x: 40)
|
29
|
+
|
30
|
+
tracer.unwrap
|
@@ -0,0 +1,274 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'trace_wrapper/version'
|
4
|
+
|
5
|
+
##
|
6
|
+
# Wraps methods on given classes or modules to output a call/return tree.
|
7
|
+
class TraceWrapper
|
8
|
+
COLOURS = {
|
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"
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def wrap(*receivers, **kwargs, &block)
|
27
|
+
init_keys = %i[output colour]
|
28
|
+
init_args = kwargs.select { |k, _| init_keys.include?(k) }
|
29
|
+
wrap_args = kwargs.reject { |k, _| init_keys.include?(k) }
|
30
|
+
|
31
|
+
new(**init_args).wrap(*receivers, **wrap_args, &block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Create a new +TraceWrapper+
|
37
|
+
#
|
38
|
+
# Options:
|
39
|
+
#
|
40
|
+
# :output - +IO+ object to write trace output to (default +STDOUT+)
|
41
|
+
# :colour - True to use shell colours in output (default +nil+ will colour if
|
42
|
+
# output is a TTY)
|
43
|
+
def initialize(output: $stdout, colour: nil)
|
44
|
+
@output = output
|
45
|
+
@colour = colour
|
46
|
+
@unwrappers = []
|
47
|
+
@main_process_id = process_id
|
48
|
+
@processes = {}
|
49
|
+
process
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Wraps methods on given +receivers+ with tracing
|
54
|
+
#
|
55
|
+
# Options
|
56
|
+
#
|
57
|
+
# :method_type - Types of methods to wrap (default: :all). Choices are:
|
58
|
+
# :instance for methods on instances of receiver(s)
|
59
|
+
# :self for methods called directly on the receiver(s)
|
60
|
+
# :all for both
|
61
|
+
# :visibility - Array of method visibility levels to wrap. Can include
|
62
|
+
# :public, :protected, :private
|
63
|
+
# (default [:public, :protected])
|
64
|
+
#
|
65
|
+
# If a block is given, the wrappers will be created just around the block
|
66
|
+
def wrap(*receivers,
|
67
|
+
method_type: :all,
|
68
|
+
visibility: %i[public protected])
|
69
|
+
unwrappers = []
|
70
|
+
Array(*receivers).each do |receiver|
|
71
|
+
if %i[all self].include?(method_type)
|
72
|
+
unwrappers += wrap_methods(receiver, visibility: visibility)
|
73
|
+
end
|
74
|
+
if %i[all instance].include?(method_type)
|
75
|
+
unwrappers += wrap_instance_methods(receiver, visibility: visibility)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
if block_given?
|
79
|
+
begin
|
80
|
+
yield(self)
|
81
|
+
ensure
|
82
|
+
unwrappers.each(&:call)
|
83
|
+
end
|
84
|
+
else
|
85
|
+
@unwrappers += unwrappers
|
86
|
+
self
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Remove any wrappers set by this tracer
|
91
|
+
def unwrap
|
92
|
+
@unwrappers.each(&:call)
|
93
|
+
@unwrappers = []
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
# Wrap standard methods (methods on the object given) with tracing
|
99
|
+
def wrap_methods(*receivers, visibility: %i[public protected])
|
100
|
+
unwrappers = []
|
101
|
+
Array(*receivers).each do |receiver|
|
102
|
+
mod, unwrapper = wrapping_module(receiver, :self, visibility)
|
103
|
+
unwrappers << unwrapper
|
104
|
+
receiver.singleton_class.send(:prepend, mod)
|
105
|
+
end
|
106
|
+
unwrappers
|
107
|
+
end
|
108
|
+
|
109
|
+
# Wrap instance methods (called on an instance of the class given) with
|
110
|
+
# tracing
|
111
|
+
def wrap_instance_methods(*receivers, visibility: %i[public protected])
|
112
|
+
unwrappers = []
|
113
|
+
Array(*receivers).each do |receiver|
|
114
|
+
mod, unwrapper = wrapping_module(receiver, :instance, visibility)
|
115
|
+
unwrappers << unwrapper
|
116
|
+
receiver.send(:prepend, mod)
|
117
|
+
end
|
118
|
+
unwrappers
|
119
|
+
end
|
120
|
+
|
121
|
+
def wrapping_module(receiver, method_type, visibility)
|
122
|
+
method_names = get_methods(receiver, method_type, visibility)
|
123
|
+
get_method = method_type == :instance ? :instance_method : :method
|
124
|
+
dot = method_type == :instance ? '#' : '.'
|
125
|
+
trace_call = method(:trace_call)
|
126
|
+
trace_return = method(:trace_return)
|
127
|
+
key_args = method(:key_args?)
|
128
|
+
|
129
|
+
mod = Module.new do
|
130
|
+
method_names.each do |name|
|
131
|
+
wrap_method =
|
132
|
+
if key_args.call(receiver.public_send(get_method, name))
|
133
|
+
lambda do |*args, **kwargs, &block|
|
134
|
+
trace_call.call(receiver, dot, name, *args, **kwargs)
|
135
|
+
result = super(*args, **kwargs, &block)
|
136
|
+
trace_return.call(receiver, dot, name, result)
|
137
|
+
result
|
138
|
+
end
|
139
|
+
else
|
140
|
+
lambda do |*args, &block|
|
141
|
+
trace_call.call(receiver, dot, name, *args)
|
142
|
+
result = super(*args, &block)
|
143
|
+
trace_return.call(receiver, dot, name, result)
|
144
|
+
result
|
145
|
+
end
|
146
|
+
end
|
147
|
+
define_method(name, wrap_method)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
unwrapper = lambda do
|
151
|
+
method_names.each do |name|
|
152
|
+
mod.send(:remove_method, name) if mod.method_defined?(name)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
[mod, unwrapper]
|
156
|
+
end
|
157
|
+
|
158
|
+
LIST_METHODS = {
|
159
|
+
instance: {
|
160
|
+
public: :public_instance_methods,
|
161
|
+
protected: :protected_instance_methods,
|
162
|
+
private: :private_instance_methods
|
163
|
+
},
|
164
|
+
self: {
|
165
|
+
public: :public_methods,
|
166
|
+
protected: :protected_methods,
|
167
|
+
private: :private_methods
|
168
|
+
}
|
169
|
+
}.freeze
|
170
|
+
|
171
|
+
def get_methods(receiver, method_type, visibility)
|
172
|
+
%i[public protected private].map do |vis|
|
173
|
+
next unless visibility.include?(vis)
|
174
|
+
|
175
|
+
lister = LIST_METHODS[method_type][vis]
|
176
|
+
receiver.public_send(lister, false) - Object.public_send(lister)
|
177
|
+
end.compact.flatten
|
178
|
+
end
|
179
|
+
|
180
|
+
def trace_call(receiver, dot, method_name, *args, **kwargs)
|
181
|
+
writeln("#{show_pid}#{function(receiver, dot, method_name)}" \
|
182
|
+
"(#{show_args(*args, **kwargs)})")
|
183
|
+
incr_indent
|
184
|
+
end
|
185
|
+
|
186
|
+
def trace_return(receiver, dot, method_name, result)
|
187
|
+
decr_indent
|
188
|
+
writeln("#{show_pid}#{function(receiver, dot, method_name)} " \
|
189
|
+
"#{colour('return', :yellow)} " \
|
190
|
+
"#{colour(short_inspect(result), :purple)}")
|
191
|
+
end
|
192
|
+
|
193
|
+
def key_args?(method)
|
194
|
+
method.parameters.any? { |k, _| %i[keyrest key].include?(k) }
|
195
|
+
end
|
196
|
+
|
197
|
+
def main
|
198
|
+
TOPLEVEL_BINDING.receiver
|
199
|
+
end
|
200
|
+
|
201
|
+
def function(receiver, dot, name)
|
202
|
+
return colour(id, :teal) if main == receiver
|
203
|
+
|
204
|
+
"#{colour(receiver, :b_green)}#{dot}#{colour(name, :teal)}"
|
205
|
+
end
|
206
|
+
|
207
|
+
def writeln(text)
|
208
|
+
@output.write("#{indent}#{text}\n")
|
209
|
+
end
|
210
|
+
|
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
|
+
def incr_indent
|
222
|
+
process[:indent] += 1
|
223
|
+
end
|
224
|
+
|
225
|
+
def decr_indent
|
226
|
+
process[:indent] = [process[:indent] - 1, 0].max
|
227
|
+
end
|
228
|
+
|
229
|
+
def indent
|
230
|
+
' ' * (process[:indent] + 1)
|
231
|
+
end
|
232
|
+
|
233
|
+
def process
|
234
|
+
proc_colours = %i[b_purple orange blue red purple cyan yellow b_blue b_red]
|
235
|
+
@processes[process_id.join(':')] ||= {
|
236
|
+
colour: proc_colours[@processes.size],
|
237
|
+
indent: 0
|
238
|
+
}
|
239
|
+
end
|
240
|
+
|
241
|
+
def process_id
|
242
|
+
[Process.pid, Thread.current.hash]
|
243
|
+
end
|
244
|
+
|
245
|
+
def process_label
|
246
|
+
return if process_id == @main_process_id
|
247
|
+
pid, tid = process_id
|
248
|
+
return pid if Thread.current == Thread.main
|
249
|
+
pid = '' if pid == @main_process_id.first
|
250
|
+
"#{pid}:#{tid.to_s[-4..-1]}"
|
251
|
+
end
|
252
|
+
|
253
|
+
def show_pid
|
254
|
+
return if process_id == @main_process_id
|
255
|
+
colour("[#{process_label}]", process[:colour])
|
256
|
+
end
|
257
|
+
|
258
|
+
def show_args(*args, **kwargs)
|
259
|
+
return if args.empty? && kwargs.empty?
|
260
|
+
parts = args.map do |v|
|
261
|
+
colour(short_inspect(v), :purple)
|
262
|
+
end
|
263
|
+
parts += kwargs.map do |k, v|
|
264
|
+
"#{k}: #{colour(short_inspect(v), :purple)}"
|
265
|
+
end
|
266
|
+
parts.join(', ')
|
267
|
+
end
|
268
|
+
|
269
|
+
def short_inspect(obj, limit = 20)
|
270
|
+
text = obj.inspect
|
271
|
+
return text if text.length <= limit
|
272
|
+
text[0...limit] + ELLIPSIS + text[-1]
|
273
|
+
end
|
274
|
+
end
|
data/test/playground.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PlayModule
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def one(text)
|
7
|
+
"#{text} and a one"
|
8
|
+
end
|
9
|
+
|
10
|
+
def two(*args, **kwargs)
|
11
|
+
yield(*args, **kwargs)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class PlayClass
|
16
|
+
class << self
|
17
|
+
def play_hello
|
18
|
+
new.play('hello', 'world')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def play(*args)
|
23
|
+
args.map(&:to_s).join(', ')
|
24
|
+
end
|
25
|
+
|
26
|
+
def play_friendly(*args)
|
27
|
+
friendly(*args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def play_solitaire(*args)
|
31
|
+
solitaire(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def friendly(*args)
|
37
|
+
"friends: #{args.map(&:to_s).join(', ')}"
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def solitaire(*args)
|
43
|
+
"solo: #{args.map(&:to_s).join(', ')}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module PlayArgs
|
48
|
+
module_function
|
49
|
+
|
50
|
+
def full(x, y = 1, a: 3)
|
51
|
+
[x, y, a]
|
52
|
+
end
|
53
|
+
|
54
|
+
def full_rest(x, y = 1, *args, a: 2, **kwargs, &block)
|
55
|
+
block.call(x, y, *args, a: a, **kwargs)
|
56
|
+
end
|
57
|
+
|
58
|
+
def rest(*args)
|
59
|
+
args
|
60
|
+
end
|
61
|
+
|
62
|
+
def key_rest(**kwargs)
|
63
|
+
kwargs
|
64
|
+
end
|
65
|
+
|
66
|
+
def both_rest(*args, **kwargs)
|
67
|
+
[args, kwargs]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class PlayFib
|
72
|
+
# Naive fibonacci implementation (we want to test some deep nesting)
|
73
|
+
def fib(n)
|
74
|
+
raise ArgumentError, 'Must be positive' if n.negative?
|
75
|
+
return 1 if n <= 1
|
76
|
+
|
77
|
+
fib(n - 1) + fib(n - 2)
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,313 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH << File.expand_path('../lib', __dir__)
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'minitest/pride'
|
7
|
+
|
8
|
+
require 'trace_wrapper'
|
9
|
+
require File.expand_path('playground', __dir__)
|
10
|
+
|
11
|
+
class TestTraceWrapper < Minitest::Test
|
12
|
+
class Output
|
13
|
+
attr_reader :output
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@output = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def write(text)
|
20
|
+
text = text.to_s unless text.is_a?(String)
|
21
|
+
@output << text
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Pipe
|
26
|
+
def initialize
|
27
|
+
@reader, @writer = IO.pipe
|
28
|
+
end
|
29
|
+
|
30
|
+
def write(*args)
|
31
|
+
@writer.write(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def read(*args)
|
35
|
+
@reader.read(*args)
|
36
|
+
end
|
37
|
+
|
38
|
+
def close
|
39
|
+
@writer.close
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
::TraceWrapper::COLOURS.each do |k, v|
|
44
|
+
const_set(k.upcase, "\e[#{v}")
|
45
|
+
end
|
46
|
+
CLEAR = "\e[0m"
|
47
|
+
ELLIPSIS = "\u2026"
|
48
|
+
RETURN = "#{YELLOW}return#{CLEAR}"
|
49
|
+
|
50
|
+
def strip_colour(text)
|
51
|
+
text.gsub(/\e\[(?:1;)?\d+m/, '')
|
52
|
+
end
|
53
|
+
|
54
|
+
def assert_equal_output(expected_output, **tracer_options)
|
55
|
+
output = Output.new
|
56
|
+
opts = tracer_options.merge(output: output)
|
57
|
+
tracer = ::TraceWrapper.new(**opts)
|
58
|
+
yield(tracer)
|
59
|
+
|
60
|
+
assert_equal(expected_output, output.output.join)
|
61
|
+
ensure
|
62
|
+
tracer.unwrap
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_wrap_output_module
|
66
|
+
mod_name = "#{B_GREEN}PlayModule#{CLEAR}"
|
67
|
+
|
68
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
69
|
+
MOD.TWO()
|
70
|
+
MOD.ONE(#{PURPLE}"abc"#{CLEAR})
|
71
|
+
MOD.ONE RETURN #{PURPLE}"abc and a one"#{CLEAR}
|
72
|
+
MOD.TWO RETURN #{PURPLE}"abc and a one"#{CLEAR}
|
73
|
+
OUTPUT
|
74
|
+
expected_output.gsub!('MOD', mod_name)
|
75
|
+
.gsub!('RETURN', RETURN)
|
76
|
+
.gsub!('ONE', "#{TEAL}one#{CLEAR}")
|
77
|
+
.gsub!('TWO', "#{TEAL}two#{CLEAR}")
|
78
|
+
|
79
|
+
subject = lambda do |tracer|
|
80
|
+
tracer.wrap(PlayModule, method_type: :self)
|
81
|
+
result = PlayModule.two do
|
82
|
+
PlayModule.one('abc')
|
83
|
+
end
|
84
|
+
|
85
|
+
assert_equal('abc and a one', result)
|
86
|
+
end
|
87
|
+
|
88
|
+
assert_equal_output(strip_colour(expected_output), colour: false, &subject)
|
89
|
+
assert_equal_output(expected_output, colour: true, &subject)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_wrap_output_class
|
93
|
+
cls_name = "#{B_GREEN}PlayClass#{CLEAR}"
|
94
|
+
|
95
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
96
|
+
CLASS.PLAY_HELLO()
|
97
|
+
CLASS#PLAY(#{PURPLE}"hello"#{CLEAR}, #{PURPLE}"world"#{CLEAR})
|
98
|
+
CLASS#PLAY RETURN #{PURPLE}"hello, world"#{CLEAR}
|
99
|
+
CLASS.PLAY_HELLO RETURN #{PURPLE}"hello, world"#{CLEAR}
|
100
|
+
OUTPUT
|
101
|
+
expected_output.gsub!('CLASS', cls_name)
|
102
|
+
.gsub!('RETURN', RETURN)
|
103
|
+
.gsub!('PLAY_HELLO', "#{TEAL}play_hello#{CLEAR}")
|
104
|
+
.gsub!('PLAY', "#{TEAL}play#{CLEAR}")
|
105
|
+
|
106
|
+
subject = lambda do |tracer|
|
107
|
+
tracer.wrap(PlayClass)
|
108
|
+
result = PlayClass.play_hello
|
109
|
+
|
110
|
+
assert_equal('hello, world', result)
|
111
|
+
end
|
112
|
+
|
113
|
+
assert_equal_output(strip_colour(expected_output), colour: false, &subject)
|
114
|
+
assert_equal_output(expected_output, colour: true, &subject)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_wrap_output_protected_methods
|
118
|
+
cls_name = "#{B_GREEN}PlayClass#{CLEAR}"
|
119
|
+
|
120
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
121
|
+
CLASS#PLAY_FRIENDLY(@1@)
|
122
|
+
CLASS#FRIENDLY(@1@)
|
123
|
+
CLASS#FRIENDLY RETURN @"friends: 1"@
|
124
|
+
CLASS#PLAY_FRIENDLY RETURN @"friends: 1"@
|
125
|
+
OUTPUT
|
126
|
+
expected_output.gsub!('CLASS', cls_name)
|
127
|
+
.gsub!('RETURN', RETURN)
|
128
|
+
.gsub!('PLAY_FRIENDLY', "#{TEAL}play_friendly#{CLEAR}")
|
129
|
+
.gsub!('FRIENDLY', "#{TEAL}friendly#{CLEAR}")
|
130
|
+
.gsub!(/@([^@]*)@/, "#{PURPLE}\\1#{CLEAR}")
|
131
|
+
|
132
|
+
subject = lambda do |tracer|
|
133
|
+
tracer.wrap(PlayClass)
|
134
|
+
result = PlayClass.new.play_friendly(1)
|
135
|
+
|
136
|
+
assert_equal('friends: 1', result)
|
137
|
+
end
|
138
|
+
|
139
|
+
assert_equal_output(strip_colour(expected_output), colour: false, &subject)
|
140
|
+
assert_equal_output(expected_output, colour: true, &subject)
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_wrap_output_private_methods
|
144
|
+
cls_name = "#{B_GREEN}PlayClass#{CLEAR}"
|
145
|
+
|
146
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
147
|
+
CLASS#PLAY_SOLITAIRE(@2@, @3@)
|
148
|
+
CLASS#SOLITAIRE(@2@, @3@)
|
149
|
+
CLASS#SOLITAIRE RETURN @"solo: 2, 3"@
|
150
|
+
CLASS#PLAY_SOLITAIRE RETURN @"solo: 2, 3"@
|
151
|
+
OUTPUT
|
152
|
+
expected_output.gsub!('CLASS', cls_name)
|
153
|
+
.gsub!('RETURN', RETURN)
|
154
|
+
.gsub!('PLAY_SOLITAIRE', "#{TEAL}play_solitaire#{CLEAR}")
|
155
|
+
.gsub!('SOLITAIRE', "#{TEAL}solitaire#{CLEAR}")
|
156
|
+
.gsub!(/@([^@]*)@/, "#{PURPLE}\\1#{CLEAR}")
|
157
|
+
|
158
|
+
subject = lambda do |tracer|
|
159
|
+
tracer.wrap(PlayClass, visibility: %i[public protected private])
|
160
|
+
result = PlayClass.new.play_solitaire(2, 3)
|
161
|
+
|
162
|
+
assert_equal('solo: 2, 3', result)
|
163
|
+
end
|
164
|
+
|
165
|
+
assert_equal_output(strip_colour(expected_output), colour: false, &subject)
|
166
|
+
assert_equal_output(expected_output, colour: true, &subject)
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_wrap_output_class_nesting
|
170
|
+
cls_name = "#{B_GREEN}PlayFib#{CLEAR}"
|
171
|
+
fib = "#{TEAL}fib#{CLEAR}"
|
172
|
+
|
173
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
174
|
+
SIG(ARG(4))
|
175
|
+
SIG(ARG(3))
|
176
|
+
SIG(ARG(2))
|
177
|
+
SIG(ARG(1))
|
178
|
+
SIG RETURN ARG(1)
|
179
|
+
SIG(ARG(0))
|
180
|
+
SIG RETURN ARG(1)
|
181
|
+
SIG RETURN ARG(2)
|
182
|
+
SIG(ARG(1))
|
183
|
+
SIG RETURN ARG(1)
|
184
|
+
SIG RETURN ARG(3)
|
185
|
+
SIG(ARG(2))
|
186
|
+
SIG(ARG(1))
|
187
|
+
SIG RETURN ARG(1)
|
188
|
+
SIG(ARG(0))
|
189
|
+
SIG RETURN ARG(1)
|
190
|
+
SIG RETURN ARG(2)
|
191
|
+
SIG RETURN ARG(5)
|
192
|
+
OUTPUT
|
193
|
+
expected_output.gsub!('SIG', "#{cls_name}##{fib}")
|
194
|
+
.gsub!('RETURN', RETURN)
|
195
|
+
.gsub!(/ARG\((\d+)\)/, "#{PURPLE}\\1#{CLEAR}")
|
196
|
+
|
197
|
+
subject = lambda do |tracer|
|
198
|
+
tracer.wrap(PlayFib, method_type: :instance)
|
199
|
+
result = PlayFib.new.fib(4)
|
200
|
+
|
201
|
+
assert_equal(5, result)
|
202
|
+
end
|
203
|
+
|
204
|
+
assert_equal_output(strip_colour(expected_output), colour: false, &subject)
|
205
|
+
assert_equal_output(expected_output, colour: true, &subject)
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_wrap_args
|
209
|
+
mod_name = "#{B_GREEN}PlayArgs#{CLEAR}"
|
210
|
+
methods_pattern = /\b(full(?:_rest)?|(?:key_|both_)?rest)\b/
|
211
|
+
|
212
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
213
|
+
MOD.full(@3@, @4@, a: @5@)
|
214
|
+
MOD.full RETURN @[3, 4, 5]@
|
215
|
+
MOD.full_rest(@5@, @6@, @7@, a: @1@, b: @2@)
|
216
|
+
MOD.full_rest RETURN @[[5, 6, 7], {:a=>1, #{ELLIPSIS}]@
|
217
|
+
MOD.rest(@42@, @"b"@)
|
218
|
+
MOD.rest RETURN @[42, "b"]@
|
219
|
+
MOD.key_rest(a: @"a"@, b: @nil@)
|
220
|
+
MOD.key_rest RETURN @{:a=>"a", :b=>nil}@
|
221
|
+
MOD.both_rest(@12@, @"d"@, a: @nil@, b: @9@)
|
222
|
+
MOD.both_rest RETURN @[[12, "d"], {:a=>nil#{ELLIPSIS}]@
|
223
|
+
OUTPUT
|
224
|
+
expected_output.gsub!('MOD', mod_name)
|
225
|
+
.gsub!('RETURN', RETURN)
|
226
|
+
.gsub!(methods_pattern, "#{TEAL}\\1#{CLEAR}")
|
227
|
+
.gsub!(/@([^@]*)@/, "#{PURPLE}\\1#{CLEAR}")
|
228
|
+
|
229
|
+
subject = lambda do |tracer|
|
230
|
+
tracer.wrap(PlayArgs)
|
231
|
+
assert_equal([3, 4, 5], PlayArgs.full(3, 4, a: 5))
|
232
|
+
assert_equal([[5, 6, 7], { a: 1, b: 2 }],
|
233
|
+
PlayArgs.full_rest(5, 6, 7, a: 1, b: 2) do |*args, **kwargs|
|
234
|
+
[args, kwargs]
|
235
|
+
end)
|
236
|
+
assert_equal([42, 'b'], PlayArgs.rest(42, 'b'))
|
237
|
+
assert_equal({ a: 'a', b: nil }, PlayArgs.key_rest(a: 'a', b: nil))
|
238
|
+
assert_equal([[12, 'd'], { a: nil, b: 9 }],
|
239
|
+
PlayArgs.both_rest(12, 'd', a: nil, b: 9))
|
240
|
+
end
|
241
|
+
|
242
|
+
assert_equal_output(strip_colour(expected_output), colour: false, &subject)
|
243
|
+
assert_equal_output(expected_output, colour: true, &subject)
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_trace_process_label_thread
|
247
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
248
|
+
MOD.ONE(@"a"@)
|
249
|
+
MOD.ONE RETURN @"a and a one"@
|
250
|
+
#{ORANGE}[:THREAD_ID]#{CLEAR}MOD.ONE(@2@)
|
251
|
+
#{ORANGE}[:THREAD_ID]#{CLEAR}MOD.ONE RETURN @"2 and a one"@
|
252
|
+
OUTPUT
|
253
|
+
expected_output.gsub!('MOD', "#{B_GREEN}PlayModule#{CLEAR}")
|
254
|
+
.gsub!('ONE', "#{TEAL}one#{CLEAR}")
|
255
|
+
.gsub!('RETURN', "#{YELLOW}return#{CLEAR}")
|
256
|
+
.gsub!(/@([^@]*)@/, "#{PURPLE}\\1#{CLEAR}")
|
257
|
+
|
258
|
+
[false, true].each do |colour|
|
259
|
+
output = Output.new
|
260
|
+
tracer = TraceWrapper.new(output: output, colour: colour)
|
261
|
+
thread = nil
|
262
|
+
tracer.wrap(PlayModule) do
|
263
|
+
assert_equal('a and a one', PlayModule.one('a'))
|
264
|
+
thread = Thread.new { PlayModule.one(2) }
|
265
|
+
thread.join
|
266
|
+
end
|
267
|
+
exp = expected_output.gsub('THREAD_ID', thread.hash.to_s[-4..-1])
|
268
|
+
exp = strip_colour(exp) unless colour
|
269
|
+
assert_equal(exp, output.output.join)
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_trace_process_label_fork
|
274
|
+
skip 'Process.fork unavailable' unless Process.respond_to?(:fork)
|
275
|
+
|
276
|
+
expected_output = <<-OUTPUT.gsub(/^ {4}/, '')
|
277
|
+
#{ORANGE}[PROCESS_ID]#{CLEAR}MOD.ONE(@3@)
|
278
|
+
#{ORANGE}[PROCESS_ID]#{CLEAR}MOD.ONE RETURN @"3 and a one"@
|
279
|
+
#{BLUE}[PROCESS_ID:THREAD_ID]#{CLEAR}MOD.ONE(@4@)
|
280
|
+
#{BLUE}[PROCESS_ID:THREAD_ID]#{CLEAR}MOD.ONE RETURN @"4 and a one"@
|
281
|
+
OUTPUT
|
282
|
+
expected_output.gsub!('MOD', "#{B_GREEN}PlayModule#{CLEAR}")
|
283
|
+
.gsub!('ONE', "#{TEAL}one#{CLEAR}")
|
284
|
+
.gsub!('RETURN', "#{YELLOW}return#{CLEAR}")
|
285
|
+
.gsub!(/@([^@]*)@/, "#{PURPLE}\\1#{CLEAR}")
|
286
|
+
|
287
|
+
[false, true].each do |colour|
|
288
|
+
pipe = Pipe.new
|
289
|
+
thread_pipe = Pipe.new
|
290
|
+
|
291
|
+
tracer = TraceWrapper.new(output: pipe, colour: colour)
|
292
|
+
pid = nil
|
293
|
+
tracer.wrap(PlayModule) do
|
294
|
+
pid = Process.fork do
|
295
|
+
PlayModule.one(3)
|
296
|
+
thread = Thread.new { PlayModule.one(4) }
|
297
|
+
thread.join
|
298
|
+
thread_pipe.write(thread.hash.to_s[-4..-1])
|
299
|
+
|
300
|
+
pipe.close
|
301
|
+
thread_pipe.close
|
302
|
+
end
|
303
|
+
pipe.close
|
304
|
+
thread_pipe.close
|
305
|
+
Process.wait(pid)
|
306
|
+
end
|
307
|
+
exp = expected_output.gsub('PROCESS_ID', pid.to_s)
|
308
|
+
.gsub('THREAD_ID', thread_pipe.read)
|
309
|
+
exp = strip_colour(exp) unless colour
|
310
|
+
assert_equal(exp, pipe.read)
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'English'
|
2
|
+
|
3
|
+
require File.expand_path('lib/trace_wrapper/version', __dir__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'trace_wrapper'
|
7
|
+
s.version = TraceWrapper::VERSION
|
8
|
+
s.license = 'Hippocratic-2.1'
|
9
|
+
s.summary = 'TraceWrapper outputs method call and returns for a class'
|
10
|
+
s.description = 'Wrap the methods of a class or module to output call info.
|
11
|
+
|
12
|
+
See a tree of calls made to wrapped methods with argument values and return
|
13
|
+
values.'
|
14
|
+
s.authors = ['Bill Ruddock']
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
+
s.test_files = s.files.grep(%r{test/})
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.homepage = 'https://github.com/biinari/trace_wrapper'
|
21
|
+
s.metadata = {
|
22
|
+
'source_code_uri' => 'https://github.com/biinari/trace_wrapper'
|
23
|
+
}
|
24
|
+
s.rdoc_options << '--main' << 'README.md'
|
25
|
+
|
26
|
+
s.add_development_dependency 'minitest', '~> 5.14'
|
27
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
28
|
+
s.add_development_dependency 'rubocop', '~> 0.80.1'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trace_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bill Ruddock
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.80.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.80.1
|
55
|
+
description: |-
|
56
|
+
Wrap the methods of a class or module to output call info.
|
57
|
+
|
58
|
+
See a tree of calls made to wrapped methods with argument values and return
|
59
|
+
values.
|
60
|
+
email:
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".rubocop.yml"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.md
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- examples/basic.rb
|
73
|
+
- examples/custom.rb
|
74
|
+
- lib/trace_wrapper.rb
|
75
|
+
- lib/trace_wrapper/version.rb
|
76
|
+
- test/playground.rb
|
77
|
+
- test/test_trace_wrapper.rb
|
78
|
+
- trace_wrapper.gemspec
|
79
|
+
homepage: https://github.com/biinari/trace_wrapper
|
80
|
+
licenses:
|
81
|
+
- Hippocratic-2.1
|
82
|
+
metadata:
|
83
|
+
source_code_uri: https://github.com/biinari/trace_wrapper
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options:
|
86
|
+
- "--main"
|
87
|
+
- README.md
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubygems_version: 3.1.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: TraceWrapper outputs method call and returns for a class
|
105
|
+
test_files:
|
106
|
+
- test/playground.rb
|
107
|
+
- test/test_trace_wrapper.rb
|