Class: RuboCop::Cop::Chef::Correctness::TmpPath

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

Overview

Use file_cache_path rather than hard-coding system temp paths

Examples:

downloading a large file into /tmp/


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

### correct
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'
RESTRICT_ON_SEND =
[:remote_file].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#file_cache_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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
# File 'lib/rubocop/cop/chef/correctness/tmp_path.rb', line 39

def on_send(node)
  remote_file?(node) do |command|
    return unless hardcoded_tmp?(command) && !file_cache_path?(command)
    add_offense(command, severity: :refactor)
  end
end