Skip to content

Commit 0a75fe8

Browse files
committed
implemented chown and chmod
1 parent 57d1dd3 commit 0a75fe8

File tree

5 files changed

+98
-17
lines changed

5 files changed

+98
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.sw?
22
pkg
33
Gemfile.lock
4+
.rbenv-version

lib/fakefs/fake/dir.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
module FakeFS
22
class FakeDir < Hash
3-
attr_accessor :name, :parent
4-
attr_reader :ctime, :mtime, :atime
3+
attr_accessor :name, :parent, :mode, :uid, :gid
4+
attr_reader :ctime, :mtime, :atime, :content
55

66
def initialize(name = nil, parent = nil)
7-
@name = name
8-
@parent = parent
9-
@ctime = Time.now
10-
@mtime = @ctime
11-
@atime = @ctime
7+
@name = name
8+
@parent = parent
9+
@ctime = Time.now
10+
@mtime = @ctime
11+
@atime = @ctime
12+
@mode = 0100000 + (0777 - File.umask)
13+
@uid = Process.uid
14+
@gid = Process.gid
15+
@content = ""
1216
end
1317

1418
def entry

lib/fakefs/fake/file.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module FakeFS
22
class FakeFile
3-
attr_accessor :name, :parent, :content, :mtime, :atime
3+
attr_accessor :name, :parent, :content, :mtime, :atime, :mode, :uid, :gid
44
attr_reader :ctime
55

66
class Inode
@@ -35,6 +35,9 @@ def initialize(name = nil, parent = nil)
3535
@ctime = Time.now
3636
@mtime = @ctime
3737
@atime = @ctime
38+
@mode = 0100000 + (0666 - File.umask)
39+
@uid = Process.uid
40+
@gid = Process.gid
3841
end
3942

4043
attr_accessor :inode

lib/fakefs/file.rb

+31-9
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,25 @@ def self.lstat(file)
233233
def self.split(path)
234234
return RealFile.split(path)
235235
end
236+
237+
def self.chmod(mode_int, filename)
238+
FileSystem.find(filename).mode = 0100000 + mode_int
239+
end
236240

241+
def self.chown(owner_int, group_int, filename)
242+
file = FileSystem.find(filename)
243+
owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
244+
group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
245+
file.uid = owner_int
246+
file.gid = group_int
247+
end
248+
249+
def self.umask
250+
RealFile.umask
251+
end
252+
237253
class Stat
238-
attr_reader :ctime, :mtime, :atime
254+
attr_reader :ctime, :mtime, :atime, :mode, :uid, :gid
239255

240256
def initialize(file, __lstat = false)
241257
if !File.exists?(file)
@@ -248,6 +264,9 @@ def initialize(file, __lstat = false)
248264
@ctime = @fake_file.ctime
249265
@mtime = @fake_file.mtime
250266
@atime = @fake_file.atime
267+
@mode = @fake_file.mode
268+
@uid = @fake_file.uid
269+
@gid = @fake_file.gid
251270
end
252271

253272
def symlink?
@@ -340,14 +359,6 @@ def atime
340359
self.class.atime(@path)
341360
end
342361

343-
def chmod(mode_int)
344-
raise NotImplementedError
345-
end
346-
347-
def chown(owner_int, group_int)
348-
raise NotImplementedError
349-
end
350-
351362
def ctime
352363
self.class.ctime(@path)
353364
end
@@ -359,6 +370,17 @@ def flock(locking_constant)
359370
def mtime
360371
self.class.mtime(@path)
361372
end
373+
374+
def chmod(mode_int)
375+
@file.mode = 0100000 + mode_int
376+
end
377+
378+
def chown(owner_int, group_int)
379+
owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
380+
group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
381+
@file.uid = owner_int
382+
@file.gid = group_int
383+
end
362384

363385
if RUBY_VERSION >= "1.9"
364386
def binmode?

test/fakefs_test.rb

+51
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,57 @@ def test_split
18241824
assert_equal path, "/this/is/what/we"
18251825
assert_equal filename, "expect.txt"
18261826
end
1827+
1828+
#########################
1829+
def test_file_default_mode
1830+
FileUtils.touch "foo"
1831+
assert_equal File.stat("foo").mode, (0100000 + 0666 - File.umask)
1832+
end
1833+
1834+
def test_dir_default_mode
1835+
Dir.mkdir "bar"
1836+
assert_equal File.stat("bar").mode, (0100000 + 0777 - File.umask)
1837+
end
1838+
1839+
def test_file_default_uid_and_gid
1840+
FileUtils.touch "foo"
1841+
assert_equal File.stat("foo").uid, Process.uid
1842+
assert_equal File.stat("foo").gid, Process.gid
1843+
end
1844+
1845+
def test_file_chmod_of_file
1846+
FileUtils.touch "foo"
1847+
File.chmod 0600, "foo"
1848+
assert_equal File.stat("foo").mode, 0100600
1849+
File.new("foo").chmod 0644
1850+
assert_equal File.stat("foo").mode, 0100644
1851+
end
1852+
1853+
def test_file_chmod_of_dir
1854+
Dir.mkdir "bar"
1855+
File.chmod 0777, "bar"
1856+
assert_equal File.stat("bar").mode, 0100777
1857+
File.new("bar").chmod 01700
1858+
assert_equal File.stat("bar").mode, 0101700
1859+
end
1860+
1861+
def test_file_chown_of_file
1862+
FileUtils.touch "foo"
1863+
File.chown 1337, 1338, "foo"
1864+
assert_equal File.stat("foo").uid, 1337
1865+
assert_equal File.stat("foo").gid, 1338
1866+
end
1867+
1868+
def test_file_chown_of_dir
1869+
Dir.mkdir "bar"
1870+
File.chown 1337, 1338, "bar"
1871+
assert_equal File.stat("bar").uid, 1337
1872+
assert_equal File.stat("bar").gid, 1338
1873+
end
1874+
1875+
def test_file_umask
1876+
assert_equal File.umask, RealFile.umask
1877+
end
18271878

18281879

18291880
def here(fname)

0 commit comments

Comments
 (0)