Class: LearnTest::Jasmine::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_test/jasmine/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, user_id, repo_name, options) ⇒ Runner

Returns a new instance of Runner.



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
# File 'lib/learn_test/jasmine/runner.rb', line 16

def initialize(username, user_id, repo_name, options)
  @current_test_path = FileUtils.pwd
  @no_color = !!options[:color]
  @color_opt = !no_color ? "" : "NoColor"
  @local = !!options[:local]
  @browser = !!options[:browser]
  @out = options[:out]
  @keep_results = options[:keep]
  @json_results = {
    username: username,
    github_user_id:user_id,
    repo_name: repo_name,
    build: {
      test_suite: [{
        framework: 'jasmine',
        formatted_output: [],
        duration: 0.0
      }]
    },
    tests: 0,
    errors: 0,
    failures: 0
  }
  @conn = Faraday.new(url: SERVICE_URL) do |faraday|
    faraday.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def browser
  @browser
end

#color_optObject (readonly)

Returns the value of attribute color_opt.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def color_opt
  @color_opt
end

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def conn
  @conn
end

#json_resultsObject

Returns the value of attribute json_results.



10
11
12
# File 'lib/learn_test/jasmine/runner.rb', line 10

def json_results
  @json_results
end

#keep_resultsObject (readonly)

Returns the value of attribute keep_results.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def keep_results
  @keep_results
end

#localObject (readonly)

Returns the value of attribute local.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def local
  @local
end

#no_colorObject (readonly)

Returns the value of attribute no_color.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def no_color
  @no_color
end

#outObject (readonly)

Returns the value of attribute out.



9
10
11
# File 'lib/learn_test/jasmine/runner.rb', line 9

def out
  @out
end

Class Method Details

.run(username, user_id, repo_name, options) ⇒ Object



12
13
14
# File 'lib/learn_test/jasmine/runner.rb', line 12

def self.run(username, user_id, repo_name, options)
  new(username, user_id, repo_name, options).run
end

Instance Method Details

#clean_upObject



123
124
125
126
127
# File 'lib/learn_test/jasmine/runner.rb', line 123

def clean_up
  test_xml_files.each do |file|
    FileUtils.rm(file)
  end
end

#make_jsonObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/learn_test/jasmine/runner.rb', line 63

def make_json
  if local || !browser
    test_xml_files.each do |f|
      parsed = JSON.parse(Crack::XML.parse(File.read(f)).to_json)["testsuites"]["testsuite"]
      json_results[:build][:test_suite][0][:formatted_output] << parsed["testcase"]
      json_results[:build][:test_suite][0][:formatted_output].flatten!
      json_results[:tests] += parsed["tests"].to_i
      json_results[:errors] += parsed["errors"].to_i
      json_results[:failures] += parsed["failures"].to_i
      json_results[:build][:test_suite][0][:duration] += parsed["time"].to_f
    end
    set_passing_test_count
  end

  if out || keep_results
    output_file = out ? out : '.results.json'
    write_json_output(output_file: output_file)
  end
end

#make_runner_htmlObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/learn_test/jasmine/runner.rb', line 101

def make_runner_html
  template = ERB.new(File.read("#{LearnTest::FileFinder.location_to_dir('jasmine/templates')}/SpecRunnerTemplate#{color_opt}.html.erb"))

  yaml = YAML.load(File.read('requires.yml'))["javascripts"]

  required_files = yaml["files"]
  required_specs = yaml["specs"]

  @javascripts = []
  @javascripts << (required_files && required_files.map {|f| "#{@current_test_path}/#{f}"})
  @javascripts << (required_specs && required_specs.map {|f| "#{@current_test_path}/#{f}"} )
  @javascripts.flatten!.compact!

  File.open("#{LearnTest::FileFinder.location_to_dir('jasmine/runners')}/SpecRunner#{color_opt}.html", 'w+') do |f|
    f << template.result(binding)
  end
end

#push_to_flatironObject



93
94
95
96
97
98
99
# File 'lib/learn_test/jasmine/runner.rb', line 93

def push_to_flatiron
  conn.post do |req|
    req.url SERVICE_ENDPOINT
    req.headers['Content-Type'] = 'application/json'
    req.body = json_results.to_json
  end
end

#runObject



44
45
46
47
48
49
50
# File 'lib/learn_test/jasmine/runner.rb', line 44

def run
  make_runner_html
  run_jasmine
  make_json
  push_to_flatiron unless local || browser
  clean_up
end

#run_jasmineObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/learn_test/jasmine/runner.rb', line 52

def run_jasmine
  if browser
    # system("open #{LearnTest::FileFinder.location_to_dir('jasmine/runners')}/SpecRunner#{color_opt}.html --args allow-file-access-from-files")
    chrome_with_file_access_command = "\"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome\" \"#{LearnTest::FileFinder.location_to_dir('jasmine/runners')}/SpecRunner#{color_opt}.html\" --allow-file-access-from-files"
    # This should give me back to the prompt - u can use & but a flag to send it to the background would be better.
    system(chrome_with_file_access_command)
  else
    system("phantomjs #{LearnTest::FileFinder.location_to_dir('jasmine/runners')}/run-jasmine.js #{LearnTest::FileFinder.location_to_dir('jasmine/runners')}/SpecRunner#{color_opt}.html")
  end
end

#set_passing_test_countObject



83
84
85
# File 'lib/learn_test/jasmine/runner.rb', line 83

def set_passing_test_count
  json_results[:passing_count] = json_results[:tests] - json_results[:failures] - json_results[:errors]
end

#test_xml_filesObject



119
120
121
# File 'lib/learn_test/jasmine/runner.rb', line 119

def test_xml_files
  Dir.entries(@current_test_path).keep_if { |f| f.match(/TEST/) }
end

#write_json_output(output_file:) ⇒ Object



87
88
89
90
91
# File 'lib/learn_test/jasmine/runner.rb', line 87

def write_json_output(output_file:)
  File.open(output_file, 'w+') do |f|
    f.write(json_results.to_json)
  end
end