Skip to content

Commit

Permalink
Support for virtual and host paths
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDaugherty committed Nov 4, 2020
1 parent 6591cf9 commit 45915e6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/better_errors/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,30 @@ def initialize(url_proc)
@url_proc = url_proc
end

def url(file, line)
def url(raw_path, line)
if virtual_path && raw_path.start_with?(virtual_path)
if host_path
file = raw_path.sub(%r{\A#{virtual_path}}, host_path)
else
file = raw_path.sub(%r{\A#{virtual_path}/}, '')
end
else
file = raw_path
end

url_proc.call(file, line)
end

private

attr_reader :url_proc

def virtual_path
@virtual_path ||= ENV['BETTER_ERRORS_VIRTUAL_PATH']
end

def host_path
@host_path ||= ENV['BETTER_ERRORS_HOST_PATH']
end
end
end
37 changes: 37 additions & 0 deletions spec/better_errors/editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,41 @@
end
end
end

describe "#url" do
subject(:url) { described_instance.url("/full/path/to/lib/file.rb", 42) }
let(:described_instance) { described_class.for_formatting_string("%{file_unencoded}")}
before do
ENV['BETTER_ERRORS_VIRTUAL_PATH'] = virtual_path
ENV['BETTER_ERRORS_HOST_PATH'] = host_path
end
let(:virtual_path) { nil }
let(:host_path) { nil }

context "when $BETTER_ERRORS_VIRTUAL_PATH is set" do
let(:virtual_path) { "/full/path/to" }

context "when $BETTER_ERRORS_HOST_PATH is not set" do
let(:host_path) { nil }

it "removes the VIRTUAL_PATH prefix, making the path relative" do
expect(url).to eq("lib/file.rb")
end
end

context "when $BETTER_ERRORS_HOST_PATH is set" do
let(:host_path) { '/Users/myname/Code' }

it "replaces the VIRTUAL_PATH prefix with the HOST_PATH" do
expect(url).to eq("/Users/myname/Code/lib/file.rb")
end
end
end

context "when $BETTER_ERRORS_VIRTUAL_PATH is not set" do
it "does not alter file paths" do
expect(url).to eq("/full/path/to/lib/file.rb")
end
end
end
end

0 comments on commit 45915e6

Please sign in to comment.