Skip to content

Commit

Permalink
Add identical check
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Dec 26, 2023
1 parent 6bc2d1f commit 866ff92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/boxing/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def initialize(destination, content)
#
# @since 0.11.0
def execute
return if identical?

FileUtils.mkdir_p(File.dirname(@destination))
File.write(@destination, render)
end
Expand All @@ -34,5 +36,23 @@ def render
@content
end
end

# Check if file exists
#
# @return [TrueClass|FalseClass]
#
# @since 0.11.0
def exist?
@destination.exist?
end

# Check if file identical
#
# @return [TrueClass|FalseClass]
#
# @since 0.11.0
def identical?
exist? && File.binread(@destination) == String.new(render).force_encoding('ASCII-8BIT')
end
end
end
7 changes: 7 additions & 0 deletions spec/boxing/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
it 'writes the template to the file' do
expect(File.read(File.join(tmpdir, 'Dockerfile'))).to eq 'test'
end

context 'when file identically exists' do
before { generator.execute }

it { is_expected.to be_exist }
it { is_expected.to be_identical }
end
end

0 comments on commit 866ff92

Please sign in to comment.