diff options
Diffstat (limited to 'spec/rubyspec/library/etc')
-rw-r--r-- | spec/rubyspec/library/etc/endgrent_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/endpwent_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getgrent_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getgrgid_spec.rb | 70 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getgrnam_spec.rb | 30 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getlogin_spec.rb | 32 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getpwent_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getpwnam_spec.rb | 28 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/getpwuid_spec.rb | 36 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/group_spec.rb | 18 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/nprocessors_spec.rb | 11 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/shared/windows.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/struct_group_spec.rb | 31 | ||||
-rw-r--r-- | spec/rubyspec/library/etc/struct_passwd_spec.rb | 43 |
14 files changed, 334 insertions, 0 deletions
diff --git a/spec/rubyspec/library/etc/endgrent_spec.rb b/spec/rubyspec/library/etc/endgrent_spec.rb new file mode 100644 index 0000000000..95f0dc05e3 --- /dev/null +++ b/spec/rubyspec/library/etc/endgrent_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/windows', __FILE__) +require 'etc' + +describe "Etc.endgrent" do + it_behaves_like(:etc_on_windows, :endgrent) +end diff --git a/spec/rubyspec/library/etc/endpwent_spec.rb b/spec/rubyspec/library/etc/endpwent_spec.rb new file mode 100644 index 0000000000..7ce8f1925b --- /dev/null +++ b/spec/rubyspec/library/etc/endpwent_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/windows', __FILE__) +require 'etc' + +describe "Etc.endpwent" do + it_behaves_like(:etc_on_windows, :endpwent) +end diff --git a/spec/rubyspec/library/etc/getgrent_spec.rb b/spec/rubyspec/library/etc/getgrent_spec.rb new file mode 100644 index 0000000000..96225e351a --- /dev/null +++ b/spec/rubyspec/library/etc/getgrent_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/windows', __FILE__) +require 'etc' + +describe "Etc.getgrent" do + it_behaves_like(:etc_on_windows, :getgrent) +end diff --git a/spec/rubyspec/library/etc/getgrgid_spec.rb b/spec/rubyspec/library/etc/getgrgid_spec.rb new file mode 100644 index 0000000000..9b6b283d52 --- /dev/null +++ b/spec/rubyspec/library/etc/getgrgid_spec.rb @@ -0,0 +1,70 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +platform_is :windows do + describe "Etc.getgrgid" do + it "returns nil" do + Etc.getgrgid(1).should == nil + Etc.getgrgid(nil).should == nil + Etc.getgrgid('nil').should == nil + end + end +end + +# TODO: verify these on non-windows, non-darwin OS +platform_is_not :windows do + describe "Etc.getgrgid" do + before :all do + @gid = `id -g`.strip.to_i + @name = `id -gn`.strip + end + + it "returns a Etc::Group struct instance for the given user" do + gr = Etc.getgrgid(@gid) + + gr.is_a?(Etc::Group).should == true + gr.gid.should == @gid + gr.name.should == @name + end + + it "returns the Etc::Group for a given gid if it exists" do + grp = Etc.getgrgid(@gid) + grp.should be_kind_of(Etc::Group) + grp.gid.should == @gid + grp.name.should == @name + end + + it "uses Process.gid as the default value for the argument" do + gr = Etc.getgrgid + + gr.gid.should == @gid + gr.name.should == @name + end + + it "returns the Group for a given gid if it exists" do + grp = Etc.getgrgid(@gid) + grp.should be_kind_of(Struct::Group) + grp.gid.should == @gid + grp.name.should == @name + end + + it "raises if the group does not exist" do + lambda { Etc.getgrgid(9876)}.should raise_error(ArgumentError) + end + + it "raises a TypeError if not passed an Integer" do + lambda { Etc.getgrgid("foo") }.should raise_error(TypeError) + lambda { Etc.getgrgid(nil) }.should raise_error(TypeError) + end + + it "can be called safely by multiple threads" do + 20.times.map do + Thread.new do + 100.times do + Etc.getgrgid(@gid).gid.should == @gid + end + end + end.each(&:join) + end + end +end diff --git a/spec/rubyspec/library/etc/getgrnam_spec.rb b/spec/rubyspec/library/etc/getgrnam_spec.rb new file mode 100644 index 0000000000..46193f49d6 --- /dev/null +++ b/spec/rubyspec/library/etc/getgrnam_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +platform_is :windows do + describe "Etc.getgrnam" do + it "returns nil" do + Etc.getgrnam(1).should == nil + Etc.getgrnam(nil).should == nil + Etc.getgrnam('nil').should == nil + end + end +end + +platform_is_not :windows do + describe "Etc.getgrnam" do + it "returns a Etc::Group struct instance for the given group" do + gr_name = Etc.getgrent.name + Etc.endgrent + gr = Etc.getgrnam(gr_name) + gr.is_a?(Etc::Group).should == true + end + + it "only accepts strings as argument" do + lambda { + Etc.getgrnam(123) + Etc.getgrnam(nil) + }.should raise_error(TypeError) + end + end +end diff --git a/spec/rubyspec/library/etc/getlogin_spec.rb b/spec/rubyspec/library/etc/getlogin_spec.rb new file mode 100644 index 0000000000..ae52942a92 --- /dev/null +++ b/spec/rubyspec/library/etc/getlogin_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +describe "Etc.getlogin" do + it "returns the name associated with the current login activity" do + getlogin_null = false + + # POSIX logname(1) shows getlogin(2)'s result + # NOTE: Etc.getlogin returns ENV['USER'] if getlogin(2) returns NULL + begin + # make Etc.getlogin to return nil if getlogin(3) returns NULL + envuser, ENV['USER'] = ENV['USER'], nil + if Etc.getlogin + # Etc.getlogin returns the same result of logname(2) + # if it returns non NULL + Etc.getlogin.should == `id -un`.chomp + else + # Etc.getlogin may return nil if the login name is not set + # because of chroot or sudo or something. + Etc.getlogin.should be_nil + getlogin_null = true + end + ensure + ENV['USER'] = envuser + end + + # if getlogin(2) returns NULL, Etc.getlogin returns ENV['USER'] + if getlogin_null + Etc.getlogin.should == ENV['USER'] + end + end +end diff --git a/spec/rubyspec/library/etc/getpwent_spec.rb b/spec/rubyspec/library/etc/getpwent_spec.rb new file mode 100644 index 0000000000..1c8057c9e5 --- /dev/null +++ b/spec/rubyspec/library/etc/getpwent_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/windows', __FILE__) +require 'etc' + +describe "Etc.getpwent" do + it_behaves_like(:etc_on_windows, :getpwent) +end diff --git a/spec/rubyspec/library/etc/getpwnam_spec.rb b/spec/rubyspec/library/etc/getpwnam_spec.rb new file mode 100644 index 0000000000..8610b5da91 --- /dev/null +++ b/spec/rubyspec/library/etc/getpwnam_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +platform_is :windows do + describe "Etc.getpwnam" do + it "returns nil" do + Etc.getpwnam(1).should == nil + Etc.getpwnam(nil).should == nil + Etc.getpwnam('nil').should == nil + end + end +end + +platform_is_not :windows do + describe "Etc.getpwnam" do + it "returns a Etc::Passwd struct instance for the given user" do + pw = Etc.getpwnam(`whoami`.strip) + pw.is_a?(Etc::Passwd).should == true + end + + it "only accepts strings as argument" do + lambda { + Etc.getpwnam(123) + Etc.getpwnam(nil) + }.should raise_error(TypeError) + end + end +end diff --git a/spec/rubyspec/library/etc/getpwuid_spec.rb b/spec/rubyspec/library/etc/getpwuid_spec.rb new file mode 100644 index 0000000000..f1c7218c0b --- /dev/null +++ b/spec/rubyspec/library/etc/getpwuid_spec.rb @@ -0,0 +1,36 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +platform_is :windows do + describe "Etc.getpwuid" do + it "returns nil" do + Etc.getpwuid(1).should == nil + Etc.getpwuid(nil).should == nil + Etc.getpwuid('nil').should == nil + end + end +end + +platform_is_not :windows do + describe "Etc.getpwuid" do + before :all do + @pw = Etc.getpwuid(`id -u`.strip.to_i) + end + + it "returns a Etc::Passwd struct instance for the given user" do + @pw.is_a?(Etc::Passwd).should == true + end + + it "uses Process.uid as the default value for the argument" do + pw = Etc.getpwuid + pw.should == @pw + end + + it "only accepts integers as argument" do + lambda { + Etc.getpwuid("foo") + Etc.getpwuid(nil) + }.should raise_error(TypeError) + end + end +end diff --git a/spec/rubyspec/library/etc/group_spec.rb b/spec/rubyspec/library/etc/group_spec.rb new file mode 100644 index 0000000000..8b92cb7bf0 --- /dev/null +++ b/spec/rubyspec/library/etc/group_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/windows', __FILE__) +require 'etc' + +describe "Etc.group" do + it_behaves_like(:etc_on_windows, :group) + + platform_is_not :windows do + it "raises a RuntimeError for parallel iteration" do + proc { + Etc.group do | group | + Etc.group do | group2 | + end + end + }.should raise_error(RuntimeError) + end + end +end diff --git a/spec/rubyspec/library/etc/nprocessors_spec.rb b/spec/rubyspec/library/etc/nprocessors_spec.rb new file mode 100644 index 0000000000..bce11d06c5 --- /dev/null +++ b/spec/rubyspec/library/etc/nprocessors_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +ruby_version_is "2.2" do + describe "Etc.nprocessors" do + it "returns the number of online processors" do + Etc.nprocessors.should be_kind_of(Integer) + Etc.nprocessors.should >= 1 + end + end +end diff --git a/spec/rubyspec/library/etc/shared/windows.rb b/spec/rubyspec/library/etc/shared/windows.rb new file mode 100644 index 0000000000..8bae235199 --- /dev/null +++ b/spec/rubyspec/library/etc/shared/windows.rb @@ -0,0 +1,7 @@ +describe :etc_on_windows, shared: true do + platform_is :windows do + it "returns nil" do + Etc.send(@method).should == nil + end + end +end diff --git a/spec/rubyspec/library/etc/struct_group_spec.rb b/spec/rubyspec/library/etc/struct_group_spec.rb new file mode 100644 index 0000000000..c33a177a98 --- /dev/null +++ b/spec/rubyspec/library/etc/struct_group_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +describe "Struct::Group" do + platform_is_not :windows do + before :all do + @g = Etc.getgrgid(`id -g`.strip.to_i) + end + + it "returns group name" do + @g.name.should == `id -gn`.strip + end + + it "returns group password" do + @g.passwd.is_a?(String).should == true + end + + it "returns group id" do + @g.gid.should == `id -g`.strip.to_i + end + + it "returns an array of users belonging to the group" do + @g.mem.is_a?(Array).should == true + end + + it "can be compared to another object" do + (@g == nil).should == false + (@g == Object.new).should == false + end + end +end diff --git a/spec/rubyspec/library/etc/struct_passwd_spec.rb b/spec/rubyspec/library/etc/struct_passwd_spec.rb new file mode 100644 index 0000000000..3e4bfee828 --- /dev/null +++ b/spec/rubyspec/library/etc/struct_passwd_spec.rb @@ -0,0 +1,43 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'etc' + +describe "Struct::Passwd" do + platform_is_not :windows do + before :all do + @pw = Etc.getpwuid(`id -u`.strip.to_i) + end + + it "returns user name" do + @pw.name.should == `id -un`.strip + end + + it "returns user password" do + @pw.passwd.is_a?(String).should == true + end + + it "returns user id" do + @pw.uid.should == `id -u`.strip.to_i + end + + it "returns user group id" do + @pw.gid.should == `id -g`.strip.to_i + end + + it "returns user personal information (gecos field)" do + @pw.gecos.is_a?(String).should == true + end + + it "returns user home directory" do + @pw.dir.is_a?(String).should == true + end + + it "returns user shell" do + @pw.shell.is_a?(String).should == true + end + + it "can be compared to another object" do + (@pw == nil).should == false + (@pw == Object.new).should == false + end + end +end |