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

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.

Related

How to get filepath to file in another folder unix?

I am trying to write some data in one Ruby file to a file in another folder but I am having trouble identifying the path to get to the file I want to write to.
My current code is:
File.write('../csv_fixtures/loans.csv', 'test worked!')
And my folder structure is as follows:
Where I am trying to run my code in 'run_spec.rb' and write to 'loans.csv'.
Additionally, this is the error I am getting:
Give the path relative to the working directory, not the file that you call File.write from. The working directory is the place you've navigated to through cd before calling the ruby code. If you ran rspec from the root of your project, then the working directory will also be the root. So, in this case, it looks like it would be ./spec/csv_fixtures/loans.csv. You can run puts Dir.pwd to see the working directory that all paths should be relative to.
If you wanted to something more like require_relative, you have to use some sort of workaround to turn it into an absolute path, such as File.dirname(__FILE__) which gives the absolute path of the folder containing the current file:
path = File.join(File.dirname(__FILE__, "../csv_fixtures/loans.csv"))

How to find text file in same directory

I am trying to read a list of baby names from the year 1880 in CSV format. My program, when run in the terminal on OS X returns an error indicating yob1880.txt doesnt exist.
No such file or directory # rb_sysopen - /names/yob1880.txt (Errno::ENOENT)
from names.rb:2:in `<main>'
The location of both the script and the text file is /Users/*****/names.
lines = []
File.expand_path('../yob1880.txt', __FILE__)
IO.foreach('../yob1880.txt') do |line|
lines << line
if lines.size >= 1000
lines = FasterCSV.parse(lines.join) rescue next
store lines
lines = []
end
end
store lines
If you're running the script from the /Users/*****/names directory, and the files also exist there, you should simply remove the "../" from your pathnames to prevent looking in /Users/***** for the files.
Use this approach to referencing your files, instead:
File.expand_path('yob1880.txt', __FILE__)
IO.foreach('yob1880.txt') do |line|
Note that the File.expand_path is doing nothing at the moment, as the return value is not captured or used for any purpose; it simply consumes resources when it executes. Depending on your actual intent, it could realistically be removed.
Going deeper on this topic, it may be better for the script to be explicit about which directory in which it locates files. Consider these approaches:
Change to the directory in which the script exists, prior to opening files
Dir.chdir(File.dirname(File.expand_path(__FILE__)))
IO.foreach('yob1880.txt') do |line|
This explicitly requires that the script and the data be stored relative to one another; in this case, they would be stored in the same directory.
Provide a specific path to the files
# do not use Dir.chdir or File.expand_path
IO.foreach('/Users/****/yob1880.txt') do |line|
This can work if the script is used in a small, contained environment, such as your own machine, but will be brittle if it data is moved to another directory or to another machine. Generally, this approach is not useful, except for short-lived scripts for personal use.
Never put a script using this approach into production use.
Work only with files in the current directory
# do not use Dir.chdir or File.expand_path
IO.foreach('yob1880.txt') do |line|
This will work if you run the script from the directory in which the data exists, but will fail if run from another directory. This approach typically works better when the script detects the contents of the directory, rather than requiring certain files to already exist there.
Many Linux/Unix utilities, such as cat and grep use this approach, if the command-line options do not override such behavior.
Accept a command-line option to find data files
require 'optparse'
base_directory = "."
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on('-d', '--dir NAME', 'Directory name') {|v| base_directory = Dir.chdir(File.dirname(File.expand_path(v))) }
end
IO.foreach(File.join(base_directory, 'yob1880.txt')) do |line|
# do lines
end
This will give your script a -d or --dir option in which to specify the directory in which to find files.
Use a configuration file to find data files
This code would allow you to use a YAML configuration file to define where the files are located:
require 'yaml'
config_filename = File.expand_path("~/yob/config.yml")
config = {}
name = nil
config = YAML.load_file(config_filename)
base_directory = config["base"]
IO.foreach(File.join(base_directory, 'yob1880.txt')) do |line|
# do lines
end
This doesn't include any error handling related to finding and loading the config file, but it gets the point across. For additional information on using a YAML config file with error handling, see my answer on Asking user for information, and never having to ask again.
Final thoughts
You have the tools to establish ways to locate your data files. You can even mix-and-match solutions for a more sophisticated solution. For instance, you could default to the current directory (or the script directory) when no config file exists, and allow the command-line option to manually override the directory, when necessary.
Here's a technique I always use when I want to normalize the current working directory for my scripts. This is a good idea because in most cases you code your script and place the supporting files in the same folder, or in a sub-folder of the main script.
This resets the current working directory to the same folder as where the script is situated in. After that it's much easier to figure out the paths to everything:
# Reset working directory to same folder as current script file
Dir.chdir(File.dirname(File.expand_path(__FILE__)))
After that you can open your data file with just:
IO.foreach('yob1880.txt')

How can I specify the file location to write and read from in Ruby?

So, I have a function that creates an object specifying user data. Then, using the Ruby YAML gem and some code, I put the object to a YAML file and save it. This saves the YAML file to the location where the Ruby script was run from. How can I tell it to save to a certain file directory? (A simplified version of) my code is this
print "Please tell me your name: "
$name=gets.chomp
$name.capitalize!
print "Please type in a four-digit PIN number: "
$pin=gets.chomp
I also have a function that enforces that the pin be a four-digit integer, but that is not important.
Then, I add this to an object
new_user=Hash.new (false)
new_user["name"]=$name
new_user["pin"]=$pin
and then add it to a YAML file and save it. If the YAML file doesn't exist, one is created. It creates it in the same file directory as the script is run in. Is there a way to change the save location?
The script fo save the object to a YAML file is this.
def put_to_yaml (new_user)
File.write("#{new_user["name"]}.yaml", new_user.to_yaml)
end
put_to_yaml(new_user)
Ultimately, the question is this: How can I change the save location of the file? And when I load it again, how can i tell it where to get the file from?
Thanks for any help
Currently when you use File.write it takes your current working directory, and appends the file name to that location. Try:
puts Dir.pwd # Will print the location you ran ruby script from.
You can specify the absolute path if you want to write it in a specific location everytime:
File.write("/home/chameleon/different_location/#{new_user["name"]}.yaml")
Or you can specify a relative path to your current working directory:
# write one level above your current working directory
File.write("../#{new_user["name"]}.yaml", new_user.to_yaml)
You can also specify relative to your current executing ruby file:
file_path = File.expand_path(File.dirname(__FILE__))
absolute_path = File.join(file_path, file_name)
File.write(absolute_path, new_user.to_yaml)
You are supplying a partial pathname (a mere file name), so we read and write from the current directory. Thus you have two choices:
Supply a full absolute pathname (personally, I like to use the Pathname class for this); or
Change the current directory first (with Dir.chdir)

How to write to parent directory of jmeter test using jmeter's 'save response to a file' node

Herro,
I know that if you use '~/' in the filename prefix then that will get you to the location of the jmeter test. What I want is to write the file to the parent directory of the test file. Is there a way to accomplish this. Basically the goal is to have the test be able to dynamically write the file to the parent directory of the test file (wherever it happens to be)
figured it out. you use '..' to go up a directory (duh) so if you wanted to name your file tendervittles and put it in the parent directory of the test file it would be
~/../tendervittles

Look for a configuration file with Ruby

I written a Ruby tool, named foobar, having a default configuration into a
file (called .foobar).
When the tool is executed without any params, the configuration file's params
can be used: ~/.foobar.
But, if the current tool's path is ~/projects/foobar and if
~/projects/foobar/.foobar exists, then this file should be used instead of
~/.foobar.
That's why the way to look for this configuration file should start from the
current folder until the current user folder.
Is there a simple way to look for this file?
cfg_file = File.open(".foobar", "r") rescue File.open("~/.foobar", "r")
Although to be honest, I almost always do two things: provide an option for a config file path, and allow overriding of default config values. The problem with the latter is that unless you know config files are ordered/have precedence, it can be confusing.
I would do this:
if File.exists(".foobar")
# open .foobar
else
# open ~/..
end

Resources