Skip to content

Commit cbfb574

Browse files
Ensure file is relative to GITHUB_WORKSPACE. (#4)
* Ensure `file` is relative to `GITHUB_WORKSPACE`. * Simplify implementation of `relative_path` and add tests. * Run `ci` workflow on `pull_request` event. Co-authored-by: Stef Schenkelaars <stef.schenkelaars@gmail.com>
1 parent c46db74 commit cbfb574

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: CI
2-
on: [push]
2+
on: [pull_request, push]
33
jobs:
44
reviewdog:
55
runs-on: ubuntu-latest

lib/rspec/github/formatter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ module Github
88
class Formatter < RSpec::Core::Formatters::BaseFormatter
99
RSpec::Core::Formatters.register self, :example_failed, :example_pending
1010

11+
def self.relative_path(path)
12+
workspace = File.realpath(ENV.fetch('GITHUB_WORKSPACE', '.'))
13+
File.realpath(path).delete_prefix("#{workspace}#{File::SEPARATOR}")
14+
end
15+
1116
def example_failed(failure)
1217
file, line = failure.example.location.split(':')
18+
file = self.class.relative_path(file)
1319
output.puts "\n::error file=#{file},line=#{line}::#{failure.message_lines.join('%0A')}"
1420
end
1521

1622
def example_pending(pending)
1723
file, line = pending.example.location.split(':')
24+
file = self.class.relative_path(file)
1825
output.puts "\n::warning file=#{file},line=#{line}::#{pending.example.full_description}"
1926
end
2027
end

spec/rspec/github/formatter_spec.rb

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'tmpdir'
4+
35
RSpec.describe RSpec::Github::Formatter do
46
let(:output) { StringIO.new }
57
let(:formatter) { described_class.new(output) }
@@ -22,6 +24,59 @@
2224
)
2325
end
2426

27+
before do
28+
allow(File).to receive(:realpath).and_call_original
29+
allow(File).to receive(:realpath).with('./spec/models/user_spec.rb')
30+
.and_return(File.join(Dir.pwd, 'spec/models/user_spec.rb'))
31+
end
32+
33+
describe '::relative_path' do
34+
around do |example|
35+
saved_github_workspace = ENV['GITHUB_WORKSPACE']
36+
ENV['GITHUB_WORKSPACE'] = github_workspace
37+
38+
FileUtils.mkpath File.dirname(absolute_path)
39+
FileUtils.touch absolute_path
40+
41+
Dir.chdir tmpdir do
42+
example.run
43+
end
44+
ensure
45+
FileUtils.rm_r tmpdir
46+
ENV['GITHUB_WORKSPACE'] = saved_github_workspace
47+
end
48+
49+
let(:tmpdir) { Dir.mktmpdir }
50+
let(:relative_path) { 'this/is/a/relative_path.rb' }
51+
let(:absolute_path) { File.join(tmpdir, relative_path) }
52+
53+
context 'if GITHUB_WORKSPACE is set' do
54+
let(:github_workspace) { tmpdir }
55+
56+
it 'returns the path relative to it when already inside it' do
57+
expect(described_class.relative_path('this/is/a/relative_path.rb')).to eq('this/is/a/relative_path.rb')
58+
end
59+
60+
it 'returns the path relative to it when in a subdirectory of it' do
61+
Dir.chdir 'this/is' do
62+
expect(described_class.relative_path('a/relative_path.rb')).to eq('this/is/a/relative_path.rb')
63+
end
64+
end
65+
end
66+
67+
context 'if GITHUB_WORKSPACE is unset' do
68+
let(:github_workspace) { nil }
69+
70+
it 'returns the unchanged relative path' do
71+
expect(described_class.relative_path('this/is/a/relative_path.rb')).to eq 'this/is/a/relative_path.rb'
72+
end
73+
74+
it 'returns the relative path without a ./ prefix' do
75+
expect(described_class.relative_path('./this/is/a/relative_path.rb')).to eq 'this/is/a/relative_path.rb'
76+
end
77+
end
78+
end
79+
2580
describe '#example_failed' do
2681
before { formatter.example_failed(notification) }
2782

@@ -43,7 +98,7 @@
4398
it 'outputs the GitHub annotation formatted error' do
4499
is_expected.to eq <<~MESSAGE
45100
46-
::error file=./spec/models/user_spec.rb,line=12::#{notification.message_lines.join('%0A')}
101+
::error file=spec/models/user_spec.rb,line=12::#{notification.message_lines.join('%0A')}
47102
MESSAGE
48103
end
49104
end
@@ -61,7 +116,7 @@
61116
it 'outputs the GitHub annotation formatted error' do
62117
is_expected.to eq <<~MESSAGE
63118
64-
::warning file=./spec/models/user_spec.rb,line=12::#{example.full_description}
119+
::warning file=spec/models/user_spec.rb,line=12::#{example.full_description}
65120
MESSAGE
66121
end
67122
end

0 commit comments

Comments
 (0)