Skip to content

Commit

Permalink
Refactoring; change revert() and ran_tests() so id,index are passed a…
Browse files Browse the repository at this point in the history
…s 1st args, summary hash is passed as last arg
  • Loading branch information
JonJagger committed Aug 8, 2020
1 parent 79e1c89 commit b467933
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 91 deletions.
17 changes: 7 additions & 10 deletions app/controllers/kata_controller.rb
Expand Up @@ -75,23 +75,20 @@ def run_tests
@log = result['log']
@outcome = result['outcome']
@light = {
'time' => t1,
'index' => @index,
'time' => t1,
'colour' => @outcome,
'predicted' => predicted,
}

args = []
args << @index # index of traffic-light event
args << files # includes @created,@deleted,@changed
args += [t1,duration] # how long runner took
args += [@stdout, @stderr, @status] # output of [test] kata.run_tests()
args << @outcome
args << predicted

@out_of_sync = false
begin
kata.ran_tests(*args)
kata.ran_tests(@id, @index, files, @stdout, @stderr, @status, {
'time' => t1,
'duration' => duration,
'colour' => @outcome,
'predicted' => predicted
})
rescue SaverService::Error => error
@out_of_sync = true
STDOUT.puts(error.message);
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/reverter_controller.rb
Expand Up @@ -13,8 +13,7 @@ def revert

index = params[:index].to_i + 1

kata.revert(files, stdout, stderr, status, {
'index' => index,
kata.revert(id, index, files, stdout, stderr, status, {
'time' => time.now,
'colour' => colour,
'revert' => [src_id, src_index]
Expand Down
9 changes: 4 additions & 5 deletions app/models/kata.rb
Expand Up @@ -59,15 +59,14 @@ 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)
def ran_tests(id, index, files, stdout, stderr, status, summary)
kata.ran_tests(id, index, files, stdout, stderr, status, summary)
end

# - - - - - - - - - - - - - - - - -

def revert(files, stdout, stderr, status, summary)
summary['id'] = id
kata.revert(files, stdout, stderr, status, summary)
def revert(id, index, files, stdout, stderr, status, summary)
kata.revert(id, index, files, stdout, stderr, status, summary)
end

# - - - - - - - - - - - - - - - - -
Expand Down
14 changes: 3 additions & 11 deletions app/models/kata_v0.rb
Expand Up @@ -44,13 +44,7 @@ def manifest(id)

# - - - - - - - - - - - - - - - - - - -

def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, predicted='none')
event_summary = {
'time' => now,
'colour' => colour,
'duration' => duration,
'predicted' => predicted,
}
def ran_tests(id, index, files, stdout, stderr, status, summary)
event_n = {
'files' => files,
'stdout' => stdout,
Expand All @@ -62,15 +56,13 @@ def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, p
dir_exists_command(id),
dir_make_command(id, index),
event_file_create_command(id, index, json_plain(lined(event_n))),
events_file_append_command(id, json_plain(event_summary) + "\n")
events_file_append_command(id, json_plain(summary) + "\n")
])
end

# - - - - - - - - - - - - - - - - - - -

def revert(files, stdout, stderr, status, summary)
id = summary['id']
index = summary['index']
def revert(id, index, files, stdout, stderr, status, summary)
event_n = {
'files' => files,
'stdout' => stdout,
Expand Down
21 changes: 7 additions & 14 deletions app/models/kata_v1.rb
Expand Up @@ -61,32 +61,25 @@ def manifest(id)

# - - - - - - - - - - - - - - - - - - -

def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, predicted='none')
event_summary = {
'index' => index,
'time' => now,
'colour' => colour,
'duration' => duration,
'predicted' => predicted,
}
def ran_tests(id, index, files, stdout, stderr, status, summary)
summary['index'] = index # See point 6 at top of file
event_n = {
'files' => files,
'files' => files,
'stdout' => stdout,
'stderr' => stderr,
'status' => status
}
saver.assert_all([
# A failing create_command() ensures the append_command() is not run.
event_file_create_command(id, index, json_plain(event_n.merge(event_summary))),
events_file_append_command(id, ",\n" + json_plain(event_summary))
event_file_create_command(id, index, json_plain(event_n.merge(summary))),
events_file_append_command(id, ",\n" + json_plain(summary))
])
end

# - - - - - - - - - - - - - - - - - - -

def revert(files, stdout, stderr, status, summary)
id = summary['id']
index = summary['index']
def revert(id, index, files, stdout, stderr, status, summary)
summary['index'] = index # See point 6 at top of file
event_n = {
'files' => files,
'stdout' => stdout,
Expand Down
40 changes: 27 additions & 13 deletions test/app_helpers/traffic_light_tip_test.rb
Expand Up @@ -15,15 +15,15 @@ def self.hex_prefix
stdout = file("Expected: 42\nActual: 54")
stderr = file('assert failed')
status = 4
kata.ran_tests(1, files, time.now, duration, stdout, stderr, status, 'red')
kata.ran_tests(kata.id, 1, files, stdout, stderr, status, summary(time.now, 'red'))

filename = 'hiker.rb'
hiker_rb = kata.files[filename]['content']
files[filename] = file(hiker_rb.sub('6 * 9','6 * 7'))
stdout = file('All tests passed')
stderr = file('')
status = 0
kata.ran_tests(2, files, time.now, duration, stdout, stderr, status, 'green')
kata.ran_tests(kata.id, 2, files, stdout, stderr, status, summary(time.now, 'green'))

events = kata.events
was_files = files_for(events, 1)
Expand Down Expand Up @@ -61,15 +61,15 @@ def self.hex_prefix
stdout = file("Expected: 42\nActual: 54")
stderr = file('assert failed')
status = 4
kata.ran_tests(1, files, time.now, duration, stdout, stderr, status, 'red')
kata.ran_tests(kata.id, 1, files, stdout, stderr, status, summary(time.now, 'red'))

filename = 'hiker.rb'
hiker_rb = kata.files[filename]['content']
files[filename] = file(hiker_rb.sub('6 * 9','6 * 7'))
stdout = file('All tests passed')
stderr = file('')
status = 0
kata.ran_tests(2, files, time.now, duration, stdout, stderr, status, 'green')
kata.ran_tests(kata.id, 2, files, stdout, stderr, status, summary(time.now, 'green'))

events = kata.events
was_files = files_for(events, 1)
Expand Down Expand Up @@ -108,7 +108,7 @@ def self.hex_prefix
stdout = file("Expected: 42\nActual: 54")
stderr = file('assert failed')
status = 4
kata.ran_tests(1, files, time.now, duration, stdout, stderr, status, 'red')
kata.ran_tests(kata.id, 1, files, stdout, stderr, status, summary(time.now, 'red'))
events = kata.events
was_files = files_for(events, was_index=0)
now_files = files_for(events, now_index=0)
Expand All @@ -128,15 +128,15 @@ def self.hex_prefix
stdout = file("Expected: 42\nActual: 54")
stderr = file('assert failed')
status = 4
kata.ran_tests(was_index=1, files, time.now, duration, stdout, stderr, status, 'red')
kata.ran_tests(kata.id, was_index=1, files, stdout, stderr, status, summary(time.now, 'red'))

filename = 'hiker.rb'
hiker_rb = kata.files[filename]['content']
files[filename] = file(hiker_rb.sub('6 * 9','6 * 7'))
stdout = file('All tests passed')
stderr = file('')
status = 0
kata.ran_tests(now_index=2, files, time.now, duration, stdout, stderr, status, 'faulty')
kata.ran_tests(kata.id, now_index=2, files, stdout, stderr, status, summary(time.now, 'faulty'))

events = kata.events
was_files = files_for(events, was_index)
Expand Down Expand Up @@ -174,7 +174,7 @@ def self.hex_prefix
stdout = file("Expected: 42\nActual: 54")
stderr = file('assert failed')
status = 4
kata.ran_tests(1, files, time.now, duration, stdout, stderr, status, 'red')
kata.ran_tests(kata.id, 1, files, stdout, stderr, status, summary(time.now, 'red'))

filename = 'hiker.rb'
hiker_rb = kata.files[filename]['content']
Expand All @@ -183,7 +183,12 @@ def self.hex_prefix
stderr = file('')
status = 0
predicted = 'red'
kata.ran_tests(2, files, time.now, duration, stdout, stderr, status, 'green', predicted)
kata.ran_tests(kata.id, 2, files, stdout, stderr, status, {
'time' => time.now,
'duration' => duration,
'colour' => 'green',
'predicted' => 'red'
})

events = kata.events
was_files = files_for(events, 1)
Expand Down Expand Up @@ -221,18 +226,17 @@ def self.hex_prefix
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')
kata.ran_tests(kata.id, 1, files, stdout_1, stderr_1, status_1, summary(time.now, '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.ran_tests(kata.id, 2, files, stdout_2, stderr_2, status_2, summary(time.now, 'green'))

kata.revert(kata.events[1].files, stdout_1, stderr_1, status_1, {
'index' => 3,
kata.revert(kata.id, 3, kata.events[1].files, stdout_1, stderr_1, status_1, {
'time' => time.now,
'colour' => 'red',
'revert' => [ kata.id, 1 ]
Expand Down Expand Up @@ -278,4 +282,14 @@ def file(content)
}
end

private

def summary(now, colour)
{ 'time' => now,
'duration' => duration,
'colour' => colour,
'predicted' => 'none'
}
end

end
7 changes: 6 additions & 1 deletion test/app_models/group_test.rb
Expand Up @@ -153,7 +153,12 @@ def self.hex_prefix
stdout = content('')
stderr = content('')
status = 0
kata.ran_tests(1, kata.files, [2018,11,30, 9,35,8,7564], duration, stdout, stderr, status, 'green')
kata.ran_tests(kata.id, 1, kata.files, stdout, stderr, status, {
'time' => [2018,11,30, 9,35,8,7564],
'duration' => duration,
'colour' => 'green',
'predicted' => 'none'
})
assert_equal 12, group.age
end
end
Expand Down
51 changes: 28 additions & 23 deletions test/app_models/kata_test.rb
Expand Up @@ -151,9 +151,7 @@ def self.hex_prefix
stdout = content('dfg')
stderr = content('uystd')
status = 3
colour = 'red'
now = [2018,11,1, 9,14,9,9154]
kata.ran_tests(1, files, now, duration, stdout, stderr, status, colour)
kata.ran_tests(kata.id, 1, files, stdout, stderr, status, summary([2018,11,1, 9,14,9,9154], 'red'))

assert_equal 13, kata.age
assert kata.active?
Expand Down Expand Up @@ -184,26 +182,22 @@ def self.hex_prefix
stdout_1 = content("Expected: 42\nActual: 54")
stderr_1 = content('assert failed')
status_1 = 4
now_1 = [2018,11,1, 9,14,9,9154]
kata.ran_tests(1, kata.files, now_1, duration, stdout_1, stderr_1, status_1, 'red')
kata.ran_tests(kata.id, 1, kata.files, stdout_1, stdout_1, stderr_1, summary(time.now, 'red'))

filename = 'hiker.rb'
hiker_rb = kata.files[filename]['content']
files[filename] = content(hiker_rb.sub('6 * 9','6 * 7'))
stdout_2 = content('All tests passed')
stderr_2 = content('')
status_2 = 0
now_2 = [2018,11,1, 9,15,19,1394]
kata.ran_tests(2, files, now_2, duration, stdout_2, stderr_2, status_2, 'green')
kata.ran_tests(kata.id, 2, files, stdout_2, stderr_2, status_2, summary(time.now, 'green'))

kata.revert(kata.events[1].files, stdout_1, stderr_1, status_1, {
'index' => 3,
'time' => [2018,11,1, 9,16,28,46],
kata.revert(kata.id, 3, kata.events[1].files, stdout_1, stderr_1, status_1, {
'time' => time.now,
'colour' => 'red',
'revert' => [ kata.id, 1 ]
});


assert_equal 4, kata.events.size
assert_equal 3, kata.lights.size

Expand Down Expand Up @@ -233,8 +227,7 @@ def self.hex_prefix
stdout = content('dfsdf')
stderr = content('76546')
status = 3
colour = 'red'
kata.ran_tests(1, kata.files, time.now, duration, stdout, stderr, status, colour)
kata.ran_tests(kata.id, 1, kata.files, stdout, stderr, status, summary(time.now, 'red'))

emanifest = kata.events[1].manifest
refute_nil emanifest
Expand All @@ -257,14 +250,15 @@ def self.hex_prefix
stdout = content('xxxx')
stderr = content('')
status = 0
colour = 'green'
kata.ran_tests(1, kata.files, time.now, duration, stdout, stderr, status, colour)
kata.ran_tests(kata.id, 1, kata.files, stdout, stderr, status, summary(time.now, 'green'))

assert_equal 'xxxx', kata.event(-1)['stdout']['content']
assert_equal kata.event(1), kata.event(-1)
stdout = content('')
stderr = content('syntax-error')
status = 1
kata.ran_tests(2, kata.files, time.now, duration, stdout, stderr, status, colour)
kata.ran_tests(kata.id, 2, kata.files, stdout, stderr, status, summary(time.now, 'green'))

assert_equal 'syntax-error', kata.event(-1)['stderr']['content']
assert_equal kata.event(2), kata.event(-1)
end
Expand All @@ -283,11 +277,12 @@ def self.hex_prefix
stdout = content('aaaa')
stderr = content('bbbb')
status = 1
colour = 'red'
kata.ran_tests(index=1, kata.files, time.now, duration, stdout, stderr, status, colour)
kata.ran_tests(kata.id, 1, kata.files, stdout, stderr, status, summary(time.now, 'red'))

# saver-outage for index=2,3,4,5
stdout['content'] = 'x1x2x3'
kata.ran_tests(index=6, kata.files, time.now, duration, stdout, stderr, status, colour)
kata.ran_tests(kata.id, 6, kata.files, stdout, stderr, status, summary(time.now, 'red'))

if v_test?(0)
assert_raises { kata.event(-1) }
else
Expand All @@ -311,9 +306,9 @@ def self.hex_prefix
stderr = content('bbbb')
status = 1
# 1st avatar
kata.ran_tests(index=1, kata.files, time.now, duration, stdout, stderr, status, 'red')
kata.ran_tests(index=2, kata.files, time.now, duration, stdout, stderr, status, 'amber')
kata.ran_tests(index=3, kata.files, time.now, duration, stdout, stderr, status, 'green')
kata.ran_tests(kata.id, 1, kata.files, stdout, stderr, status, summary(time.now, 'red'))
kata.ran_tests(kata.id, 2, kata.files, stdout, stderr, status, summary(time.now, 'amber'))
kata.ran_tests(kata.id, 3, kata.files, stdout, stderr, status, summary(time.now, 'green'))

events = kata.events
assert_equal 4, events.size, :event_not_appended_to_events_json
Expand All @@ -323,7 +318,7 @@ def self.hex_prefix

# 2nd avatar - no refresh, so index not advanced to 2
error = assert_raises(SaverService::Error) {
kata.ran_tests(index=1, kata.files, time.now, duration, stdout, stderr, status, 'green')
kata.ran_tests(kata.id, 1, kata.files, stdout, stderr, status, summary(time.now, 'green'))
}

events = kata.events
Expand Down Expand Up @@ -404,4 +399,14 @@ def self.hex_prefix
end
end

private

def summary(now, colour)
{ 'time' => now,
'duration' => duration,
'colour' => colour,
'predicted' => 'none'
}
end

end

0 comments on commit b467933

Please sign in to comment.