appmap 0.27.0 → 0.28.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -3
  3. data/CHANGELOG.md +9 -0
  4. data/README.md +120 -31
  5. data/exe/appmap +3 -1
  6. data/lib/appmap.rb +40 -35
  7. data/lib/appmap/command/record.rb +2 -61
  8. data/lib/appmap/cucumber.rb +89 -0
  9. data/lib/appmap/hook.rb +6 -1
  10. data/lib/appmap/metadata.rb +62 -0
  11. data/lib/appmap/middleware/remote_recording.rb +2 -6
  12. data/lib/appmap/railtie.rb +2 -2
  13. data/lib/appmap/rspec.rb +8 -36
  14. data/lib/appmap/trace.rb +8 -8
  15. data/lib/appmap/util.rb +40 -0
  16. data/lib/appmap/version.rb +1 -1
  17. data/spec/fixtures/rails_users_app/Gemfile +1 -0
  18. data/spec/fixtures/rails_users_app/features/api_users.feature +13 -0
  19. data/spec/fixtures/rails_users_app/features/support/env.rb +4 -0
  20. data/spec/fixtures/rails_users_app/features/support/hooks.rb +11 -0
  21. data/spec/fixtures/rails_users_app/features/support/steps.rb +18 -0
  22. data/spec/hook_spec.rb +8 -2
  23. data/spec/rails_spec_helper.rb +2 -0
  24. data/spec/rspec_feature_metadata_spec.rb +2 -0
  25. data/spec/spec_helper.rb +4 -0
  26. data/spec/util_spec.rb +21 -0
  27. data/test/cucumber_test.rb +72 -0
  28. data/test/fixtures/cucumber4_recorder/Gemfile +5 -0
  29. data/test/fixtures/cucumber4_recorder/appmap.yml +3 -0
  30. data/test/fixtures/cucumber4_recorder/features/say_hello.feature +5 -0
  31. data/test/fixtures/cucumber4_recorder/features/support/env.rb +5 -0
  32. data/test/fixtures/cucumber4_recorder/features/support/hooks.rb +11 -0
  33. data/test/fixtures/cucumber4_recorder/features/support/steps.rb +9 -0
  34. data/test/fixtures/cucumber4_recorder/lib/hello.rb +7 -0
  35. data/test/fixtures/cucumber_recorder/Gemfile +5 -0
  36. data/test/fixtures/cucumber_recorder/appmap.yml +3 -0
  37. data/test/fixtures/cucumber_recorder/features/say_hello.feature +5 -0
  38. data/test/fixtures/cucumber_recorder/features/support/env.rb +5 -0
  39. data/test/fixtures/cucumber_recorder/features/support/hooks.rb +11 -0
  40. data/test/fixtures/cucumber_recorder/features/support/steps.rb +9 -0
  41. data/test/fixtures/cucumber_recorder/lib/hello.rb +7 -0
  42. metadata +26 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd23faab460498cfac79b397c4fc87920df8d1a984142909542f524057d1e4cb
4
- data.tar.gz: a967224c65d6a479287768389feb73a378e6e1be133f1033d06be6034a43b18e
3
+ metadata.gz: d94a20303fe329af9dfa6f4a0d4b14666a7eb25cba58d0ea6ede64b5991e85f9
4
+ data.tar.gz: c441d6ee26453338fa449fed7762a0768155eaa934168c2245c47a5deb78cb38
5
5
  SHA512:
6
- metadata.gz: 7822e4c821cb5e57579163fdd080bc3b74fb5ad0c8999bc6e3d29ace636301d8ed931966abfe9a59a6a59603f83af03e82130c241293ee198173ef6f6f41ebd1
7
- data.tar.gz: 8ba03667929d4571e13439ab53420934e0b1c9ee4605dfd71e9a7ff82b984d34e37ca43e206cbf5dc750201df93dc8dc9babbdafbadf12a86051e52efc6ef985
6
+ metadata.gz: a66524169eeb5e8d78974a8ac37122f52d5e2285cb4ff6f88f58947e8016d2b4ac458430df07a5b4e9c5ab37fdc7122bb887c694e375a7013bc8f34bf1b6b6b2
7
+ data.tar.gz: 665354eca628d2d0f68706cf297667b9b3392a43e6dc364923f1f45f5316a6d3db54dea2d1e75ae83cd53c3ed9cb40ecc3794c10c9d6ee1c310aeed6710bb931
data/.gitignore CHANGED
@@ -1,7 +1,6 @@
1
1
  .DS_Store
2
2
  /html/
3
- /.appmap/
4
- /.bundle/
3
+ .bundle
5
4
  /.yardoc
6
5
  /_yardoc/
7
6
  /coverage/
@@ -16,4 +15,3 @@ appmap.json
16
15
  .vscode
17
16
  .byebug_history
18
17
 
19
- /test/fixtures/rspec_recorder/.bundle/
@@ -1,3 +1,12 @@
1
+ # v0.28.0
2
+
3
+ * Change behavior of **AppMap.record** to return a complete AppMap as a Hash.
4
+ * Update README with information about recording Cucumber tests.
5
+ * **AppMap.initialize** automatically runs when `AppMap` is required, unless disabled
6
+ by environment variable `APPMAP_INITIALIZE=false`.
7
+ * **AppMap.hook** no longer takes a `configuration` argument.
8
+ * Add **AppMap::Util.scenario_filename**.
9
+
1
10
  # v0.27.0
2
11
 
3
12
  * Add **AppMap.record** to programatically record and capture an AppMap of a Ruby block.
data/README.md CHANGED
@@ -1,18 +1,23 @@
1
1
  - [About](#about)
2
- - [Testing](#testing)
3
2
  - [Installation](#installation)
4
3
  - [Configuration](#configuration)
5
4
  - [Running](#running)
6
5
  - [RSpec](#rspec)
6
+ - [Cucumber](#cucumber)
7
7
  - [Remote recording](#remote-recording)
8
8
  - [Ruby on Rails](#ruby-on-rails)
9
9
  - [Uploading AppMaps](#uploading-appmaps)
10
+ - [Development](#development)
11
+ - [Running tests](#running-tests)
12
+ - [Using fixture apps](#using-fixture-apps)
13
+ - [`test/fixtures`](#testfixtures)
14
+ - [`spec/fixtures`](#specfixtures)
10
15
  - [Build status](#build-status)
11
16
 
12
17
  # About
13
18
 
14
19
  `appmap-ruby` is a Ruby Gem for recording
15
- [AppMaps](https://github.com/applandinc/appmap) of your code.
20
+ [AppMaps](https://github.com/applandinc/appmap) of your code.
16
21
  "AppMap" is a data format which records code structure (modules, classes, and methods), code execution events
17
22
  (function calls and returns), and code metadata (repo name, repo URL, commit
18
23
  SHA, labels, etc). It's more granular than a performance profile, but it's less
@@ -22,7 +27,7 @@ There are several ways to record AppMaps of your Ruby program using the `appmap`
22
27
 
23
28
  * Run your RSpec tests with the environment variable `APPMAP=true`. An AppMap will be generated for each spec.
24
29
  * Run your application server with AppMap remote recording enabled, and use the AppMap.
25
- browser extension to start, stop, and upload recordings.
30
+ browser extension to start, stop, and upload recordings.
26
31
  * Run the command `appmap record <program>` to record the entire execution of a program.
27
32
 
28
33
  Once you have recorded some AppMaps (for example, by running RSpec tests), you use the `appland upload` command
@@ -31,17 +36,6 @@ by the [AppLand CLI](https://github.com/applandinc/appland-cli/releases), to
31
36
  Then, on the [AppLand website](https://app.land), you can
32
37
  visualize the design of your code and share links with collaborators.
33
38
 
34
- # Testing
35
- Before running tests, configure `local.appmap` to point to your local `appmap-ruby` directory.
36
- ```
37
- $ bundle config local.appmap $(pwd)
38
- ```
39
-
40
- Run the tests via `rake`:
41
- ```
42
- $ bundle exec rake test
43
- ```
44
-
45
39
  # Installation
46
40
 
47
41
  Add `gem 'appmap'` to your Gemfile just as you would any other dependency.
@@ -60,7 +54,7 @@ group :development, :test do
60
54
  end
61
55
  ```
62
56
 
63
- Then install with `bundle`.
57
+ Then install with `bundle`.
64
58
 
65
59
  # Configuration
66
60
 
@@ -90,7 +84,7 @@ Each entry in the `packages` list is a YAML object which has the following keys:
90
84
 
91
85
  ## RSpec
92
86
 
93
- To instrument RSpec tests, follow these additional steps:
87
+ To record RSpec tests, follow these additional steps:
94
88
 
95
89
  1) Require `appmap/rspec` in your `spec_helper.rb`.
96
90
 
@@ -98,28 +92,16 @@ To instrument RSpec tests, follow these additional steps:
98
92
  require 'appmap/rspec'
99
93
  ```
100
94
 
101
- 2) Add `appmap: true` to the tests you want to instrument.
102
-
103
- ```ruby
104
- describe Hello, appmap: true do
105
- describe 'says hello' do
106
- it 'when prompted' do
107
- expect(Hello.new.say_hello).to eq('Hello!')
108
- end
109
- end
110
- end
111
- ```
112
-
113
- 3) *Optional* Add `feature: '<feature name>'` and `feature_group: '<feature group name>'` annotations to your
95
+ 2) *Optional* Add `feature: '<feature name>'` and `feature_group: '<feature group name>'` annotations to your
114
96
  examples.
115
97
 
116
- 4) Run the tests with the environment variable `APPMAP=true`:
98
+ 3) Run the tests with the environment variable `APPMAP=true`:
117
99
 
118
100
  ```sh-session
119
101
  $ APPMAP=true bundle exec rspec -t appmap
120
102
  ```
121
103
 
122
- Each RSpec test will output a data file into the directory `tmp/appmap/rspec`. For example:
104
+ Each RSpec test will output an AppMap file into the directory `tmp/appmap/rspec`. For example:
123
105
 
124
106
  ```
125
107
  $ find tmp/appmap/rspec
@@ -143,6 +125,44 @@ If you include the `feature` and `feature_group` metadata, these attributes will
143
125
 
144
126
  If you don't explicitly declare `feature` and `feature_group`, then they will be inferred from the spec name and example descriptions.
145
127
 
128
+ ## Cucumber
129
+
130
+ To record Cucumber tests, follow these additional steps:
131
+
132
+ 1) Require `appmap/cucumber` in `support/env.rb`:
133
+
134
+ ```ruby
135
+ require 'appmap/cucumber'
136
+ ```
137
+
138
+ 2) Create an `Around` hook in `support/hooks.rb` to record the scenario:
139
+
140
+
141
+ ```ruby
142
+ if AppMap::Cucumber.enabled?
143
+ Around('not @appmap-disable') do |scenario, block|
144
+ appmap = AppMap.record do
145
+ block.call
146
+ end
147
+
148
+ AppMap::Cucumber.write_scenario(scenario, appmap)
149
+ end
150
+ end
151
+ ```
152
+
153
+ 3) Run the tests with the environment variable `APPMAP=true`:
154
+
155
+ ```sh-session
156
+ $ APPMAP=true bundle exec cucumber
157
+ ```
158
+
159
+ Each Cucumber test will output an AppMap file into the directory `tmp/appmap/cucumber`. For example:
160
+
161
+ ```
162
+ $ find tmp/appmap/cucumber
163
+ Hello_Says_hello_when_prompted.appmap.json
164
+ ```
165
+
146
166
  ## Remote recording
147
167
 
148
168
  To manually record ad-hoc AppMaps of your Ruby app, use AppMap remote recording.
@@ -183,5 +203,74 @@ Note that using this method is kind of a blunt instrument. Recording RSpecs and
183
203
 
184
204
  For instructions on uploading, see the documentation of the [AppLand CLI](https://github.com/applandinc/appland-cli).
185
205
 
206
+ # Development
207
+
208
+ ## Running tests
209
+
210
+ Before running tests, configure `local.appmap` to point to your local `appmap-ruby` directory.
211
+ ```
212
+ $ bundle config local.appmap $(pwd)
213
+ ```
214
+
215
+ Run the tests via `rake`:
216
+ ```
217
+ $ bundle exec rake test
218
+ ```
219
+
220
+ ## Using fixture apps
221
+
222
+ ### `test/fixtures`
223
+
224
+ The fixture apps in `test/fixtures` are plain Ruby projects that exercise the basic functionality of the
225
+ `appmap` gem. To develop in a fixture, simple enter the fixture directory and `bundle`.
226
+
227
+ ### `spec/fixtures`
228
+
229
+ The fixture apps in `spec/fixtures` are simple Rack, Rails4, and Rails5 apps.
230
+ You can use them to interactively develop and test the recording features of the `appmap` gem.
231
+ These fixture apps are more sophisticated than `test/fixtures`, because they include additional
232
+ resources such as a PostgreSQL database.
233
+
234
+ To build the fixture container images, first run:
235
+
236
+ ```sh-session
237
+ $ bundle exec rake fixtures:all
238
+ ```
239
+
240
+ This will build the `appmap.gem`, along with a Docker image for each fixture app.
241
+
242
+ Then move to the directory of the fixture you want to use, and provision the environment.
243
+ In this example, we use Ruby 2.6.
244
+
245
+ ```sh-session
246
+ $ export RUBY_VERSION=2.6
247
+ $ docker-compose up -d pg
248
+ $ sleep 10s # Or some reasonable amount of time
249
+ $ docker-compose run --rm app ./create_app
250
+ ```
251
+
252
+ Now you can start a development container.
253
+
254
+ ```sh-session
255
+ $ docker-compose run --rm -v $PWD/../../..:/src/appmap-ruby app bash
256
+ Starting rails_users_app_pg_1 ... done
257
+ root@6fab5f89125f:/app# cd /src/app
258
+ root@6fab5f89125f:/src/app# bundle config local.appmap /src/appmap-ruby
259
+ root@6fab5f89125f:/src/app# bundle update appmap
260
+ ```
261
+
262
+ At this point, the bundle is built with the `appmap` gem located in `/src/appmap`, which is volume-mounted from the host.
263
+ So you can edit the fixture code and the appmap code and run test commands such as `rspec` and `cucumber` in the container.
264
+ For example:
265
+
266
+ ```sh-session
267
+ root@6fab5f89125f:/src/app# bundle exec rspec
268
+ Configuring AppMap from path appmap.yml
269
+ ....
270
+
271
+ Finished in 0.07357 seconds (files took 2.1 seconds to load)
272
+ 4 examples, 0 failures
273
+ ```
274
+
186
275
  # Build status
187
276
  [![Build Status](https://travis-ci.org/applandinc/appmap-ruby.svg?branch=master)](https://travis-ci.org/applandinc/appmap-ruby)
data/exe/appmap CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  require 'gli'
5
5
 
6
+ ENV['APPMAP_INITIALIZE'] = 'false'
7
+
6
8
  require 'appmap'
7
9
  require 'appmap/version'
8
10
 
@@ -136,7 +138,7 @@ module AppMap
136
138
  protected
137
139
 
138
140
  def interpret_config_option(fname)
139
- AppMap.configure fname
141
+ AppMap.initialize fname
140
142
  end
141
143
 
142
144
  def interpret_output_file_option(file_name)
@@ -10,53 +10,43 @@ end
10
10
  require 'appmap/version'
11
11
 
12
12
  module AppMap
13
- BATCH_HEADER_NAME = 'AppLand-Scenario-Batch'
14
-
15
13
  class << self
16
- @config = nil
17
- @config_file_path = nil
14
+ @configuration = nil
15
+ @configuration_file_path = nil
18
16
 
19
- # configuration gets the AppMap configuration.
17
+ # configuration gets the configuration. If there is no configuration, the default
18
+ # configuration is initialized.
20
19
  def configuration
21
- raise "AppMap is not configured" unless @config
22
-
23
- @config
20
+ @configuration ||= configure
24
21
  end
25
22
 
26
- # configure applies the configuration from a file. This method can only be performed once.
27
- # Be sure and call it before +hook+ if you want non-default configuration.
28
- #
29
- # Default behavior is to configure from "appmap.yml".
30
- def configure(config_file_path = 'appmap.yml')
31
- if @config
32
- return @config if @config_file_path == config_file_path
23
+ # configuration= sets the configuration. This is only expected to happen once per
24
+ # Ruby process.
25
+ def configuration=(config)
26
+ warn 'AppMap is already configured' if @configuration && config
33
27
 
34
- raise "AppMap is already configured from #{@config_file_path}, can't reconfigure from #{config_file_path}"
35
- end
36
-
37
- warn "Configuring AppMap from path #{config_file_path}"
38
- require 'appmap/hook'
39
- AppMap::Hook::Config.load_from_file(config_file_path).tap do |config|
40
- @config = config
41
- @config_file_path = config_file_path
42
- end
28
+ @configuration = config
43
29
  end
44
30
 
45
- # Activate the code hooks which record function calls as trace events.
31
+ # initialize configures AppMap for recording. Default behavior is to configure from "appmap.yml".
32
+ # This method also activates the code hooks which record function calls as trace events.
46
33
  # Call this function before the program code is loaded by the Ruby VM, otherwise
47
34
  # the load events won't be seen and the hooks won't activate.
48
- def hook(config = configure)
35
+ def initialize(config_file_path = 'appmap.yml')
36
+ warn "Configuring AppMap from path #{config_file_path}"
49
37
  require 'appmap/hook'
50
- AppMap::Hook.hook(config)
38
+ self.configuration = Hook::Config.load_from_file(config_file_path)
39
+ Hook.hook(configuration)
51
40
  end
52
41
 
53
- # Access the AppMap::Tracers, which can be used to start tracing, stop tracing, and record events.
42
+ # tracing can be used to start tracing, stop tracing, and record events.
54
43
  def tracing
55
44
  require 'appmap/trace'
56
- @tracing ||= Trace::Tracers.new
45
+ @tracing ||= Trace::Tracing.new
57
46
  end
58
47
 
59
- # Record a block and return the array of events.
48
+ # record records the events which occur while processing a block,
49
+ # and returns an AppMap as a Hash.
60
50
  def record
61
51
  tracer = tracing.trace
62
52
  begin
@@ -65,17 +55,32 @@ module AppMap
65
55
  tracing.delete(tracer)
66
56
  end
67
57
 
68
- [].tap do |events|
69
- events << tracer.next_event.to_h while tracer.event?
58
+ events = [].tap do |event_list|
59
+ event_list << tracer.next_event.to_h while tracer.event?
70
60
  end
61
+ {
62
+ 'version' => AppMap::APPMAP_FORMAT_VERSION,
63
+ 'metadata' => detect_metadata,
64
+ 'classMap' => class_map(tracer.event_methods),
65
+ 'events' => events
66
+ }
71
67
  end
72
68
 
73
- # Build a class map from a config and a list of Ruby methods.
74
- def class_map(config, methods)
69
+ # class_map builds a class map from a config and a list of Ruby methods.
70
+ def class_map(methods)
75
71
  require 'appmap/class_map'
76
- AppMap::ClassMap.build_from_methods(config, methods)
72
+ ClassMap.build_from_methods(configuration, methods)
73
+ end
74
+
75
+ # detect_metadata returns default metadata detected from the Ruby system and from the
76
+ # filesystem.
77
+ def detect_metadata
78
+ require 'appmap/metadata'
79
+ @metadata ||= Metadata.detect.freeze
80
+ @metadata.deep_dup
77
81
  end
78
82
  end
79
83
  end
80
84
 
81
85
  require 'appmap/railtie' if defined?(::Rails::Railtie)
86
+ AppMap.initialize unless ENV['APPMAP_INITIALIZE'] == 'false'
@@ -5,66 +5,7 @@ module AppMap
5
5
  RecordStruct = Struct.new(:config, :program)
6
6
 
7
7
  class Record < RecordStruct
8
- class << self
9
- # Builds a Hash of metadata which can be detected by inspecting the system.
10
- def detect_metadata
11
- {
12
- language: {
13
- name: 'ruby',
14
- engine: RUBY_ENGINE,
15
- version: RUBY_VERSION
16
- },
17
- client: {
18
- name: 'appmap',
19
- url: AppMap::URL,
20
- version: AppMap::VERSION
21
- }
22
- }.tap do |m|
23
- if defined?(::Rails)
24
- m[:frameworks] ||= []
25
- m[:frameworks] << {
26
- name: 'rails',
27
- version: ::Rails.version
28
- }
29
- end
30
- m[:git] = git_metadata if git_available
31
- end
32
- end
33
-
34
- protected
35
-
36
- def git_available
37
- @git_available = system('git status 2>&1 > /dev/null') if @git_available.nil?
38
- end
39
-
40
- def git_metadata
41
- git_repo = `git config --get remote.origin.url`.strip
42
- git_branch = `git rev-parse --abbrev-ref HEAD`.strip
43
- git_sha = `git rev-parse HEAD`.strip
44
- git_status = `git status -s`.split("\n").map(&:strip)
45
- git_last_annotated_tag = `git describe --abbrev=0 2>/dev/null`.strip
46
- git_last_annotated_tag = nil if git_last_annotated_tag.blank?
47
- git_last_tag = `git describe --abbrev=0 --tags 2>/dev/null`.strip
48
- git_last_tag = nil if git_last_tag.blank?
49
- git_commits_since_last_annotated_tag = `git describe`.strip =~ /-(\d+)-(\w+)$/[1] rescue 0 if git_last_annotated_tag
50
- git_commits_since_last_tag = `git describe --tags`.strip =~ /-(\d+)-(\w+)$/[1] rescue 0 if git_last_tag
51
-
52
- {
53
- repository: git_repo,
54
- branch: git_branch,
55
- commit: git_sha,
56
- status: git_status,
57
- git_last_annotated_tag: git_last_annotated_tag,
58
- git_last_tag: git_last_tag,
59
- git_commits_since_last_annotated_tag: git_commits_since_last_annotated_tag,
60
- git_commits_since_last_tag: git_commits_since_last_tag
61
- }
62
- end
63
- end
64
-
65
8
  def perform(&block)
66
- AppMap::Hook.hook(config)
67
-
68
9
  tracer = AppMap.tracing.trace
69
10
 
70
11
  events = []
@@ -85,8 +26,8 @@ module AppMap
85
26
  quit = true
86
27
  event_thread.join
87
28
  yield AppMap::APPMAP_FORMAT_VERSION,
88
- self.class.detect_metadata,
89
- AppMap.class_map(config, tracer.event_methods),
29
+ AppMap.detect_metadata,
30
+ AppMap.class_map(tracer.event_methods),
90
31
  events
91
32
  end
92
33