Skip to content

Commit 807255a

Browse files
author
jordanbreen28
committed
(maint) - Opt for use of temp_file
This commit now writes the file_type definitions to a tempfile as oppose to a file within the source code, as this was causing issues with CI. Using a tempfile is also better practice.
1 parent ea9961f commit 807255a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lib/puppet-languageserver-sidecar/puppet_helper.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def self.retrieve_via_puppet_strings(cache, options = {})
108108

109109
file_doc.types.each do |item|
110110
result.append!(item) unless name == 'whit' || name == 'component'
111-
FileUtils.rm_f(finder.temp_file) if item.key == 'file' && finder.temp_file # Remove the temp_file.rb if it exists
111+
finder.temp_file.unlink if item.key == 'file' && File.exist?(finder.temp_file.path) # Remove the temp_file.rb if it exists
112112
end
113113
end
114114

@@ -182,6 +182,7 @@ def initialize(puppet_env, object_types)
182182
# @param from_root_path [String] The path which files can be found within. If nil, only the default Puppet locations are searched e.g. vardir
183183
# @return [Array[String]] A list of all files that are found. This is the absolute path to the file.
184184
def find(from_root_path = nil)
185+
require 'tempfile'
185186
paths = []
186187
search_paths = @module_paths.nil? ? [] : @module_paths
187188
search_paths << @env_path unless @env_path.nil?
@@ -198,7 +199,8 @@ def find(from_root_path = nil)
198199
next unless path_in_root?(from_root_path, search_root) && Dir.exist?(search_root)
199200

200201
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Using '#{search_root}' as a directory to search")
201-
202+
# name of temp file to store the file type definitions (if any)
203+
@temp_file = Tempfile.new('file.rb')
202204
all_object_info.each do |object_type, paths_to_search|
203205
next unless object_types.include?(object_type)
204206

@@ -213,16 +215,11 @@ def find(from_root_path = nil)
213215
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Searching glob '#{glob}''")
214216

215217
Dir.glob(glob) do |filename|
216-
# name of temp file to store the file type definitions (if any)
217-
@temp_file = 'temp_file.rb'
218218
# if filename matches file.rb or /file/<any>.rb then we need to loop through each file type definition
219219
if filename.match?(%r{/type/file/.*.rb|/type/file.rb})
220-
# Create/Open the temp file and write the file type definitions to it
221-
File.open(@temp_file, 'a') do |f|
222-
# Read each file type definition and write it to the temp file
223-
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.")
224-
f.puts(File.read(filename))
225-
end
220+
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.")
221+
# Read each file type definition and write it to the temp file
222+
@temp_file.write(File.read(filename))
226223
else
227224
paths << filename
228225
end
@@ -231,7 +228,10 @@ def find(from_root_path = nil)
231228
end
232229
end
233230
#  Add the temp_file.rb to the paths array for searching (if exists)
234-
paths << @temp_file if @temp_file && File.exist?(@temp_file)
231+
if @temp_file && File.exist?(@temp_file.path)
232+
paths << @temp_file.path
233+
@temp_file.close
234+
end
235235
paths
236236
end
237237

0 commit comments

Comments
 (0)