Listing files of webdirectory with rails 4 - ruby-on-rails-4

Ruby 2.0, Rails 4.
I made a site which serves static files.
I used
Dir.glob
to list static files
Now I have to save the files outside the app because the slugsize in Heroku will be too big otherwise
For this reason, i would like a directory listing of a public web folder. (Served by apache server)
I tried:
Gemfile:
gem 'net-ssh'
Controller:
def index
require 'net/ssh'
ssh = Net::SSH.start( 'http://www.domain.ch/path/to/directory', 'gast')
#files =ssh.exec!( 'ls . ').split("\n")
ssh.close
end
This raised the error:
Errno::ENOENT - No such file or directory - getaddrinfo:
net-ssh (2.7.0) lib/net/ssh/transport/session.rb:67:in `block in initialize'
/home/benutzer/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
/home/benutzer/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/timeout.rb:97:in `timeout'
net-ssh (2.7.0) lib/net/ssh/transport/session.rb:67:in `initialize'
net-ssh (2.7.0) lib/net/ssh.rb:200:in `start'
...
I never heard of a file called getaddrinfo
The answer on this thread `initialize': No such file or directory - getaddrinfo (Errno::ENOENT) when Rails new app (updating rvm) didnt solve the problem.
Additional info:
$gem -v bundler
2.0.5
$rvm - v
rvm 1.21.12 (stable)
$rvm - v
rvm 1.23.9 (master)

One of the big advantages of Ruby is, that it relies on just a couple of basic things to implement different functionalities. One of the things where this taken to an extreme is IO. Borrowed from the UNIX principle "everything is a file", in Ruby "everything is IO".
So the error Errno::ENOENT - No such file or directory - getaddrinfo: is just a generic IO error, telling you that the address http://www.domain.ch/path/to/directory could not be found. It would be even easier to spot this error if you had provided the line-numbers in your code, as they map 1 to 1 to the stack-trace you posted.
From what I know about SSH, it does not care about URLs. It cares about host and user, optionally about a password, but it's better to use key based authentication.
So if you look at the net/ssh example, you will see that you have to pass the host and not an URL. In your example this would translate to something like this:
require 'net/ssh'
Net::SSH.start('www.domain.ch', 'gast') do |ssh|
ssh.exec!('ls path/to/directory')
end

Related

Can't start test server on physical device

I installed calabash on a new machine, but tests that I ran on my old machine will not run.
As far as I can tell, both machines are set up the same way. They pull the project from the same repository, which includes a Gemfile with calabash-cucumber version 0.18.0. I set the same BUNDLE_ID, DEVICE_ENDPOINT, and DEVICE_TARGET values and use the same physical device.
When I try to run the tests in the console on the new machine, I get this:
$ bundle exec calabash-ios console
Running irb...
irb(main):001:0> start_test_server_in_background
ArgumentError: Could not find a device with a UDID or name matching 'com.my.apps.bundle.id'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/run_loop-2.1.1/lib/run_loop/device.rb:126:in `device_with_identifier'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/run_loop-2.1.1/lib/run_loop/device.rb:160:in `detect_device'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/run_loop-2.1.1/lib/run_loop/core.rb:71:in `run_with_options'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/run_loop-2.1.1/lib/run_loop.rb:134:in `run'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/calabash-cucumber-0.18.0/lib/calabash-cucumber/launcher.rb:718:in `block in new_run_loop'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/calabash-cucumber-0.18.0/lib/calabash-cucumber/launcher.rb:716:in `times'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/calabash-cucumber-0.18.0/lib/calabash-cucumber/launcher.rb:716:in `new_run_loop'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/calabash-cucumber-0.18.0/lib/calabash-cucumber/launcher.rb:584:in `relaunch'
from /Users/rjones/gambit/gemstubs/ruby/2.1.0/gems/calabash-cucumber-0.18.0/lib/calabash-cucumber/core.rb:943:in `start_test_server_in_background'
from (irb):1
from /Users/rjones/.rbenv/versions/2.1.5/bin/irb:11:in `<main>'
Any ideas why this isn't working?
Please update to 0.19.0.
Can you also paste the exact command you are using to start the
irb(main):001:0> start_test_server_in_background
ArgumentError: Could not find a device with a UDID or name matching
'com.my.apps.bundle.id'
It looks like you set your DEVICE_TARGET to the bundle id? If not, then you have found a bug. It is possible that 0.18.0 is not compatible with run-loop 2.1.1. Downgrade to run_loop 2.0.9 if you want to verify that this is the problem.
I experienced the same issue, when updating cucumber gem from version 1.3.19 to version 2.3.3. I also run tests on physical devices
edit: Sorry, forgot to mention, that I updated run_loop too from version 2.0.6 to 2.1.3
So the versions:
run_loop (2.1.3)
calabash-cucumber (0.18.1)
I guess these are the two that could affect this part, and cucumber was not involved
I found a solution, by setting the variable DEVICE instead of DEVICE_TARGET
For example:
BUNDLE_ID=<bundle_id> DEVICE=<dev_udid> DEVICE_ENDPOINT=<dev_ip> cucumber
Instead of
BUNDLE_ID=<bundle_id> DEVICE_TARGET=<dev_udid> DEVICE_ENDPOINT=<dev_ip> cucumber

RSpec 3, capybara 2.3.0, Guard 2.6.1, Rails 4.1.1 uninitialized constant (NameError)

I've recently had to migrate a project from rails 4.0 to rails 4.1 and in the process I've also updated RSpec to RSpec (3.0.0).
The project previously had the following in certain feature specs being tested with Capybara:
require 'spec_helper' require 'support/integration_helpers.rb'
require 'support/admin_support/admin_user_creation_helpers.rb'
include BuildDefaults
The include BuildDefaults refers to a module spec/support/build_defaults.rb and has various helper methods to ensure that the database has been populated correctly. For some reason this has now stopped working and gives the following error (and stack trace):
path_to/spec/features/admin_features/admin_login_out_spec.rb:4:in
<top (required)>': uninitialized constant BuildDefaults (NameError)
from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
block in load_spec_files' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
each' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
load_spec_files' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:97:in setup' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:85:inrun' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:in run' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:ininvoke' from
/Users/u_name/.rvm/gems/ruby-2.1.2#gemset_name/gems/rspec-core-3.0.2/exe/rspec:4:in
<top (required)>' from
/Users/u_name/Documents/rails/work/c_central_4.1.1/gemset_name/bin/rspec:7:in
' from
/Users/u_name/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from
/Users/u_name/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from -e:1:in `'
Would anybody happen to know why this has happened?
I would greatly appreciate any help on the matter.
I have looked at various other SO questions with a similar title as this one but none of them seem to be relevant.
Side note 1
It is worth noting that I have removed Spork from my test environment as it was not playing nicely for some reasons but have instead configured the project to use Spring. Though I don't think that this has anything to do with the issue, I did have to completely rebuild the test environment (running rails g rspec:install for instance) and then used Transpec to update the Rspec syntax.
Having spent some time looking around for a solution to this I eventually came across the official upgrade documentation (I know, I know).
As it says in the docs,
Default helper files created in RSpec 3.x have changed
In prior versions, only a single spec_helper.rb file was generated. This file
has been moved to rails_helper.rb. The new spec_helper.rb is the same
standard helper generated by running rspec --init.
This change was made to accomplish two general goals:
Keep the installation process in sync with regular RSpec changes
Provide an out-of-the-box way to avoid loading Rails for those specs
that do not require it
This second point got me thinking about the fact that a feature spec for capybara would need to load the application stack. As such, by loading the rails_helper.rb instead of the spec_helper.rb the spec/support/build_defaults.rb is now included in the load path.
Hope this helps somebody in the future.

Permission denied error with phantomjs

I am using Rails 4.0.2, Guard 2.2.4, guard-rspec 4.2.4, rspec-rails 2.14.0, Capybara 2.2.1 and Poltergeist 1.5.0 on Ruby 2.0.0-p353 and OSX Mavericks.
When I run bundle exec guard I got a lot of failure with this error message :
An error occurred in an after hook
Errno::EACCES: Permission denied - /usr/local/Cellar/phantomjs
occurred at /Users/gillesmath/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/open3.rb:211:in `spawn'
I checked the permission on /usr/local/Cellar/phantomjs and didn't notice anything unusual. I can run phantomjs without 'sudo'.
My spec_helper file look like this :
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", FILE)
require 'rspec/rails'
require 'rspec/autorun'
require "capybara/rspec"
require 'webmock/rspec'
require 'capybara/poltergeist'
Dir[Rails.root.join("spec/support/*/.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
WebMock.disable_net_connect!(:net_http_connect_on_start => true, :allow_localhost => true)
Capybara.javascript_driver = :poltergeist
I can't see what is wrong because the owner:group is the same for both the rails app and the /usr/local/Cellar/phantomjs dir and its content.
Thanks for your help
That error makes it look like it's trying to execute the /usr/local/Cellar/phantomjs directory instead of the executable inside it. I think Poltergeist scans your PATH for the first thing called phantomjs. Is /usr/local/Cellar in your PATH?
If so, I'd recommend taking it out, since executables should never appear in there directly.
If you can't, another option is to configure Poltergeist with an explicit path to phantomjs in your setup code: https://github.com/jonleighton/poltergeist#customization
It looks like someone has already opened a pull request with a fix to skip directories in the Cliver gem that Poltergeist uses, so hopefully that will be fixed soon!

Why can't chefspec find the chef_handler cookbook?

in trying to write chefspec tests, following the examples on the chefspec README (https://github.com/acrmp/chefspec), I get the following error. I tried adding "depends 'chef_handler'" to my metadata.rb, without success:
$ bundle exec rspec
*[2013-08-15T11:55:01-07:00] WARN: found a directory cookbooks in the cookbook path, but it contains no cookbook files. skipping.
F*
Pending:
example::default should include
# Your recipe examples go here.
# ./spec/default_spec.rb:6
example::single_node should do something
# Your recipe examples go here.
# ./spec/single_node_spec.rb:5
Failures:
1) example::default logs the foo attribute
Failure/Error: chef_run.converge 'example::default'
Chef::Exceptions::CookbookNotFound:
Cookbook chef_handler not found. If you're loading chef_handler from another cookbook, make sure you configure the dependency in your metadata
# ./spec/default_spec.rb:16:in `block (2 levels) in <top (required)>'
I had the same problem trying to test a custom Chef handler, but I was trying to use Berkshelf to pull dependencies down via ChefSpec's native support for Berkshelf. Here's what worked for me:
Add a spec/spec_helper.rb with
require 'chefspec'
require 'chefspec/berkshelf'
Add a .rspec file to the root of the cookbook project with
--color
--format progress
--require spec_helper
Ensure your spec (spec/default_spec.rb) is setup correctly
describe 'my_chef_handlers::default' do
handler_path = File.join('files', 'default')
let(:chef_run) do
chef_runner = ChefSpec::Runner.new do |node|
node.set['chef_handler']['handler_path'] = handler_path
node.set['statsd']['server'] = '127.0.0.1'
end
chef_runner.converge 'my_chef_handlers::default'
end
end
Setting up the ChefSpec runner outside the let statement caused cookbook not found errors.
I ran into the same problem recently. Because chefspec aims to be fast and only simulate chef runs, it doesn't clone cookbooks from the chef server. It requires that the chef_handler cookbook needs to be local. By default, it looks for it at the same level as the cookbook you are testing.
e.g.
./test_cookbook
./chef_handler

Given a typical Rails 3 environment, why am I unable to execute any tests?

I'm working on writing simple unit tests for a Rails 3 project, but I'm unable to actually execute any tests.
Case in point, attempting to run the test auto-generated by Rails fails:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
Results in the following error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load --
test_helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from user_test.rb:1:in `<main>'
Commenting out the require 'test_helper' line and attempting to run the test results in this error:
user_test.rb:3:in `<main>': uninitialized constant Object::ActiveSupport (NameError)
The action pack gems appear to be properly installed and up to date:
actionmailer (3.0.3, 2.3.5)
actionpack (3.0.3, 2.3.5)
activemodel (3.0.3)
activerecord (3.0.3, 2.3.5)
activeresource (3.0.3, 2.3.5)
activesupport (3.0.3, 2.3.5)
Ruby is at 1.9.2p0 and Rails is at 3.0.3.
The sample dump of my test directory is as follows:
/fixtures
/functional
/integration
/performance
/unit
-- /helpers
-- user_helper_test.rb
-- user_test.rb
test_helper.rb
I've never seen this problem before - I've run the typical rake tasks for preparing the test environment. I have nothing out of the ordinary in my application or environment configuration files, nor have I installed any unusual gems that would interfere with the test environment.
Edit March 9th
Xavier Holt's suggestion, explicitly specifying the path to the test_helper worked; however, this revealed an issue with ActiveSupport.
Now when I attempt to run the test, I receive the following error message (as also listed above):
user_test.rb:3:in `<main>': uninitialized constant Object::ActiveSupport (NameError)
But as you can see above, Action Pack is all installed and update to date.
Edit March 13th
When attempting to run tests using rake test:units the following stack trace is dumped to the console:
test/unit/bookmark_test.rb:3:in `<top (required)>': uninitialized constant Objec
t::ActiveSupport (NameError)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `block in <main>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `<main>'
rake aborted!
So looking into the file listed above, I see the following:
#!/usr/bin/env ruby
# Load the test files from the command line.
ARGV.each { |f| load f unless f =~ /^-/ }
To my knowledge, everything looks as expected.
Your test/test_helper file should have been created when you generated the application. It contains this valuable content:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
The second line here is the most important: it requires the config/environment.rb file at the root of your application, which in turn requires a lot of other things, including the valuable (I like that word today, ok?) ActiveSupport constant.
When you generate a controller, model or scaffold it'll also generate tests for those. I just ran rails g scaffold ticket in my app and it generated test/unit/ticket_test.rb which contains this:
require 'test_helper'
class TicketTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
The first line of this file will require the test/test_helper.rb file that we jut saw. This will load ActiveSupport and the TestCase class within it, thereby making this test feasible. Everything else just flows on from there.
With all that explanation out of the way (even though it's something that you already know), I'm placing a large wager on it's something that's masscaring your LOAD_PATH, causing the test directory to be removed from it.
What's really unusual is that when you do specify the full path to the test/test_helper.rb you're saying it loads it, but ActiveSupport is still undefined. Well, that should be loaded as-per the description above. Is it actually loading config/environment.rb? Can you put something such as:
puts "LOADING CONFIG/ENVIRONMENT.RB"
At the top of your config/environment.rb file and then run the tests again? It should be output. Very unusual.
Continuing on the thread about LOAD_PATH... Got a dirty little secret you're not telling us about?
Actually, Dan Cheail makes a good point. You could be running the tests using ruby test/unit/ticket_test.rb in which case test_helper wouldn't be available, but still that still doesn't explain why when you specify the full path you're still getting an undefined constant ActiveSupport.
If you want to run a single test you should be doing this:
ruby -Itest test/unit/ticket_test.rb
That -I option there adds the test directory to the load path, meaning the test_helper file will be available through a straight require 'test_helper'. If it still errors after this, I reckon your test/test_helper.rb is either empty or broken.
The problem you're having is the way you're executing tests. Simply calling ruby test/unit/user_test.rb doesn't set up the load path, which explains the problems you've been having.
rake test:units is what you want and should work straight away.
Sorry about the post here, but I am unable to comment on questions yet.
what environment are you running, Win (has an issue with a .gemspec file) Linux, Mac?
Are you using RVM?
Test-Unit is installed by default with Rails, if you installed the gem Test-Unit you will get a conflict between the 2. try uninstalling the gem and your tests should start working.
If running on windows I would gem uninstall "autotest", then navigate to the following dir
drive:\Ruby192\lib\ruby\gems\1.9.1\specifications
In here you will find .gemspec files. Ensure that you dont have 2 autotest.gemspec files or any, for that matter. if so remove(delete) them, then download and gem install the autotest gem again. Grab the latest by using version switch.
You should be able to run your autotests. I did come across this once before, so to fix I simply removed the view and helper test files and wrote everything in the standard test file. Other than that I know running on windows autotest had issues, because of the ruby installer and bundler not clearing out things correctly and forgetting files.
I will find the link for you, to better explain.