monkeybox 0.0.1
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.
- data/monkeybox.rb +78 -0
- data/test_monkeybox.rb +52 -0
- metadata +68 -0
    
        data/monkeybox.rb
    ADDED
    
    | @@ -0,0 +1,78 @@ | |
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'open4'
         | 
| 3 | 
            +
            require 'quick_attr'
         | 
| 4 | 
            +
            require 'std_capture'
         | 
| 5 | 
            +
            #require 'sandbox'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            #what happens if you are running a sandbox, but you give it something with a syntax error?
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            class MonkeyBox
         | 
| 10 | 
            +
            	extend QuickAttr
         | 
| 11 | 
            +
            	quick_attr :code,:output,:returned,:error
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            	def wrap monkeys
         | 
| 14 | 
            +
            		%{
         | 
| 15 | 
            +
            			require 'std_capture'
         | 
| 16 | 
            +
            			require 'yaml'
         | 
| 17 | 
            +
            			out = nil
         | 
| 18 | 
            +
            			err = nil
         | 
| 19 | 
            +
            			ret  = nil
         | 
| 20 | 
            +
            			out = StdCapture.capture {
         | 
| 21 | 
            +
            				begin
         | 
| 22 | 
            +
            					ret = eval {#{code}}
         | 
| 23 | 
            +
            				rescue Exception => e
         | 
| 24 | 
            +
            					err = exception
         | 
| 25 | 
            +
            				end
         | 
| 26 | 
            +
            			}
         | 
| 27 | 
            +
            			puts {:output => out,
         | 
| 28 | 
            +
            				:returned => ret,		
         | 
| 29 | 
            +
            				:error => {:class => err.class,
         | 
| 30 | 
            +
            					:message => err.message,
         | 
| 31 | 
            +
            					:backtrace => err.backtrace}
         | 
| 32 | 
            +
            			}				
         | 
| 33 | 
            +
            		}
         | 
| 34 | 
            +
            	end
         | 
| 35 | 
            +
            	def upwrap (output)
         | 
| 36 | 
            +
            		result = YAML::load(output)
         | 
| 37 | 
            +
            	end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            	def run
         | 
| 40 | 
            +
            	
         | 
| 41 | 
            +
            	  Open4::popen4("ruby #{__FILE__}") do |pid, stdin, stdout, stderr|
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            			#puts code
         | 
| 44 | 
            +
            			stdin.puts code
         | 
| 45 | 
            +
                  	stdin.close
         | 
| 46 | 
            +
                  	yaml = stdout.read.strip
         | 
| 47 | 
            +
                  	#puts "YAML: <#{yaml}>"
         | 
| 48 | 
            +
                  	report = YAML::load(yaml) || {} #need to catch puts, because the test prints a message if the test fails.
         | 
| 49 | 
            +
             			output report[:output]
         | 
| 50 | 
            +
             			returned report[:returned]
         | 
| 51 | 
            +
             			error report[:error]
         | 
| 52 | 
            +
            			
         | 
| 53 | 
            +
             			err = stderr.read
         | 
| 54 | 
            +
            			raise(err) if (err != "" )
         | 
| 55 | 
            +
             | 
| 56 | 
            +
            		#you have to check if there was an error your self. 
         | 
| 57 | 
            +
             | 
| 58 | 
            +
            		end
         | 
| 59 | 
            +
            		self
         | 
| 60 | 
            +
            	end
         | 
| 61 | 
            +
            end
         | 
| 62 | 
            +
            	if __FILE__ == $0 then
         | 
| 63 | 
            +
            		returned = nil
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            		error = nil
         | 
| 66 | 
            +
            		output = StdCapture.capture {
         | 
| 67 | 
            +
            		code = $stdin.read
         | 
| 68 | 
            +
            			begin
         | 
| 69 | 
            +
            				returned = eval(code)
         | 
| 70 | 
            +
            			rescue Exception => e
         | 
| 71 | 
            +
            				error = e
         | 
| 72 | 
            +
            			end
         | 
| 73 | 
            +
            		}
         | 
| 74 | 
            +
            		
         | 
| 75 | 
            +
            		puts ({:returned => returned,:output => output, :error => error, :error_trace => (error ? error.backtrace : nil) }.to_yaml)
         | 
| 76 | 
            +
            		puts "end".to_yaml #there is a problem with returning null, stdout seems to drop the last new line, so need something acceptable after.
         | 
| 77 | 
            +
            	end
         | 
| 78 | 
            +
             | 
    
        data/test_monkeybox.rb
    ADDED
    
    | @@ -0,0 +1,52 @@ | |
| 1 | 
            +
            require 'test/unit'
         | 
| 2 | 
            +
            require 'monkeybox'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class TestMonkeyBox < Test::Unit::TestCase
         | 
| 5 | 
            +
            	def test_simple
         | 
| 6 | 
            +
            		sb = MonkeyBox.new.code "print \"hello\"; true"
         | 
| 7 | 
            +
            		sb.run
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            		assert_equal "hello", sb.output
         | 
| 10 | 
            +
            	
         | 
| 11 | 
            +
            	end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            	def test_returned
         | 
| 14 | 
            +
            		sb = MonkeyBox.new.code("print \"hello\"; true").run
         | 
| 15 | 
            +
            		assert sb.returned
         | 
| 16 | 
            +
            		sb2 = MonkeyBox.new.code("false").run
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            		assert !sb2.returned
         | 
| 19 | 
            +
            	end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            	def test_constant
         | 
| 22 | 
            +
            		sb = MonkeyBox.new.code "class NewConstant; end; nil"
         | 
| 23 | 
            +
            		sb.run
         | 
| 24 | 
            +
            		assert_equal "", sb.output
         | 
| 25 | 
            +
            		assert_equal nil, sb.returned	
         | 
| 26 | 
            +
            		begin
         | 
| 27 | 
            +
            			eval "MonkeyBox::NewConstant"
         | 
| 28 | 
            +
            			raise "expected NameError"
         | 
| 29 | 
            +
            		rescue NameError => n
         | 
| 30 | 
            +
            		end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            		begin
         | 
| 33 | 
            +
            			eval "NewConstant"
         | 
| 34 | 
            +
            			raise "expected NameError"
         | 
| 35 | 
            +
            		rescue NameError => n
         | 
| 36 | 
            +
            		end
         | 
| 37 | 
            +
            	
         | 
| 38 | 
            +
            	end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            	def test_error
         | 
| 41 | 
            +
            		sb = MonkeyBox.new.code "raise \"an exception\""
         | 
| 42 | 
            +
            		sb.run
         | 
| 43 | 
            +
            		#puts sb.output
         | 
| 44 | 
            +
            		assert_equal "", sb.output
         | 
| 45 | 
            +
            		assert sb.error
         | 
| 46 | 
            +
            		assert sb.error.is_a?(Exception)
         | 
| 47 | 
            +
            		
         | 
| 48 | 
            +
            	end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
            end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
             | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,68 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            name: monkeybox
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              hash: 29
         | 
| 5 | 
            +
              prerelease: false
         | 
| 6 | 
            +
              segments: 
         | 
| 7 | 
            +
              - 0
         | 
| 8 | 
            +
              - 0
         | 
| 9 | 
            +
              - 1
         | 
| 10 | 
            +
              version: 0.0.1
         | 
| 11 | 
            +
            platform: ruby
         | 
| 12 | 
            +
            authors: 
         | 
| 13 | 
            +
            - Dominic Tarr
         | 
| 14 | 
            +
            autorequire: 
         | 
| 15 | 
            +
            bindir: bin
         | 
| 16 | 
            +
            cert_chain: []
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            date: 2010-09-10 00:00:00 +12:00
         | 
| 19 | 
            +
            default_executable: 
         | 
| 20 | 
            +
            dependencies: []
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            description: "\t\tmonkeybox runs ruby code in another ruby process.\n"
         | 
| 23 | 
            +
            email: dominic.tarr@gmail.com
         | 
| 24 | 
            +
            executables: []
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            extensions: []
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            extra_rdoc_files: []
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            files: 
         | 
| 31 | 
            +
            - monkeybox.rb
         | 
| 32 | 
            +
            - test_monkeybox.rb
         | 
| 33 | 
            +
            has_rdoc: true
         | 
| 34 | 
            +
            homepage: http://github.com/dominictarr/monkeybox
         | 
| 35 | 
            +
            licenses: []
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            post_install_message: 
         | 
| 38 | 
            +
            rdoc_options: []
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            require_paths: 
         | 
| 41 | 
            +
            - ./
         | 
| 42 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 43 | 
            +
              none: false
         | 
| 44 | 
            +
              requirements: 
         | 
| 45 | 
            +
              - - ">="
         | 
| 46 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 47 | 
            +
                  hash: 3
         | 
| 48 | 
            +
                  segments: 
         | 
| 49 | 
            +
                  - 0
         | 
| 50 | 
            +
                  version: "0"
         | 
| 51 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 52 | 
            +
              none: false
         | 
| 53 | 
            +
              requirements: 
         | 
| 54 | 
            +
              - - ">="
         | 
| 55 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 56 | 
            +
                  hash: 3
         | 
| 57 | 
            +
                  segments: 
         | 
| 58 | 
            +
                  - 0
         | 
| 59 | 
            +
                  version: "0"
         | 
| 60 | 
            +
            requirements: []
         | 
| 61 | 
            +
             | 
| 62 | 
            +
            rubyforge_project: 
         | 
| 63 | 
            +
            rubygems_version: 1.3.7
         | 
| 64 | 
            +
            signing_key: 
         | 
| 65 | 
            +
            specification_version: 3
         | 
| 66 | 
            +
            summary: run sandbox ruby from monkeypatching, by running the code in another ruby process.
         | 
| 67 | 
            +
            test_files: []
         | 
| 68 | 
            +
             |