I'm new to cucumber and I just found out Before hooks.
I'm already doing this on minitest/spec.
I want to create a git repository before any scenario and destroy it after.
this is what I have:
Before do
require 'tmpdir'
#directory = Dir.mktmpdir('temp-repo')
#orig_directory = Dir.pwd
Dir.chdir(#directory)
`git init`
`touch dummy`
`git add .`
`git commit -m 'dummy commit'`
end
After do
Dir.chdir(#orig_directory)
FileUtils.rmtree(#directory)
end
But when i run cucumber it fails with this message:
Lexing error on line 6: ' #directory = Dir.mktmpdir('temp-repo')'
I already looked into the wiki and some other questions here but can't figure out how to get that working.
The hooks should be registered in the support files (for example features/support/env.rb) not in your features.
It's stated in the second sentence of the wiki.
Related
I'm building a simple utility for cloning down git repositories of other ruby programs my company has built, then install and configure them.
So, I'm very new to Ruby, so I apologize if this is a super noob question.
But I have a class that handles creating the directories it needs and cloning the repo, but it's returning this error when I run the code, and I don't know why...
This is the error:
Cloning the requested Git Repository now!
-----------------------------------------
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/git-1.12.0/lib/git/lib.rb:1125:in `command': git "-c" "core.quotePath=true"
"-c" "color.ui=false" clone "--" "" "{:path=>\\"\\", :bare=>true}" 2>&1:fatal: The empty string is not a valid path (Git::GitExecuteError)
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/git-1.12.0/lib/git/lib.rb:116:in `clone'
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/git-1.12.0/lib/git/base.rb:21:in `clone'
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/git-1.12.0/lib/git.rb:176:in `clone'
from C:/Users/conno/OneDrive/Desktop/Ruby Installer/Pixelated-Ruby-Installer/classes/Gitclone.rb:43:in `clone'
from ./starter.rb:48:in `<main>'
This is my code (again, remember I am very new. So literally any and all advice is helpful):
#!/bin/ruby
# class declaration
class Gitclone
require "fileutils"
require "git"
require "rubygems"
# attribute reader so other classes/methods can read the vars in this class
attr_reader :giturl, :destpa
# init class method to set instance variables for giturl and destination path
def initialize(giturl, destpa)
#giturl = giturl
#destpa = destpa
end
# class method to create directories and clone git repositories
def self.clone
# I don't think this needs to be here. It was added for the sake of seeing if it would fix the issue
giturl = #giturl
destpa = #destpa
# see last comment about the above 2 lines
puts "Creating directories!"
21.times {
print "-"
}
puts ""
sleep(2)
# right now, this is hardcoded. I should probably make it it's own method we can call...
# it creates new directories
FileUtils.mkdir_p "/etc/pixelated/ruby/bin"
puts "We have created the /etc/pixelated/ruby/bin directory!"
54.times {
print "-"
}
puts ""
sleep(2)
puts "Cloning the requested Git Repository now!"
41.times {
print "-"
}
puts ""
sleep(2)
# this bit here simply uses the Git gem to clone the repository URL stored in the 'giturl' instance variable to the path stored in the 'destpa' instance variable
git = Git.clone("#{giturl}", path: "#{destpa}", bare: true)
# notify the user if the repository was cloned successfuly
if Dir.exist?("/etc/pixelated/ruby/bin/$softtype")
then
puts "Successfully Cloned the Repo!"
puts ""
else
puts "ERROR! Repo was not cloned! Did you give us the right link?"
puts ""
end
end
end
I did declare the variables that I'm using, yet it apparently thinks they haven't?
I am almost certain that this is my noob brain using variable wrong...
Can someone offer me some guidance please?
EDIT: I may have answered my own question. I realized I'm booted into my Windows OS, not Arch Linux, and my code is using Linux paths, not Windows paths. Gonna put it up on one of my ubuntu servers and see if it works there.
So, I am trying to run the test but I am getting an error says.
Aruba::LaunchError:Command "seedly-calculator.rb" not found in PATH-variable
-seedly-calculator
-bin
-src
-seedly-calculator.rb
I have tried to change the path in rake file but it doesn't work.
My seedly-calculator.rb file is in the root directory.
require "rspec/core/rake_task"
namespace :spec do
desc "Run the functional suite against the CLI"
RSpec::Core::RakeTask.new(:functional, [] => [:set_path])
task :set_path do
project_bin_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', 'bin')
ENV['PATH'] = project_bin_dir + ':'+ ENV['PATH']
end
end
it shows error like:
Failure/Error: let(:command) { run "seedly-calculator.rb" }
Aruba::LaunchError:
Command "seedly-calculator.rb" not found in PATH-variable "/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/exe:/Users/bilaltariq/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/../bin:/Users/bilaltariq/.rbenv/versions/2.6.2/bin:/usr/local/Cellar/rbenv/1.1.1/libexec:/Users/bilaltariq/.rbenv/shims:/Users/bilaltariq/.asdf/shims:/Users/bilaltariq/.asdf/bin:/usr/local/bin:/Users/bilaltariq/.bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin".
I expect it to hit the file so i can write some test.
am i doing something wrong?
require 'spec_helper'
RSpec.describe 'Command Validation', type: :aruba do
let(:command) { run "seedly-calculator.rb" }
it "wrong/missing arguments" do
command.write("lookup\n")
stop_all_commands
expect(command.output).to end_with("Missing bank_name argument.\n")
end
end
seedly-calculator.rb:
#!/usr/bin/env ruby
# Complete bin/setup so that after it is
# run, ruby seedly-calculator.rb can be used to launch
# it.
# frozen_string_literal: true
require_relative './src/runner'
if !ARGV.length.zero?
input = ARGV
Runner.new.send('process_input', input)
else
puts "Arguments required!."
end
Update
To run a ruby script using run you need to make sure your ruby script is executable and contains a shebang so your system knows to run it with ruby. Here's example from this starter example
#!/usr/bin/env ruby
file = ARGV[0]
if file.nil? || file.empty?
abort "aruba-test-cli [file]: Filename is missing"
elsif !File.exist? file
abort "aruba-test-cli [file]: File does not exist"
end
puts File.read(file).chomp
So in your case you'll need to add this to the first line of your seedly-calculator.rb file
#!/usr/bin/env ruby
Then run this from command line to make it executable.
chmod +x #!/usr/bin/env ruby
I made a simple example forked off the one I reffed above. See this commit
Rspec convention is that it should match the same file structure of your project. It is not a good idea to set PATH manually.
Rake tasks are normally put in a tasks folder so you should have in project root a tasks folder
my_project/tasks/something.rake
Then you should have a spec folder that matches
my_project/spec/tasks/something_spec.rb
Then you should be able to get rid of task :set_path do end block and just run the spec without that.
You should also have a Gemfile to load your gems, run bundle install then invoke your test with
bundle exec rspec spec/tasks/sometask_spec.rb
I'm automating a bit of git workflow and have found some curious behaviour when using the git gem's in_branch method and wondered if anyone could explain why or how this issue occurs? Here's some test code that should reproduce the issue:
#!/usr/bin/env ruby
require 'git'
# git details
repo_name = 'SOME_REPO'
working_dir = "#SOME_DIR/git_test_repo"
repo_owner = 'SOME_GIT_USER'
repo_host = 'SOME_GIT_HOST'
repo_dir = "#{working_dir}/#{repo_name}"
remote_repo = "git##{repo_host}:#{repo_owner}/#{repo_name}.git"
branch_name = 'testbranch'
commit_message = 'log line breaking in_branch test'
Dir.mkdir(working_dir) unless Dir.exist?(working_dir)
Dir.chdir(working_dir)
Git.clone(remote_repo, repo_name) unless Dir.exists?(repo_dir)
repo = Git.open(repo_dir)
repo.pull(remote = 'origin', branch = 'master')
repo.branch(branch_name).in_branch(message = commit_message) do
File.write(repo_dir + '/test.txt', Time.now)
repo.add('.')
# -----this line breaks it --------------
puts 'committing changes'
# ---------------------------------------
end
When this code runs, the last puts line before the end of the in_branch block, when actually run, somehow causes the changes in the branch to be reverted, but when it's commented out, all the code behaves as expected. I've tested output lines anywhere in the block, and they all behave fine. It seems to happen across many versions of ruby (custom installs, rvm installs) and different OS's (linuxes and mac).
Is there some arcane behaviour of ruby and its terminal output I need to be aware of here?
Not really "arcane behavior of ruby". But the result of the last line in a method is what the method returns. Putting a puts in that position will usually break the method.
Just testing and I see the same behavior using print (which does not add a newline).
This is not a newline issue, moving the (now print) line above the repo.add line and all works.
Anymore thoughts?
I've inherited a Rails (3) app and am trying to get to grips with the existing Cucumber tests. I've got the following setup in the app's 'features' folder (I've missed out any files which aren't relevant, eg extra features and steps)
/features
/people
new-person.feature
/step_definitions
people_steps.rb
web_steps.rb
/support
env.rb
paths.rb
selectors.rb
If I run 'rake' then it runs all the features in features/people/new-person.feature, correctly using the steps listed in step_definitions.
However, I don't want to run rake every time as it takes too long, I just want to run a specific test in Cucumber, e.g. cucumber features/people/new-person.feature -l 8
When I do this, it runs the feature but hasn't loaded the steps. I get this back:
Using the default profile...
Feature: Add a new person
In order to allocate tasks to people
As a community manager
I want to add a new person
Scenario: Secondary navigation should contain "Add new person" # features/people/new-person.feature:8
Given I am on the new person page # features/people/new-person.feature:9
Undefined step: "I am on the new person page" (Cucumber::Undefined)
features/people/new-person.feature:9:in `Given I am on the new person page'
Then I should not see "Add new person" # features/people/new-person.feature:10
Undefined step: "I should not see "Add new person"" (Cucumber::Undefined)
features/people/new-person.feature:10:in `Then I should not see "Add new person"'
1 scenario (1 undefined)
2 steps (2 undefined)
0m0.005s
You can implement step definitions for undefined steps with these snippets:
Given /^I am on the new person page$/ do
pending # express the regexp above with the code you wish you had
end
Then /^I should not see "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
If you want snippets in a different programming language, just make sure a file
with the appropriate file extension exists where cucumber looks for step definitions.
Why isn't Cucumber loading the steps in? I'm guessing that I need to require the steps somewhere but I can't work out where.
Thanks, Max
Max Williams found the answer to his question:
EDIT - found the answer, here: https://rspec.lighthouseapp.com/projects/16211/tickets/401-envrb-not-loaded-when-running-individual-features-in-sub-directories
In config/cucumber.yml there's a line that looks like this:
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~#wip"
change it to
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~#wip --require features/"
This is like adding --require features/ to the end of your cucumber test run and makes it load everything up properly.
I did not already have a cucumber.yml file to change, for whatever reason. So, simply creating a blank /config/cucumber.yml file and putting the line in it:
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~#wip --require features/"
did not work. (That is not surprising, I figured there was supposed to be more to it than just that one line.)
I found a good example of a FULL, working, cucumber.yml example file here: http://jdfrens.blogspot.com/2010/03/uh-yes-i-did-define-that-cucumber-step.html
I have ~30 git repositories cloned from github that I use for web/ruby/javascript development. Is it possible to bulk update all of them with a script?
I have everything pretty organized (folder structure):
- Workspace
- Android
- Chrome
- GitClones
- Bootstrap
~ etc...30 some repositories
- iPhone
- osx
- WebDev
I have a ruby script to clone repositories with octokit, but are there any suggestions on how to do git pull (overwriting/rebasing local) in all the repositories under GitClones?
Normally I would just do a pull whenever I was about to use that repo, but I am going to a place where internet connectivity is only going to be available sometimes. So I would like to update everything I can while I have internet.
Thanks! (Running osx 10.8.2)
If you must do it in Ruby, here's a quick and dirty script:
#!/usr/bin/env ruby
Dir.entries('./').select do |entry|
next if %w{. .. ,,}.include? entry
if File.directory? File.join('./', entry)
cmd = "cd #{entry} && git pull"
`#{cmd}`
end
end
Don't forget to chmod +x the file you copy this into and ensure it's in your GitClones directory.
Sure, but why use ruby when shell will suffice?
function update_all() {
for dir in GitClones/*; do
cd "$dir" && git pull
done
}
Change beginning of glob to taste. This does two useful things:
It only git pull when it contains .git subdir
It skips dot (.) dirs since no one has git repos which start with a dot.
Enjoy
# Assumes run from Workspace
Dir['GitClones/[^.]*'].select {|e| File.directory? e }.each do |e|
Dir.chdir(e) { `git pull` } if File.exist? File.join(e, '.git')
end
Revised to provider better output and be OS agnostic. This one cleans local changes, and updates code.
#!/usr/bin/env ruby
require 'pp'
# no stdout buffering
STDOUT.sync = true
# checks for windows/unix for chaining commands
OS_COMMAND_CHAIN = RUBY_PLATFORM =~ /mswin|mingw|cygwin/ ? "&" : ";"
Dir.entries('.').select do |entry|
next if %w{. .. ,,}.include? entry
if File.directory? File.join('.', entry)
if File.directory? File.join('.', entry, '.git')
full_path = "#{Dir.pwd}/#{entry}"
git_dir = "--git-dir=#{full_path}/.git --work-tree=#{full_path}"
puts "\nUPDATING '#{full_path}' \n\n"
puts `git #{git_dir} clean -f #{OS_COMMAND_CHAIN} git #{git_dir} checkout . #{OS_COMMAND_CHAIN} git #{git_dir} pull`
end
end
end