trouble setting up cucumber to test without actually changing the file system - ruby

I have been struggling for a while with this problem. I'm trying to test a ruby CLI that uses a lot of fileutils operations like making directories and copying files, etc. But when I'm writing tests, I need some way to test the functionality without touching and changing the actual filesystem. I am trying to use the fakefs gem but I am running into a error right away
When I successfully run `dotfiles init` # aruba-0.5.3/lib/aruba/cucumber.rb:71
No such file or directory - No such file or directory - /tmp/aruba-out20130817-15800-ke8nou (Errno::ENOENT)
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `open'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `block in initialize'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tmpdir.rb:142:in `create'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:136:in `initialize'
features/dotfiles_repository.feature:8:in `When I successfully run `dotfiles init`'
So its not finding /tmp/aruba which is where cucumber aruba does all of its file operations according to the readme.
The tests run fine without fakefs, so I'm assuming its something with the setup.
Here is my features/support/env.rb file
require 'aruba/cucumber'
require 'fakefs/safe'
Before do
FakeFS.activate!
end
After do
FakeFS::FileSystem.clear
FakeFS.deactivate!
end
Does anyone have any idea what I may be doing wrong? Any help is appreciated. Even if its an idea that doesn't use fakefs, I would like to hear it. Thanks

Related

Ruby Rake ActiveRecord Migrate

I'm trying to build a simple ruby script that connects to a database and runs some basic queries.
The code is here: https://github.com/mastermindg/rack-activrecord-example
It's not a service - only a script that is run manually to do batch jobs. My problem is that I need to populate the database for testing purposes. I know how to do this in Sinatra and Rails but it's failing as-is:
NoMethodError: undefined method `set' for main:Object
Did you mean? send
/usr/src/app/app.rb:7:in `<top (required)>'
/usr/src/app/Rakefile:2:in `<top (required)>'
I've got the database.yml but I can't tell how to load it since set is failing.
How do I connect to and query a database using ActiveRecord with basic Rack?
1) Add this to your Rakefile after the requires:
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(root, 'config/database.yml')))
2) Remove set :database_file, 'config/database.yml' from your app.rb file (I think set is a sinatra/activerecord method).
Running rake db:create may give you an error on your project now,because you're using json instead of JSON in your app.rb file (depending on your local versions), so. . .
3) Change puts json Resource.select('id', 'name').all to puts JSON Resource.select('id', 'name').all in your app.rb file.
Now rake db:create will throw a database error, but that's an error related to your specific database configuration, make an appropriate adjustment to that (this is off-topic from the original question, so I won't address it further) and your app should run as you desire.
More info:
This gist shows example contents of a Rakefile that you could use to run Active Record tasks without using Rails or Sinatra.

Cannot open file ruby.png in Gosu using Learn Game Programming with Ruby

I'm using the Learn Game Programming with Ruby book and I'm trying to just execute the sample code.
I get the following error, using the sample code.
❯ ruby WhackARuby/WhackARuby_1/whack_a_ruby.rb code
/Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosu-0.10.4/lib/gosu/patches.rb:40:in `initialize': Cannot open file ruby.png (RuntimeError)
from /Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosuu0.10.4/lib/gosu/patches.rb:40:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `<main>'
The sample code looks like this:
require 'gosu'
class WhackARuby < Gosu::Window
def initialize
super(800, 600)
self.caption = 'Whack the Ruby!'
#image = Gosu::Image.new('ruby.png')
end
end
Any thoughts on what could be going on here? I've tried changing the offending line to #image = Gosu::Image.new('./ruby.png') for example and that didn't help.
I doubt this is the cause, but my ruby version is ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
EDIT includes file path:
Invariably the problem is because the file doesn't exist where you think it is.
There are many ways to reference a file. The File documentation has expand_path, realpath, absolute_path, all of which make it easy to reference a file based on an absolute or relative path, and relative to the currently running file, application or a particular directory. How to use them is covered in their examples.
It's important to make sure you know what directory the code considers it's current-working-directory, and/or where the file is. The first is important when using a relative path, and the second is if you don't want to care about your current path and know that the file ALWAYS exists in a certain place.
And, then there's also the case when the file's name is different than what you think it is, or it doesn't even exist.
In my case the solution was using a more explicit filepath:
Gosu::Image.new("#{__dir__}/ruby.png")

Travis CI: Failing because executing a local bash executable isn't found, but it's definitely here

My gem uses a local copy of Tidy HTML Validator. It's available in lib/tidy/tidy. My tests run perfectly locally, but on Travis CI, I'm getting the error that Tidy can't be found.
Errno::ENOENT:
No such file or directory - tidy
The code looks like this:
stdin, stdout, stderr = Open3.popen3('tidy -quiet')
Is there a restriction or something that prevents executing a local executable? I also call a local copy of the Jigsaw CSS Validator which is a Java jar file and is executed like this:
`java -jar css-validator.jar --output=soap12 file:#{uri}`
This works perfectly on Travis CI. Could there be a problem with Open3?
Here's the link to the GitHub repo: https://github.com/jmuheim/headhunter
Here's the link to the failing Travis CI build: https://travis-ci.org/jmuheim/headhunter
Update
I have rewritten the code that executes the external java executable to also use Open3, and it's working. So the problem has to do with tidy.
For further debugging I have renamed tidy into tidy2 (to make sure that really the wanted executable is called), and I added a manual File.exist? 'tidy' before calling it, so this would raise an error if it couldn't be found.
Dir.chdir(VALIDATOR_DIR) do
raise "Could not find tidy in #{Dir.pwd}" unless File.exists? EXECUTABLE
# Docs for Tidy: http://tidy.sourceforge.net/docs/quickref.html
stdin, stdout, stderr = Open3.popen3("#{EXECUTABLE} -quiet")
# ...
end
The file is definitely found, but executing it still results in this error.
1) Headhunter::HtmlValidator#validate returns a local response when calling the validator succeeds
Failure/Error: subject.validate('invalid.html', read_file('html_validator/invalid.html'))
Errno::ENOENT:
No such file or directory - tidy2
# ./lib/headhunter/html_validator.rb:20:in `block in validate'
# ./lib/headhunter/html_validator.rb:16:in `chdir'
# ./lib/headhunter/html_validator.rb:16:in `validate'
# ./spec/lib/headhunter/html_validator_spec.rb:9:in `block (4 levels) in <top (required)>'
# ./spec/lib/headhunter/html_validator_spec.rb:8:in `block (3 levels) in <top (required)>'
So the only thing I can guess now is that be there is a permissions problem? Is there a way to set permissions to local executables with Travis CI?
I also tried putting tidy into bin like somebody suggested in the comments, but this didn't seem to change a thing, too.
It looks like your tidy2 executable is a Mach-O binary for OS X, but the Travis worker runs Linux. Try to include a suitable ELF binary from http://tidy.sourceforge.net/#binaries.
A couple things:
Typically the environment is different than your current environment when spawning programs so you have to check what the environment is. This is the case in cron, monit, and for some ruby method calls.
In your case, for Open, it uses Process.spawn so you're not guaranteed to be in your own directory. Open.popen3 takes a :chdir option, so set that to your Dir.pwd and see what happens.
You can also try just using absolute paths.
Sources
http://ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-popen3
https://stackoverflow.com/a/5917927/463225
http://ruby-doc.org/core-1.9.3/Process.html#method-c-spawn

Checking if proxy is working in Ruby

I'm new to Ruby and trying to find a way to check if a proxy is working. I've tried using the 'net/ping' library but I am just getting some errors while requiring it(in Interactive Ruby the script works even though there are the errors, it just closes itself when I run the script normally though)
I'm trying to find a solution to make it either ignore the error, fix the error or find another way to check if the proxy works.
Thanks for help.
This is the error message I get when I try to require the 'net/ping'
proxy.rb(main):005:0> require 'net/ping'
LoadError: no such file to load -- windows/console
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in'
from C:\Users\X\Desktop\proxy.rb:5:in require'
from C:\Users\X\Desktop\proxy.rb:5
from C:/Ruby191/bin/irb:12:in'
In my opinion the only way to check if a proxy is working is to connect through it and make a request.

Metric Fu: RCov fails to load spec_helper

I am trying to run metric_fu on a Rails 3 application. All is good, with the exception of rcov. I have RSpec configured and my tests follow the spec/**/*.rb format and run fine in RSpec. Trying to check coverage with rcov, however, I get the following errors:
** Running the specs/tests in the [test] environment
No file to analyze was found. All the files loaded by rcov matched one of the
following expressions, and were thus ignored:
[/\A\/Users\/Eric\/\.rvm\/rubies\/ruby\-1\.8\.7\-p299\/lib/,
/\btc_[^.]*.rb/,
/_test\.rb\z/,
/\btest\//,
/\bvendor\//,
/\A\/Users\/Eric\/\.rvm\/gems\/ruby\-1\.8\.7\-p299#-backend\/gems\/rcov\-0\.9\.8\/lib\/rcov\/formatters\/base_formatter\.rb\z/,
/\bvendor\//,
/\bconfig\//,
/\benvironment\//,
/\/gems\//,
/\/Library\//,
/\/usr\//,
/spec/]
You can solve this by doing one or more of the following:
* rename the files not to be ignored so they don't match the above regexps
* use --include-file to give a list of patterns for files not to be ignored
* use --exclude-only to give the new list of regexps to match against
* structure your code as follows:
test/test_*.rb for the test cases
lib/**/*.rb for the target source code whose coverage you want
making sure that the test/test_*.rb files are loading from lib/, e.g. by
using the -Ilib command-line argument, adding
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
to test/test_*.rb, or running rcov via a Rakefile (read the RDoc
documentation or README.rake in the source distribution).
/Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- spec_helper (LoadError)
from /Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./spec/models/account_spec.rb:1
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19
When I run rcov directly, it appears that part of the issue is rcov not correctly loading the spec_helper in my specs, which is required as "require 'spec_helper'".
Any help on how to resolve this? Thanks.
Adding "-Ispec" to the Metric-Fu configuration fixes this.

Resources