Skip to content

Commit

Permalink
(PDK-1523) Use PDK::Util::Filesystem.stat instead of File.stat
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Nov 8, 2019
1 parent add41f4 commit b8a3cf4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,7 @@ PDK/FileRead:
PDK/FileZeroPredicate:
Exclude:
- lib/pdk/util/filesystem.rb

PDK/FileStat:
Exclude:
- lib/pdk/util/filesystem.rb
33 changes: 33 additions & 0 deletions ext/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,39 @@ def autocorrect(node)
end
end
end

class FileStat < Cop
MSG = 'Use PDK::Util::Filesystem.stat instead of File.stat'.freeze

def_node_matcher :file_stat?,
'(send (const nil? :File) :stat ...)'

def_node_matcher :allow_file?, <<-MATCHER
(send
(send nil? {:allow :expect} (const nil? :File))
{:to :not_to}
...)
MATCHER

def_node_search :receive_stat?, '(send nil? :receive (sym :stat))'

def on_send(node)
return unless file_stat?(node) || (allow_file?(node) && receive_stat?(node))

add_offense(node)
end

def autocorrect(node)
->(corrector) do
const = if file_stat?(node)
node.children[0].loc.expression
else
node.children[0].children[2].loc.expression
end
corrector.replace(const, 'PDK::Util::Filesystem')
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pdk/module/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def stage_path(path)
dest_path = File.join(build_dir, relative_path)

if PDK::Util::Filesystem.directory?(path)
PDK::Util::Filesystem.mkdir_p(dest_path, mode: File.stat(path).mode)
PDK::Util::Filesystem.mkdir_p(dest_path, mode: PDK::Util::Filesystem.stat(path).mode)
elsif File.symlink?(path)
warn_symlink(path)
else
Expand Down Expand Up @@ -240,7 +240,7 @@ def build_package
name: entry,
}

orig_mode = File.stat(entry).mode
orig_mode = PDK::Util::Filesystem.stat(entry).mode
min_mode = Minitar.dir?(entry) ? 0o755 : 0o644

entry_meta[:mode] = orig_mode | min_mode
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/module/update_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def unified_diff(path, old_content, new_content, lines_of_context = 3)

require 'diff/lcs/hunk'

file_mtime = File.stat(path).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
file_mtime = PDK::Util::Filesystem.stat(path).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
now = Time.now.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')

output << "--- #{path}\t#{file_mtime}"
Expand Down
5 changes: 5 additions & 0 deletions lib/pdk/util/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def zero?(*args)
File.zero?(*args)
end
module_function :zero?

def stat(*args)
File.stat(*args)
end
module_function :stat
#:nocov:
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/module/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
context 'when the path is a directory' do
before(:each) do
allow(PDK::Util::Filesystem).to receive(:directory?).with(path_to_stage).and_return(true)
allow(File).to receive(:stat).with(path_to_stage).and_return(instance_double(File::Stat, mode: 0o100755))
allow(PDK::Util::Filesystem).to receive(:stat).with(path_to_stage).and_return(instance_double(File::Stat, mode: 0o100755))
end

it 'creates the directory in the build directory' do
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/module/update_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
before(:each) do
allow(PDK::Util::Filesystem).to receive(:readable?).with(dummy_file).and_return(true)
allow(PDK::Util::Filesystem).to receive(:read_file).with(dummy_file).and_return(original_content)
allow(File).to receive(:stat).with(dummy_file).and_return(instance_double(File::Stat, mtime: Time.now - 60))
allow(PDK::Util::Filesystem).to receive(:stat).with(dummy_file).and_return(instance_double(File::Stat, mtime: Time.now - 60))
end

context 'when the file can not be opened for reading' do
Expand Down

0 comments on commit b8a3cf4

Please sign in to comment.