diff options
Diffstat (limited to 'spec/ruby/library/etc')
20 files changed, 436 insertions, 0 deletions
diff --git a/spec/ruby/library/etc/confstr_spec.rb b/spec/ruby/library/etc/confstr_spec.rb new file mode 100644 index 0000000000..786cb16407 --- /dev/null +++ b/spec/ruby/library/etc/confstr_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'etc' + +platform_is_not :windows, :android do + describe "Etc.confstr" do + it "returns a String for Etc::CS_PATH" do + Etc.confstr(Etc::CS_PATH).should.instance_of?(String) + end + + it "raises Errno::EINVAL for unknown configuration variables" do + -> { Etc.confstr(-1) }.should.raise(Errno::EINVAL) + end + end +end diff --git a/spec/ruby/library/etc/endgrent_spec.rb b/spec/ruby/library/etc/endgrent_spec.rb new file mode 100644 index 0000000000..88de231d87 --- /dev/null +++ b/spec/ruby/library/etc/endgrent_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/windows' +require 'etc' + +describe "Etc.endgrent" do + it_behaves_like :etc_on_windows, :endgrent +end diff --git a/spec/ruby/library/etc/endpwent_spec.rb b/spec/ruby/library/etc/endpwent_spec.rb new file mode 100644 index 0000000000..e4e564d251 --- /dev/null +++ b/spec/ruby/library/etc/endpwent_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/windows' +require 'etc' + +describe "Etc.endpwent" do + it_behaves_like :etc_on_windows, :endpwent +end diff --git a/spec/ruby/library/etc/getgrent_spec.rb b/spec/ruby/library/etc/getgrent_spec.rb new file mode 100644 index 0000000000..45a4442262 --- /dev/null +++ b/spec/ruby/library/etc/getgrent_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/windows' +require 'etc' + +describe "Etc.getgrent" do + it_behaves_like :etc_on_windows, :getgrent +end diff --git a/spec/ruby/library/etc/getgrgid_spec.rb b/spec/ruby/library/etc/getgrgid_spec.rb new file mode 100644 index 0000000000..472d4c82c8 --- /dev/null +++ b/spec/ruby/library/etc/getgrgid_spec.rb @@ -0,0 +1,69 @@ +require_relative '../../spec_helper' +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 + grpname = nil + guard -> { + grpname = IO.popen(%w'id -gn', err: IO::NULL, &:read).chomp + $?.success? + } do + describe "Etc.getgrgid" do + before :all do + @gid = `id -g`.strip.to_i + @name = grpname + 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.is_a?(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 "raises if the group does not exist" do + -> { Etc.getgrgid(9876)}.should.raise(ArgumentError) + end + + it "raises a TypeError if not passed an Integer" do + -> { Etc.getgrgid("foo") }.should.raise(TypeError) + -> { Etc.getgrgid(nil) }.should.raise(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 +end diff --git a/spec/ruby/library/etc/getgrnam_spec.rb b/spec/ruby/library/etc/getgrnam_spec.rb new file mode 100644 index 0000000000..325ea7b297 --- /dev/null +++ b/spec/ruby/library/etc/getgrnam_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +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, :android 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 + -> { + Etc.getgrnam(123) + Etc.getgrnam(nil) + }.should.raise(TypeError) + end + end +end diff --git a/spec/ruby/library/etc/getlogin_spec.rb b/spec/ruby/library/etc/getlogin_spec.rb new file mode 100644 index 0000000000..2bc598c0af --- /dev/null +++ b/spec/ruby/library/etc/getlogin_spec.rb @@ -0,0 +1,43 @@ +require_relative '../../spec_helper' +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 + if ENV['TRAVIS'] and platform_is(:darwin) + # See https://travis-ci.org/ruby/spec/jobs/285967744 + # and https://travis-ci.org/ruby/spec/jobs/285999602 + Etc.getlogin.should.instance_of?(String) + else + # Etc.getlogin returns the same result of logname(2) + # if it returns non NULL + if system("which logname", out: File::NULL, err: File::NULL) + Etc.getlogin.should == `logname`.chomp + else + # fallback to `id` command since `logname` is not available + Etc.getlogin.should == `id -un`.chomp + end + end + else + # Etc.getlogin may return nil if the login name is not set + # because of chroot or sudo or something. + Etc.getlogin.should == 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/ruby/library/etc/getpwent_spec.rb b/spec/ruby/library/etc/getpwent_spec.rb new file mode 100644 index 0000000000..9a911aed8f --- /dev/null +++ b/spec/ruby/library/etc/getpwent_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/windows' +require 'etc' + +describe "Etc.getpwent" do + it_behaves_like :etc_on_windows, :getpwent +end diff --git a/spec/ruby/library/etc/getpwnam_spec.rb b/spec/ruby/library/etc/getpwnam_spec.rb new file mode 100644 index 0000000000..a0b3c9e1fe --- /dev/null +++ b/spec/ruby/library/etc/getpwnam_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +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 + -> { + Etc.getpwnam(123) + Etc.getpwnam(nil) + }.should.raise(TypeError) + end + end +end diff --git a/spec/ruby/library/etc/getpwuid_spec.rb b/spec/ruby/library/etc/getpwuid_spec.rb new file mode 100644 index 0000000000..3e35dfe6d5 --- /dev/null +++ b/spec/ruby/library/etc/getpwuid_spec.rb @@ -0,0 +1,36 @@ +require_relative '../../spec_helper' +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 + -> { + Etc.getpwuid("foo") + Etc.getpwuid(nil) + }.should.raise(TypeError) + end + end +end diff --git a/spec/ruby/library/etc/group_spec.rb b/spec/ruby/library/etc/group_spec.rb new file mode 100644 index 0000000000..d7addbbec1 --- /dev/null +++ b/spec/ruby/library/etc/group_spec.rb @@ -0,0 +1,27 @@ +require_relative '../../spec_helper' +require_relative 'shared/windows' +require 'etc' + +describe "Etc.group" do + it_behaves_like :etc_on_windows, :group + + platform_is_not :windows, :android do + it "returns a Etc::Group struct" do + group = Etc.group + begin + group.should.instance_of?(Etc::Group) + ensure + Etc.endgrent + end + end + + it "raises a RuntimeError for parallel iteration" do + proc { + Etc.group do | group | + Etc.group do | group2 | + end + end + }.should.raise(RuntimeError) + end + end +end diff --git a/spec/ruby/library/etc/nprocessors_spec.rb b/spec/ruby/library/etc/nprocessors_spec.rb new file mode 100644 index 0000000000..482719dde0 --- /dev/null +++ b/spec/ruby/library/etc/nprocessors_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require 'etc' + +describe "Etc.nprocessors" do + it "returns the number of online processors" do + Etc.nprocessors.should.is_a?(Integer) + Etc.nprocessors.should >= 1 + end +end diff --git a/spec/ruby/library/etc/passwd_spec.rb b/spec/ruby/library/etc/passwd_spec.rb new file mode 100644 index 0000000000..0602b7e10b --- /dev/null +++ b/spec/ruby/library/etc/passwd_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require 'etc' + +platform_is_not :windows do + describe "Etc.passwd" do + it "returns a Etc::Passwd struct" do + passwd = Etc.passwd + begin + passwd.should.instance_of?(Etc::Passwd) + ensure + Etc.endpwent + end + end + end +end diff --git a/spec/ruby/library/etc/shared/windows.rb b/spec/ruby/library/etc/shared/windows.rb new file mode 100644 index 0000000000..8bae235199 --- /dev/null +++ b/spec/ruby/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/ruby/library/etc/struct_group_spec.rb b/spec/ruby/library/etc/struct_group_spec.rb new file mode 100644 index 0000000000..b2147e306d --- /dev/null +++ b/spec/ruby/library/etc/struct_group_spec.rb @@ -0,0 +1,35 @@ +require_relative '../../spec_helper' +require 'etc' + +describe "Etc::Group" do + platform_is_not :windows do + grpname = IO.popen(%w'id -gn', err: IO::NULL, &:read) + next unless $?.success? + grpname.chomp! + + before :all do + @g = Etc.getgrgid(`id -g`.strip.to_i) + end + + it "returns group name" do + @g.name.should == grpname + 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/ruby/library/etc/struct_passwd_spec.rb b/spec/ruby/library/etc/struct_passwd_spec.rb new file mode 100644 index 0000000000..dc37c17e7d --- /dev/null +++ b/spec/ruby/library/etc/struct_passwd_spec.rb @@ -0,0 +1,43 @@ +require_relative '../../spec_helper' +require 'etc' + +describe "Etc::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 diff --git a/spec/ruby/library/etc/sysconf_spec.rb b/spec/ruby/library/etc/sysconf_spec.rb new file mode 100644 index 0000000000..81ce1ca258 --- /dev/null +++ b/spec/ruby/library/etc/sysconf_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper' +require 'etc' + +platform_is_not :windows do + describe "Etc.sysconf" do + %w[ + SC_ARG_MAX SC_CHILD_MAX SC_HOST_NAME_MAX SC_LOGIN_NAME_MAX SC_NGROUPS_MAX + SC_CLK_TCK SC_OPEN_MAX SC_PAGESIZE SC_RE_DUP_MAX SC_STREAM_MAX + SC_SYMLOOP_MAX SC_TTY_NAME_MAX SC_TZNAME_MAX SC_VERSION + ].each do |const| + it "returns the value of POSIX.1 system configuration variable #{const}" do + var = Etc.const_get(const) + value = Etc.sysconf(var) + if value.nil? + value.should == nil + else + value.should.is_a?(Integer) + end + end + end + end +end diff --git a/spec/ruby/library/etc/sysconfdir_spec.rb b/spec/ruby/library/etc/sysconfdir_spec.rb new file mode 100644 index 0000000000..eb2d6b649a --- /dev/null +++ b/spec/ruby/library/etc/sysconfdir_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'etc' + +describe "Etc.sysconfdir" do + it "returns a String" do + Etc.sysconfdir.should.instance_of?(String) + end +end diff --git a/spec/ruby/library/etc/systmpdir_spec.rb b/spec/ruby/library/etc/systmpdir_spec.rb new file mode 100644 index 0000000000..ed34cb43fc --- /dev/null +++ b/spec/ruby/library/etc/systmpdir_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'etc' + +describe "Etc.systmpdir" do + it "returns a String" do + Etc.systmpdir.should.instance_of?(String) + end +end diff --git a/spec/ruby/library/etc/uname_spec.rb b/spec/ruby/library/etc/uname_spec.rb new file mode 100644 index 0000000000..1c5fe2a741 --- /dev/null +++ b/spec/ruby/library/etc/uname_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'etc' + +describe "Etc.uname" do + it "returns a Hash with the documented keys" do + uname = Etc.uname + uname.should.is_a?(Hash) + uname.should.key?(:sysname) + uname.should.key?(:nodename) + uname.should.key?(:release) + uname.should.key?(:version) + uname.should.key?(:machine) + end +end |
