Skip to content

Instantly share code, notes, and snippets.

@moss
Created August 11, 2013 19:24
Show Gist options
  • Save moss/6206468 to your computer and use it in GitHub Desktop.
Save moss/6206468 to your computer and use it in GitHub Desktop.
#!/Users/moss/.rvm/rubies/ruby-1.9.2-p320/bin/ruby
class Runner
def initialize basepath, executor = ExecutionService.new
@basepath = basepath
@executor = executor
@cmds_term = TmuxSession.new '0', executor
@test_term = TmuxSession.new '1', executor
end
def run *args
e = exercise args
@cmds_term.enter "cd #{e.path}"
@cmds_term.enter "mvim --cmd \"set fullscreen\" -O #{e.test_name} #{e.source_name}"
@test_term.enter "cd #{e.path}"
@test_term.enter e.test_command
end
def exercise args
return SelfEdit.new if args[0] == 'selfedit'
Exercise.new @basepath, args[0], args[1]
end
end
class Language
def initialize name, test_extension, source_extension, test_command_prefix
@name = name
@test_extension = test_extension
@source_extension = source_extension
@test_command_prefix = test_command_prefix
end
def test_name exercise
exercise + @test_extension + @source_extension
end
def source_name exercise
exercise + @source_extension
end
def test_command exercise
@test_command_prefix + ' ' + test_name(exercise)
end
def to_s
@name
end
end
class Exercise
LANGUAGES = {
'javascript' => Language.new('javascript', '_test.spec', '.js', 'rerun --no-growl -cx -- jasmine-node --growl'),
'ruby' => Language.new('ruby', '_test', '.rb', 'rerun -cx'),
'clojure' => Language.new('clojure', '_test', '.clj', 'rerun -cx clj')
}
def initialize basepath, language, exercise
raise "I don't know #{language}." unless LANGUAGES.has_key? language
@basepath = basepath
@language = LANGUAGES[language]
@exercise = exercise
end
def path
"#{@basepath}/#{@language}/#{@exercise}"
end
[:test_name, :source_name, :test_command].each do |method|
define_method method do
@language.send method, @exercise
end
end
end
class SelfEdit
def path
'~/bin'
end
def test_name
source_name
end
def source_name
'exr.rb'
end
def test_command
"rerun -cx #{source_name} selftest"
end
end
class ExecutionService
def exec command
`#{command}`
end
end
class TmuxSession
def initialize name, executor
@name = name
@executor = executor
end
def enter command
@executor.exec %Q{tmux send-keys -t #{@name} "#{command}" C-m}
end
def type key
@executor.exec %Q{tmux send-keys -t 1 #{key}}
end
end
unless ARGV[0] == 'selftest'
Runner.new("~/src/exercism").run(*ARGV)
else
require 'minitest/autorun'
class MockExecutionService
include MiniTest::Assertions
attr_reader :history
def initialize
@history = []
end
def exec command
@history << command
end
def should_have_run command
assert history.include?(command), "Should have run:\n #{command}\nin history:\n #{history.join("\n ")}"
end
end
describe Runner do
before do
@executor = MockExecutionService.new
@runner = Runner.new '/exercism', @executor
end
it "should run Ruby exercises" do
@runner.run 'ruby', 'bob'
@executor.history.must_equal [
'tmux send-keys -t 0 "cd /exercism/ruby/bob" C-m',
'tmux send-keys -t 0 "mvim --cmd "set fullscreen" -O bob_test.rb bob.rb" C-m',
'tmux send-keys -t 1 "cd /exercism/ruby/bob" C-m',
'tmux send-keys -t 1 "rerun -cx bob_test.rb" C-m',
]
end
it "should run JavaScript exercises" do
@runner.run 'javascript', 'anagram'
@executor.should_have_run 'tmux send-keys -t 0 "cd /exercism/javascript/anagram" C-m'
@executor.should_have_run 'tmux send-keys -t 0 "mvim --cmd "set fullscreen" -O anagram_test.spec.js anagram.js" C-m'
@executor.should_have_run 'tmux send-keys -t 1 "cd /exercism/javascript/anagram" C-m'
@executor.should_have_run 'tmux send-keys -t 1 "rerun --no-growl -cx -- jasmine-node --growl anagram_test.spec.js" C-m'
end
it "should let you edit this script itself" do
@runner.run 'selfedit'
@executor.should_have_run 'tmux send-keys -t 0 "cd ~/bin" C-m'
@executor.should_have_run 'tmux send-keys -t 0 "mvim --cmd "set fullscreen" -O exr.rb exr.rb" C-m'
@executor.should_have_run 'tmux send-keys -t 1 "cd ~/bin" C-m'
@executor.should_have_run 'tmux send-keys -t 1 "rerun -cx exr.rb selftest" C-m'
end
end
describe Exercise do
it "should support Clojure" do
e = Exercise.new 'exercism', 'clojure', 'bob'
e.path.must_equal 'exercism/clojure/bob'
e.test_name.must_equal 'bob_test.clj'
e.test_command.must_equal 'rerun -cx clj bob_test.clj'
end
end
end
@moss
Copy link
Author

moss commented Aug 11, 2013

Quickly starts up environment for exercism.io exercises. YMMV. No, seriously, YM will V.

exr.rb javascript bob runs the Javascript version of the Bob exercise. Expects you to have two tmux panes open. Changes to the directory for the exercise in both panes. Starts autorunning the test script in one pane. Opens up split screen MacVim in fullscreen mode with the tests and the source file. Sometimes it guesses wrong about the name of the source file.

This has all kinds of hard-coded assumptions about my environment. It also only supports languages I've already started.

exr.rb selftest runs the tests.

exr.rb selfedit opens up the script in vim and starts selftest autorunning in the terminal.

Share and enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment