Skip to content

Commit

Permalink
implemented chown and chmod
Browse files Browse the repository at this point in the history
  • Loading branch information
mmriis committed Jan 25, 2012
1 parent 57d1dd3 commit 0a75fe8
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*.sw?
pkg
Gemfile.lock
.rbenv-version
18 changes: 11 additions & 7 deletions lib/fakefs/fake/dir.rb
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/fakefs/fake/file.rb
@@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 31 additions & 9 deletions lib/fakefs/file.rb
Expand Up @@ -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)
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down
51 changes: 51 additions & 0 deletions test/fakefs_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 0a75fe8

Please sign in to comment.