Skip to content

Ignore ENOENT when reading file annotations #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2025
Merged
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
15 changes: 8 additions & 7 deletions lib/code_ownership/private/ownership_mappers/file_annotations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,20 @@ def update_cache(cache, files)

sig { params(filename: String).returns(T.nilable(CodeTeams::Team)) }
def file_annotation_based_owner(filename)
# If for a directory is named with an ownable extension, we need to skip
# so File.foreach doesn't blow up below. This was needed because Cypress
# screenshots are saved to a folder with the test suite filename.
return if File.directory?(filename)
return unless File.file?(filename)

# The annotation should be on line 1 but as of this comment
# there's no linter installed to enforce that. We therefore check the
# first line (the Ruby VM makes a single `read(1)` call for 8KB),
# and if the annotation isn't in the first two lines we assume it
# doesn't exist.

line1 = File.foreach(filename).first
begin
line1 = File.foreach(filename).first
rescue Errno::EISDIR, Errno::ENOENT
# Ignore files that fail to read to avoid intermittent bugs.
# Ignoring directories is needed because, e.g., Cypress screenshots
# are saved to a folder with the test suite filename.
return
end

return if !line1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ module CodeOwnership
end

describe '.for_file' do
context 'path is a directory' do
it 'returns nil' do
write_configuration
write_file('config/teams/bar.yml', <<~CONTENTS)
name: Bar
CONTENTS

expect(CodeOwnership.for_file('config/teams')).to be_nil
end
end

context 'path does not exist' do
it 'returns nil' do
write_configuration
write_file('config/teams/bar.yml', <<~CONTENTS)
name: Bar
CONTENTS

expect(CodeOwnership.for_file('config/teams/foo.yml')).to be_nil
end
end

context 'ruby owned file' do
before do
write_configuration
Expand Down