Test with input files in rspec. How to organize? - ruby

I have the following structure:
MyProject --> Main folder of my project.
MyProject/my_class.rb
MyProject/inputs/input1.txt
MyProject/inputs/input2.txt
MyProject/rspec/spec_helper.rb
MyProject/rspec/my_class_spec.rb
What is an elegant way or most common way that I can make it so that I can use the input1.txt and input2.txt in my_class_spec.rb?

You can access content of input1.txt from my_class_spec.rb like this:
file1_content = File.read(File.expand_path '../../inputs/input1.txt', __FILE__)

Related

How not to change require_relative after moving files

I have some question about best way to do this:
I have file framework/helpers/test_helper.rb
it starts with line
require_relative '../../framework/app_manager/app_manager'
After some toughts i want to reorganize framework/helpers folder and move this file to
framework/helpers/test/test_helper.rb
After that i must change first line of this file to
require_relative '../../../framework/app_manager/app_manager'
So my question is - that is the best way to require_relative file, so I will not need to change require_relative line every time i move file from folder to folder.
The best way is not to use require_relative at all.
Put directories that include files you'll need to require in your $LOAD_PATH, and then just require them.
If you change your directory structure later, you just need to change the lines that set up your $LOAD_PATH, and not any require lines.
This is exactly why to use the $LOAD_PATH as ruby intended, and avoid require_relative.
http://joshuapaling.com/blog/2015/03/22/ruby-load-path.html

Use Ruby, Cucumber and Aruba to check for file in testing home directory

I'm using Cucumber and Aruba to test a Ruby command line app written on top of GLI. To prevent tests from affecting production data, I update ENV['HOME'] to point to a testing directory. I'd like to check for the existence of a file in the testing ENV['HOME'] directory. I'd like to use Aruba for this, but I have been unable to get ENV['HOME'] to expand properly.
For example:
Scenario: Testing config files are found
Given I switch ENV['HOME'] to be "set_a" of test_arena
Then a file named "#{ENV['HOME']}/config.xml" should exist
Is it possible to pass ENV['HOME'] to Aruba's Then a file named "" should exist step_definition and have it expand to the full path?
I'm still interested in seeing if it's possible to do this natively with Cucumber/Aruba. In the mean time, here's a cut down example of what I'm doing:
In features/app_name.feature file, define the following Scenario:
Scenario: Testing config files are found
Given I switch ENV['HOME'] to be "test_arenas/set_a"
Then a test_arena file named "config.xml" should exist
Then, in the features/step_definitions/app_name.rb file define the steps:
Given(/^I switch ENV\['HOME'\] to be "(.*?)"$/) do |testing_dir|
ENV['HOME'] = File.join(File.expand_path(File.dirname(__FILE__)),
'..','..', testing_dir)
end
Then(/^a test_arena file named "(.*?)" should exist$/) do |file_name|
file_path = "#{ENV['HOME']}/#{file_name}"
expect(File.exists?(file_path)).to be_truthy
end
This isn't as robust at Aruba's check_file_presence but it gets the basic job done.
For a little more background, the idea behind this approach is to have a test_arenas directory sitting at the root of the app's directory structure. For each test, an individual test_arenas/set_X directory is created that contains the necessary files. Prior to each test, ENV['HOME'] is pointed to the respective test_arenas/set_X directory.

Yard: Specify a different path for the compiled doc (instead of doc/)?

Is there any way to tell Yard not to clutter up my Rails project's doc/ folder? I'd like it to save its files in doc/yard/ or something like that. Sadly I didn't find any option for that.
Thank you for help.
yardoc --output-dir /path/to/yard/doc app.rb
You probably want to conifgure this and forget about it. If so, .yardopts in your project root directory is the way to go - you just write the option in the file, one per line, so something like:
$ echo --output-dir /path/to/yard/doc >> .yardopts
(or use vi or whatever to edit the file by hand)
With that in place you can just do
$ yard doc

Join multiple Coffeescript files into one file? (Multiple subdirectories)

I've got a bunch of .coffee files that I need to join into one file.
I have folders set up like a rails app:
/src/controller/log_controller.coffee
/src/model/log.coffee
/src/views/logs/new.coffee
Coffeescript has a command that lets you join multiple coffeescripts into one file, but it only seems to work with one directory. For example this works fine:
coffee --output app/controllers.js --join --compile src/controllers/*.coffee
But I need to be able to include a bunch of subdirectories kind of like this non-working command:
coffee --output app/all.js --join --compile src/*/*.coffee
Is there a way to do this? Is there a UNIXy way to pass in a list of all the files in the subdirectories?
I'm using terminal in OSX.
They all have to be joined in one file because otherwise each separate file gets compiled & wrapped with this:
(function() { }).call(this);
Which breaks the scope of some function calls.
From the CoffeeScript documentation:
-j, --join [FILE] : Before compiling, concatenate all scripts together in the order they were passed, and write them into the specified file. Useful for building large projects.
So, you can achieve your goal at the command line (I use bash) like this:
coffee -cj path/to/compiled/file.js file1 file2 file3 file4
where file1 - fileN are the paths to the coffeescript files you want to compile.
You could write a shell script or Rake task to combine them together first, then compile. Something like:
find . -type f -name '*.coffee' -print0 | xargs -0 cat > output.coffee
Then compile output.coffee
Adjust the paths to your needs. Also make sure that the output.coffee file is not in the same path you're searching with find or you will get into an infinite loop.
http://man.cx/find |
http://www.rubyrake.org/tutorial/index.html
Additionally you may be interested in these other posts on Stackoverflow concerning searching across directories:
How to count lines of code including sub-directories
Bash script to find a file in directory tree and append it to another file
Unix script to find all folders in the directory
I've just release an alpha release of CoffeeToaster, I think it may help you.
http://github.com/serpentem/coffee-toaster
The most easy way to use coffee command line tool.
coffee --output public --join --compile app
app is my working directory holding multiple subdirectories and public is where ~output.js file will be placed. Easy to automate this process if writing app in nodejs
This helped me (-o output directory, -j join to project.js, -cw compile and watch coffeescript directory in full depth):
coffee -o web/js -j project.js -cw coffeescript
Use cake to compile them all in one (or more) resulting .js file(s). Cakefile is used as configuration which controls in which order your coffee scripts are compiled - quite handy with bigger projects.
Cake is quite easy to install and setup, invoking cake from vim while you are editing your project is then simply
:!cake build
and you can refresh your browser and see results.
As I'm also busy to learn the best way of structuring the files and use coffeescript in combination with backbone and cake, I have created a small project on github to keep it as a reference for myself, maybe it will help you too around cake and some basic things. All compiled files are in www folder so that you can open them in your browser and all source files (except for cake configuration) are in src folder. In this example, all .coffee files are compiled and combined in one output .js file which is then included in html.
Alternatively, you could use the --bare flag, compile to JavaScript, and then perhaps wrap the JS if necessary. But this would likely create problems; for instance, if you have one file with the code
i = 0
foo = -> i++
...
foo()
then there's only one var i declaration in the resulting JavaScript, and i will be incremented. But if you moved the foo function declaration to another CoffeeScript file, then its i would live in the foo scope, and the outer i would be unaffected.
So concatenating the CoffeeScript is a wiser solution, but there's still potential for confusion there; the order in which you concatenate your code is almost certainly going to matter. I strongly recommend modularizing your code instead.

When using Webby/Compass Integration what directory do the *.sass files go in?

I just setup Webby/Compass integration.
(https://github.com/Compass/compass/wiki/webby-integration)
Where do I put my Compass/Sass source files, and in what directory do they get
output as stylesheets?
You can put your SASS files wherever you want (under the 'content/' directory). So if the directory containing your CSS files is 'content/css', then put them there.
The only important thing is that you set the metadata part correctly, at the top of the SASS file itself. Like this:
$ cat content/css/site.sass
---
filter: sass
extension: css
layout: nil
---
[..cut..]
It looks like you can set the source-file yourself, from the documentation:
Compass.configuration do |config|
config.project_path = File.dirname(__FILE__)
config.sass_dir = File.join('src', 'stylesheets' )
end
It looks like it defaults to "src/stylesheets". When you build it it will probably get rendered to "output/css/" but I never used webby myself so im not 100% sure.
Okay found it in this repository
Apparently it belongs in the ./content/stylesheets directory of your webby project, and is output to the ./output/stylesheets directory.
What perplexes me is "why" it works this way. Why File.join? It looks like the default "src" is being replaced by "stylesheets" rather than joining a new string. Curious.

Resources