Browse files

Manually setting the state_file path sets mode too

When manually setting the state file, set the mode too.
Failure to do so causes an implicit conversion of nil to integer on
first usage.

Also provided a mode method to set the mode explicitly.
  • Loading branch information...
1 parent ea2d2ae commit 2ef324650aaa494b28f7ac0465e48cc789cb374a @tomlea tomlea committed with tomlea Oct 2, 2012
Showing with 20 additions and 0 deletions.
  1. +5 −0 lib/uuid.rb
  2. +15 −0 test/test-uuid.rb
View
5 lib/uuid.rb
@@ -114,6 +114,10 @@ def self.mode
@mode
end
+ def self.mode=(mode)
+ @mode = mode
+ end
+
##
# Generates a new UUID string using +format+. See FORMATS for a list of
# supported formats.
@@ -185,6 +189,7 @@ def self.state_file(mode = 0644)
# hosts).
def self.state_file=(path)
@state_file = path
+ @mode ||= 0644
end
##
View
15 test/test-uuid.rb
@@ -37,6 +37,21 @@ def test_state_file_specify
assert_equal path, UUID.state_file
end
+ def test_mode_is_set_on_state_file_specify
+ UUID.class_eval{ @state_file = nil; @mode = nil }
+ path = File.join("/tmp", "ruby-uuid-test")
+ File.delete path if File.exist?(path)
+
+ UUID.state_file = path
+
+ old_umask = File.umask(0022)
+ UUID.new.generate
+ File.umask(old_umask)
+
+ UUID.class_eval{ @state_file = nil; @mode = nil }
+ assert_equal '0644', sprintf('%04o', File.stat(path).mode & 0777)
+ end
+
def test_with_no_state_file
UUID.state_file = false
assert !UUID.state_file

0 comments on commit 2ef3246

Please sign in to comment.