Skip to content

xli/rubyterminal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RubyTerminal prepares a ruby runtime environment for launching new ruby process instantly. For example, a big Rails project may need 5 seconds to launch a test process even you just run simple method test which just cost 0.1 second when you are doing TDD. RubyTerminal can help you get rid of this kind waiting.

Start RubyTerminal by the following command after installed rubyterminal gem:

rt <ruby_environment_script_file_path>

After RubyTerminal started, it will create a ‘.terminal.running’ file to mark it’s launching directory, the file would be deleted after RubyTerminal shutdown. Then RubyTerminal starts look for a file named ‘.terminal.input’ in it’s launching directory and read it’s content as command to execute in a forked process. The output of command execution would be put into another file named ‘.terminal.output’ in the same directory.

There are two model to create a ‘.terminal.input’ by RubyTerminal, one is a command could be used in a terminal called ‘ert’, another one is a ruby file could be loaded in a ruby script.

Put the following code in first line code would be run before your ruby code, e.g. first line in test_helper.rb:

require 'ert'

When RubyTerminal is detected by the script, it will create a ‘.terminal.input’ file with current script execution command and arguments, then wait for execution output and exit when the ‘.terminal.input’ file gone, which is deleted by the RubyTerminal usually after the execution finished.

If it does not find RubyTerminal, nothing happens and your ruby script would be run as normal.

ert has same usage with ruby expect you should not type in ‘ruby’, for example:

ert test/unit/blabla_test.rb -n test_method_blabla

And you also could run this command inside sub-directory of the RubyTerminal launching directory:

You RubyTerminal may launch at /User/xx/project
You could run ert at /User/xx/project/test/unit

When you use ‘ert’ in terminal, there is no need to add code:

require 'ert'

inside your project, otherwise you just use ‘ruby’ in terminal is OK.

My project got problem with spending too much time to wait for rails test process starts. My test just need 1 sec to run, but the process need 5 sec to launch. As many time as I run my test, as much time as I waste on waiting on launching the process.

To setup using RubyTerminal cleanly, I created a new ruby script called ‘test_environment.rb’ as my project’s RubyTerminal environment initializer inside the ‘test’ directory with the following code:

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

Then add the following code at the very top of ‘test/test_helper.rb’:

require 'rubygems' # unless you didn't install ruby_terminal from gem
# Define reload paths as you need here, default value is ['app', 'lib'], usually you don't
# need change the default value, and you must launch RubyTerminal in the root directory of
# your Rails project. We'll talk more about it at Limitation paragraph below.
# RELOAD_PATHS = ['app', 'lib']
require 'ert'

Then, start RubyTerminal in my project root directory with test/test_environment.rb while developing.

rt test/test_environment.rb

Done. Develop as usual.

For the test/test_environment.rb, RubyTerminal provides an option –rails_test to let you just do it with RubyTerminal without creating new file:

rt --rails_test

Code above will do exactly same with test_environment.rb above.

As RubyTerminal loads your ruby environment script as cache, and forks a new process to execute you ruby script, anything loaded by your ruby environment script would not be reloaded unless you do it in your ruby execution script. For example, if you load your ‘test/test_helper.rb’ in RubyTerminal ‘rt test/test_helper.rb’, and your test_helper.rb loads some ruby files, for example:

require 'messaging'

Then any change inside ‘messaging’ would not be reloaded in RubyTerminal. You’ll need to restart RubyTerminal or write some script in your test_helper.rb to reload ‘messaging’.

As Rails project would load all your models, controllers, helpers and libs as cache in test model, RubyTerminal will reload all files inside ‘RAILS_ROOT/app’ and ‘RAILS_ROOT/lib’ as default after you required ‘ert’ (Do same thing when you use ‘ert’ in terminal). You could define a const RELOAD_PATHS to replace the default reload paths.

If you want to take care of reloading some source files by yourself, you could take a look at ‘ruby_terminal/reloader’, which could help your reload source files easier.

Make sure you do reload source work or define RELOAD_PATHS for your Rails project before code:

require 'ert'
rails-test-serving

For showing me how to clean up rails project environment.

Author: Li Xiao <iam@li-xiao.com>

Requires: Ruby 1.8.6 or later

License: Copyright 2010 by Li Xiao.

Released under an MIT-LICENSE.  See the LICENSE file
included in the distribution.

This software is provided “as is” and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.

About

RubyTerminal arms to prepare a ruby runtime environment for a program to launch new ruby process instantly.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages