Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/appmap/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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(File.join(APPMAP_OUTPUT_DIR, scenario_filename), appmap)
AppMap::Util.write_appmap(APPMAP_OUTPUT_DIR, scenario_filename, appmap, source_location: scenario.location.to_s)
end

def enabled?
Expand Down Expand Up @@ -85,6 +85,7 @@ def update_metadata(scenario, base_metadata)
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'] ||= []
Expand Down
2 changes: 1 addition & 1 deletion lib/appmap/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def save(name:, class_map:, source_location:, test_status:, test_failure:, excep
}.compact
fname = AppMap::Util.scenario_filename(name)

AppMap::Util.write_appmap(File.join(APPMAP_OUTPUT_DIR, fname), appmap)
AppMap::Util.write_appmap(APPMAP_OUTPUT_DIR, fname, appmap, source_location: source_location)
end

def enabled?
Expand Down
2 changes: 1 addition & 1 deletion lib/appmap/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
'classMap' => AppMap.class_map(tracer.event_methods),
'events' => events
}
AppMap::Util.write_appmap('appmap.json', appmap)
AppMap::Util.write_appmap('.', 'appmap.json', appmap)
end
2 changes: 1 addition & 1 deletion lib/appmap/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def save(name:, class_map:, source_location:, test_status:, test_failure:, excep
}.compact
fname = AppMap::Util.scenario_filename(name)

AppMap::Util.write_appmap(File.join(APPMAP_OUTPUT_DIR, fname), appmap)
AppMap::Util.write_appmap(APPMAP_OUTPUT_DIR, fname, appmap, source_location: source_location)
end

def enabled?
Expand Down
18 changes: 16 additions & 2 deletions lib/appmap/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,32 @@ def swaggerize_path(path)
end.join('/')
end

def mirror_test_folders?
ENV['APPMAP_MIRROR_TEST_FOLDERS'] != 'false'
end

# Atomically writes AppMap data to +filename+.
def write_appmap(filename, appmap)
def write_appmap(output_dir, filename, appmap, source_location: nil)
require 'tmpdir'

path_tokens = [ output_dir ]
if source_location && mirror_test_folders?
source_path, _ = source_location.split(':', 2)[0]
source_dir = Array(Pathname.new(source_path).dirname.each_filename)[1..-1].join('/')
path_tokens.push(source_dir) unless source_dir == ''
end
path_tokens.push(filename)
appmap_path = File.join(*path_tokens)

# This is what Ruby Tempfile does; but we don't want the file to be unlinked.
mode = File::RDWR | File::CREAT | File::EXCL
::Dir::Tmpname.create([ 'appmap_', '.json' ]) do |tmpname|
tempfile = File.open(tmpname, mode)
tempfile.write(JSON.generate(appmap))
tempfile.close
# Atomically move the tempfile into place.
FileUtils.mv tempfile.path, filename
FileUtils.mkdir_p Pathname.new(appmap_path).dirname
FileUtils.mv tempfile.path, appmap_path
end
end

Expand Down
14 changes: 7 additions & 7 deletions spec/rails_recording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
include_context 'rails integration test setup'

describe 'rspec metadata' do
let(:appmap_json_files) { Dir.glob("#{tmpdir}/appmap/rspec/*.appmap.json") }
let(:appmap_json_files) { Dir.glob("#{tmpdir}/appmap/rspec/**/*.appmap.json") }

it 'appmap: false disables recording' do
test_names = appmap_json_files.map(&File.method(:read)).map(&JSON.method(:parse)).map do |json|
Expand All @@ -20,7 +20,7 @@
describe 'an API route' do
describe 'creating an object' do
let(:appmap_json_file) do
'Api_UsersController_POST_api_users_with_required_parameters_creates_a_user.appmap.json'
'controllers/Api_UsersController_POST_api_users_with_required_parameters_creates_a_user.appmap.json'
end

it 'http_server_request is recorded in the appmap' do
Expand Down Expand Up @@ -104,7 +104,7 @@
end

context 'with an object-style message' do
let(:appmap_json_file) { 'Api_UsersController_POST_api_users_with_required_parameters_with_object-style_parameters_creates_a_user.appmap.json' }
let(:appmap_json_file) { 'controllers/Api_UsersController_POST_api_users_with_required_parameters_with_object-style_parameters_creates_a_user.appmap.json' }

it 'message properties are recorded in the appmap' do
expect(events).to include(
Expand All @@ -126,7 +126,7 @@

describe 'listing objects' do
context 'with a custom header' do
let(:appmap_json_file) { 'Api_UsersController_GET_api_users_with_a_custom_header_lists_the_users.appmap.json' }
let(:appmap_json_file) { 'controllers/Api_UsersController_GET_api_users_with_a_custom_header_lists_the_users.appmap.json' }

it 'custom header is recorded in the appmap' do
expect(events).to include(
Expand All @@ -144,7 +144,7 @@
describe 'a UI route' do
describe 'rendering a page using a template file' do
let(:appmap_json_file) do
'UsersController_GET_users_lists_the_users.appmap.json'
'controllers/UsersController_GET_users_lists_the_users.appmap.json'
end

it 'records the template file' do
Expand Down Expand Up @@ -186,7 +186,7 @@

describe 'rendering a page using a text template' do
let(:appmap_json_file) do
'UsersController_GET_users_login_shows_the_user.appmap.json'
'controllers/UsersController_GET_users_login_shows_the_user.appmap.json'
end

it 'records the normalized path info' do
Expand Down Expand Up @@ -257,7 +257,7 @@
include_context 'rails integration test setup'

let(:appmap_json_file) do
'Api_UsersController_POST_api_users_with_required_parameters_creates_a_user.appmap.json'
'controllers/Api_UsersController_POST_api_users_with_required_parameters_creates_a_user.appmap.json'
end

it 'http_server_request is recorded' do
Expand Down
2 changes: 1 addition & 1 deletion spec/record_sql_rails_pg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run_specs(orm_module)
'RAILS_ENV' => 'test'
end

let(:appmap_json) { File.join tmpdir, "appmap/rspec/#{test_case}.appmap.json" }
let(:appmap_json) { File.join tmpdir, "appmap/rspec/controllers/#{test_case}.appmap.json" }
let(:appmap) { JSON.parse(File.read(appmap_json)) }
let(:tmpdir) { app.tmpdir }
let(:sql_events) { appmap['events'].select { |ev| ev.include? 'sql_query' } }
Expand Down