Skip to content

Commit

Permalink
Merge pull request #11054 from dependabot/dev/brettfo/nuget-missing-d…
Browse files Browse the repository at this point in the history
…iscovery

store discovery files under $HOME so they're not cleaned up by the OS
  • Loading branch information
randhircs authored Dec 4, 2024
2 parents a7e5899 + b481428 commit 6d1fb85
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
4 changes: 3 additions & 1 deletion nuget/lib/dependabot/nuget/analysis/analysis_json_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class AnalysisJsonReader

sig { returns(String) }
def self.temp_directory
File.join(NativeDiscoveryJsonReader.temp_directory, "analysis")
d = File.join(Dir.tmpdir, "analysis")
FileUtils.mkdir_p(d)
d
end

sig { params(dependency_name: String).returns(String) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def self.testonly_clear_caches
cache_dependency_file_paths_to_discovery_json_path.clear
end

sig { void }
def self.testonly_clear_discovery_files
# this will get recreated when necessary
FileUtils.rm_rf(discovery_directory)
end

# Runs NuGet dependency discovery in the given directory and returns a new instance of NativeDiscoveryJsonReader.
# The location of the resultant JSON file is saved.
sig do
Expand Down Expand Up @@ -99,7 +105,7 @@ def self.dependency_file_path(repo_contents_path:, dependency_file:)

sig { returns(String) }
def self.discovery_map_file_path
File.join(temp_directory, "discovery_map.json")
File.join(discovery_directory, "discovery_map.json")
end

sig { params(workspace_path: String).returns(String) }
Expand All @@ -124,7 +130,7 @@ def self.discovery_file_path_from_workspace_path(workspace_path)
discovery_json_counter = 1
new_discovery_json_path = ""
loop do
new_discovery_json_path = File.join(temp_directory, "discovery.#{discovery_json_counter}.json")
new_discovery_json_path = File.join(discovery_directory, "discovery.#{discovery_json_counter}.json")
break unless File.exist?(new_discovery_json_path)

discovery_json_counter += 1
Expand All @@ -144,8 +150,8 @@ def self.cache_key_from_dependency_file_paths(dependency_file_paths)
end

sig { returns(String) }
def self.temp_directory
t = File.join(Dir.tmpdir, ".dependabot")
def self.discovery_directory
t = File.join(Dir.home, ".dependabot")
FileUtils.mkdir_p(t)
t
end
Expand All @@ -155,7 +161,7 @@ def self.discovery_json_reader(repo_contents_path:, workspace_path:)
discovery_file_path = discovery_file_path_from_workspace_path(workspace_path)
discovery_json = DependencyFile.new(
name: Pathname.new(discovery_file_path).cleanpath.to_path,
directory: temp_directory,
directory: discovery_directory,
type: "file",
content: File.read(discovery_file_path)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def update_analysis

sig { returns(String) }
def dependency_file_path
File.join(NativeDiscoveryJsonReader.temp_directory, "dependency", "#{dependency.name}.json")
d = File.join(Dir.tmpdir, "dependency")
FileUtils.mkdir_p(d)
File.join(d, "#{dependency.name}.json")
end

sig { returns(T::Array[String]) }
Expand Down
5 changes: 1 addition & 4 deletions nuget/spec/dependabot/nuget/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
it_behaves_like "a dependency file fetcher"

def clean_common_files
# deletes `discovery_map.json` and `discovery.1.json`, etc.
Dir.glob(File.join(Dependabot::Nuget::NativeDiscoveryJsonReader.temp_directory, "discovery*.json")).each do |f|
File.delete(f)
end
Dependabot::Nuget::NativeDiscoveryJsonReader.testonly_clear_discovery_files
end

def clean_repo_files
Expand Down
5 changes: 1 addition & 4 deletions nuget/spec/dependabot/nuget/file_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ def ensure_job_file(&_block)
end

def clean_common_files
# deletes `discovery_map.json` and `discovery.1.json`, etc.
Dir.glob(File.join(Dependabot::Nuget::NativeDiscoveryJsonReader.temp_directory, "discovery*.json")).each do |f|
File.delete(f)
end
Dependabot::Nuget::NativeDiscoveryJsonReader.testonly_clear_discovery_files
end

def run_parser_test(&_block)
Expand Down
5 changes: 1 addition & 4 deletions nuget/spec/dependabot/nuget/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ def ensure_job_file(&_block)
end

def clean_common_files
# deletes `discovery_map.json` and `discovery.1.json`, etc.
Dir.glob(File.join(Dependabot::Nuget::NativeDiscoveryJsonReader.temp_directory, "discovery*.json")).each do |f|
File.delete(f)
end
Dependabot::Nuget::NativeDiscoveryJsonReader.testonly_clear_discovery_files
end

def run_update_test(&_block)
Expand Down
5 changes: 1 addition & 4 deletions nuget/spec/dependabot/nuget/update_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ def ensure_job_file(&_block)
end

def clean_common_files
# deletes `discovery_map.json` and `discovery.1.json`, etc.
Dir.glob(File.join(Dependabot::Nuget::NativeDiscoveryJsonReader.temp_directory, "discovery*.json")).each do |f|
File.delete(f)
end
Dependabot::Nuget::NativeDiscoveryJsonReader.testonly_clear_discovery_files
end

def run_analyze_test(&_block)
Expand Down

0 comments on commit 6d1fb85

Please sign in to comment.