test: improve logger test and search functionality

- Improve the logger test to include more specific assertions.
- Add timestamp filtering to the logger search function.
- Fix a bug in the logger search function that prevented it from correctly handling continuation lines.
- Update redisclient tests to use the correct return type.

Co-authored-by: mahmmoud.hassanein <mahmmoud.hassanein@gmail.com>
This commit is contained in:
2025-01-13 18:46:13 +02:00
parent 91f8520229
commit 81f377a532
4 changed files with 31 additions and 13 deletions

View File

@@ -81,16 +81,25 @@ fn test_logger() {
create: false create: false
)! )!
println('/tmp/testlogs/${files[0]}')
content := file.read()!.trim_space() content := file.read()!.trim_space()
items := logger.search()! items_stdout := logger.search(
assert items.len == 6 // still wrong: TODO timestamp_from: ourtime.new('2022-11-1 20:14:35')!
timestamp_to: ourtime.new('2025-11-1 20:14:35')!
logtype: .stdout
)!
assert items_stdout.len == 2
items_error := logger.search(
timestamp_from: ourtime.new('2022-11-1 20:14:35')!
timestamp_to: ourtime.new('2025-11-1 20:14:35')!
logtype: .error
)!
assert items_error.len == 4
} }
fn testsuite_end() { fn testsuite_end() {
if os.exists('/tmp/testlogs') { // if os.exists('/tmp/testlogs') {
os.rmdir_all('/tmp/testlogs')! // os.rmdir_all('/tmp/testlogs')!
} // }
} }

View File

@@ -30,7 +30,6 @@ pub fn (mut l Logger) search(args_ SearchArgs) ![]LogItem {
// Get time range // Get time range
from_time := timestamp_from.unix() from_time := timestamp_from.unix()
to_time := timestamp_to.unix() to_time := timestamp_to.unix()
if from_time > to_time { if from_time > to_time {
return error('from_time cannot be after to_time: ${from_time} < ${to_time}') return error('from_time cannot be after to_time: ${from_time} < ${to_time}')
} }
@@ -82,20 +81,30 @@ pub fn (mut l Logger) search(args_ SearchArgs) ![]LogItem {
continue continue
} }
if collecting && line.len > 14 && line[13] == `-` {
process(mut result, current_item, current_time, args, from_time, to_time)!
collecting = false
}
// Parse log line // Parse log line
is_error := line.starts_with('E') is_error := line.starts_with('E')
if !collecting { if !collecting {
// Start new item // Start new item
current_item = LogItem{ current_item = LogItem{
timestamp: current_time timestamp: current_time
cat: line_trim[2..12].trim_space() cat: line[2..12].trim_space()
log: line_trim[15..].trim_space() log: line[15..].trim_space()
logtype: if is_error { .error } else { .stdout } logtype: if is_error { .error } else { .stdout }
} }
// println('new current item: ${current_item}')
collecting = true collecting = true
} else { } else {
// Continuation line // Continuation line
current_item.log += '\n' + line_trim[15..] if line_trim.len < 16 {
current_item.log += '\n'
} else {
current_item.log += '\n' + line[15..]
}
} }
} }

View File

@@ -3,7 +3,7 @@ import freeflowuniverse.herolib.core.redisclient
fn setup() !&redisclient.Redis { fn setup() !&redisclient.Redis {
mut redis := redisclient.core_get()! mut redis := redisclient.core_get()!
redis.selectdb(10) or { panic(err) } redis.selectdb(10) or { panic(err) }
return &redis return redis
} }
fn cleanup(mut redis redisclient.Redis) ! { fn cleanup(mut redis redisclient.Redis) ! {

View File

@@ -8,7 +8,7 @@ fn setup() !&redisclient.Redis {
mut redis := redisclient.core_get()! mut redis := redisclient.core_get()!
// Select db 10 to be away from default one '0' // Select db 10 to be away from default one '0'
redis.selectdb(10) or { panic(err) } redis.selectdb(10) or { panic(err) }
return &redis return redis
} }
fn cleanup(mut redis redisclient.Redis) ! { fn cleanup(mut redis redisclient.Redis) ! {