fakeetc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 565d1540a513a0abc02d642fd11880f9e7e0aec8
4
+ data.tar.gz: 8c52464577b501000d64c1c60d2d0a5a58af874b
5
+ SHA512:
6
+ metadata.gz: 9ea9528987f1a7389509d7d8fb898dc8788ea81de51cf1dddd38d22de491dbf0f3c870b46709aaaca460eec086262d10073686f5afbe121baa974dbcc62a7a2b
7
+ data.tar.gz: 3cc88df0c3dd76b0676aae198d4b70855eee8e343ba3d204407907347a455d8e97bc447caae038e127660eca932a60b20386469772c469a87caf944791888ed3
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sebastian Boehm
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ fakeetc
2
+ =======
3
+
4
+ [![Gem Version](http://img.shields.io/gem/v/fakeetc.svg?style=flat-square)][gem]
5
+ [![Build Status](http://img.shields.io/travis/sometimesfood/fakeetc.svg?style=flat-square)][travis]
6
+ [![Code Climate](http://img.shields.io/codeclimate/github/sometimesfood/fakeetc.svg?style=flat-square)][codeclimate]
7
+
8
+ A fake Ruby `Etc` module for your tests.
9
+
10
+ Intended as a drop-in replacement for [Etc][etc] in unit tests.
11
+
12
+ [etc]: http://ruby-doc.org/stdlib-2.2.0/libdoc/etc/rdoc/Etc.html
13
+
14
+ Usage
15
+ -----
16
+
17
+ ```ruby
18
+ require 'fakeetc'
19
+
20
+ FakeEtc.add_groups({
21
+ 'foo' => { gid: 42, mem: [] },
22
+ 'bar' => { gid: 43, mem: ['johndoe'] }
23
+ })
24
+ FakeEtc do
25
+ Etc.getgrnam('bar')
26
+ end
27
+ # => #<struct Struct::Group name="bar", passwd="x", gid=43, mem=["johndoe"]>
28
+ ```
29
+
30
+ Copyright
31
+ ---------
32
+
33
+ Copyright (c) 2015 Sebastian Boehm. See LICENSE for details.
34
+
35
+ [gem]: https://rubygems.org/gems/fakeetc
36
+ [travis]: https://travis-ci.org/sometimesfood/fakeetc
37
+ [codeclimate]: https://codeclimate.com/github/sometimesfood/fakeetc
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
@@ -0,0 +1,82 @@
1
+ # Most functions in this file are based on those in Chris Wanstrath's
2
+ # excellent fakefs library (lib/fakefs/base.rb).
3
+ #
4
+ # The following is a verbatim copy of the original license:
5
+ #
6
+ # Copyright (c) 2009 Chris Wanstrath
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to
13
+ # permit persons to whom the Software is furnished to do so, subject to
14
+ # the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be
17
+ # included in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+
27
+ RealEtc = Etc
28
+
29
+ module FakeEtc
30
+ @groups = {}
31
+
32
+ def self.activated?
33
+ @activated
34
+ end
35
+
36
+ def self.activate
37
+ @activated = true
38
+ Object.class_eval do
39
+ remove_const :Etc
40
+ const_set :Etc, FakeEtc
41
+ end
42
+ end
43
+
44
+ def self.deactivate
45
+ Object.class_eval do
46
+ remove_const :Etc
47
+ const_set :Etc, RealEtc
48
+ end
49
+ @activated = false
50
+ end
51
+
52
+ def self.with
53
+ if activated?
54
+ yield
55
+ else
56
+ begin
57
+ activate
58
+ yield
59
+ ensure
60
+ deactivate
61
+ end
62
+ end
63
+ end
64
+
65
+ def self.without
66
+ if !activated?
67
+ yield
68
+ else
69
+ begin
70
+ deactivate
71
+ yield
72
+ ensure
73
+ activate
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def FakeEtc(&block)
80
+ return ::FakeEtc unless block
81
+ ::FakeEtc.with(&block)
82
+ end
@@ -0,0 +1,46 @@
1
+ module FakeEtc
2
+ def self.add_groups(group_hash)
3
+ group_hash.each do |group_name, group_info|
4
+ group = Struct::Group.new(group_name,
5
+ 'x',
6
+ group_info[:gid],
7
+ group_info[:mem])
8
+ @groups[group_name] = group
9
+ end
10
+ end
11
+
12
+ def self.clear_groups
13
+ @groups = {}
14
+ end
15
+
16
+ def self.getgrnam(group_name)
17
+ group = @groups[group_name]
18
+ fail ArgumentError, "can't find group for #{group_name}" if group.nil?
19
+ group
20
+ end
21
+
22
+ def self.getgrgid(gid)
23
+ group = @groups.values.find { |g| g.gid == gid }
24
+ fail ArgumentError, "can't find group for #{gid}" if group.nil?
25
+ group
26
+ end
27
+
28
+ def self.getgrent
29
+ @group_ents ||= @groups.values
30
+ @group_ents.shift
31
+ end
32
+
33
+ def self.endgrent
34
+ @group_ents = nil
35
+ end
36
+
37
+ def self.group
38
+ return getgrent unless block_given?
39
+
40
+ @groups.values.each do |g|
41
+ yield g
42
+ end
43
+ endgrent
44
+ nil
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ module FakeEtc
2
+ def self.sysconfdir
3
+ RealEtc.sysconfdir
4
+ end
5
+
6
+ def self.systmpdir
7
+ RealEtc.systmpdir
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module FakeEtc
2
+ class << self
3
+ [:endpwent,
4
+ :getpwent,
5
+ :getpwnam,
6
+ :getpwuid,
7
+ :passwd,
8
+ :setpwent].each do |m|
9
+ define_method(m) do
10
+ fail NotImplementedError, "FakeEtc.#{m} not implemented yet"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module FakeEtc
2
+ VERSION = '0.1.0'
3
+ end
data/lib/fakeetc.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'fakeetc/base'
2
+ require 'fakeetc/groups'
3
+ require 'fakeetc/users'
4
+ require 'fakeetc/system'
5
+ require 'fakeetc/version'
6
+
7
+ module FakeEtc
8
+ end
@@ -0,0 +1,152 @@
1
+ require_relative 'spec_helper'
2
+
3
+ require 'fakeetc'
4
+
5
+ describe FakeEtc do
6
+ before(:each) do
7
+ @groups = {
8
+ 'cheeses' => { gid: 10, mem: ['red_leicester', 'tilsit'] },
9
+ 'parrots' => { gid: 20, mem: ['norwegian_blue', 'macaw'] }
10
+ }
11
+ FakeEtc.add_groups(@groups)
12
+ end
13
+
14
+ after(:each) do
15
+ FakeEtc.endgrent
16
+ FakeEtc.clear_groups
17
+ end
18
+
19
+ describe 'activate' do
20
+ it 'should replace Etc with FakeEtc' do
21
+ real_etc = Etc
22
+ fake_etc = FakeEtc
23
+ Etc.must_equal real_etc
24
+ FakeEtc.activate
25
+ Etc.must_equal fake_etc
26
+ FakeEtc.deactivate
27
+ Etc.must_equal real_etc
28
+ end
29
+ end
30
+
31
+ describe 'with' do
32
+ it 'should activate Etc in a block' do
33
+ real_etc = Etc
34
+ fake_etc = FakeEtc
35
+
36
+ FakeEtc.with do
37
+ Etc.must_equal fake_etc
38
+ end
39
+ Etc.must_equal real_etc
40
+ end
41
+ end
42
+
43
+ describe 'without' do
44
+ it 'should activate Etc in a block' do
45
+ real_etc = Etc
46
+ fake_etc = FakeEtc
47
+
48
+ FakeEtc.with do
49
+ Etc.must_equal fake_etc
50
+ FakeEtc.without do
51
+ Etc.must_equal real_etc
52
+ end
53
+ Etc.must_equal fake_etc
54
+ end
55
+ Etc.must_equal real_etc
56
+ end
57
+ end
58
+
59
+ describe 'getgrnam' do
60
+ it 'should find groups by name' do
61
+ cheese_group = FakeEtc.getgrnam('cheeses')
62
+ cheese_group.must_be_instance_of Struct::Group
63
+ cheese_group.gid.must_equal @groups['cheeses'][:gid]
64
+ cheese_group.mem.must_equal @groups['cheeses'][:mem]
65
+ end
66
+
67
+ it 'should raise exceptions for non-existent groups' do
68
+ group_name = 'not-a-group'
69
+ err = -> { FakeEtc.getgrnam(group_name) }.must_raise ArgumentError
70
+ err.message.must_match "can't find group for #{group_name}"
71
+ end
72
+ end
73
+
74
+ describe 'getgrgid' do
75
+ it 'should find groups by gid' do
76
+ parrot_group = FakeEtc.getgrgid(@groups['parrots'][:gid])
77
+ parrot_group.must_be_instance_of Struct::Group
78
+ parrot_group.gid.must_equal @groups['parrots'][:gid]
79
+ parrot_group.mem.must_equal @groups['parrots'][:mem]
80
+ end
81
+
82
+ it 'should raise exceptions for non-existent groups' do
83
+ gid = 247
84
+ err = -> { FakeEtc.getgrgid(gid) }.must_raise ArgumentError
85
+ err.message.must_match "can't find group for #{gid}"
86
+ end
87
+ end
88
+
89
+ describe 'getgrent' do
90
+ it 'should return all group entries in order' do
91
+ cheese_group = FakeEtc.getgrent
92
+ parrot_group = FakeEtc.getgrent
93
+ nil_group = FakeEtc.getgrent
94
+
95
+ cheese_group.name.must_equal 'cheeses'
96
+ parrot_group.name.must_equal 'parrots'
97
+ nil_group.must_be_nil
98
+ end
99
+ end
100
+
101
+ describe 'endgrent' do
102
+ it 'should reset group traversal' do
103
+ cheese_group_1 = FakeEtc.getgrent
104
+ FakeEtc.endgrent
105
+ cheese_group_2 = FakeEtc.getgrent
106
+ cheese_group_1.must_equal cheese_group_2
107
+ cheese_group_1.name.must_equal 'cheeses'
108
+ end
109
+ end
110
+
111
+ describe 'group' do
112
+ it 'should execute a given block for each group entry' do
113
+ groups = {}
114
+ FakeEtc.group { |g| groups[g.name] = { gid: g.gid, mem: g.mem } }
115
+ groups.must_equal @groups
116
+ end
117
+
118
+ it 'should behave like getgrent if there was no block given' do
119
+ cheese_group = FakeEtc.group
120
+ parrot_group = FakeEtc.group
121
+ nil_group = FakeEtc.group
122
+
123
+ cheese_group.name.must_equal 'cheeses'
124
+ parrot_group.name.must_equal 'parrots'
125
+ nil_group.must_be_nil
126
+ end
127
+ end
128
+
129
+ describe 'sysconfdir' do
130
+ it 'should call RealEtc.sysconfdir' do
131
+ FakeEtc.sysconfdir.must_equal RealEtc.sysconfdir
132
+ end
133
+ end
134
+
135
+ describe 'systmpdir' do
136
+ it 'should call RealEtc.systmpdir' do
137
+ FakeEtc.systmpdir.must_equal RealEtc.systmpdir
138
+ end
139
+ end
140
+ end
141
+
142
+ describe 'FakeEtc' do
143
+ it 'should activate FakeEtc and run a given block' do
144
+ real_etc = Etc
145
+ fake_etc = FakeEtc
146
+
147
+ FakeEtc do
148
+ Etc.must_equal fake_etc
149
+ end
150
+ Etc.must_equal real_etc
151
+ end
152
+ end
@@ -0,0 +1 @@
1
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fakeetc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Boehm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 10.4.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 10.4.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 5.5.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 5.5.1
41
+ description: |
42
+ FakeEtc is a fake Etc module for your tests.
43
+ email:
44
+ - sebastian@sometimesfood.org
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - Rakefile
50
+ - README.md
51
+ - LICENSE
52
+ - lib/fakeetc/base.rb
53
+ - lib/fakeetc/groups.rb
54
+ - lib/fakeetc/system.rb
55
+ - lib/fakeetc/users.rb
56
+ - lib/fakeetc/version.rb
57
+ - lib/fakeetc.rb
58
+ - spec/fakeetc_spec.rb
59
+ - spec/spec_helper.rb
60
+ homepage: https://github.com/sometimesfood/fakeetc
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.14
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A fake Etc module for your tests
84
+ test_files:
85
+ - spec/fakeetc_spec.rb
86
+ - spec/spec_helper.rb