aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_helper.rb
blob: 2d4fe8f664c4e97bda2a072df2a321efcc8a8ef0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# encoding: utf-8
require "simplecov"
require "tmpdir"
require "fileutils"
require "minitest/autorun"
require "shoulda"
require "timeout"
require "logger"
require "date"

# require gem files
require "roo"
require "minitest/reporters"
if ENV["USE_REPORTERS"]
  Minitest::Reporters.use!(
    [
      Minitest::Reporters::DefaultReporter.new,
      Minitest::Reporters::SpecReporter.new
    ]
  )
end

TESTDIR = File.join(File.dirname(__FILE__), "files")
ROO_FORMATS = [
  :excelx,
  :excelxm,
  :openoffice,
  :libreoffice
]

require "helpers/test_accessing_files"
require "helpers/test_comments"
require "helpers/test_formulas"
require "helpers/test_labels"
require "helpers/test_sheets"
require "helpers/test_styles"


# very simple diff implementation
# output is an empty string if the files are equal
# otherwise differences a printen (not compatible to
# the diff command)
def file_diff(fn1,fn2)
  result = ""
  File.open(fn1) do |f1|
    File.open(fn2) do |f2|
      while f1.eof? == false and f2.eof? == false
        line1 = f1.gets.chomp
        line2 = f2.gets.chomp
        result << "<#{line1}\n>#{line2}\n" if line1 != line2
      end
      if f1.eof? == false
        while f1.eof? == false
          line1 = f1.gets
          result << "<#{line1}\n"
        end
      end
      if f2.eof? == false
        while f2.eof? == false
          line2 = f2.gets
          result << ">#{line2}\n"
        end
      end
    end
  end
  result
end

class File
  def File.delete_if_exist(filename)
    if File.exist?(filename)
      File.delete(filename)
    end
  end
end

def local_server(port)
  raise ArgumentError unless port.to_i > 0
  "http://0.0.0.0:#{port}"
end

def start_local_server(filename, port = nil)
  require "rackup"
  content_type = filename.split(".").last
  port ||= TEST_RACK_PORT

  web_server = Proc.new do |env|
    [
      "200",
      { "Content-Type" => content_type },
      [File.read("#{TESTDIR}/#{filename}")]
    ]
  end

  t = Thread.new { Rackup::Handler::WEBrick.run web_server, Host: "0.0.0.0", Port: port , Logger: WEBrick::BasicLog.new(nil,1) }
  # give the app a chance to startup
  sleep(0.2)

  yield
ensure
  t.kill
end

# call a block of code for each spreadsheet type
# and yield a reference to the roo object
def with_each_spreadsheet(options)
  if options[:format]
    formats = Array(options[:format])
    invalid_formats = formats - ROO_FORMATS
    unless invalid_formats.empty?
      raise "invalid spreadsheet types: #{invalid_formats.join(', ')}"
    end
  else
    formats = ROO_FORMATS
  end
  formats.each do |format|
    begin
      yield Roo::Spreadsheet.open(File.join(TESTDIR,
        fixture_filename(options[:name], format)))
    rescue => e
      raise e, "#{e.message} for #{format}", e.backtrace unless options[:ignore_errors]
    end
  end
end

def get_extension(oo)
  case oo
  when Roo::OpenOffice
    ".ods"
  when Roo::Excelx
    ".xlsx"
  end
end

def fixture_filename(name, format)
  case format
  when :excelx
    "#{name}.xlsx"
  when :excelxm
    "#{name}.xlsm"
  when :openoffice, :libreoffice
    "#{name}.ods"
  else
    raise ArgumentError, "unexpected format #{format}"
  end
end

def skip_long_test
  msg = "This is very slow, test use `LONG_RUN=true bundle exec rake` to run it"
  skip(msg) unless ENV["LONG_RUN"]
end

def skip_jruby_incompatible_test
  msg = "This test uses a feature incompatible with JRuby"
  skip(msg) if defined?(JRUBY_VERSION)
end

def with_timezone(new_tz)
  if new_tz
    begin
      prev_tz, ENV['TZ'] = ENV['TZ'], new_tz
      yield
    ensure
      ENV['TZ'] = prev_tz
    end
  else
    yield
  end
end