Class: RuboCop::Cop::Chef::ChefCorrectness::TmpPath

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/chef/correctness/tmp_path.rb

Overview

Use file_cache_path rather than hard-coding tmp paths

Examples:

downloading a large file into /tmp/


# bad
remote_file '/tmp/large-file.tar.gz' do

# good
remote_file "#{Chef::Config[:file_cache_path]}/large-file.tar.gz" do

Constant Summary collapse

MSG =
'Use file_cache_path rather than hard-coding tmp paths'.freeze

Instance Method Summary collapse

Instance Method Details

#file_cache_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/rubocop/cop/chef/correctness/tmp_path.rb', line 52

def file_cache_path?(path)
  path_str = path.to_s.scan(/"(.*)"/)[0][0]
  path_str.start_with?("\#\{Chef::Config[:file_cache_path]\}")
end

#hardcoded_tmp?(path) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/rubocop/cop/chef/correctness/tmp_path.rb', line 47

def hardcoded_tmp?(path)
  path_str = path.to_s.scan(/"(.*)"/)[0][0]
  path_str.start_with?('/tmp/')
end

#on_send(node) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/rubocop/cop/chef/correctness/tmp_path.rb', line 39

def on_send(node)
  remote_file?(node) do |command|
    if hardcoded_tmp?(command) && !file_cache_path?(command)
      add_offense(command, location: :expression, message: MSG, severity: :refactor)
    end
  end
end