Class: AppMap::RSpec::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/appmap/rspec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



19
20
21
22
23
24
25
26
# File 'lib/appmap/rspec.rb', line 19

def initialize
  @config = AppMap::Config.load_from_file('appmap.yml')

  raise "Missing AppMap configuration setting: 'name'" unless @config.name

  @features = AppMap.inspect(@config)
  @functions = @features.map(&:collect_functions).flatten
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/appmap/rspec.rb', line 17

def config
  @config
end

#featuresObject (readonly)

Returns the value of attribute features.



17
18
19
# File 'lib/appmap/rspec.rb', line 17

def features
  @features
end

#functionsObject (readonly)

Returns the value of attribute functions.



17
18
19
# File 'lib/appmap/rspec.rb', line 17

def functions
  @functions
end

Instance Method Details

#sanitize_filename(fname, separator: '_') ⇒ Object

Cribbed from v5 version of ActiveSupport:Inflector#parameterize: github.com/rails/rails/blob/v5.2.4/activesupport/lib/active_support/inflector/transliterate.rb#L92



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/appmap/rspec.rb', line 62

def sanitize_filename(fname, separator: '_')
  # Replace accented chars with their ASCII equivalents.
  fname = fname.encode('utf-8', invalid: :replace, undef: :replace, replace: '_')

  # Turn unwanted chars into the separator.
  fname.gsub!(/[^a-z0-9\-_]+/i, separator)

  re_sep = Regexp.escape(separator)
  re_duplicate_separator        = /#{re_sep}{2,}/
  re_leading_trailing_separator = /^#{re_sep}|#{re_sep}$/i

  # No more than one of the separator in a row.
  fname.gsub!(re_duplicate_separator, separator)

  # Finally, Remove leading/trailing separator.
  fname.gsub(re_leading_trailing_separator, '')
end

#save(example_name, events: nil, feature_name: nil, feature_group_name: nil, labels: nil) ⇒ Object



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
# File 'lib/appmap/rspec.rb', line 32

def save(example_name, events: nil, feature_name: nil, feature_group_name: nil, labels: nil)
  require 'appmap/command/record'
   = AppMap::Command::Record..tap do |m|
    m[:name] = example_name
    m[:app] = @config.name
    m[:feature] = feature_name if feature_name
    m[:feature_group] = feature_group_name if feature_group_name
    m[:labels] = labels if labels
    m[:frameworks] ||= []
    m[:frameworks] << {
      name: 'rspec',
      version: Gem.loaded_specs['rspec-core']&.version&.to_s
    }
    m[:recorder] = {
      name: 'rspec'
    }
  end

  appmap = {
    version: AppMap::APPMAP_FORMAT_VERSION,
    classMap: features,
    metadata: ,
    events: events
  }.compact
  fname = sanitize_filename(example_name)
  File.write(File.join(APPMAP_OUTPUT_DIR, "#{fname}.appmap.json"), JSON.generate(appmap))
end

#setupObject



28
29
30
# File 'lib/appmap/rspec.rb', line 28

def setup
  FileUtils.mkdir_p APPMAP_OUTPUT_DIR
end