Can't set custom options in .rspec - ruby-on-rails

I'm trying to put some custom options in my .rspec because I'd like to have this custom formatter.
This is the only content of my .rspec file:
--format Fuubar
--color
When i'm launching my tests with
rspec some_spec.rb
nothing is happening, but when I'm using
rspec some_spec.rb --format Fuubar --color spec
or putting the options in my spec_helper.rb
config.color = true
config.formatter = "Fuubar"
everything is working fine, so i think there's something I'm missing with the .rspec file.
Any idea?

Where is your .rspec file located? There is only 3 places where Rspec will look for this file:
$PROJECT_ROOT/.rspec-local
$PROJECT_ROOT/.rspec
~/.rspec
$PROJECT_ROOT is not an actual environment variable, but the full path to your project.
Please make sure your .rspec file is in one of these locations, and not inside the spec directory or something like that.
Source: https://www.relishapp.com/rspec/rspec-core/v/3-0/docs/configuration/read-command-line-configuration-options-from-files

The problem was the location of the .rspec file.
The correct structure should be
-- my_tests/
---- spec/
------ folder1/
------ folder2/
------ folder3/
------ spec_helper.rb
----- .rspec

Related

Can I set rspec --format documentation as the default?

Per the Rspec documentation, by default when you run rspec you get the progress formatter (looks like this: ".....").
There is another formatting option rspec --format documentation that goes through each test one by one. My question: how can I enable --format documentation by default without having to type it in the command line every time?
Option 1
Add it to .rspec file (or create one in the project's root directory) - options added to it will be applied to every test run within current project:
# .rspec
--color
--format documentation
Option 2
Add it to RSpec.configure block:
RSpec.configure do |config|
config.formatter = :documentation
end
Option 3
Specify rspec's options globally by adding them to ~/.rspec.
RSpec.configure do |config|
config.color = true
config.formatter = :documentation
config.order = 'default'
end
You can create your own personal RSpec settings by creating a ~/.rspec file:
--color
--format documentation
The project .rspec file should only contain the minimum settings required to run the spec suite to avoid developer wars.

Add TODO list (rake notes) output to source control

I want my TODO list which I get as output of 'rake notes' to be under source control (Git).
Is there a way I can configure 'rake notes' to spit out out a text file which can be under source control?
Not completely understanding your question's direction, so I'll just go from the beginning. :)
Create a rake file :)
The most common is under Rails.root / lib, but it's preferable (depending on Elitests or purists) to put it under lib/tasks/output_notes.rake (yes, .rake, not .rb)
Rake scripts have namespaces. "db" is a namespace for running "rake db:migrate" for example. You can see some examples here: https://github.com/rails/rails/tree/master/railties/lib/rails/tasks
Your script might look something like this.
namespace :vasa do
desc "Output notes"
task :output_notes do
output_file_name = "#{DateTime.now.to_i}_notes.txt"
File.open(output_file_name, 'w') { |file| file.write(Note.dump_all) }
end
end
You can then run rake vasa:output_notes. Want to add the file to your repo automagically?
# after File.open ...
`
git add #{output_file_name}
git commit -m "added notes"
`
The backticks will run it as a system command.

`rake notes` should scan haml

According to the guides:
... is done in files with extension .builder, .rb, .erb, .haml and .slim for both default and custom annotations.
But it isn't working, even when manually configured:
$ rails -v
Rails 4.2.3
$ grep -r annotations config/environments/development.rb
config/environments/development.rb: config.annotations.register_extensions('haml') { |a| /#\s*(#{a}):?\s*(.*)$/ }
$ grep -r TODO app/views
app/views/orders/show.html.haml: -# TODO: Add link
$ rake notes
app/models/order.rb:
* [12] [TODO] Refactor
Anyone know how to get it working?
Short answer
Add this line
SourceAnnotationExtractor::Annotation.register_extensions("haml") { |tag| /(?:\/\/|#)\s*(#{tag}):?\s*(.*)$/ }
at the end of Rakefile inside of your Rails app.
Longer answer
rake notes
is defined there :
https://github.com/rails/rails/blob/master/railties/lib/rails/tasks/annotations.rake
task :notes do
SourceAnnotationExtractor.enumerate "OPTIMIZE|FIXME|TODO", tag: true
end
this task doesn't depend on :environment, so for example, no code inside config/initializers/ or config/environment will be executed for this task. Putting any configuration there won't have any effect for rake notes.
Only config/application.rb will be required, from Rakefile.
https://github.com/rails/rails/blob/master/railties/lib/rails/source_annotation_extractor.rb tells us that we can define a new file extension for Annotations like this :
SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ }
so adding this line inside Rakefile or config/application.rb will define the new annotation before the notes task is executed.
I'm not sure why it doesn't work out of the box for haml, since it's defined in haml-rails.
For the time being, with Rails 4.2.2 and haml-rails 0.9.0, the short answer above should do.
Put the following into config/initializers/ :
Rails.application.config.annotations.tap do |notes|
notes.register_extensions('haml') { |annotation| %r(#\s*(#{annotation}):?\s*(.*?)$) }
end
for more details you can refer this

Can you make folder with rake build?

I have no experience with Ruby or rake or anything, but I am using slate for API documentation, and it uses Ruby and rake and stuff to build the file. I know nothing at all about these things, but what I do know is this: when I do a rake build it updates a folder (slate/build). I then have to manually copy slate/build to ../app/docs after every single rake build. Is there something I can do that will copy that folder on every rake build automatically for me?
Add to your Rakefile:
ROOT = File.expand_path('..', __FILE__)
task :build_and_move => [:build] do
cp_r(File.join(ROOT, 'slate/build'), File.join(ROOT, '../app/docs'))
# or
# mv(File.join(ROOT, 'slate/build'), File.join(ROOT, '../app/docs'))
end
and then run rake build_and_move.
You can use FileUtils for this.
Docs: http://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-copy
Example from the docs:
Copies src to dest. If src is a directory, this method copies all its contents recursively. If dest is a directory, copies src to dest/src.
FileUtils.cp 'eval.c', 'eval.c.org'
FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
FileUtils.cp 'symlink', 'dest' # copy content, "dest" is not a symlink

Capistrano: deploy.rb file refactoring

I have following code in my deploy.rb
namespace :app do
desc "copies the configuration frile from ~/shared/config/*.yml to ~/config"
task :copy_config_files,:roles => :app do
run "cp -fv #{deploy_to}/shared/config/hoptoad.rb #{release_path}/config/initializers"
run "cp -fv #{deploy_to}/shared/config/app_config.yml #{release_path}/config/app_config.yml"
end
end
I thought it would be a good idea to keep my deploy.rb file clean and I attempted to move above code to capistrano_utilities.rb under config. I am using Rails application. And I added following line of code to deploy.rb
require File.expand_path(File.dirname(__FILE__) + "/../lib/capistrano_utilities")
Now I am getting following error.
undefined method `namespace' for main:Object (NoMethodError)
The value of self in the deploy.rb is Capistrano::Configuration . While the value of self in capistrano_utilities is Main. So I understand why I am getting namespace method error. What is the fix for this problem?
In your config/deploy.rb, try load instead of require. Also, capistrano already runs as if you're at the RAILS_ROOT, so there's no need to use __FILE__:
load "lib/capistrano_utilities"
In a capistrano config file, load is redefined to load another configuration file into the current configuration. When passing a path to it, it actually calls load_from_file (a private method defined by capistrano) that just reads the file from disk and instance_eval's it.
Check your Capfile on Rails.root.
if you use capistrano 3, you see this line;
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Now, put your file on "lib/capistrano/tasks/capistrano_utilities.cap" and it will be loaded.

Resources