From c10da72e1521e5ccebdbfa4020ab02e4a209642d Mon Sep 17 00:00:00 2001 From: Dan Mayer Date: Sat, 9 Feb 2019 11:37:24 -0700 Subject: [PATCH] improve tests and docs for filename_from_key --- lib/coverband/reporters/base.rb | 21 +++++++++++++++++++-- test/unit/reports_base_test.rb | 25 +++++++++++++------------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/coverband/reporters/base.rb b/lib/coverband/reporters/base.rb index 3e043425..e5f7d843 100644 --- a/lib/coverband/reporters/base.rb +++ b/lib/coverband/reporters/base.rb @@ -61,14 +61,31 @@ def merge_arrays(first, second) merged end + ### + # filename_from_key code takes: + # key: which is a full path the same as reported by Coverage + # roots: if a collection of all possible full app paths + # EX: [Coverband.configuration.root_paths, "#{current_root}/"] + # The LAST item should be the current file system root + # it expands that expands and adds a '/' as that isn't there from Dir.pwd + # + # NOTEs on configuration.root_paths usage + # strings: matching is pretty simple for full string paths + # regex: to get regex to work for changing deploy directories + # the regex must be double escaped in double quotes + # (if using \d for example) + # or use single qoutes + # example: '/box/apps/app_name/releases/\d+/' + # example: '/var/local/company/company.d/[0-9]*/' + ### def filename_from_key(key, roots) filename = key roots.each do |root| filename = filename.gsub(/^#{root}/, './') end - # the filename for SimpleCov is expected to be a full path. + # The filename for Coverage report is expected to be a full LOCAL path. # roots.last should be roots << current_root}/ - # a fully expanded path of config.root + # a fully expanded path of the file on the current runtime system filename = filename.gsub('./', roots.last) filename end diff --git a/test/unit/reports_base_test.rb b/test/unit/reports_base_test.rb index 92f4bc8f..b58bc1aa 100644 --- a/test/unit/reports_base_test.rb +++ b/test/unit/reports_base_test.rb @@ -17,33 +17,34 @@ class ReportsBaseTest < Minitest::Test assert_equal expected_path, Coverband::Reporters::Base.send(:filename_from_key, key, roots) end - test 'filename_from_key fix filename a changing deploy path with double quotes' do + test 'filename_from_key fix filename a changing deploy path with quotes' do Coverband.configure do |config| config.reporter = 'std_out' config.root = '/full/remote_app/path' end + expected_path = '/full/remote_app/path/app/models/user.rb' key = '/box/apps/app_name/releases/20140725203539/app/models/user.rb' - # the code takes config.root expands and adds a '/' for the final path in roots - # note to get regex to work for changing deploy directories it must be double escaped in double quotes or use single qoutes - roots = ['/box/apps/app_name/releases/\\d+/', '/full/remote_app/path/'] + roots = ["/box/apps/app_name/releases/\\d+/", '/full/remote_app/path/'] + assert_equal expected_path, Coverband::Reporters::Base.send(:filename_from_key, key, roots) - expected_path = '/full/remote_app/path/app/models/user.rb' + roots = ['/box/apps/app_name/releases/\d+/', '/full/remote_app/path/'] assert_equal expected_path, Coverband::Reporters::Base.send(:filename_from_key, key, roots) end - test 'filename_from_key fix filename a changing deploy path with single quotes' do + test 'filename_from_key fix filename a changing deploy path real world examples' do + current_app_root = '/var/local/company/company.d/79' Coverband.configure do |config| config.reporter = 'std_out' - config.root = '/full/remote_app/path' + config.root = current_app_root end - key = '/box/apps/app_name/releases/20140725203539/app/models/user.rb' - # the code takes config.root expands and adds a '/' for the final path in roots - # note to get regex to work for changing deploy directories it must be double escaped in double quotes or use single qoutes - roots = ['/box/apps/app_name/releases/\d+/', '/full/remote_app/path/'] + expected_path = '/var/local/company/company.d/79/app/controllers/dashboard_controller.rb' + key = '/var/local/company/company.d/78/app/controllers/dashboard_controller.rb' - expected_path = '/full/remote_app/path/app/models/user.rb' + roots = ['/var/local/company/company.d/[0-9]*/', "#{current_app_root}/"] + assert_equal expected_path, Coverband::Reporters::Base.send(:filename_from_key, key, roots) + roots = ["/var/local/company/company.d/[0-9]*/", "#{current_app_root}/"] assert_equal expected_path, Coverband::Reporters::Base.send(:filename_from_key, key, roots) end