Posts Tagged “Ruby”

Update: I've refactored the code significantly and introduced the ability to explicitly unguard methods from within specs.

One problem I find myself having when writing tests is ensuring that no execution ever leaves my little test sandbox. For example, when writing tests for github-gem I keep looking through the code to find every place a function ends up `shelling out` and it's tiresome and error-prone. I finally decided I was going about this backwards. Testing is supposed to find problems for me, right? So why not raise an error whenever `` or Kernel#system() is called and let the tests tell me what else I need to mock?

Put the following code in your spec_helper.rb file and an exception will be raised whenever `` or Kernel#system() is called during a test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Module
  def metaclass
    class << self;self;end
  end
end
 
module Spec::Example::ExampleGroupSubclassMethods
  def add_guard(klass, name, is_class = false)
    guarded = nil # define variable now for scoping
    target = (is_class ? klass.metaclass : klass)
    sep = (is_class ? "." : "#")
    target.class_eval do
      guarded = instance_method(name)
      define_method name do |*args|
        raise "Testing guards violated: Cannot call #{klass}#{sep}#{name}"
      end
    end
    @guards ||= []
    @guards << [klass, name, is_class, guarded]
  end
 
  def add_class_guard(klass, name)
    add_guard(klass, name, true)
  end
 
  def unguard(klass, name, is_class = false)
    row = @guards.find { |(k,n,i)| k == klass and n == name and i == is_class }
    raise "#{klass}#{is_class ? '.' : '#'}#{name} is not guarded" if row.nil?
    (is_class ? klass.metaclass : klass).class_eval do
      define_method name, row.last
    end
    @guards.delete row
  end
 
  def class_unguard(klass, name)
    unguard(klass, name, true)
  end
 
  def unguard_all
    @guards ||= []
    @guards.each do |klass, name, is_class, guarded|
      (is_class ? klass.metaclass : klass).class_eval do
        define_method name, guarded
      end
    end
    @guards.clear
  end
end
 
Spec::Runner.configure do |configuration|
  configuration.prepend_before(:all) do
    self.class.send :include, Spec::Example::ExampleGroupSubclassMethods
  end
 
  configuration.prepend_before(:each) do
    add_guard Kernel, :'`'
    add_guard Kernel, :system
    add_class_guard Process, :fork
    add_class_guard Open3, :popen3 if defined? Open3
  end
 
  configuration.append_after(:each) do
    unguard_all
  end
end

Note: if you want, you can write :` instead of :'`', but my syntax highlighter here doesn't like that.

Comments Comments Off on Guarding against `โ€Œ` and Kernel#system() in your tests

Well, after about 5 hours of hacking, I got a ruby script together to migrate all my data from Typo 4.11 to WordPress, so the site is back from the dead, and now running on an engine that DreamHost is actually happy with.

Update: The conversion script is now available at GitHub.

Comments Comments Off on Back from the grave

Haml is a new markup format for Ruby on Rails apps that just hit 1.0. At first glance it looks pretty odd, but it turns out to be really easy to write in, and it’s shorter and, actually, easier to read than the equivalent eRB.

I think I’m going to convert Typo to use Haml for all its templates. I already did Azure’s layout file and it was pretty simple.

Comments 2 Comments »

Typo 4.0 is out! Many thanks to Scott Laird for his wonderful work on Akismet support and the new gem installer.

Comments Comments Off on Typo 4.0 is out

Well, it looks like we’re almost at the 4.0 milestone for Typo. Tonight I knocked off 7 tickets (a couple just got re-categorized, but others got fixed/closed). Now there’s just 1 left, and it’s slaird’s responsibility. Unfortunately I have no idea what the time frame is on finishing it.

In any case, I’ll be pushing to get 4.0 out the door as soon as possible.

BTW, anybody currently on trunk and Rails 1.1, I highly encourage you to upgrade to HEAD. Why? Because I squelched the exceedingly verbose logging of sidebar rendering (in a quick test, a single sidebar rendered on the front page shoved 70k into the log file). It’s a fairly ugly hack, but it’ll save you from having to wipe your logs every week.

Comments Comments Off on Nearing 4.0

Well, Rails 1.1 is out now. And that is a problem for Typo. See, when we tried running it under Rails 1.1rc1 we discovered that this new version of Rails has certain problems with components. Basically, Rails isn’t setting up the component namespaces that we’re expecting it to. I guess this is what I’ll be spending tomorrow evening hammering on.

Update: As of 10:57 AM this morning, Typo trunk now supports Rails 1.1 (and should work with Rails 1.0 too).

Comments Comments Off on Rails 1.1 and Typo

After a couple hours of hacking I managed to come up with a ruby script that generated a GraphViz dot file that describes all of the Universal Type Identifiers on the system. I’ll release it later once I make it a bit more usable, but right now I have a dot file to hand out. If you don’t have GraphViz installed you can download the dmg, or you can use OmniGraffle. Just open up the file in Graphviz.app or OmniGraffle.app - note however, that OmniGraffle seems to screw up the border and widths of all the nodes, so if you don’t have a registered version (and thus cannot edit the file), you should stick with Graphviz.

Download: utigraph.zip

Comments Comments Off on Graph of all Universal Type Identifiers

Just a quick post about AJAX on Rails, to test out my latest feature, which is Lightbox support for Typo:

If you click that and it doesn’t overlay the image on top of the current page, then you’ve run into a bug in Safari that I’m trying to figure out the cause of. Grrrrr. The bug has been worked around.

Comments Comments Off on AJAX on Rails

Inspired by a script mentioned on ranchero.com, I wrote a ruby script that generates an RSS feed for all the Crash Reporter logs on your machine. Just create a New Special Subscription in NetNewsWire, point it at the script, and you’re all set.

Comments 10 Comments »

So why has been talking about MouseHole lately. If you’re unaware, MouseHole is a ruby script that acts as a web proxy and filters HTML documents via ruby scripts. Or for a much better look at it, go look at what why wrote.

Anyway, I wanted a way to do JavaScript cross-site AJAX tricks, like what Greasemonkey enables. Unfortunately, because MouseHole is a pre-processor, not a JavaScript extension, it’s not possible. So I spent a few hours and wrote a way for MouseHole scripts to provide content that doesn’t actually exist (as opposed to mutating existing content). This way JavaScript can do an AJAX call within the same domain (so it doesn’t hit the security limitations) but that call is intercepted by your script, so you could pull info from other sites and send it back, or whatever you wish.

I haven’t actually done anything with this new capability yet aside from a rather silly example script which simply adds a button to Google that asks MouseHole for a random number. I’d really like to extend Google Maps, but that will require delving into how it works, and it’s far too late to do that now, as I need to get to bed.

Anyhow, I put together a patch and sent it to the MouseHole Scripters mailing list. If you check out MouseHole from the CVS repository, you can apply my patch and test it. Or you can just wait to see if it gets added to MouseHole.

Comments Comments Off on MouseHole and JavaScript