Skip to content
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ rspec --format RSpec::Github::Formatter --format progress
rspec --format RSpec::Github::Formatter --format documentation
```

When running in GitHub Actions, each annotation ends with a link to the spec file at the failing line in the tested commit (`GITHUB_SHA`, which for pull requests is the merge commit GitHub tests). The annotation's own link goes to the commit view, which only shows files changed in that commit.

If you want to disable annotations for pending specs you can do that by adding `--tag ~skip` to your command:
```bash
rspec --format RSpec::Github::Formatter --tag ~skip
Expand Down
23 changes: 21 additions & 2 deletions lib/rspec/github/notification_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'erb'

module RSpec
module Github
class NotificationDecorator
Expand All @@ -19,8 +21,20 @@ def line
end

def annotation
"#{example.full_description}\n\n#{message}"
.gsub(Regexp.union(ESCAPE_MAP.keys), ESCAPE_MAP)
[example.full_description, message, file_url].compact.join("\n\n")
.gsub(Regexp.union(ESCAPE_MAP.keys), ESCAPE_MAP)
end

# Link to the spec file at the failing line in the tested commit (GITHUB_SHA, which is the merge
# commit on pull_request events). The annotation itself links to the commit view, which does not
# show files that were not changed in that commit.
def file_url
server_url = ENV.fetch('GITHUB_SERVER_URL', nil)
repository = ENV.fetch('GITHUB_REPOSITORY', nil)
sha = ENV.fetch('GITHUB_SHA', nil)
return if [server_url, repository, sha].any? { |value| value.nil? || value.empty? }

"#{server_url}/#{repository}/blob/#{sha}/#{url_path}#L#{line}"
end

def path
Expand All @@ -46,6 +60,11 @@ def raw_path
example.location.split(':')[0]
end

# Percent-encode each path segment so characters like `#` or spaces don't break the URL.
def url_path
path.split('/').map { |segment| ERB::Util.url_encode(segment) }.join('/')
end

def workspace
File.realpath(ENV.fetch('GITHUB_WORKSPACE', '.'))
end
Expand Down
43 changes: 41 additions & 2 deletions spec/rspec/github/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,23 @@
)
end

let(:github_env) { {} }

around do |example|
keys = %w[GITHUB_SERVER_URL GITHUB_REPOSITORY GITHUB_SHA]
saved = keys.to_h { |key| [key, ENV.fetch(key, nil)] }
keys.each { |key| ENV.delete(key) }
github_env.each { |key, value| ENV[key] = value }
example.run
ensure
saved.each { |key, value| ENV[key] = value }
end

before do
allow(File).to receive(:realpath).and_call_original
allow(File).to receive(:realpath).with('./spec/models/user_spec.rb')
.and_return(File.join(Dir.pwd, 'spec/models/user_spec.rb'))
raw_path = location.split(':').first
allow(File).to receive(:realpath).with(raw_path)
.and_return(File.expand_path(raw_path, Dir.pwd))
end

describe '#example_failed' do
Expand Down Expand Up @@ -61,6 +74,32 @@
MESSAGE
end

context 'when running in GitHub Actions' do
let(:github_env) do
{
'GITHUB_SERVER_URL' => 'https://github.com',
'GITHUB_REPOSITORY' => 'octo/repo',
'GITHUB_SHA' => 'abc123'
}
end

it 'appends a link to the spec file at the failing line' do
is_expected.to eq <<~MESSAGE

::error file=spec/models/user_spec.rb,line=12::#{example.full_description}%0A%0A#{notification.message_lines.join('%0A')}%0A%0Ahttps://github.com/octo/repo/blob/abc123/spec/models/user_spec.rb#L12
MESSAGE
end

context 'when the path contains characters that are not URL safe' do
let(:location) { './spec/models/user#admin spec.rb:12' }

it 'percent-encodes the path in the link' do
# `%` is escaped once more as `%25` by the workflow command; GitHub unescapes it to `%23` / `%20`.
is_expected.to include 'https://github.com/octo/repo/blob/abc123/spec/models/user%2523admin%2520spec.rb#L12'
end
end
end

context 'relative_path to GITHUB_WORKSPACE' do
around do |example|
saved_github_workspace = ENV.fetch('GITHUB_WORKSPACE', nil)
Expand Down
Loading