Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding code and specs for creating empty structure with --init switch #517

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/docs/defining_steps/using_table_mapping.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Feature: Table mapping
}
"""
When I run `cucumber features/f.feature`
Then the stderr should contain a warning message
Then the stderr should not contain anything
And it should pass with:
"""
Feature: with table
Expand Down
2 changes: 1 addition & 1 deletion features/docs/wire_protocol_table_diffing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Feature: Wire protocol table diffing
| ["diff_failed"] | ["fail",{"message":"Not same", "exception":"DifferentException", "backtrace":["a.cs:12","b.cs:34"]}] |
| ["end_scenario"] | ["success"] |
When I run `cucumber -f progress --backtrace`
Then the stderr should contain a warning message
Then the stderr should not contain anything
And it should fail with:
"""
F
Expand Down
2 changes: 2 additions & 0 deletions lib/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'cucumber/runtime'
require 'cucumber/cli/main'
require 'cucumber/broadcaster'
require 'cucumber/skeleton_creator'
require 'cucumber/step_definitions'
require 'cucumber/term/ansicolor'

Expand All @@ -26,3 +27,4 @@ def logger=(logger)
end
end
end

12 changes: 9 additions & 3 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def to_sexp #:nodoc:
# # => ['phone number', 'ADDRESS']
#
def map_headers!(mappings={}, &block)
Kernel.warn "[warning] map_headers! will be deprecated, please use map_headers instead."
# TODO: Remove this method for 2.0
clear_cache!
@header_mappings = mappings
@header_conversion_proc = block
Expand All @@ -261,11 +261,18 @@ def map_headers(mappings={}, &block)
# end
#
def map_column!(column_name, strict=true, &conversion_proc)
Kernel.warn "[warning] map_column! will be deprecated"
# TODO: Remove this method for 2.0
@conversion_procs[column_name.to_s] = { :strict => strict, :proc => conversion_proc }
self
end

# Returns a new Table with an additional column mapping. See #map_column!
def map_column(column_name, strict=true, &conversion_proc)
conversion_procs = @conversion_procs.dup
conversion_procs[column_name.to_s] = { :strict => strict, :proc => conversion_proc }
self.class.new(raw.dup, conversion_procs, @header_mappings.dup, @header_conversion_proc)
end

# Compares +other_table+ to self. If +other_table+ contains columns
# and/or rows that are not in self, new columns/rows are added at the
# relevant positions, marking the cells in those rows/columns as
Expand Down Expand Up @@ -303,7 +310,6 @@ def map_column!(column_name, strict=true, &conversion_proc)
# a Table argument, if you want to compare that table to some actual values.
#
def diff!(other_table, options={})
Kernel.warn "[warning] diff! will be deprecated"
options = {
:missing_row => true,
:surplus_row => true,
Expand Down
11 changes: 11 additions & 0 deletions lib/cucumber/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ def parse!(args)
opts.on("--dotcucumber DIR", "Write metadata to DIR") do |dir|
@options[:dotcucumber] = dir
end
opts.on_tail("--init", "Should create an empty structure for your code",
"Creating an empty structure for BDD") do
unless File.directory?("features") || File.exists?("cucumber.yml")
Cucumber::SkeletonCreator.run
@out_stream.puts "Cucumber has built an empty folder structure"
Kernel.exit(0)
else
@out_stream.puts "'features' directory or 'cucumber.yml' exists, so no structure has been build"
Kernel.exit(1)
end
end
opts.on_tail("--version", "Show version.") do
@out_stream.puts Cucumber::VERSION
Kernel.exit(0)
Expand Down
2 changes: 0 additions & 2 deletions lib/cucumber/core_ext/disable_mini_and_test_unit_autorun.rb

This file was deleted.

7 changes: 6 additions & 1 deletion lib/cucumber/runtime.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'fileutils'
require 'multi_json'
require 'multi_test'
require 'gherkin/rubify'
require 'gherkin/i18n'
require 'cucumber/configuration'
Expand All @@ -20,7 +21,6 @@ class Runtime
include Runtime::UserInterface

def initialize(configuration = Configuration.default)
require 'cucumber/core_ext/disable_mini_and_test_unit_autorun'
@current_scenario = nil
@configuration = Configuration.parse(configuration)
@support_code = SupportCode.new(self, @configuration)
Expand All @@ -40,6 +40,7 @@ def load_programming_language(language)

def run!
load_step_definitions
disable_minitest_test_unit_autorun
fire_after_configuration_hook

tree_walker = @configuration.build_tree_walker(self)
Expand Down Expand Up @@ -184,6 +185,10 @@ def load_step_definitions
@support_code.load_files!(files)
end

def disable_minitest_test_unit_autorun
MultiTest.disable_autorun
end

def log
Cucumber.logger
end
Expand Down
12 changes: 12 additions & 0 deletions lib/cucumber/skeleton_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Cucumber
# The class which is responsible for default directory structure
class SkeletonCreator
def self.run()
require 'FileUtils' unless defined?(FileUtils)
FileUtils.mkdir_p 'features/step_definitions'
FileUtils.mkdir_p 'features/support'
FileUtils.touch 'cucumber.yml'
FileUtils.touch 'features/support/env.rb'
end
end
end
47 changes: 47 additions & 0 deletions spec/cucumber/ast/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,53 @@ def @table.columns; super; end
end
end

describe '#map_column' do
it "should allow mapping columns" do
new_table = @table.map_column('one') { |v| v.to_i }
new_table.hashes.first['one'].should == 4444
end

it "applies the block once to each value" do
headers = ['header']
rows = ['value']
table = Table.new [headers, rows]
count = 0
new_table = table.map_column('header') { |value| count +=1 }
new_table.rows
count.should eq rows.size
end

it "should allow mapping columns and take a symbol as the column name" do
new_table = @table.map_column(:one) { |v| v.to_i }
new_table.hashes.first['one'].should == 4444
end

it "should allow mapping columns and modify the rows as well" do
new_table = @table.map_column(:one) { |v| v.to_i }
new_table.rows.first.should include(4444)
new_table.rows.first.should_not include('4444')
end

it "should pass silently if a mapped column does not exist in non-strict mode" do
lambda {
new_table = @table.map_column('two', false) { |v| v.to_i }
new_table.hashes
}.should_not raise_error
end

it "should fail if a mapped column does not exist in strict mode" do
lambda {
new_table = @table.map_column('two', true) { |v| v.to_i }
new_table.hashes
}.should raise_error('The column named "two" does not exist')
end

it "should return a new table" do
(@table.map_column(:one) { |v| v.to_i }).should_not == @table
end
end


describe "#match" do
before(:each) do
@table = Table.new([
Expand Down
32 changes: 32 additions & 0 deletions spec/cucumber/cli/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,38 @@ def after_parsing(args)
end
end


context '--init' do
it "should fail, because 'feature' directory exist" do
after_parsing('--init') do
output_stream.string.should =~ /'features' directory or 'cucumber.yml' exists, so no structure has been build/
end
end
it "exits the program" do
when_parsing('--init') { Kernel.should_receive(:exit) }
end

it "should work in empty directory" do
require 'tmpdir' unless defined?(Dir.mktmpdir)
require 'FileUtils' unless defined?(FileUtils)
test_dir = Dir.mktmpdir
current_dir = FileUtils.pwd
FileUtils.cd(test_dir)
after_parsing('--init') do
output_stream.string.should =~ /Cucumber has built an empty folder structure/
File.exists?("cucumber.yml").should be_true
File.directory?("features").should be_true
FileUtils.cd("features")
File.directory?("step_definitions").should be_true
File.directory?("support").should be_true
FileUtils.cd("support")
File.exists?("env.rb").should be_true
end
FileUtils.cd(current_dir)
FileUtils.rm_rf(test_dir)
end
end

context 'environment variables (i.e. MODE=webrat)' do
it "places all of the environment variables into a hash" do
after_parsing('MODE=webrat FOO=bar') do
Expand Down