From 334fbe08118389b51c93414a6feb63289214a3b4 Mon Sep 17 00:00:00 2001 From: Jon Jagger Date: Thu, 6 Aug 2020 13:33:56 +0100 Subject: [PATCH] Add traffic-light tip test for reverted traffic-light and fix event.revert error --- app/controllers/reverter_controller.rb | 2 +- app/models/event.rb | 32 ++-- app/models/kata.rb | 170 +++++++++++---------- app/models/kata_v0.rb | 2 +- app/models/kata_v1.rb | 9 +- test/app_helpers/traffic_light_tip_test.rb | 43 +++++- 6 files changed, 156 insertions(+), 102 deletions(-) diff --git a/app/controllers/reverter_controller.rb b/app/controllers/reverter_controller.rb index 9bb88c056..425894d8d 100644 --- a/app/controllers/reverter_controller.rb +++ b/app/controllers/reverter_controller.rb @@ -10,7 +10,7 @@ def revert colour = event.colour index = params[:index].to_i + 1 - kata.revert(now_index, index, files, time.now, stdout, stderr, status, colour) + kata.revert(index, files, time.now, stdout, stderr, status, colour, now_index) visible_files = files.map{ |filename,file| [filename, file['content']] }.to_h diff --git a/app/models/event.rb b/app/models/event.rb index 57b4e78d1..0b81d85c7 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -2,16 +2,15 @@ class Event - def initialize(kata, hash) + def initialize(kata, summary) @kata = kata - @hash = hash + @summary = summary end attr_reader :kata - def index - @hash['index'] - end + # - - - - - - - - - - - - - - - - - - - - + # four core properties def files event['files'] @@ -29,27 +28,40 @@ def status event['status'] end - def revert - event['revert'] + # - - - - - - - - - - - - - - - - - - - - + # summary properties + + def index + @summary['index'] + end + + def time_a + @summary['time'] end def time - Time.mktime(*@hash['time']) + Time.mktime(*@summary['time']) end def predicted - @hash['predicted'] || 'none' + @summary['predicted'] || 'none' end def colour # '' unless light? - (@hash['colour'] || '').to_sym + (@summary['colour'] || '').to_sym + end + + def revert + @summary['revert'] end def light? colour.to_s != '' end + # - - - - - - - - - - - - - - - - - - - - + def manifest kata.manifest.to_json.merge({'visible_files' => files}) end diff --git a/app/models/kata.rb b/app/models/kata.rb index 359fb6d45..080a89d84 100644 --- a/app/models/kata.rb +++ b/app/models/kata.rb @@ -55,6 +55,92 @@ def avatar_name # - - - - - - - - - - - - - - - - - + def run_tests + Runner.new(@externals).run(@params) + end + + def ran_tests(index, files, at, duration, stdout, stderr, status, colour, predicted='none') + kata.ran_tests(id, index, files, at, duration, stdout, stderr, status, colour, predicted) + end + + # - - - - - - - - - - - - - - - - - + + def revert(index, files, at, stdout, stderr, status, colour, now_index) + kata.revert(id, index, files, at, stdout, stderr, status, colour, now_index) + end + + # - - - - - - - - - - - - - - - - - + + def events + kata.events(id).map.with_index do |h,index| + h['index'] ||= index + Event.new(self, h) + end + end + + def events_json + kata.events_json(id) + end + + def event(index) + kata.event(id, index) + end + + def lights + events.select(&:light?) + end + + def active? + lights != [] + end + + def age + created = Time.mktime(*manifest.created) + (most_recent_event.time - created).to_i # in seconds + end + + def files + most_recent_event.files + end + + def stdout + most_recent_event.stdout + end + + def stderr + most_recent_event.stderr + end + + def status + most_recent_event.status + end + + # - - - - - - - - - - - - - - - - - + + def diff_info(was_index, now_index) + m,e,was,now = kata.diff_info(id, was_index, now_index) + was_files = diff_files(was) + now_files = diff_files(now) + events = e.map.with_index do |h,index| + h['index'] ||= index + Event.new(self, h) + end + [m,events,was_files,now_files] + end + + # - - - - - - - - - - - - - - - - - + + def tipper_info(was_index, now_index) + e,was_files,now_files = kata.tipper_info(id, was_index, now_index) + events = e.map.with_index do |h,index| + h['index'] ||= index + Event.new(self, h) + end + [events,plain(was_files),plain(now_files)] + end + + # - - - - - - - - - - - - - - - - - + def theme=(value) # value == 'dark'|'light' filename = kata_id_path(id, 'theme') @@ -121,90 +207,6 @@ def predict=(value) # - - - - - - - - - - - - - - - - - - def diff_info(was_index, now_index) - m,e,was,now = kata.diff_info(id, was_index, now_index) - was_files = diff_files(was) - now_files = diff_files(now) - events = e.map.with_index do |h,index| - h['index'] ||= index - Event.new(self, h) - end - [m,events,was_files,now_files] - end - - # - - - - - - - - - - - - - - - - - - - def tipper_info(was_index, now_index) - e,was_files,now_files = kata.tipper_info(id, was_index, now_index) - events = e.map.with_index do |h,index| - h['index'] ||= index - Event.new(self, h) - end - [events,plain(was_files),plain(now_files)] - end - - # - - - - - - - - - - - - - - - - - - - def run_tests - Runner.new(@externals).run(@params) - end - - def ran_tests(index, files, at, duration, stdout, stderr, status, colour, predicted='none') - kata.ran_tests(id, index, files, at, duration, stdout, stderr, status, colour, predicted) - end - - # - - - - - - - - - - - - - - - - - - - def revert(now_index, index, files, at, stdout, stderr, status, colour) - kata.revert(id, now_index, index, files, at, stdout, stderr, status, colour) - end - - # - - - - - - - - - - - - - - - - - - - def events - kata.events(id).map.with_index do |h,index| - h['index'] ||= index - Event.new(self, h) - end - end - - def events_json - kata.events_json(id) - end - - def event(index) - kata.event(id, index) - end - - def lights - events.select(&:light?) - end - - def active? - lights != [] - end - - def age - created = Time.mktime(*manifest.created) - (most_recent_event.time - created).to_i # in seconds - end - - def files - most_recent_event.files - end - - def stdout - most_recent_event.stdout - end - - def stderr - most_recent_event.stderr - end - - def status - most_recent_event.status - end - def manifest @manifest ||= Manifest.new(kata.manifest(id)) end diff --git a/app/models/kata_v0.rb b/app/models/kata_v0.rb index aa6bdd2e2..4e91ba54e 100644 --- a/app/models/kata_v0.rb +++ b/app/models/kata_v0.rb @@ -68,7 +68,7 @@ def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, p # - - - - - - - - - - - - - - - - - - - - def revert(id, now_index, index, files, now, stdout, stderr, status, colour) + def revert(id, index, files, now, stdout, stderr, status, colour, now_index) event_summary = { 'index' => index, 'time' => now, diff --git a/app/models/kata_v1.rb b/app/models/kata_v1.rb index edf5c29bc..171595ab5 100644 --- a/app/models/kata_v1.rb +++ b/app/models/kata_v1.rb @@ -10,10 +10,11 @@ # 4. No longer stores file contents in lined format. # 5. Uses Oj as its JSON gem. # 6. Stores explicit index in events.json summary file. -# This makes using index==-1 robust when traffic-lights -# are lost due to Saver outages. +# This improves robustness when there are Saver outages. +# For example index==-1. # was { ... } # 0 -# { ... } # 1 then 2-23 outage +# { ... } # 1 +# then 2-23 outage # { ... } # 24 # now { ..., "index" => 0 } # { ..., "index" => 1 } @@ -83,7 +84,7 @@ def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, p # - - - - - - - - - - - - - - - - - - - - def revert(id, now_index, index, files, now, stdout, stderr, status, colour) + def revert(id, index, files, now, stdout, stderr, status, colour, now_index) event_summary = { 'index' => index, 'time' => now, diff --git a/test/app_helpers/traffic_light_tip_test.rb b/test/app_helpers/traffic_light_tip_test.rb index 051efcd5a..2906a3202 100644 --- a/test/app_helpers/traffic_light_tip_test.rb +++ b/test/app_helpers/traffic_light_tip_test.rb @@ -213,12 +213,51 @@ def self.hex_prefix # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -=begin test 'D57', %w( traffic-light tip for revert ) do + in_new_kata do |kata| + files = kata.files + stdout_1 = file("Expected: 42\nActual: 54") + stderr_1 = file('assert failed') + status_1 = 4 + kata.ran_tests(1, files, time.now, duration, stdout_1, stderr_1, status_1, 'red') + + filename = 'hiker.rb' + hiker_rb = kata.files[filename]['content'] + files[filename] = file(hiker_rb.sub('6 * 9','6 * 7')) + stdout_2 = file('All tests passed') + stderr_2 = file('') + status_2 = 0 + kata.ran_tests(2, files, time.now, duration, stdout_2, stderr_2, status_2, 'green') + + kata.revert(3, kata.events[1].files, time.now, stdout_1, stderr_1, status_1, 'red', 1) + + events = kata.events + was_files = files_for(events, 2) + now_files = files_for(events, 3) + diff = differ.diff(kata.id, was_files, now_files) + number = 3 + + expected = + '' + + '' + + "" + + "" + + '' + + '
#{number}
' + + '' + + '' + + "" + + "" + + "" + + '' + + '
1
1
 hiker.rb
' + + actual = traffic_light_tip_html(diff, kata.avatar_index, events, 3, number) + assert_equal expected, actual + end end -=end private