Permalink
Browse files

implemented chown and chmod

  • Loading branch information...
1 parent 57d1dd3 commit 0a75fe8f324eca698b1cb9286363b5bce846338b @mmriis committed Jan 25, 2012
Showing with 98 additions and 17 deletions.
  1. +1 −0 .gitignore
  2. +11 −7 lib/fakefs/fake/dir.rb
  3. +4 −1 lib/fakefs/fake/file.rb
  4. +31 −9 lib/fakefs/file.rb
  5. +51 −0 test/fakefs_test.rb
View
@@ -1,3 +1,4 @@
*.sw?
pkg
Gemfile.lock
+.rbenv-version
View
@@ -1,14 +1,18 @@
module FakeFS
class FakeDir < Hash
- attr_accessor :name, :parent
- attr_reader :ctime, :mtime, :atime
+ attr_accessor :name, :parent, :mode, :uid, :gid
+ attr_reader :ctime, :mtime, :atime, :content
def initialize(name = nil, parent = nil)
- @name = name
- @parent = parent
- @ctime = Time.now
- @mtime = @ctime
- @atime = @ctime
+ @name = name
+ @parent = parent
+ @ctime = Time.now
+ @mtime = @ctime
+ @atime = @ctime
+ @mode = 0100000 + (0777 - File.umask)
+ @uid = Process.uid
+ @gid = Process.gid
+ @content = ""
end
def entry
View
@@ -1,6 +1,6 @@
module FakeFS
class FakeFile
- attr_accessor :name, :parent, :content, :mtime, :atime
+ attr_accessor :name, :parent, :content, :mtime, :atime, :mode, :uid, :gid
attr_reader :ctime
class Inode
@@ -35,6 +35,9 @@ def initialize(name = nil, parent = nil)
@ctime = Time.now
@mtime = @ctime
@atime = @ctime
+ @mode = 0100000 + (0666 - File.umask)
+ @uid = Process.uid
+ @gid = Process.gid
end
attr_accessor :inode
View
@@ -233,9 +233,25 @@ def self.lstat(file)
def self.split(path)
return RealFile.split(path)
end
+
+ def self.chmod(mode_int, filename)
+ FileSystem.find(filename).mode = 0100000 + mode_int
+ end
+ def self.chown(owner_int, group_int, filename)
+ file = FileSystem.find(filename)
+ owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ file.uid = owner_int
+ file.gid = group_int
+ end
+
+ def self.umask
+ RealFile.umask
+ end
+
class Stat
- attr_reader :ctime, :mtime, :atime
+ attr_reader :ctime, :mtime, :atime, :mode, :uid, :gid
def initialize(file, __lstat = false)
if !File.exists?(file)
@@ -248,6 +264,9 @@ def initialize(file, __lstat = false)
@ctime = @fake_file.ctime
@mtime = @fake_file.mtime
@atime = @fake_file.atime
+ @mode = @fake_file.mode
+ @uid = @fake_file.uid
+ @gid = @fake_file.gid
end
def symlink?
@@ -340,14 +359,6 @@ def atime
self.class.atime(@path)
end
- def chmod(mode_int)
- raise NotImplementedError
- end
-
- def chown(owner_int, group_int)
- raise NotImplementedError
- end
-
def ctime
self.class.ctime(@path)
end
@@ -359,6 +370,17 @@ def flock(locking_constant)
def mtime
self.class.mtime(@path)
end
+
+ def chmod(mode_int)
+ @file.mode = 0100000 + mode_int
+ end
+
+ def chown(owner_int, group_int)
+ owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ @file.uid = owner_int
+ @file.gid = group_int
+ end
if RUBY_VERSION >= "1.9"
def binmode?
View
@@ -1824,6 +1824,57 @@ def test_split
assert_equal path, "/this/is/what/we"
assert_equal filename, "expect.txt"
end
+
+ #########################
+ def test_file_default_mode
+ FileUtils.touch "foo"
+ assert_equal File.stat("foo").mode, (0100000 + 0666 - File.umask)
+ end
+
+ def test_dir_default_mode
+ Dir.mkdir "bar"
+ assert_equal File.stat("bar").mode, (0100000 + 0777 - File.umask)
+ end
+
+ def test_file_default_uid_and_gid
+ FileUtils.touch "foo"
+ assert_equal File.stat("foo").uid, Process.uid
+ assert_equal File.stat("foo").gid, Process.gid
+ end
+
+ def test_file_chmod_of_file
+ FileUtils.touch "foo"
+ File.chmod 0600, "foo"
+ assert_equal File.stat("foo").mode, 0100600
+ File.new("foo").chmod 0644
+ assert_equal File.stat("foo").mode, 0100644
+ end
+
+ def test_file_chmod_of_dir
+ Dir.mkdir "bar"
+ File.chmod 0777, "bar"
+ assert_equal File.stat("bar").mode, 0100777
+ File.new("bar").chmod 01700
+ assert_equal File.stat("bar").mode, 0101700
+ end
+
+ def test_file_chown_of_file
+ FileUtils.touch "foo"
+ File.chown 1337, 1338, "foo"
+ assert_equal File.stat("foo").uid, 1337
+ assert_equal File.stat("foo").gid, 1338
+ end
+
+ def test_file_chown_of_dir
+ Dir.mkdir "bar"
+ File.chown 1337, 1338, "bar"
+ assert_equal File.stat("bar").uid, 1337
+ assert_equal File.stat("bar").gid, 1338
+ end
+
+ def test_file_umask
+ assert_equal File.umask, RealFile.umask
+ end
def here(fname)

0 comments on commit 0a75fe8

Please sign in to comment.