Class: Rasta::Fixture::FixtureRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rasta/fixture_runner.rb

Overview

Manage requests to run spreadsheets and send each to the FixtureLoader

Instance Method Summary collapse

Constructor Details

#initializeFixtureRunner

Returns a new instance of FixtureRunner.



59
60
61
62
# File 'lib/rasta/fixture_runner.rb', line 59

def initialize
  @workbooks = []
  prepare_results_directory
end

Instance Method Details

#add(spreadsheet) ⇒ Object

Raises:

  • (IOError)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rasta/fixture_runner.rb', line 64

def add(spreadsheet)
  require 'rasta/spreadsheet'
  raise IOError, "File not found: #{spreadsheet}" if ! File.exists?(File.expand_path(spreadsheet))
  workingfile = copy(spreadsheet)
  config = Configuration.instance
  @excel = Rasta::Spreadsheet::Excel.instance
  @excel.pagecount = config.pagecount.to_i
  @excel.recordcount = config.recordcount.to_i
  @excel.continue = config.continue
  @excel.visible = config.visible
  @workbooks << workingfile
end

#copy(spreadsheet) ⇒ Object

Copies the spreadsheet with results in the [results_path] parameter provided when calling runxls.rb. If no parameter is provided, the results are saved into the results folder within the working dir.



81
82
83
84
85
86
87
88
89
# File 'lib/rasta/fixture_runner.rb', line 81

def copy(spreadsheet)
  config = Configuration.instance
  testfilename = generate_testfile(spreadsheet)
  while File.exists?(File.expand_path(testfilename))
    testfilename = generate_testfile(spreadsheet)
  end
  FileUtils.cp(spreadsheet, testfilename)
  return testfilename
end

#do_require(filename) ⇒ Object

Raises:

  • (LoadError)


141
142
143
# File 'lib/rasta/fixture_runner.rb', line 141

def do_require(filename)
  raise LoadError, "Unable to require file '#{filename}'" unless require filename
end

#executeObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rasta/fixture_runner.rb', line 145

def execute
  loaded_classes = load_test_fixtures
  initialize_rspec
  begin
    start_rspec
    @workbooks.each do |workbook| 
      begin
        book = Rasta::Spreadsheet::Book.new(workbook)
        book.each do |sheet| 
          FixtureLoader.new(sheet, loaded_classes)
        end
      ensure
        book.save
      end
    end
    # move this in the loop once I work out the 
    # way to do this in rspec properly
    stop_rspec
  ensure
    @excel.cleanup
  end
end

#generate_testfile(spreadsheet) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/rasta/fixture_runner.rb', line 90

def generate_testfile(spreadsheet)
  config = Configuration.instance
  config.result_index += 1
  filename = config.results_path + '/' + File.basename(spreadsheet) 
  filename.sub!(/(\.\w{3})$/, ".#{config.result_index}" + '\1')
  return filename
end

#load_test_fixturesObject

Load the files in the fixture path and track which classes got loaded so we can hopefully reduce the chance of namespace issues. There may be a better way to handle this



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rasta/fixture_runner.rb', line 120

def load_test_fixtures
  before_classes = []
  after_classes = []
  ObjectSpace.each_object(Class) { |x| before_classes << x.name } 
  config = Configuration.instance
  if config.require.size > 0
    config.require.each { |file| do_require file }
  else
    # Look through all of the .rb files in the fixture path to see if 
    # we can find the file that has the class specified
    if File.directory?(config.fixture_path)
      fixture_files = File.join(config.fixture_path.gsub('\\','/'), "**", "*.rb")
      Dir.glob(fixture_files).each {|f| do_require f }
    else
      do_require config.fixture_path
    end  
  end
  ObjectSpace.each_object(Class) { |x| after_classes << x.name } 
  return (after_classes - before_classes)
end

#prepare_results_directoryObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rasta/fixture_runner.rb', line 97

def prepare_results_directory
  config = Configuration.instance
  if File.expand_path(config.results_path) != config.results_path
    config.results_path = Dir.getwd + '/' + config.results_path
  end
  # Remove the existing results 
  begin
    FileUtils.rm(Dir.glob(File.join(config.results_path, "*"))) if File.exists?(config.results_path)
  rescue Errno::EACCES
    # The file is probably open so ignore and we'll create a new file
  end
  # Create a new results directory
  begin
    FileUtils.mkdir_p(config.results_path) if !File.exists?(config.results_path)
  rescue => e
    puts "Creating directory #{config.results_path}"
    raise IOError, e.message
  end
end