Skip to content

Commit

Permalink
Merge pull request #8842 from dependabot/dev/brettfo/directory-packag…
Browse files Browse the repository at this point in the history
…es-props-not-found

use common helper to look for `Directory.Packages.props`
  • Loading branch information
abdulapopoola authored Jan 23, 2024
2 parents 6a67893 + c30cf0b commit 7cfbbdd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
26 changes: 3 additions & 23 deletions nuget/lib/dependabot/nuget/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def project_files
project_files += fsproj_file
project_files += sln_project_files
project_files += proj_files
project_files += project_files.filter_map { |f| directory_packages_props_file_from_project_file(f) }
project_files += project_files.filter_map do |f|
named_file_up_tree_from_project_file(f, "Directory.Packages.props")
end
project_files
end
rescue Octokit::NotFound, Gitlab::Error::NotFound
Expand Down Expand Up @@ -191,28 +193,6 @@ def proj_files
@proj_files ||= find_and_fetch_with_suffix(".proj")
end

def directory_packages_props_file_from_project_file(project_file)
# walk up the tree from each project file stopping at the first `Directory.Packages.props` file found
# https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management#central-package-management-rules

found_directory_packages_props_file = nil
directory_path = Pathname.new(directory)
full_project_dir = Pathname.new(project_file.directory).join(project_file.name).dirname
full_project_dir.ascend.each do |base|
break if found_directory_packages_props_file

candidate_file_path = Pathname.new(base).join("Directory.Packages.props").cleanpath.to_path
candidate_directory = Pathname.new(File.dirname(candidate_file_path))
relative_candidate_directory = candidate_directory.relative_path_from(directory_path)
candidate_file = repo_contents(dir: relative_candidate_directory).find do |f|
f.name.casecmp?("Directory.Packages.props")
end
found_directory_packages_props_file = fetch_file_from_host(candidate_file.name) if candidate_file
end

found_directory_packages_props_file
end

def find_and_fetch_with_suffix(suffix)
repo_contents.select { |f| f.name.end_with?(suffix) }.map { |f| fetch_file_from_host(f.name) }
end
Expand Down
21 changes: 13 additions & 8 deletions nuget/spec/dependabot/nuget/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
# end
end

context "NuGet.config can be found when starting in a subdirectory" do
context "directory-relative files can be found when starting in a subdirectory" do
let(:directory) { "/src/some-project/" }

before do
Expand All @@ -344,19 +344,24 @@
"bump",
"main"
)
stub_request(:get, File.join(url, "src/some-project/.config?ref=sha"))
.with(headers: { "Authorization" => "token token" })
.to_return(
status: 404,
body: "{}",
headers: { "content-type" => "application/json" }
)

# these files explicitly don't exist
["src/some-project/.config", "src/some-project/Directory.Packages.props"].each do |file|
stub_request(:get, File.join(url, "#{file}?ref=sha"))
.with(headers: { "Authorization" => "token token" })
.to_return(
status: 404,
body: "{}",
headers: { "content-type" => "application/json" }
)
end
end

it "fetches the NuGet.config file from several directories up" do
expect(file_fetcher_instance.files.map(&:name))
.to match_array(
%w(
../../Directory.Packages.props
../../NuGet.Config
some-project.csproj
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>

0 comments on commit 7cfbbdd

Please sign in to comment.