lib/fakeetc/users.rb in fakeetc-0.2.0 vs lib/fakeetc/users.rb in fakeetc-0.3.0
- old
+ new
@@ -1,7 +1,11 @@
module FakeEtc # rubocop:disable Documentation
class << self
+ # @return [String] the short user name of the currently "logged
+ # in" fake user
+ attr_writer :login
+
# Adds users to the FakeEtc user list.
#
# @param user_hash [Hash{String=>Hash{Symbol=>Integer,String}}]
# the list of users that should be added
#
@@ -19,24 +23,27 @@
# shell: '/bin/bash' },
# })
#
# @return [void]
def add_users(user_hash)
- passwd = 'x'
-
user_hash.each do |user_name, user_info|
- user = Struct::Passwd.new(user_name,
- passwd,
- user_info[:uid],
- user_info[:gid],
- user_info[:gecos],
- user_info[:dir],
- user_info[:shell])
- @users[user_name] = user
+ @users[user_name] = to_passwd(user_name, user_info)
end
end
+ def to_passwd(user_name, user_info)
+ passwd = 'x'
+ Struct::Passwd.new(user_name,
+ passwd,
+ user_info[:uid],
+ user_info[:gid],
+ user_info[:gecos],
+ user_info[:dir],
+ user_info[:shell])
+ end
+ private :to_passwd
+
# Clears the user list.
# @return [void]
def clear_users
@users = {}
end
@@ -50,18 +57,17 @@
user = @users[user_name]
fail ArgumentError, "can't find user for #{user_name}" if user.nil?
user
end
- # Finds a user by their user id.
+ # Finds a user by their user id. Returns the current user if no
+ # uid is supplied.
# @param uid [Integer] the user's id
# @return [Struct::Passwd] the user
# @raise [ArgumentError] if no user with the given id can be found
- def getpwuid(uid)
- user = @users.values.find { |u| u.uid == uid }
- fail ArgumentError, "can't find user for #{uid}" if user.nil?
- user
+ def getpwuid(*uid)
+ getbyargs(:user, uid)
end
# Returns an entry from the user list. Each successive call
# returns the next entry or `nil` if the end of the list has been
# reached.
@@ -87,8 +93,14 @@
def passwd
return getpwent unless block_given?
@users.values.each { |u| yield u }
endpwent
nil
+ end
+
+ # @return [String] the short user name of the currently "logged
+ # in" fake user
+ def getlogin
+ @login
end
end
end