override_kernel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'app_mode', '~> 1.0.0'
4
+
5
+ group :rake do
6
+ gem 'rake_tasks', '~> 0.0.1'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ app_mode (1.0.1)
5
+ rake (0.8.7)
6
+ rake_tasks (0.0.4)
7
+ rake (~> 0.8.7)
8
+
9
+ PLATFORMS
10
+ ruby
11
+
12
+ DEPENDENCIES
13
+ app_mode (~> 1.0.0)
14
+ rake_tasks (~> 0.0.1)
data/README ADDED
@@ -0,0 +1,48 @@
1
+ == Welcome to OverrideKernel
2
+
3
+ OverrideKernel changes Kernel functions
4
+ from nasty wildebeasts into fluffy, harmless bunnies.
5
+ Currently, only the exit function is dealt with in such a manner.
6
+
7
+ == Getting Started
8
+
9
+ 1. Install OverrideKernel at the command prompt if you haven't yet:
10
+
11
+ gem install override_kernel
12
+
13
+ 2. Require the gem in your Gemfile:
14
+
15
+ gem 'override_kernel', '~> 0.0.1'
16
+
17
+ 3. Require the gem wherever you need to use it:
18
+
19
+ require 'override_kernel'
20
+
21
+ == Usage
22
+
23
+ Simply use the following call to start brushing the exit function off like it's
24
+ a harmless pest:
25
+
26
+ start_override_exit
27
+
28
+ This line will gladly put it back in your way:
29
+
30
+ stop_override_exit
31
+
32
+ You can check the state of the application by
33
+ checking OverrideKernel.override_exit.
34
+
35
+ == Notes
36
+
37
+ * Please note that it is up to you to 'reset' the application's state should
38
+ it be found to be 'dead'. The following line will do the deed:
39
+
40
+ OverrideKernel::AppState.state = :alive
41
+
42
+ == Additional Documentation
43
+
44
+ rake rdoc:app
45
+
46
+ == License
47
+
48
+ OverrideKernel is released under the GPLv3 license.
@@ -0,0 +1,45 @@
1
+ # This file contains a monkey patch of Kernel functions for use during testing.
2
+
3
+ #--
4
+ ################################################################################
5
+ # Copyright (C) 2011 Travis Herrick #
6
+ ################################################################################
7
+ # #
8
+ # \v^V,^!v\^/ #
9
+ # ~% %~ #
10
+ # { _ _ } #
11
+ # ( * - ) #
12
+ # | / | #
13
+ # \ _, / #
14
+ # \__.__/ #
15
+ # #
16
+ ################################################################################
17
+ # This program is free software: you can redistribute it #
18
+ # and/or modify it under the terms of the GNU General Public License #
19
+ # as published by the Free Software Foundation, #
20
+ # either version 3 of the License, or (at your option) any later version. #
21
+ # #
22
+ # Commercial licensing may be available for a fee under a different license. #
23
+ ################################################################################
24
+ # This program is distributed in the hope that it will be useful, #
25
+ # but WITHOUT ANY WARRANTY; #
26
+ # without even the implied warranty of MERCHANTABILITY #
27
+ # or FITNESS FOR A PARTICULAR PURPOSE. #
28
+ # See the GNU General Public License for more details. #
29
+ # #
30
+ # You should have received a copy of the GNU General Public License #
31
+ # along with this program. If not, see <http://www.gnu.org/licenses/>. #
32
+ ################################################################################
33
+ #++
34
+
35
+ # Monkey patch Kernel functions for testing.
36
+ module Kernel
37
+ # Patch exit so that it sets a flag rather than ends the application.
38
+ # (A call to exit will cause testing to stop otherwise).
39
+ alias :old_exit :exit
40
+ def exit(status = true)
41
+ old_exit(status) unless OverrideKernel.override_exit
42
+ OverrideKernel::AppState.state = :dead
43
+ return status
44
+ end
45
+ end
@@ -0,0 +1,79 @@
1
+ # This file contains constants that are used
2
+ # to support overriding Kernel functionality.
3
+
4
+ #--
5
+ ################################################################################
6
+ # Copyright (C) 2011 Travis Herrick #
7
+ ################################################################################
8
+ # #
9
+ # \v^V,^!v\^/ #
10
+ # ~% %~ #
11
+ # { _ _ } #
12
+ # ( * - ) #
13
+ # | / | #
14
+ # \ _, / #
15
+ # \__.__/ #
16
+ # #
17
+ ################################################################################
18
+ # This program is free software: you can redistribute it #
19
+ # and/or modify it under the terms of the GNU General Public License #
20
+ # as published by the Free Software Foundation, #
21
+ # either version 3 of the License, or (at your option) any later version. #
22
+ # #
23
+ # Commercial licensing may be available for a fee under a different license. #
24
+ ################################################################################
25
+ # This program is distributed in the hope that it will be useful, #
26
+ # but WITHOUT ANY WARRANTY; #
27
+ # without even the implied warranty of MERCHANTABILITY #
28
+ # or FITNESS FOR A PARTICULAR PURPOSE. #
29
+ # See the GNU General Public License for more details. #
30
+ # #
31
+ # You should have received a copy of the GNU General Public License #
32
+ # along with this program. If not, see <http://www.gnu.org/licenses/>. #
33
+ ################################################################################
34
+ #++
35
+
36
+ module OverrideKernel
37
+
38
+ # Contains a list of methods that will be dynamically handled by the module.
39
+ # The following methods will be created:
40
+ #
41
+ # :start_#{method_name} #=> @method_name = true
42
+ # :stop_#{method_name} #=> @method_name = false
43
+ # :#{method_name} #=> Returns the value of @method_name
44
+ METHOD_LIST = [
45
+ 'override_exit',
46
+ ]
47
+
48
+ # This constant is used to indicate whether the application would have
49
+ # been halted by Kernel functions.
50
+ AppState = StateManager.new(:alive, [:alive, :dead])
51
+
52
+ ############################################################################
53
+ module_function
54
+ ############################################################################
55
+
56
+ def method_missing(method, *args, &block)
57
+ if METHOD_LIST.include?(method.to_s.sub(/^(start|stop)_/, ''))
58
+ var = '@' + method.to_s.sub(/^(start|stop)_/, '')
59
+ case method.to_s
60
+ when /^start/
61
+ send :instance_variable_set, var.to_sym, true
62
+ when /^stop/
63
+ send :instance_variable_set, var.to_sym, false
64
+ when var[1..-1]
65
+ send :instance_variable_get, var.to_sym
66
+ end
67
+ else
68
+ super
69
+ end
70
+ end
71
+
72
+ def respond_to_missing?(method, include_private)
73
+ if METHOD_LIST.include?(method.to_s.sub(/^(start|stop)_/, ''))
74
+ return true
75
+ else
76
+ super
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,6 @@
1
+ gem_name = File.basename(__FILE__, '.rb')
2
+
3
+ require 'app_mode'
4
+
5
+ require_relative File.join(gem_name, gem_name)
6
+ require_relative File.join(gem_name, 'exit')
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'override_kernel'
3
+ s.version = '0.0.1'
4
+
5
+ s.summary = 'Overrides specific Kernel functions.'
6
+ s.description = %Q{
7
+ OverrideKernel changes Kernel functions
8
+ from nasty wildebeasts into fluffy, harmless bunnies.
9
+ Currently, only the exit function is dealt with in such a manner.
10
+ }.strip
11
+
12
+ s.author = 'Travis Herrick'
13
+ s.email = 'tthetoad@gmail.com'
14
+ s.homepage = 'http://www.bitbucket.org/ToadJamb/gems_override_kernel'
15
+
16
+ s.license = 'GPLv3'
17
+
18
+ s.extra_rdoc_files << 'README'
19
+
20
+ s.require_paths = ['lib']
21
+ s.files = Dir['lib/**/*.rb', '*']
22
+ s.test_files = Dir['test/**/*.rb']
23
+
24
+ s.add_dependency 'app_mode', '~> 1.0.0'
25
+
26
+ s.add_development_dependency 'rake_tasks', '~> 0.0.1'
27
+
28
+ s.has_rdoc = true
29
+ end
data/rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler.require :rake
data/test/exit_test.rb ADDED
@@ -0,0 +1,108 @@
1
+ #--
2
+ ################################################################################
3
+ # Copyright (C) 2011 Travis Herrick #
4
+ ################################################################################
5
+ # #
6
+ # \v^V,^!v\^/ #
7
+ # ~% %~ #
8
+ # { _ _ } #
9
+ # ( * - ) #
10
+ # | / | #
11
+ # \ _, / #
12
+ # \__.__/ #
13
+ # #
14
+ ################################################################################
15
+ # This program is free software: you can redistribute it #
16
+ # and/or modify it under the terms of the GNU General Public License #
17
+ # as published by the Free Software Foundation, #
18
+ # either version 3 of the License, or (at your option) any later version. #
19
+ # #
20
+ # Commercial licensing may be available for a fee under a different license. #
21
+ ################################################################################
22
+ # This program is distributed in the hope that it will be useful, #
23
+ # but WITHOUT ANY WARRANTY; #
24
+ # without even the implied warranty of MERCHANTABILITY #
25
+ # or FITNESS FOR A PARTICULAR PURPOSE. #
26
+ # See the GNU General Public License for more details. #
27
+ # #
28
+ # You should have received a copy of the GNU General Public License #
29
+ # along with this program. If not, see <http://www.gnu.org/licenses/>. #
30
+ ################################################################################
31
+ #++
32
+
33
+ require_relative 'require'
34
+
35
+ class ExitTest < Test::Unit::TestCase
36
+
37
+ def setup
38
+ override_exit(true)
39
+ reset_app_state
40
+ end
41
+
42
+ def teardown
43
+ override_exit(false)
44
+ end
45
+
46
+ def test_without_override
47
+ override_exit(false)
48
+ assert_raise(SystemExit) { exit }
49
+ end
50
+
51
+ def test_basic_exit_call
52
+ assert_alive
53
+ assert_nothing_raised { exit }
54
+ assert_dead
55
+ end
56
+
57
+ def test_exit_parameter
58
+ assert_alive
59
+ assert_nothing_raised { exit(-1) }
60
+ assert_dead
61
+ end
62
+
63
+ def test_exit_return_value
64
+ out = nil
65
+ assert_alive
66
+ assert_nothing_raised { out = exit }
67
+ assert_dead
68
+ assert_equal true, out
69
+
70
+ assert_alive
71
+ assert_nothing_raised { out = exit(-1) }
72
+ assert_dead
73
+ assert_equal(-1, out)
74
+
75
+ assert_alive
76
+ assert_nothing_raised { out = exit('blah') }
77
+ assert_dead
78
+ assert_equal 'blah', out
79
+ end
80
+
81
+ ############################################################################
82
+ private
83
+ ############################################################################
84
+
85
+ def override_exit(value)
86
+ if value
87
+ OverrideKernel.start_override_exit
88
+ else
89
+ OverrideKernel.stop_override_exit
90
+ end
91
+ end
92
+
93
+ def reset_app_state
94
+ OverrideKernel::AppState.state = :alive
95
+ end
96
+
97
+ def assert_alive
98
+ alive = OverrideKernel::AppState.alive
99
+ reset_app_state unless alive
100
+ assert alive, 'The application is not running when it was expected to be.'
101
+ end
102
+
103
+ def assert_dead
104
+ dead = OverrideKernel::AppState.dead
105
+ reset_app_state if dead
106
+ assert dead, 'The application is running when it was not expected to be.'
107
+ end
108
+ end
@@ -0,0 +1,53 @@
1
+ #--
2
+ ################################################################################
3
+ # Copyright (C) 2011 Travis Herrick #
4
+ ################################################################################
5
+ # #
6
+ # \v^V,^!v\^/ #
7
+ # ~% %~ #
8
+ # { _ _ } #
9
+ # ( * - ) #
10
+ # | / | #
11
+ # \ _, / #
12
+ # \__.__/ #
13
+ # #
14
+ ################################################################################
15
+ # This program is free software: you can redistribute it #
16
+ # and/or modify it under the terms of the GNU General Public License #
17
+ # as published by the Free Software Foundation, #
18
+ # either version 3 of the License, or (at your option) any later version. #
19
+ # #
20
+ # Commercial licensing may be available for a fee under a different license. #
21
+ ################################################################################
22
+ # This program is distributed in the hope that it will be useful, #
23
+ # but WITHOUT ANY WARRANTY; #
24
+ # without even the implied warranty of MERCHANTABILITY #
25
+ # or FITNESS FOR A PARTICULAR PURPOSE. #
26
+ # See the GNU General Public License for more details. #
27
+ # #
28
+ # You should have received a copy of the GNU General Public License #
29
+ # along with this program. If not, see <http://www.gnu.org/licenses/>. #
30
+ ################################################################################
31
+ #++
32
+
33
+ require_relative 'require'
34
+
35
+ class OverrideKernelTest < Test::Unit::TestCase
36
+ METHOD_LIST = [
37
+ :override_exit,
38
+ ]
39
+
40
+ def test_all_methods
41
+ METHOD_LIST.each do |method|
42
+ {
43
+ "start_#{method}".to_sym => true,
44
+ "stop_#{method}".to_sym => false,
45
+ }.each do |method_name, value|
46
+ assert_respond_to OverrideKernel, method_name
47
+ assert_nothing_raised { OverrideKernel.send method_name }
48
+ assert_respond_to OverrideKernel, method
49
+ assert_equal value, OverrideKernel.send(method)
50
+ end
51
+ end
52
+ end
53
+ end
data/test/require.rb ADDED
@@ -0,0 +1,5 @@
1
+ Bundler.require :test
2
+
3
+ require 'test/unit'
4
+
5
+ require_relative '../lib/override_kernel'
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: override_kernel
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Travis Herrick
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-08-10 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: app_mode
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake_tasks
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 0
45
+ - 1
46
+ version: 0.0.1
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ description: |-
51
+ OverrideKernel changes Kernel functions
52
+ from nasty wildebeasts into fluffy, harmless bunnies.
53
+ Currently, only the exit function is dealt with in such a manner.
54
+ email: tthetoad@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README
61
+ files:
62
+ - lib/override_kernel/override_kernel.rb
63
+ - lib/override_kernel/exit.rb
64
+ - lib/override_kernel.rb
65
+ - Gemfile.lock
66
+ - override_kernel.gemspec
67
+ - Gemfile
68
+ - rakefile
69
+ - README
70
+ - test/override_kernel_test.rb
71
+ - test/require.rb
72
+ - test/exit_test.rb
73
+ has_rdoc: true
74
+ homepage: http://www.bitbucket.org/ToadJamb/gems_override_kernel
75
+ licenses:
76
+ - GPLv3
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: -3169159202290470423
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Overrides specific Kernel functions.
106
+ test_files:
107
+ - test/override_kernel_test.rb
108
+ - test/require.rb
109
+ - test/exit_test.rb