Peter's Blog

Redefining the Impossible

Items filed under netbeans


Netbeans and cygwin ruby don't have a happy relationship. To use the cygwin ruby interpreter it is necessary to launch Netbeans (6.1) with the arguments:

-J-Druby.no.sync-stdio=true

as otherwise Netbeans will pass a windows-style file name to the cygwin ruby that the latter cannot recognise (C:\Projects\Blah instead of /cygdrive/c/Projects/Blah). Unfortunately the ruby interpreter configuration in Netbeans tries to be clever and insists you point it to a ruby executable, you can't give it a batch file that will hack things into working shape.

rspec on cygwin has some similar problems. Here are some hacks to the rspec plugin to make it work from netbeans:

In vendor/plugins/rspec/bin/bin, change:

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))

to

$LOAD_PATH.unshift(File.dirname(__FILE__).sub( /C:\//, '/cygdrive/c/') + "/../lib")

In vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb, change:

load file

to

load File.expand_path(file).gsub(/C:/, '/cygdrive/c')

Tip: to diagnose problems with 'require' not finding files, put this:

p $:

before the failing line to see what directories are in the load path. If you see 'C:\' and it's cygwin then fix it.

Unfortunately it takes a good 20 seconds for cygwin/rspec to run a trivial test script. The rspec server starts and runs but netbeans/rspec seem reluctant to use it (it runs tests ok from the command line).

Why am I still bothering with cygwin? Because I still want to test gcc C code by compiling it into ruby extensions and using ruby test codes (rspec or unit testing).

In tracing these problems I came across more reasons that Netbeans is worth the hassle:

  • ctrl-tab to switch to last open tab. ctrl and hold tab brings up a handy little context menu to choose a tab from (kinda like windows task switching with tab).
  • press ctrl and mouse over an identifier to bring up rdoc info about it. Ctrl-click to go to it's definition.
  • alt-b to go back to where you came from.
  • click on an 'end' and the matching begin/do/if is highlighted and vice versa, which is damn useful.
  • select an identifier and all matching identifiers in your file are automatically highlighted.

Filed under: cygwin netbeans rspec ruby

Add a comment

I've been trying out rspec, a new testing framework for ruby that allows one to better follow the principles of Behaviour Driven Design (BDD). BDD is a step on from Test Driven Design (TDD) approach exemplified in the standard ruby unit test framework. TDD is the process of starting ones development by writing the tests that your new module should pass. As you write each test you then implement the code to pass it and hence, voila, you have nicely designed and robust software. In BDD you approach testing the code from how it is supposed to behave. When liasing with clients this is a better approach as everyone can talk in the same language, essentially in terms of requirements so one can try to get less bogged down in implementation details.

Here are some rspec tests for a class I have been working on:

   1  describe ReportHelper do
   2    before(:each) do
   3      #
   4      # Create a new clean Report class for each individual test
   5      #
   6      @oReport = ReportHelper::Report.new
   7    end
   8  
   9    it "should allow a column to be added" do
  10      @oReport.AddColumn( 'First Column')
  11    end
  12  
  13    it "should allow multiple columns to be added" do
  14      @oReport.AddColumn( 'First Column')
  15      @oReport.AddColumn( 'Second Column')
  16    end
  17  
  18    it "should allow rows to be added to the report" do
  19      @oReport.AddColumn( 'First Column')
  20      @oReport.AddColumn( 'Second Column')
  21      @oReport.AddRow do
  22        # pass
  23      end
  24    end
  25  
  26    it "should only allow rows to be added if columns exist" do
  27      lambda{ @oReport.AddRow}.should raise_error(RuntimeError, 'No columns defined')
  28    end
  29  
  30    it "should return simple cell values" do
  31      @oReport.AddColumn( 'First Column')
  32      @oReport.AddColumn( 'Second Column')
  33  
  34      @oReport.AddRow do |oRow|
  35        oRow[ 'First Column'] = 98
  36        oRow[ 'Second Column'] = 198
  37      end
  38  
  39      @oReport[0]['First Column'].should == 98
  40      @oReport[0]['Second Column'].should == 198
  41    end
  42  
  43  end

Each test has a descriptive string that can be interpreted in two ways: as a test that must be passed or as a requirement for the software. The rspec toolset includes something that will pull out all these strings and give a summary of what testing has been done/what the requirements for the software are. If an auditor asks how the software is tested, bang, there is a full report of the tests. At a practical level (i.e. makes life easier for me) I prefer this to the ruby unit test system where one has to think up meaningful_method_names for each test. The strings are easier to write and read and reduce the need for comments.

Example report:

ReportHelper

- should allow a column to be added
- should allow multiple columns to be added
- should allow rows to be added to the report
- should only allow rows to be added if columns exist
- should return simple cell values

Rspec contains sweeter syntactic sugar than the ruby unit test module. For example, rspec overloads every object in the system with a 'should' method for doing the equivalent of assert tests:

    @oReport[0]['First Column'].should == 98

Compare this to test/unit:

    assert_equal 98, @oReport[0]['First Column']

apart from more typing, assert_equal has the expectation/actual things in what is intuitively to me the wrong order and I'm always having to correct these lines because the error messages have the terms swapped.

Rspec can test assertions more cleanly:

    lambda{ @oReport.AddRow}.should raise_error(RuntimeError, 'No columns defined')

although it's a pity that the lambda is necessary (and probably no 1 noob trap) but with test/unit the following always seemed like a lot of typing:

    oError = assert_raises RuntimeError do
      @oReport.AddRow
    end
    assert_equal 'No columns defined', oError.message

rspec is nicely integrated into rails, plugins being available to generate rspec tested controllers and models and suchlike, rake tasks to generate documents (woohoo) etc. It is integrated into netbeans and runs ok on Windows although when the tests are run (CTRL-F6) there is a tedious few seconds wasted in startup time. It is possible to run an rspec server in the background to eliminate this startup but it doesn't work on windows native ruby, it gives an error lamenting Microsofts glorious omission of a fork api.

There is much more to Rspec than this. One aim seems to be to write psuedo testing languages and have clients use these to write stories about what the software will do, these stories turning into a test framework. As the slashdot tag goes, 'goodluckwiththat'. At a practical level, I'm liking rspec even if only as a unit test framework with added syntactic sugar.


Add a comment

I think I was aware of the existance of something called Netbeans in the Java world but for some reason I had always assumed it was some kind of object packaging thing, like Java Beans.

I had been using E Texteditor for about three months and a few things were niggling at me about it:

  • My project has many files and I have many files open at once. The files all open in tabs and soon I have too many tabs to be shown in a horizontal bar. With E I end up having to horizontal scroll all the time to get to the tab for the file that I have selected: it doesn't automatically scroll to that tab. As I say, it's a niggle but I've done it too many times.
  • It comes with lots of textmate bundles and more can be downloaded but first you need spend time working out what they are supposed to do and how you are supposed to use them, then you have to determine just how broken the script is after its port from an OS X environment and whether you can be bothered to fix it.
  • Unlike every other editor on the windows platform the INS button doesn't toggle into overwrite mode.
  • I became familiar with it and started to get bored. It lacked depth apart from writing my own textmate-like stuff which I don't have time to do.

The one thing that kept me using E was the ruby bundle that dumped unit test output into a window with stack traces so I could easily click on a line where an error occured and open it in the editor.

Still I started researching what else was available. I use linux at home all the time now and I wanted a cross platform editor/ide.

First I tried Jedit which is another editor written in Java. While it has many features it had a number of it's own niggles:

  • the way it displays text is just ugly. It has trouble rendering fonts in anything like a crisp and clear way. There are settings for subpixel antialiasing or whatever it is but I couldn't get it working nicely under Windows. It was better under Ubuntu.
  • (silly niggle) instead of a single tree the file manager has two selection controls, a tree for directory and another for the files in the directory. This means mucho switching between the two controls and just reminds me of Windows 3.1 file open dialogs. The project manager is better and has the traditional single file selection tree holding both files and directories.
  • the mechanism for running a command and capturing the errors wasn't set up for ruby and I didn't feel inclined to develop a regular expression to parse them. Laziness on my part.

The font thing put me off Jedit the most, it's just awful.

Then I found Netbeans and discovered that despite the misleading name it's an editor/IDE. It comes from the java world and is written in java but it has plenty of modules available to support C/C++ and Ruby/Rails. And the Rails support actually works. I'll emphasise that again, it works. Unlike the fragile stuff in Eclipse and the half-hearted E texteditor bundle ports, the rails features work and are usable: this is an environment that I can use without having to keep a command line open in the background.

The rails support gives me:

  • can run and debug a rails environment from the IDE. If I'm editing a file for a rails view, for example, I can press CTRL-R (not the default) and it will start a rails server and open firefox and the URL that relates to that view! The way it runs the server from the IDE is robust and I can even hibernate my laptop, resume and continue running it. That's robust.
  • When I say debug I mean the debugger works. It's not an interactive debugger in that I haven't found a way to evaluate expressions from a breakpoint but just being able to breakpoint, single step and watch variables is something.
  • run a rails console from the IDE. This is nice except the up/down keys don't work for command history which is a major downer.
  • run migrations
  • The project tree for a rails application displays the project directories in a slightly flatter way which makes navigating the project much easier. For example, the app directory is suppressed. It's still in the file system but the project doesn't bother to display it.
  • Pressing ctrl-shift-a in a view will take you to the controller for that view.
  • Pressing ctrl-shift-t in a controller will take you to the tests for that controller.
  • There is a built-in database client that lets me browse the mysql databases and run SQL statements.
  • There's a built-in gem administrator that shows me what gems I have installed and, interestingly, lists what more are available. This works better under windows than linux as under the latter it is unable to run sudo and install new gems.
  • Administer the rails plugins in a project.

General Features:

  • Very nice project-search facility, best I've used.
  • Refactoring that works! Want to change the name of something, run the refactoring, get a preview of what changes it will make, then apply the changes. I've never dared to try refactoring before but I could use this.
  • Runs just as nice on windows and ubuntu.

Netbeans annoyances:

  • Initially Netbeans wouldn't run, it would just lock up on opening. I tried installing a development version, googling etc but no joy. I was determined to get it going and ultimately I downloaded the Process Monitor to see what it was trying to do. It turned out that it was trying to use the cygwin version of subversion for something or other (who knows what) and this was locking up. I resolved the situation by simply renaming the cygwin svn.exe to svnhidden.exe and netbeans could start. Once it was sentient enough to open a settings window I installed a windows command line version of subversion for it to use. I'm going to add some keywords here to help other googlers find this fix: netbeans hang lockup freeze dammit ffs.
  • Double-clicking in the project tree doesn't always open a file. Often have to right click and click on open.
  • Can't seem to use save-as to copy a file. Have to go through irritating 'new file' wizard and copy/paste.
  • No apparent python support, not that I care much these days.
  • Not sure how well it would support a mixed ruby/c development project.

Conclusion: Netbeans is the nicest rails environment I have found so far.

UPDATE: don't just take my word for it.


Filed under: e editor netbeans

2 Comments