-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcucumber.rb
More file actions
110 lines (89 loc) · 2.88 KB
/
cucumber.rb
File metadata and controls
110 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true
require 'appmap'
require 'appmap/util'
require 'fileutils'
module AppMap
module Cucumber
APPMAP_OUTPUT_DIR = 'tmp/appmap/cucumber'
ScenarioAttributes = Struct.new(:name, :feature, :feature_group)
ProviderStruct = Struct.new(:scenario) do
def feature_group
# e.g. <Cucumber::Core::Ast::Location::Precise: cucumber/api/features/authenticate.feature:1>
feature_path.split('/').last.split('.')[0]
end
end
# ProviderBefore4 provides scenario name, feature name, and feature group name for Cucumber
# versions before 4.0.
class ProviderBefore4 < ProviderStruct
def attributes
ScenarioAttributes.new(scenario.name, scenario.feature.name, feature_group)
end
def feature_path
scenario.feature.location.to_s
end
end
# Provider4 provides scenario name, feature name, and feature group name for Cucumber
# versions 4.0 and later.
class Provider4 < ProviderStruct
def attributes
ScenarioAttributes.new(scenario.name, scenario.name.split(' ')[0..1].join(' '), feature_group)
end
def feature_path
scenario.location.file
end
end
class << self
def init
warn 'Configuring AppMap recorder for Cucumber'
FileUtils.mkdir_p APPMAP_OUTPUT_DIR
end
def write_scenario(scenario, appmap)
appmap['metadata'] = update_metadata(scenario, appmap['metadata'])
scenario_filename = AppMap::Util.scenario_filename(appmap['metadata']['name'])
AppMap::Util.write_appmap(APPMAP_OUTPUT_DIR, scenario_filename, appmap, source_location: scenario.location.to_s)
end
def enabled?
AppMap.recording_enabled?(:cucumber)
end
def run
init
end
protected
def cucumber_version
Gem.loaded_specs['cucumber']&.version&.to_s
end
def provider(scenario)
major, = cucumber_version.split('.').map(&:to_i)
if major < 4
ProviderBefore4
else
Provider4
end.new(scenario)
end
def update_metadata(scenario, base_metadata)
attributes = provider(scenario).attributes
base_metadata.tap do |m|
m['name'] = attributes.name
m['feature'] = attributes.feature
m['feature_group'] = attributes.feature_group
m['source_location'] = scenario.location.to_s
m['labels'] ||= []
m['labels'] += (scenario.tags&.map(&:name) || [])
m['frameworks'] ||= []
m['frameworks'] << {
'name' => 'cucumber',
'version' => Gem.loaded_specs['cucumber']&.version&.to_s
}
m['recorder'] = {
'name' => 'cucumber',
'type' => 'tests'
}
end
end
end
end
end
if AppMap::Cucumber.enabled?
require 'appmap'
AppMap::Cucumber.run
end