Class: Opal::Test::Unit::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/test/unit/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory, **options) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
# File 'lib/opal/test/unit/runner.rb', line 7

def initialize(directory, **options)
  @directory = directory
  @options = options
end

Instance Method Details

#runObject



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
# File 'lib/opal/test/unit/runner.rb', line 12

def run
  tmpfile_path = "#{Dir.tmpdir}/tmp.rb"
  File.open(tmpfile_path, "w") do |tmpfile|
    Dir.glob("#{@directory}/**/*test.rb").each do |filename|
      tmpfile.write("require \"#{filename.sub(/^#{@directory}\//, "")}\"\n")
    end
    tmpfile.write(<<~SRC)
      require "opal/platform"

      run_test = Proc.new do
        result = Opal::Test::Unit::TestCase.run

        Opal::Test::Unit::ResultPrinter.print_summary(result)
        if result.failure_messages.count > 0 || result.errors.count > 0
          exit 1
        end
      end

      %x(
        var isNode = (typeof process !== "undefined" && typeof require !== "undefined");
        if (isNode) {
          run_test();
        } else {
          window.onload = run_test;
        }
      )
    SRC
  end
  run_on_opal @directory, tmpfile_path

  File.delete tmpfile_path

  puts "Done."
end

#run_on_opal(dir, filename) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/opal/test/unit/runner.rb', line 47

def run_on_opal(dir, filename)
  options_hash = {
    file:     File.open(filename),
    filename: File.basename(filename),
    load_paths: $LOAD_PATH + ["./lib", dir]
  }.merge(@options)

  cli = Opal::CLI.new options_hash

  begin
    cli.run
    exit cli.exit_status
  rescue Opal::CliRunners::RunnerError => e
    $stderr.puts e.message
    exit 72
  end
end