Sublime Text and RubyTest running in a Docker container managed by Fig

Posted by ZedTuX 0n R00t on September 1, 2014

Presentation

Docker is a well known application which ease the creation of Linux containers.

Fig is a docker orchestration tool which helps you (a lot) in the management of containers.

Use case

Now let’s say you’re using Sublime Text as IDE and you’re writing tests using the RubyTest Sublime Text plugin (so for Ruby but should work the same for other languages) and you would like to use Docker in order to containerise your application.

To do so you’ve updated the RubyTest configuration in order to use fig run web ... .

The issue

You will have the issue that Sublime Text is not able to show you the output from the container accessed using Fig, in the console window, as Fig is streaming the output in a way that Sublime Text can’t handle.

A solution

I have written this small Ruby script which will catch the stream from Fig and print it to the stdout in a way that makes Sublime Text to work as expected.

  1. Create a file named fig-to-sublime in a folder included in your $PATH and copy/past the following:

    #!/usr/bin/env ruby
    require 'open3'
    # Avoid any kind of buffering. Flush immediately the output when calling `puts`
    $stdout.sync = true
    # Fig path
    file_path = `which fig`.strip
    # Fig command line building
    cmd = file_path.dup
    cmd << ' run web '
    cmd << ARGV.join(' ')
    # Stream the output from Fig
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
    while line = stdout.gets
    puts line
    end
    end
    view raw fig-to-sublime hosted with ❤ by GitHub
  2. Make it executable: chmod +x /path/to/fig-to-sublime
  3. Update the RubyTest.sublime-settings file as the following:
    {
    "erb_verify_command": "erb -xT - {file_name} | ruby -c",
    "ruby_verify_command": "ruby -c {file_name}",
    "run_ruby_unit_command": "ruby -Itest {relative_path}",
    "run_single_ruby_unit_command": "ruby -Itest {relative_path} -n '{test_name}'",
    "run_cucumber_command": "fig-to-sublime cucumber -r features {relative_path}",
    "run_single_cucumber_command": "fig-to-sublime cucumber -r features {relative_path} -l{line_number}",
    "run_rspec_command": "fig-to-sublime rspec {relative_path}",
    "run_single_rspec_command": "fig-to-sublime rspec {relative_path}:{line_number}",
    "ruby_unit_folder": "test",
    "ruby_cucumber_folder": "features",
    "ruby_rspec_folder": "spec",
    "check_for_rbenv": false,
    "check_for_rvm": false,
    "check_for_bundler": false,
    "check_for_spring": false,
    "ruby_use_scratch" : false,
    "save_on_run": false,
    "ignored_directories": [".git", "vendor", "tmp"],
    "hide_panel": false,
    "before_callback": "",
    "after_callback": "",
    "theme": "Packages/RubyTest/TestConsole.hidden-tmTheme",
    "syntax": "Packages/RubyTest/TestConsole.tmLanguage"
    }
  4. Run your tests :)