|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require 'tmpdir' |
| 4 | + |
3 | 5 | RSpec.describe RSpec::Github::Formatter do |
4 | 6 | let(:output) { StringIO.new } |
5 | 7 | let(:formatter) { described_class.new(output) } |
|
22 | 24 | ) |
23 | 25 | end |
24 | 26 |
|
| 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 | + |
25 | 80 | describe '#example_failed' do |
26 | 81 | before { formatter.example_failed(notification) } |
27 | 82 |
|
|
43 | 98 | it 'outputs the GitHub annotation formatted error' do |
44 | 99 | is_expected.to eq <<~MESSAGE |
45 | 100 |
|
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')} |
47 | 102 | MESSAGE |
48 | 103 | end |
49 | 104 | end |
|
61 | 116 | it 'outputs the GitHub annotation formatted error' do |
62 | 117 | is_expected.to eq <<~MESSAGE |
63 | 118 |
|
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} |
65 | 120 | MESSAGE |
66 | 121 | end |
67 | 122 | end |
|
0 commit comments