Skip to content

Commit 45915e6

Browse files
Support for virtual and host paths
1 parent 6591cf9 commit 45915e6

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/better_errors/editor.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,30 @@ def initialize(url_proc)
7070
@url_proc = url_proc
7171
end
7272

73-
def url(file, line)
73+
def url(raw_path, line)
74+
if virtual_path && raw_path.start_with?(virtual_path)
75+
if host_path
76+
file = raw_path.sub(%r{\A#{virtual_path}}, host_path)
77+
else
78+
file = raw_path.sub(%r{\A#{virtual_path}/}, '')
79+
end
80+
else
81+
file = raw_path
82+
end
83+
7484
url_proc.call(file, line)
7585
end
7686

7787
private
7888

7989
attr_reader :url_proc
90+
91+
def virtual_path
92+
@virtual_path ||= ENV['BETTER_ERRORS_VIRTUAL_PATH']
93+
end
94+
95+
def host_path
96+
@host_path ||= ENV['BETTER_ERRORS_HOST_PATH']
97+
end
8098
end
8199
end

spec/better_errors/editor_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,41 @@
230230
end
231231
end
232232
end
233+
234+
describe "#url" do
235+
subject(:url) { described_instance.url("/full/path/to/lib/file.rb", 42) }
236+
let(:described_instance) { described_class.for_formatting_string("%{file_unencoded}")}
237+
before do
238+
ENV['BETTER_ERRORS_VIRTUAL_PATH'] = virtual_path
239+
ENV['BETTER_ERRORS_HOST_PATH'] = host_path
240+
end
241+
let(:virtual_path) { nil }
242+
let(:host_path) { nil }
243+
244+
context "when $BETTER_ERRORS_VIRTUAL_PATH is set" do
245+
let(:virtual_path) { "/full/path/to" }
246+
247+
context "when $BETTER_ERRORS_HOST_PATH is not set" do
248+
let(:host_path) { nil }
249+
250+
it "removes the VIRTUAL_PATH prefix, making the path relative" do
251+
expect(url).to eq("lib/file.rb")
252+
end
253+
end
254+
255+
context "when $BETTER_ERRORS_HOST_PATH is set" do
256+
let(:host_path) { '/Users/myname/Code' }
257+
258+
it "replaces the VIRTUAL_PATH prefix with the HOST_PATH" do
259+
expect(url).to eq("/Users/myname/Code/lib/file.rb")
260+
end
261+
end
262+
end
263+
264+
context "when $BETTER_ERRORS_VIRTUAL_PATH is not set" do
265+
it "does not alter file paths" do
266+
expect(url).to eq("/full/path/to/lib/file.rb")
267+
end
268+
end
269+
end
233270
end

0 commit comments

Comments
 (0)