diff --git a/README.md b/README.md index 9e2603d..5b3c593 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/rspec/github/notification_decorator.rb b/lib/rspec/github/notification_decorator.rb index f260308..7914e5f 100644 --- a/lib/rspec/github/notification_decorator.rb +++ b/lib/rspec/github/notification_decorator.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'erb' + module RSpec module Github class NotificationDecorator @@ -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 @@ -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 diff --git a/spec/rspec/github/formatter_spec.rb b/spec/rspec/github/formatter_spec.rb index 808e76b..d09db5a 100644 --- a/spec/rspec/github/formatter_spec.rb +++ b/spec/rspec/github/formatter_spec.rb @@ -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 @@ -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)