Rspec test fails on Jenkins when open file from /tmp folder - ruby-on-rails

I have such trouble with Jenkins.
I'm trying to test PDF generation using RSpec.
RSpec test failing, when trying to open receuntly generated PDF file. (File is saving into /tmp directory)
//TEST
pdf_generator = PdfGenerator::InvoicePdf.new(invoice)
pdf_file_path = pdf_generator.to_pdf
and I get error(in Jenkins only):
Failures:
1) OrdersController GET show while authorized as customer INVOICE .generate_pdf pdf contains correct text
Failure/Error: pdf_file_path = pdf_generator.to_pdf
Errno::ENOENT:
No such file or directory - /var/lib/jenkins/jobs/TicketCo for Gerrit/workspace/tmp/1371464314.pdf
# ./lib/pdf_generator/invoice_pdf.rb:80:in `to_pdf'
# ./spec/controllers/orders_controller_spec.rb:413:in `block (5 levels) in <top (required)>'
and here is the line in method, which causes error:
def to_pdf
Prawn::Document.generate(temp_pdf_file_path)
....
end
Can someone help me with this trouble ?
Maybe I need to change PDF path for Jenkins ?

I got an answer. If you have such problem just create /tmp folder in Jenkins workspace. In configurate tab you can add this command
mkdir -p tmp

Related

How to view screenshot in terminal with rspec and rails 5.1

I am following this tutorial to test system specs with rails 5.1. If I deliberately fail a test, I get the following output in the terminal
WARN: Screenshot could not be saved. `page.current_path` is empty.
F
Failures:
1) Homepage shows greeting
Failure/Error: expect(page).to have_content 'Hello Worlds'
expected to find text "Hello Worlds" in "Hello World"
[Screenshot]: tmp/screenshots/failures_r_spec_example_groups_homepage_shows_greeting_506.png
# ./spec/spec/system/home_spec.rb:10:in `block (2 levels) in <top (required)>'
The file specified for the screenshot exists, but the screen shot is not shown in the terminal. I can open the screen shot manually, so it is being generated correctly.
If I type chromedriver --version I get
ChromeDriver 2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef)
and I am using version 63.0.3239.132 of Chrome.
How do I get the screenshots to open in terminal on Mac OSX?
Screenshot display depends on what kind of terminal you use and RAILS_SYSTEM_TESTING_SCREENSHOT environment variable.
For build-in terminal:
ENV['RAILS_SYSTEM_TESTING_SCREENSHOT'] = 'artifact'
For iTerm2:
ENV['RAILS_SYSTEM_TESTING_SCREENSHOT'] = 'inline'

Rails 4, Paperclip, Upload in FTP. How to rename?

I'm using Rails 4 and Paperclip.
Beacuse I need to upload files on FTP server i'm using this great gem:
https://github.com/xing/paperclip-storage-ftp
Everything works perfect in local, but in FTP I can't rename files using this code:
def rename_myfile
if self.rename.present?
path = self.myfile.path
FileUtils.move(myfile.path, File.join(File.dirname(myfile.path), self.rename))
self.myfile_file_name = self.rename
end
end
I got an error:
No such file or directory # sys_fail2 - (/myfiles/19/original/myfileOriginalName.jpg, /myfiles/19/original/myfileRenamedName.jpg)
How to enter in ftp with FileUtils.move???
Create and Delete are working very well!
https://github.com/xing/paperclip-storage-ftp/issues/28
You have to build the full path to the file not just the file's dirname and name. change your FileUtils.move line to this:
orig_full_path = Rails.root.join "public", myfile.path # assuming they're saved in your public directory
new_full_path = Rails.root.join "public", File.dirname(myfile.path), self.rename
FileUtils.move orig_full_path, new_full_path
The idea here is to get the absolute path to your files. Before you were just giving FileUtils this path: /myfiles/19/original/myfileOriginalName.jpg which means it will look for the file in a folder /myfiles in the root of your file system. But they're actually in your Rails folder. So you should use Rails.root.join to get the true absolute path: /Users/me/my_rails_project/public/myfiles/19/original/myfileOriginalName.jpg.

Cucumber not finding step definitions

My Cucumber just won't find the step definitions. The file structure (Only the specs folder inside the Rails root) looks like this:
-> specs
-> features
-> main_structure.feature
-> step_definitions
-> main_structure_steps.rb
This is the main_structure.feature:
Feature: Main structure
Scenario: Viewing the Structure page
When I am on the structure page
And this the main_structure_steps.rb:
When(/^I am on the structure page$/) do
visit '/'
end
Now I run the cucumber command like this:
→ cucumber spec/features -r features
I get this output:
Using the default profile...
Feature: Main structure
Scenario: Viewing the Structure page # spec/features/main_structure.feature:2
When I am on the structure page # spec/features/main_structure.feature:3
Undefined step: "I am on the structure page" (Cucumber::Undefined)
spec/features/main_structure.feature:3:in `When I am on the structure page'
1 scenario (1 undefined)
1 step (1 undefined)
0m0.229s
You can implement step definitions for undefined steps with these snippets:
When(/^I am on the structure page$/) do
pending # express the regexp above with the code you wish you had
end
/Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1037:in `block in process_args': invalid option: -r (OptionParser::InvalidOption)
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1016:in `new'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1016:in `process_args'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1066:in `_run'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:795:in `block in autorun'
There is also an error message at the bottom, that doesn't appear when I run the test in RubyMine. But in both cases, the step definitions are not found. This is the Rubymine output:
Testing started at 21:29 ...
You can implement step definitions for undefined steps with these snippets:
When(/^I am on the structure page$/) do
pending # express the regexp above with the code you wish you had
end
1 scenario (1 undefined)
1 step (1 undefined)
0m0.001s
Process finished with exit code 0
Tell me if you need any additional infos.
Try
cucumber spec/features/main_structure.feature -r spec/features
Similar to my question:Cucumber Test acting in a strange way
You can implement step definitions for undefined steps with these snippets:
When(/^I am on the structure page$/) do
pending # express the regexp above with the code you wish you had
end
As the error saying. Put the code above in web_steps.rb and then replace pending # express the regexp above with the code you wish you had with the code you want
I Also had a similar problem with Java and for me the reason was that I had 2 Cucumber plugins installed in NetBeans IDE. I left Cetriolo and removed Cucumber Features and tests started working
make sure glue syntax is like below:
glue={"packageName"}
Here packageName is the package where you step definition java file exists

S3 upload files with rake script, carrierwave and fog not working

I need to upload a bunch of files to S3. I'm in a rails project using carrierwave+fog to upload the files... Everything works fine from the console, if I do the following:
image = ImageUploader.new
image.store!(File.open("image.jpg"))
This was to test, now I have to upload a bunch of files so I create a rake script inside the lib/tasks folder named upload.rake... Inside this upload.rake I do the following:
task :upload => :environment do
path = "app/assets/images"
Dir.foreach(path) do |file|
if file != "." && file !=".."
uploader = ImageUploader.new
uploader.store!(File.open(File.join(path,file)))
puts file
end
end
end
But it just doesn't work, I can't understand why... It gives this error:
rake aborted!
Broken pipe (Errno::EPIPE)
Anybody has an idea of what may be happening?? It's driving me crazy... this is the last part of the --trace
.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/openssl/buffering.rb:292:in `syswrite_nonblock'
.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/openssl/buffering.rb:292:in `write_nonblock'
.rvm/gems/ruby-1.9.2-p320/gems/excon-0.16.7/lib/excon/socket.rb:139:in `write'
.rvm/gems/ruby-1.9.2-p320/gems/excon-0.16.7/lib/excon/ssl_socket.rb:84:in `write'
.rvm/gems/ruby-1.9.2-p320/gems/excon-0.16.7/lib/excon/connection.rb:269:in `request_kernel'
.rvm/gems/ruby-1.9.2-p320/gems/excon-0.16.7/lib/excon/connection.rb:103:in `request'
.rvm/gems/ruby-1.9.2-p320/gems/fog-1.6.0/lib/fog/core/connection.rb:20:in `request'
.rvm/gems/ruby-1.9.2-p320/gems/fog-1.6.0/lib/fog/aws/storage.rb:392:in `request'
.rvm/gems/ruby-1.9.2-p320/gems/fog-1.6.0/lib/fog/aws/requests/storage/put_object.rb:43:in `put_object'
.rvm/gems/ruby-1.9.2-p320/gems/fog-1.6.0/lib/fog/aws/models/storage/file.rb:133:in `save'
Thanks!!
I was running into this a few days ago and the solution was that I wasn't specifying my bucket correctly - so It may be worth having a look to make sure that your bucket name is correct and that the permissions are set correctly on it. It seems to be a bit of a ridiculously general error when there is no bucket specified!

Running whenever cron in Windows

Total newbie at cronjobs and that kinda stuff, never done it before, so now I tried to get my hands dirty using whenever as a plugin, after seeing it on RailsCasts.
So I am trying to run a cron job for my Ruby application, but it seems like it's not working, maybe becuase of the code or maybe because of me using Windows 7?
Here is the code from my Server model
def self.ping
Server.all.each do |t|
if t.name.serverUp?
#response = 'Up'
else
#response = 'Down'
end
self.update_attribute(:serverStatus, #response.to_s)
end
end
def serverUp?
if system 'ping '+name.to_s+' -n 1 > nul'
#response = 'Up'
else
#response = 'Down'
end
self.update_attribute(:serverStatus, #response.to_s)
#response
end
And here is the code from my schedule.rb file which has the cron job in it
#every 2.minutes do
# runner "Server.last.name = 'Works'"
# runner "for x in Server.all.each {x.serverUp?}", environment =>"development"
#end
every 2.minutes do
runner "Server.ping", environment =>"development"
end
I tried both methods out, (the latter I used most recently, to which I created the self.ping method in the Server model.
So when I run whenever in my rails directory, this is the output I get:
PS C:\SIS> whenever
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd C:/SIS
&& script/rails runner -e production '\''Server.ping'\'''
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
1st) Funnily enough, the environment doesn't seem to have changed to development even though I stated it in the code.
2nd) The crontab file wasn't updated (I don't even know if there existed one to begin with?)
So I tried to create a crontab whenever file by running whenever -w ping, but the results was
PS C:\SIS> whenever -w ping
[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid.
So this is my problem, any help would be appreciated.
Edit: Ok, I decided to use rufus-scheduler in order to update my Server list frequently. I tested out whether it would work to create a new server into the table every mintues, and that seemed to work.
However, because I wasn't able to stop it from creating servers, I removed it from the bundle, deleted the 'task_scheduler' file and then re-installed it and done everything like before.
However, when I now try to run the server, I get the following error:
C:/SIS/config/initializers/task_scheduler.rb:1:in '<top (required)>': undefined method 'start_new' for Rufus::Scheduler:
Module (NoMethodError)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'block in load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in 'new_constants_in'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:201:in 'block (2 levels) in <class:Engine>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:200:in 'each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:200:in 'block in <class:Engine>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in 'instance_exec'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in 'run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:50:in 'block in run_initializers'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in 'each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in 'run_initializers'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:134:in 'initialize!'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:77:in 'method_missing'
from C:/SIS/config/environment.rb:5:in '<top (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'require'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'block in require'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in 'new_constants_in'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'require'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:103:in 'require_environment!'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:22:in '<top (required)>'
from script/rails:6:in 'require'
from script/rails:6:in '<main>'
The code in the task_scheduler is as follows:
scheduler = Rufus::Scheduler.start_new
scheduler.every '1m' do
# Server.all.each do
# |server| server.name.serverUp?
# end
Server.ping
end
Not sure which of the two methods to use, hence I commented the first one out.
Help would be greatly appreciated.
Thanks in advance
Edit
Ok, finally solved it, had to put require 'rubygems'
require 'rufus/scheduler' on the top of my code.
Now it works fine :-)
Cron is a Unix program. It doesn't exist on Windows.
Although it's nowhere near as flexible as cron, windows has the AT command which may be able to solve your problems, however you can only schedule it to run at most once a day. To see how to use AT run AT /? on your command line.
For much more fine-grained control over running tasks you can use the GUI based windows task scheduler. you can find this in Control Panel -> System and Security -> Administrative Tools -> Task Scheduler
I had the same problem, this is how I solved it:
I created a small EXE in AutoIT, the exe will read the names of files/urls to execute from an ini file (config.ini)
I named the EXE CRON.exe and had it start via schedulled tasks.
The files/urls in the config.ini can be either other executables or urls, the script will work out how to run each type.
AutoIT is a very simple scripting language, you can see my CRON.exe code below:
$iniFile='config.ini'
$i=0
While 1
$i+=1
$file=IniRead($iniFile, 'crons', $i, 'NULL')
If $file='NULL' Then ExitLoop
If StringInStr($file, 'http://') Then
InetGet($file, #TempDir, 1)
Else
$wd=StringSplit($file, '\', 3)
$workingDir=''
For $i=0 To UBound($wd)-2
$workingDir&=$wd[$i]&'\'
Next
RunWait($file, $workingDir)
EndIf
WEnd
Example of ini file:
[CRONS]
;paths of the files to execute
1=C:\anotherEXE.exe
2=http://mySite.com/myCron.php
All files are executed in order with no overlap, you can optionally change the AutoIT code to use Run instead of RunWait if you want to execute all files at the same time.
Yes I agree with Jorg M, cron job is unix program and also rufus scheduler internally call cron unix process so stay away from both and you should use delayed job ActiveRecord which help you to do the same.
This is as per my experience, there could other way but this is best which I ever used.
Best Luck

Resources