From: Daniel Pittman Date: 2011-06-29T01:23:45+09:00 Subject: Re: How to test code that behaves conditionally based on gem existence? On Mon, Jun 27, 2011 at 20:22, Josh Cheek wrote: > Working on https://rubygems.org/gems/bundler-bouncer, the problem is it uses > ANSI escape sequences to colour the output, which doesn't work on Windows. > I've seen some gems that will use term-ansicolor to colour the output, if it > is present, and otherwise won't use it. That seemed easier than trying to > detect whether we were on Windows or not, but I still can't figure out how > to test something like that. Everything I've thought of has gotten really > ugly really quick. Any suggestions? I usually make sure the test for "use ANSI colour" is in a separate function. That can test for the presence of the gem, and anything else you want it to do. It also, conveniently, makes it easy to stub out that individual function to make tests run as if it wasn't present. I would use it to either define different output functions, or embed it inside the same output functions, rather than having it visible elsewhere in the code. Hide the colour part in your output abstraction. Daniel -- ⎋ Puppet Labs Developer – http://puppetlabs.com ✉ Daniel Pittman ✆ Contact me via gtalk, email, or phone: +1 (503) 893-2285 ♲ Made with 100 percent post-consumer electrons