fakeetc 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/NEWS.md +12 -0
- data/README.md +1 -0
- data/lib/fakeetc/base.rb +34 -0
- data/lib/fakeetc/groups.rb +5 -6
- data/lib/fakeetc/users.rb +27 -15
- data/lib/fakeetc/version.rb +1 -1
- data/spec/fakeetc_spec.rb +59 -2
- data/spec/spec_helper.rb +10 -0
- metadata +9 -9
- data/NEWS +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1010bf297fbbf86c82442423a8d50ddf0943790e
|
4
|
+
data.tar.gz: c2067599d8eae21e06db5df1faf83be24c886302
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70f5945042165e7d71e27f2473d299fc38fffe66a0f349594e69f426195f628b73d064da1e9c01dba2cc957d2dfeb753ad58949466323382b15ad2cc78cdb44a
|
7
|
+
data.tar.gz: 78ce6591cac73f0a906ff2ce36f13a4be894ddf28295a1d723896468a5f282b6e83e732ff1463e8a5b61f8ad72ac5f3a00910b3ff88cbecdee2fce15624e5b68
|
data/NEWS.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# fakeetc NEWS
|
2
|
+
## 0.3.0 (2015-06-03)
|
3
|
+
- Add FakeEtc.getlogin
|
4
|
+
- Let FakeEtc.getpwuid default to current user
|
5
|
+
- Let FakeEtc.getgrgid default to current user's primary group
|
6
|
+
|
7
|
+
## 0.2.0 (2015-02-13)
|
8
|
+
- Add support for user functions (FakeEtc.getgrgid, FakeEtc.getgrnam, ...)
|
9
|
+
- Add documentation (yardoc)
|
10
|
+
|
11
|
+
## 0.1.0 (2015-02-02)
|
12
|
+
- First public release
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@ fakeetc
|
|
4
4
|
[![Gem Version](http://img.shields.io/gem/v/fakeetc.svg?style=flat-square)][gem]
|
5
5
|
[![Build Status](http://img.shields.io/travis/sometimesfood/fakeetc.svg?style=flat-square)][travis]
|
6
6
|
[![Code Climate](http://img.shields.io/codeclimate/github/sometimesfood/fakeetc.svg?style=flat-square)][codeclimate]
|
7
|
+
[![Test Coverage](https://img.shields.io/codeclimate/coverage/github/sometimesfood/fakeetc.svg?style=flat-square)][codeclimate]
|
7
8
|
|
8
9
|
A fake Ruby `Etc` module for your tests.
|
9
10
|
|
data/lib/fakeetc/base.rb
CHANGED
@@ -86,6 +86,40 @@ module FakeEtc # rubocop:disable Documentation
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
end
|
89
|
+
|
90
|
+
def self.idbyargs(user_or_group, args)
|
91
|
+
fail ArgumentError unless [:user, :group].include?(user_or_group)
|
92
|
+
return args.first unless args.size.zero?
|
93
|
+
user_or_group == :user ? getpwnam(getlogin).uid : getpwnam(getlogin).gid
|
94
|
+
end
|
95
|
+
private_class_method :idbyargs
|
96
|
+
|
97
|
+
def self.getbyid(user_or_group, id)
|
98
|
+
fail ArgumentError unless [:user, :group].include?(user_or_group)
|
99
|
+
if user_or_group == :user
|
100
|
+
records = @users
|
101
|
+
id_name = :uid
|
102
|
+
else
|
103
|
+
records = @groups
|
104
|
+
id_name = :gid
|
105
|
+
end
|
106
|
+
records.values.find { |r| r.method(id_name).call == id }
|
107
|
+
end
|
108
|
+
private_class_method :getbyid
|
109
|
+
|
110
|
+
def self.getbyargs(user_or_group, args)
|
111
|
+
argument_error = "wrong number of arguments (#{args.size} for 0..1)"
|
112
|
+
fail ArgumentError, argument_error, caller if args.size > 1
|
113
|
+
|
114
|
+
id = idbyargs(user_or_group, args)
|
115
|
+
entry = getbyid(user_or_group, id)
|
116
|
+
|
117
|
+
unless entry
|
118
|
+
fail ArgumentError, "can't find #{user_or_group} for #{id}", caller
|
119
|
+
end
|
120
|
+
entry
|
121
|
+
end
|
122
|
+
private_class_method :getbyargs
|
89
123
|
end
|
90
124
|
|
91
125
|
# Runs a code block with FakeEtc.
|
data/lib/fakeetc/groups.rb
CHANGED
@@ -18,7 +18,7 @@ module FakeEtc # rubocop:disable Documentation
|
|
18
18
|
group = Struct::Group.new(group_name,
|
19
19
|
passwd,
|
20
20
|
group_info[:gid],
|
21
|
-
group_info[:mem])
|
21
|
+
group_info[:mem] || [])
|
22
22
|
@groups[group_name] = group
|
23
23
|
end
|
24
24
|
end
|
@@ -40,15 +40,14 @@ module FakeEtc # rubocop:disable Documentation
|
|
40
40
|
group
|
41
41
|
end
|
42
42
|
|
43
|
-
# Finds a group by its gid.
|
43
|
+
# Finds a group by its gid. Returns the current user's primary
|
44
|
+
# group if no gid is supplied.
|
44
45
|
# @param gid [Integer] the group's gid
|
45
46
|
# @return [Struct::Group] the group
|
46
47
|
# @raise [ArgumentError] if no group with the given gid can be
|
47
48
|
# found
|
48
|
-
def getgrgid(gid)
|
49
|
-
group
|
50
|
-
fail ArgumentError, "can't find group for #{gid}" if group.nil?
|
51
|
-
group
|
49
|
+
def getgrgid(*gid)
|
50
|
+
getbyargs(:group, gid)
|
52
51
|
end
|
53
52
|
|
54
53
|
# Returns an entry from the group list. Each successive call
|
data/lib/fakeetc/users.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module FakeEtc # rubocop:disable Documentation
|
2
2
|
class << self
|
3
|
+
# @return [String] the short user name of the currently "logged
|
4
|
+
# in" fake user
|
5
|
+
attr_writer :login
|
6
|
+
|
3
7
|
# Adds users to the FakeEtc user list.
|
4
8
|
#
|
5
9
|
# @param user_hash [Hash{String=>Hash{Symbol=>Integer,String}}]
|
@@ -21,20 +25,23 @@ module FakeEtc # rubocop:disable Documentation
|
|
21
25
|
#
|
22
26
|
# @return [void]
|
23
27
|
def add_users(user_hash)
|
24
|
-
passwd = 'x'
|
25
|
-
|
26
28
|
user_hash.each do |user_name, user_info|
|
27
|
-
|
28
|
-
passwd,
|
29
|
-
user_info[:uid],
|
30
|
-
user_info[:gid],
|
31
|
-
user_info[:gecos],
|
32
|
-
user_info[:dir],
|
33
|
-
user_info[:shell])
|
34
|
-
@users[user_name] = user
|
29
|
+
@users[user_name] = to_passwd(user_name, user_info)
|
35
30
|
end
|
36
31
|
end
|
37
32
|
|
33
|
+
def to_passwd(user_name, user_info)
|
34
|
+
passwd = 'x'
|
35
|
+
Struct::Passwd.new(user_name,
|
36
|
+
passwd,
|
37
|
+
user_info[:uid],
|
38
|
+
user_info[:gid],
|
39
|
+
user_info[:gecos],
|
40
|
+
user_info[:dir],
|
41
|
+
user_info[:shell])
|
42
|
+
end
|
43
|
+
private :to_passwd
|
44
|
+
|
38
45
|
# Clears the user list.
|
39
46
|
# @return [void]
|
40
47
|
def clear_users
|
@@ -52,14 +59,13 @@ module FakeEtc # rubocop:disable Documentation
|
|
52
59
|
user
|
53
60
|
end
|
54
61
|
|
55
|
-
# Finds a user by their user id.
|
62
|
+
# Finds a user by their user id. Returns the current user if no
|
63
|
+
# uid is supplied.
|
56
64
|
# @param uid [Integer] the user's id
|
57
65
|
# @return [Struct::Passwd] the user
|
58
66
|
# @raise [ArgumentError] if no user with the given id can be found
|
59
|
-
def getpwuid(uid)
|
60
|
-
user
|
61
|
-
fail ArgumentError, "can't find user for #{uid}" if user.nil?
|
62
|
-
user
|
67
|
+
def getpwuid(*uid)
|
68
|
+
getbyargs(:user, uid)
|
63
69
|
end
|
64
70
|
|
65
71
|
# Returns an entry from the user list. Each successive call
|
@@ -90,5 +96,11 @@ module FakeEtc # rubocop:disable Documentation
|
|
90
96
|
endpwent
|
91
97
|
nil
|
92
98
|
end
|
99
|
+
|
100
|
+
# @return [String] the short user name of the currently "logged
|
101
|
+
# in" fake user
|
102
|
+
def getlogin
|
103
|
+
@login
|
104
|
+
end
|
93
105
|
end
|
94
106
|
end
|
data/lib/fakeetc/version.rb
CHANGED
data/spec/fakeetc_spec.rb
CHANGED
@@ -22,6 +22,7 @@ describe FakeEtc do
|
|
22
22
|
}
|
23
23
|
FakeEtc.add_groups(@groups)
|
24
24
|
FakeEtc.add_users(@users)
|
25
|
+
FakeEtc.login = 'norwegian_blue'
|
25
26
|
end
|
26
27
|
|
27
28
|
after(:each) do
|
@@ -29,6 +30,7 @@ describe FakeEtc do
|
|
29
30
|
FakeEtc.clear_users
|
30
31
|
FakeEtc.endgrent
|
31
32
|
FakeEtc.clear_groups
|
33
|
+
FakeEtc.login = nil
|
32
34
|
end
|
33
35
|
|
34
36
|
describe 'activate' do
|
@@ -44,7 +46,7 @@ describe FakeEtc do
|
|
44
46
|
end
|
45
47
|
|
46
48
|
describe 'with' do
|
47
|
-
it 'should activate
|
49
|
+
it 'should activate FakeEtc in a block' do
|
48
50
|
real_etc = Etc
|
49
51
|
fake_etc = FakeEtc
|
50
52
|
|
@@ -53,10 +55,23 @@ describe FakeEtc do
|
|
53
55
|
end
|
54
56
|
Etc.must_equal real_etc
|
55
57
|
end
|
58
|
+
|
59
|
+
it 'should run the block if FakeEtc is already activated' do
|
60
|
+
real_etc = Etc
|
61
|
+
fake_etc = FakeEtc
|
62
|
+
|
63
|
+
FakeEtc.with do
|
64
|
+
Etc.must_equal fake_etc
|
65
|
+
FakeEtc.with do
|
66
|
+
Etc.must_equal fake_etc
|
67
|
+
end
|
68
|
+
end
|
69
|
+
Etc.must_equal real_etc
|
70
|
+
end
|
56
71
|
end
|
57
72
|
|
58
73
|
describe 'without' do
|
59
|
-
it 'should
|
74
|
+
it 'should deactivate FakeEtc in a block' do
|
60
75
|
real_etc = Etc
|
61
76
|
fake_etc = FakeEtc
|
62
77
|
|
@@ -69,6 +84,23 @@ describe FakeEtc do
|
|
69
84
|
end
|
70
85
|
Etc.must_equal real_etc
|
71
86
|
end
|
87
|
+
|
88
|
+
it 'should run the block if FakeEtc is already deactivated' do
|
89
|
+
real_etc = Etc
|
90
|
+
fake_etc = FakeEtc
|
91
|
+
|
92
|
+
FakeEtc.with do
|
93
|
+
Etc.must_equal fake_etc
|
94
|
+
FakeEtc.without do
|
95
|
+
Etc.must_equal real_etc
|
96
|
+
FakeEtc.without do
|
97
|
+
Etc.must_equal real_etc
|
98
|
+
end
|
99
|
+
end
|
100
|
+
Etc.must_equal fake_etc
|
101
|
+
end
|
102
|
+
Etc.must_equal real_etc
|
103
|
+
end
|
72
104
|
end
|
73
105
|
|
74
106
|
describe 'getgrnam' do
|
@@ -94,6 +126,14 @@ describe FakeEtc do
|
|
94
126
|
parrot_group.mem.must_equal @groups['parrots'][:mem]
|
95
127
|
end
|
96
128
|
|
129
|
+
it 'should return the primary group of the current user' do
|
130
|
+
FakeEtc.login = 'red_leicester'
|
131
|
+
cheese_group = FakeEtc.getgrgid
|
132
|
+
cheese_group.must_be_instance_of Struct::Group
|
133
|
+
cheese_group.gid.must_equal @groups['cheeses'][:gid]
|
134
|
+
cheese_group.mem.must_equal @groups['cheeses'][:mem]
|
135
|
+
end
|
136
|
+
|
97
137
|
it 'should raise exceptions for non-existent groups' do
|
98
138
|
gid = 247
|
99
139
|
err = -> { FakeEtc.getgrgid(gid) }.must_raise ArgumentError
|
@@ -182,6 +222,17 @@ describe FakeEtc do
|
|
182
222
|
norwegian_blue.shell.must_equal @users['norwegian_blue'][:shell]
|
183
223
|
end
|
184
224
|
|
225
|
+
it 'should return the current user' do
|
226
|
+
FakeEtc.login = 'red_leicester'
|
227
|
+
red_leicester = FakeEtc.getpwuid
|
228
|
+
red_leicester.must_be_instance_of Struct::Passwd
|
229
|
+
red_leicester.uid.must_equal @users['red_leicester'][:uid]
|
230
|
+
red_leicester.gid.must_equal @users['red_leicester'][:gid]
|
231
|
+
red_leicester.gecos.must_equal @users['red_leicester'][:gecos]
|
232
|
+
red_leicester.dir.must_equal @users['red_leicester'][:dir]
|
233
|
+
red_leicester.shell.must_equal @users['red_leicester'][:shell]
|
234
|
+
end
|
235
|
+
|
185
236
|
it 'should raise exceptions for non-existent groups' do
|
186
237
|
uid = 247
|
187
238
|
err = -> { FakeEtc.getpwuid(uid) }.must_raise ArgumentError
|
@@ -234,6 +285,12 @@ describe FakeEtc do
|
|
234
285
|
nil_user.must_be_nil
|
235
286
|
end
|
236
287
|
end
|
288
|
+
|
289
|
+
describe 'getlogin' do
|
290
|
+
it 'should return the name of the current fake user' do
|
291
|
+
FakeEtc.getlogin.must_equal 'norwegian_blue'
|
292
|
+
end
|
293
|
+
end
|
237
294
|
end
|
238
295
|
|
239
296
|
describe 'FakeEtc' do
|
data/spec/spec_helper.rb
CHANGED
@@ -1 +1,11 @@
|
|
1
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
2
|
+
require 'simplecov'
|
3
|
+
require 'codeclimate-test-reporter'
|
4
|
+
ignored_directories = %w(/spec/ /vendor/ /.bundle/)
|
5
|
+
formatters = [SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
CodeClimate::TestReporter::Formatter]
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[*formatters]
|
8
|
+
SimpleCov.start { add_filter(ignored_directories) }
|
9
|
+
end
|
10
|
+
|
1
11
|
require 'minitest/autorun'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakeetc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastian Boehm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 5.
|
33
|
+
version: 5.6.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 5.
|
40
|
+
version: 5.6.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: yard
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,10 +60,6 @@ executables: []
|
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
- Rakefile
|
64
|
-
- README.md
|
65
|
-
- LICENSE
|
66
|
-
- NEWS
|
67
63
|
- lib/fakeetc/base.rb
|
68
64
|
- lib/fakeetc/groups.rb
|
69
65
|
- lib/fakeetc/system.rb
|
@@ -72,6 +68,10 @@ files:
|
|
72
68
|
- lib/fakeetc.rb
|
73
69
|
- spec/fakeetc_spec.rb
|
74
70
|
- spec/spec_helper.rb
|
71
|
+
- Rakefile
|
72
|
+
- README.md
|
73
|
+
- NEWS.md
|
74
|
+
- LICENSE
|
75
75
|
homepage: https://github.com/sometimesfood/fakeetc
|
76
76
|
licenses:
|
77
77
|
- MIT
|
@@ -84,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
84
|
requirements:
|
85
85
|
- - '>='
|
86
86
|
- !ruby/object:Gem::Version
|
87
|
-
version:
|
87
|
+
version: 1.9.3
|
88
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
89
|
requirements:
|
90
90
|
- - '>='
|
data/NEWS
DELETED