Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep mtime if no change #212

Merged
merged 8 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/itamae/resource/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,27 @@ def action_delete(options)
end

def action_edit(options)
change_target = attributes.modified ? @temppath : attributes.path

if attributes.mode
run_specinfra(:change_file_mode, @temppath, attributes.mode)
run_specinfra(:change_file_mode, change_target, attributes.mode)
else
mode = run_specinfra(:get_file_mode, attributes.path).stdout.chomp
run_specinfra(:change_file_mode, @temppath, mode)
run_specinfra(:change_file_mode, change_target, mode)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has same problem as :change_file_owner. I'll fix.

end

if attributes.owner || attributes.group
run_specinfra(:change_file_owner, @temppath, attributes.owner, attributes.group)
run_specinfra(:change_file_owner, change_target, attributes.owner, attributes.group)
else
owner = run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
group = run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
run_specinfra(:change_file_owner, @temppath, owner)
run_specinfra(:change_file_group, @temppath, group)
run_specinfra(:change_file_owner, change_target, owner)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make any change because owner is got fromrun_specinfra(:get_file_owner_user, attributes.path)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand following:

  • Case: attributes.modified is true.
    • change_target is @temppath.
    • need :change_file_owner.
  • Case: attributes.modified is false.
    • change_target is attributes.path.
    • DON'T need :change_file_owner.

So I must fix to following:

diff --git a/lib/itamae/resource/file.rb b/lib/itamae/resource/file.rb
index d938523..1d26af7 100644
--- a/lib/itamae/resource/file.rb
+++ b/lib/itamae/resource/file.rb
@@ -95,11 +95,11 @@ def action_edit(options)

         if attributes.owner || attributes.group
           run_specinfra(:change_file_owner, change_target, attributes.owner, attributes.group)
-        else
+        elsif attributes.modified
           owner = run_specinfra(:get_file_owner_user, attributes.path).stdout.chomp
           group = run_specinfra(:get_file_owner_group, attributes.path).stdout.chomp
-          run_specinfra(:change_file_owner, change_target, owner)
-          run_specinfra(:change_file_group, change_target, group)
+          run_specinfra(:change_file_owner, @temppath, owner)
+          run_specinfra(:change_file_group, @temppath, group)
         end

         if attributes.modified

Is it OK?

(elsif attributes.modified removes extra run_specinfra calling)

run_specinfra(:change_file_group, change_target, group)
end

run_specinfra(:move_file, @temppath, attributes.path)
if attributes.modified
run_specinfra(:move_file, @temppath, attributes.path)
end
end

private
Expand Down
15 changes: 15 additions & 0 deletions spec/integration/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@
it { should be_grouped_into "itamae" }
end

describe file('/tmp/file_edit_with_content_change_updates_timestamp') do
its(:mtime) { should be > DateTime.iso8601("2016-05-02T01:23:45Z") }
end

describe file('/tmp/file_edit_without_content_change_keeping_timestamp') do
its(:mtime) { should eq(DateTime.iso8601("2016-05-02T12:34:56Z")) }
end

describe file('/home/itamae2') do
it { should be_directory }
it { should be_owned_by "itamae2" }
Expand All @@ -240,3 +248,10 @@
it { should be_grouped_into "itamae2" }
end

describe file('/tmp/file_with_content_change_updates_timestamp') do
its(:mtime) { should be > DateTime.iso8601("2016-05-01T01:23:45Z") }
end

describe file('/tmp/file_without_content_change_keeping_timestamp') do
its(:mtime) { should eq(DateTime.iso8601("2016-05-01T12:34:56Z")) }
end
38 changes: 38 additions & 0 deletions spec/integration/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,28 @@

###

execute "touch -d 2016-05-02T01:23:45 /tmp/file_edit_with_content_change_updates_timestamp"

file "/tmp/file_edit_with_content_change_updates_timestamp" do
action :edit
block do |content|
content[0 .. -1] = "Hello, world"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using gsub like content.gsub!(/\A.+\z/, "Hello, world")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your response!

I fixed it using gsub! as /tmp/file_edit_sample. (8a1cf21)

end
end

###

execute "touch -d 2016-05-02T12:34:56 /tmp/file_edit_without_content_change_keeping_timestamp"

file "/tmp/file_edit_without_content_change_keeping_timestamp" do
action :edit
block do |content|
# no change
end
end

###

file '/tmp/file_without_content_change_updates_mode_and_owner' do
action :create
content 'Hello, world'
Expand All @@ -420,6 +442,22 @@

###

execute "touch -d 2016-05-01T01:23:45 /tmp/file_with_content_change_updates_timestamp"

file "/tmp/file_with_content_change_updates_timestamp" do
content "Hello, world"
end

###

execute "echo 'Hello, world' > /tmp/file_without_content_change_keeping_timestamp ; touch -d 2016-05-01T12:34:56 /tmp/file_without_content_change_keeping_timestamp"

file "/tmp/file_without_content_change_keeping_timestamp" do
content "Hello, world\n"
end

###

unless run_command("echo -n Hello").stdout == "Hello"
raise "run_command in a recipe failed"
end
Expand Down