Skip to content

Commit 866ff92

Browse files
committed
Add identical check
1 parent 6bc2d1f commit 866ff92

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/boxing/generator.rb

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def initialize(destination, content)
1818
#
1919
# @since 0.11.0
2020
def execute
21+
return if identical?
22+
2123
FileUtils.mkdir_p(File.dirname(@destination))
2224
File.write(@destination, render)
2325
end
@@ -34,5 +36,23 @@ def render
3436
@content
3537
end
3638
end
39+
40+
# Check if file exists
41+
#
42+
# @return [TrueClass|FalseClass]
43+
#
44+
# @since 0.11.0
45+
def exist?
46+
@destination.exist?
47+
end
48+
49+
# Check if file identical
50+
#
51+
# @return [TrueClass|FalseClass]
52+
#
53+
# @since 0.11.0
54+
def identical?
55+
exist? && File.binread(@destination) == String.new(render).force_encoding('ASCII-8BIT')
56+
end
3757
end
3858
end

spec/boxing/generator_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@
1919
it 'writes the template to the file' do
2020
expect(File.read(File.join(tmpdir, 'Dockerfile'))).to eq 'test'
2121
end
22+
23+
context 'when file identically exists' do
24+
before { generator.execute }
25+
26+
it { is_expected.to be_exist }
27+
it { is_expected.to be_identical }
28+
end
2229
end

0 commit comments

Comments
 (0)