module FakeEtc
FakeEtc
is a fake Etc module intended as a drop-in replacement for Etc in unit tests.
Constants
- VERSION
Current
FakeEtc
version.
Attributes
@return [String] the short user name of the currently “logged
in" fake user
Public Class Methods
Activates FakeEtc
. @return [void]
# File lib/fakeetc/base.rb, line 42 def self.activate @activated = true Object.class_eval do remove_const :Etc const_set :Etc, FakeEtc end end
Adds groups to the FakeEtc
group list.
@param group_hash [Hash{String=>Hash{Symbol=>Integer,String}}]
the list of groups that should be added
@example
FakeEtc.add_groups({ 'empty' => { gid: 42, mem: [] }, 'anonymous' => { gid: 43, mem: ['johndoe'] } })
@return [void]
# File lib/fakeetc/groups.rb, line 15 def add_groups(group_hash) passwd = 'x' group_hash.each do |group_name, group_info| group = Struct::Group.new(group_name, passwd, group_info[:gid], group_info[:mem] || []) @groups[group_name] = group end end
Adds users to the FakeEtc
user list.
@param user_hash [Hash{String=>Hash{Symbol=>Integer,String}}]
the list of users that should be added
@example
FakeEtc.add_users({ 'janedoe' => { uid: 10, gid: 20, gecos: 'Jane Doe', dir: '/home/janedoe', shell: '/bin/zsh' }, 'jackdoe' => { uid: 50, gid: 60, gecos: 'Jack Doe', dir: '/home/jackdoe', shell: '/bin/bash' }, })
@return [void]
# File lib/fakeetc/users.rb, line 27 def add_users(user_hash) user_hash.each do |user_name, user_info| @users[user_name] = to_passwd(user_name, user_info) end end
Clears the group list. @return [void]
# File lib/fakeetc/groups.rb, line 28 def clear_groups @groups = {} end
Clears the user list. @return [void]
# File lib/fakeetc/users.rb, line 47 def clear_users @users = {} end
Deactivates FakeEtc
. @return [void]
# File lib/fakeetc/base.rb, line 52 def self.deactivate Object.class_eval do remove_const :Etc const_set :Etc, RealEtc end @activated = false end
Ends the process of scanning through the group list. @return [void]
# File lib/fakeetc/groups.rb, line 67 def endgrent @grents = nil end
Ends the process of scanning through the user list. @return [void]
# File lib/fakeetc/users.rb, line 85 def endpwent @pwents = nil end
Returns an entry from the group list. Each successive call returns the next entry or ‘nil` if the end of the list has been reached.
To reset scanning the group list, use {endgrent}.
@return [Struct::Group] the next entry in the group list
# File lib/fakeetc/groups.rb, line 60 def getgrent @grents ||= @groups.values @grents.shift end
Finds a group by its gid. Returns the current user’s primary
group if no gid is supplied.
@param gid [Integer] the group’s gid @return [Struct::Group] the group @raise [ArgumentError] if no group with the given gid can be
found
# File lib/fakeetc/groups.rb, line 49 def getgrgid(*gid) getbyargs(:group, gid) end
Finds a group by its group name. @param group_name [String] the group’s name @return [Struct::Group] the group @raise [ArgumentError] if no group with the given name can be
found
# File lib/fakeetc/groups.rb, line 37 def getgrnam(group_name) group = @groups[group_name] fail ArgumentError, "can't find group for #{group_name}" if group.nil? group end
@return [String] the short user name of the currently “logged
in" fake user
# File lib/fakeetc/users.rb, line 102 def getlogin @login 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.
To reset scanning the user list, use {endpwent}.
@return [Struct::Passwd] the next entry in the user list
# File lib/fakeetc/users.rb, line 78 def getpwent @pwents ||= @users.values @pwents.shift end
Finds a user by their user name. @param user_name [String] the user’s name @return [Struct::Passwd] the user @raise [ArgumentError] if no user with the given name can be
found
# File lib/fakeetc/users.rb, line 56 def getpwnam(user_name) 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. 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
# File lib/fakeetc/users.rb, line 67 def getpwuid(*uid) getbyargs(:user, uid) end
Executes a block for each group entry. @yield [Struct::Group] the group entry @return [void]
# File lib/fakeetc/groups.rb, line 75 def group return getgrent unless block_given? @groups.values.each { |g| yield g } endgrent nil end
Executes a block for each user entry. @yield [Struct::Passwd] the user entry @return [void]
# File lib/fakeetc/users.rb, line 93 def passwd return getpwent unless block_given? @users.values.each { |u| yield u } endpwent nil end
@return [String] the system’s configuration directory
# File lib/fakeetc/system.rb, line 3 def self.sysconfdir RealEtc.sysconfdir end
@return [String] the system’s temp directory
# File lib/fakeetc/system.rb, line 8 def self.systmpdir RealEtc.systmpdir end
Runs a code block with FakeEtc
. @return [Object] the block’s return value
# File lib/fakeetc/base.rb, line 62 def self.with if activated? yield else begin activate yield ensure deactivate end end end
Runs a code block without FakeEtc
. @return [Object] the block’s return value
# File lib/fakeetc/base.rb, line 77 def self.without if !activated? yield else begin deactivate yield ensure activate end end end
Private Class Methods
# File lib/fakeetc/base.rb, line 110 def self.getbyargs(user_or_group, args) argument_error = "wrong number of arguments (#{args.size} for 0..1)" fail ArgumentError, argument_error, caller if args.size > 1 id = idbyargs(user_or_group, args) entry = getbyid(user_or_group, id) unless entry fail ArgumentError, "can't find #{user_or_group} for #{id}", caller end entry end
# File lib/fakeetc/base.rb, line 97 def self.getbyid(user_or_group, id) fail ArgumentError unless [:user, :group].include?(user_or_group) if user_or_group == :user records = @users id_name = :uid else records = @groups id_name = :gid end records.values.find { |r| r.method(id_name).call == id } end
# File lib/fakeetc/base.rb, line 90 def self.idbyargs(user_or_group, args) fail ArgumentError unless [:user, :group].include?(user_or_group) return args.first unless args.size.zero? user_or_group == :user ? getpwnam(getlogin).uid : getpwnam(getlogin).gid end
# File lib/fakeetc/users.rb, line 33 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