Class: Cukable::Converter

Inherits:
Object
  • Object
show all
Includes:
Conversion, Helper
Defined in:
lib/cukable/conversion.rb

Overview

Main front-end for converting features, used by the cuke2fit script.

Instance Method Summary (collapse)

Methods included from Conversion

#feature_to_fitnesse, #fitnesse_to_features

Methods included from Helper

#clean_cell, #clean_filename, #literalize, #remove_cruft, #table_digest, #unescape, #wikify, #wikify_path

Instance Method Details

- (Object) create_accelerator(fitnesse_path)

Create an empty AaaAccelerator page under the given path.



312
313
314
315
316
# File 'lib/cukable/conversion.rb', line 312

def create_accelerator(fitnesse_path)
  accel_path = File.join(fitnesse_path, 'AaaAccelerator')
  FileUtils.mkdir_p(accel_path)
  create_wiki_page(accel_path, '', 'test')
end

- (Object) create_feature_root(fitnesse_path)

Create a page at the top level of the converted features, containing variable definitions needed to invoke rubyslim



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/cukable/conversion.rb', line 279

def create_feature_root(fitnesse_path)
  FileUtils.mkdir_p(fitnesse_path)
  content = [
    'These variables must be defined for rubyslim to work:',
    '!define TEST_SYSTEM {slim}',
    '!define TEST_RUNNER {rubyslim}',
    '!define COMMAND_PATTERN {rubyslim}',
    '',
    'Extra command-line arguments to pass to Cucumber:',
    '!define CUCUMBER_ARGS {}',
    '',
    '!contents -R9 -p -f -h',
  ].join("\n")
  create_wiki_page(fitnesse_path, content, 'suite')
end

- (Object) create_setup_page(fitnesse_path)

Create a SetUp page as a child of the given path.



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/cukable/conversion.rb', line 297

def create_setup_page(fitnesse_path)
  setup_path = File.join(fitnesse_path, 'SetUp')
  FileUtils.mkdir_p(setup_path)
  content = [
    '!| import |',
    '| Cukable |',
    '',
    '| script | Cuke |',
    '| accelerate; | ${PAGE_PATH}.${PAGE_NAME} | ${CUCUMBER_ARGS} |',
  ].join("\n")
  create_wiki_page(setup_path, content)
end

- (Object) create_suite_stubs(wiki_path, root_path)

Create a stub content.txt file in the given directory, and all ancestor directories, if a content.txt does not already exist.

Examples:

create_suite_stubs('FitNesseRoot/PageOne/PageTwo/PageThree')
  # Creates these files, and their containing directories:
  #   FitNesseRoot/PageOne/content.txt
  #   FitNesseRoot/PageOne/PageTwo/content.txt
  #   FitNesseRoot/PageOne/PageTwo/PageThree/content.txt


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/cukable/conversion.rb', line 256

def create_suite_stubs(wiki_path, root_path)
  # Content string to put in each stub file
  content = '!contents -R9 -p -f -h'
  # Starting with `wiki_path`
  path = wiki_path
  # Until the root level is reached
  until path == root_path || path == '.'
    # If there is no content.txt file, create one
    if !File.exists?(File.join(path, 'content.txt'))
      create_wiki_page(path, content, 'suite')
    end
    # If there is no accelerator child, create one
    if !File.directory?(File.join(path, 'AaaAccelerator'))
      create_accelerator(path)
    end
    # Get the parent path
    path = File.dirname(path)
  end
end

- (Object) create_wiki_page(path, content, type = 'normal')

Create a new wiki page at the given path, with the given content.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/cukable/conversion.rb', line 211

def create_wiki_page(path, content, type='normal')
  FileUtils.mkdir_p(path)
  # Write the content
  File.open(File.join(path, 'content.txt'), 'w') do |file|
    file.write(content)
  end
  # Write the properties.xml
  File.open(File.join(path, 'properties.xml'), 'w') do |file|
    file.puts '<?xml version="1.0"?>'
    file.puts '<properties>'
    file.puts '  <Edit>true</Edit>'
    file.puts '  <Files>true</Files>'
    file.puts '  <Properties>true</Properties>'
    file.puts '  <RecentChanges>true</RecentChanges>'
    file.puts '  <Refactor>true</Refactor>'
    file.puts '  <Search>true</Search>'
    if type == 'test'
      file.puts '  <Test/>'
    elsif type == 'suite'
      file.puts '  <Suite/>'
    end
    file.puts '  <Versions>true</Versions>'
    file.puts '  <WhereUsed>true</WhereUsed>'
    file.puts '</properties>'
  end
end

- (Object) features_to_fitnesse(features_path, fitnesse_path)

Convert all .feature files in features_path to FitNesse wiki pages under fitnesse_path.

Examples:

features_to_fitnesse('features/account', 'FitNesseRoot/AccountTests')


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cukable/conversion.rb', line 164

def features_to_fitnesse(features_path, fitnesse_path)
  # Status messages to return
  messages = []

  # Strip trailing slash
  fitnesse_path = fitnesse_path.gsub(/\/$/, '')

  # Ensure FitNesse directory already exists
  if !File.directory?(fitnesse_path)
    raise ArgumentError, "FitNesse path must be an existing directory."
  end

  # Create a root-level suite and SetUp page
  root_path = File.join(fitnesse_path, wikify_path(features_path))
  create_feature_root(root_path)
  create_setup_page(root_path)

  # Get all .feature files
  features = Dir.glob(File.join(features_path, '**', '*feature'))

  # Create a test page for each .feature file
  features.each do |feature_path|
    # Determine the appropriate wiki path name
    wiki_path = File.join(fitnesse_path, wikify_path(feature_path))
    # Fill ancestors of the wiki path with stubs for suites
    create_suite_stubs(File.dirname(wiki_path), fitnesse_path)
    # Convert the .feature to wikitext
    content = feature_to_fitnesse(File.open(feature_path)).join("\n")
    # Write the wikitext to a wiki page
    create_wiki_page(wiki_path, content, 'test')
    # Show user some status output
    messages << "OK: #{feature_path} => #{wiki_path}"
  end
  return messages
end