Skip to content

Commit

Permalink
Add tests to workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-jozwikowski committed Jan 24, 2024
1 parent fecb4fc commit 0d13e25
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ jobs:
with:
go-version: 1.21

- name: Run tests
run: |
go test mcv_test.go
- name: Variables
id: vars
run: |
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Run tests

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

jobs:
Run-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: Run tests
run: |
go test mcv_test.go
12 changes: 2 additions & 10 deletions mcv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"monolog-cli-viewer/src/cli"
"monolog-cli-viewer/src/colors"
"monolog-cli-viewer/src/data"
"monolog-cli-viewer/src/templates"
"monolog-cli-viewer/src/viewer"
"os"
Expand All @@ -28,16 +29,7 @@ func main() {
Template: initiateTemplate(), // get template from the CLI params, or the default one
})
if *cli.RuntimeConfig.Test == true {
// test values @todo - put those to tests
values := []string{
`{"message":"Some test message","context":{"user":{"id":1},"session":{"id":"bq2fk4i3nhkgbj4eua964g5r63"}},"level":"NOTICE","channel":"default","timestamp":"1699540146122"}`,
`{"message":"Checking support on authenticator.","context":{"firewall_name":"main","authenticator":"App\\Security\\AppAuthenticator"},"level":100,"level_name":"DEBUG","channel":"security","datetime":"2023-11-14T00:37:26.623539+02:00","extra":{}}`,
`2023-10-23 11:03:16: [9a4e77e9afa8] [ERROR] [Whatever] Login Error`,
`==> some/path/to\file.log <==`,
`[2023-10-23T11:07:47.038324+00:00] default.INFO: User logged in {"user":{"id":"54767261-98c6-4a57-9064-0d35fd06d1fc"}} []`,
}

for _, line := range values {
for _, line := range data.GetTestData() {
logLineItem := viewer.InitLogLine(line)
fmt.Print(logLineItem.GetFormattedString())
}
Expand Down
111 changes: 111 additions & 0 deletions mcv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"monolog-cli-viewer/src/data"
"monolog-cli-viewer/src/templates"
"monolog-cli-viewer/src/viewer"
"testing"
"text/template"
)

var templ *template.Template

func init() {
templ, _ = templates.GetTemplatateByName(templates.DefaultTemplateName)
}

func TestDefault(t *testing.T) {
viewer.SetSettings(viewer.Settings{
NoNewLine: false,
ShowFileChangeLine: false,
ShowParsedLinesOnly: false,
Template: templ,
})

runTests(t, "TestDefault", []string{
"NOTICE:default\t2023-11-09 15:29:06\tSome test message\r\n{\"session\":{\"id\":\"bq2fk4i3nhkgbj4eua964g5r63\"},\"user\":{\"id\":1}}\r\n\n",
"DEBUG:security\t2023-11-14 00:37:26\tChecking support on authenticator.\r\n{\"authenticator\":\"App\\\\Security\\\\AppAuthenticator\",\"firewall_name\":\"main\"}\r\n\n",
"2023-10-23 11:03:16: [9a4e77e9afa8] [ERROR] [Whatever] Login Error\n\n",
"",
"INFO:default\t2023-10-23 11:07:47\tUser logged in \r\n{\"user\":{\"id\":\"54767261-98c6-4a57-9064-0d35fd06d1fc\"}}\r\n\n",
})
}

func TestNoNewLine(t *testing.T) {
viewer.SetSettings(viewer.Settings{
NoNewLine: true,
ShowFileChangeLine: false,
ShowParsedLinesOnly: false,
Template: templ,
})

runTests(t, "TestDefault", []string{
"NOTICE:default\t2023-11-09 15:29:06\tSome test message\r\n{\"session\":{\"id\":\"bq2fk4i3nhkgbj4eua964g5r63\"},\"user\":{\"id\":1}}\r\n",
"DEBUG:security\t2023-11-14 00:37:26\tChecking support on authenticator.\r\n{\"authenticator\":\"App\\\\Security\\\\AppAuthenticator\",\"firewall_name\":\"main\"}\r\n",
"2023-10-23 11:03:16: [9a4e77e9afa8] [ERROR] [Whatever] Login Error\n",
"",
"INFO:default\t2023-10-23 11:07:47\tUser logged in \r\n{\"user\":{\"id\":\"54767261-98c6-4a57-9064-0d35fd06d1fc\"}}\r\n",
})
}

func TestShowFileChange(t *testing.T) {
viewer.SetSettings(viewer.Settings{
NoNewLine: false,
ShowFileChangeLine: true,
ShowParsedLinesOnly: false,
Template: templ,
})

runTests(t, "TestDefault", []string{
"NOTICE:default\t2023-11-09 15:29:06\tSome test message\r\n{\"session\":{\"id\":\"bq2fk4i3nhkgbj4eua964g5r63\"},\"user\":{\"id\":1}}\r\n\n",
"DEBUG:security\t2023-11-14 00:37:26\tChecking support on authenticator.\r\n{\"authenticator\":\"App\\\\Security\\\\AppAuthenticator\",\"firewall_name\":\"main\"}\r\n\n",
"2023-10-23 11:03:16: [9a4e77e9afa8] [ERROR] [Whatever] Login Error\n\n",
"==> some/path/to\\file.log <==\n\n",
"INFO:default\t2023-10-23 11:07:47\tUser logged in \r\n{\"user\":{\"id\":\"54767261-98c6-4a57-9064-0d35fd06d1fc\"}}\r\n\n",
})
}

func TestParsedLinesOnly(t *testing.T) {
viewer.SetSettings(viewer.Settings{
NoNewLine: false,
ShowFileChangeLine: false,
ShowParsedLinesOnly: true,
Template: templ,
})

runTests(t, "TestDefault", []string{
"NOTICE:default\t2023-11-09 15:29:06\tSome test message\r\n{\"session\":{\"id\":\"bq2fk4i3nhkgbj4eua964g5r63\"},\"user\":{\"id\":1}}\r\n\n",
"DEBUG:security\t2023-11-14 00:37:26\tChecking support on authenticator.\r\n{\"authenticator\":\"App\\\\Security\\\\AppAuthenticator\",\"firewall_name\":\"main\"}\r\n\n",
"",
"",
"INFO:default\t2023-10-23 11:07:47\tUser logged in \r\n{\"user\":{\"id\":\"54767261-98c6-4a57-9064-0d35fd06d1fc\"}}\r\n\n",
})
}

func TestNoNewLineFileChangeParsedOnly(t *testing.T) {
viewer.SetSettings(viewer.Settings{
NoNewLine: true,
ShowFileChangeLine: true,
ShowParsedLinesOnly: true,
Template: templ,
})

runTests(t, "TestDefault", []string{
"NOTICE:default\t2023-11-09 15:29:06\tSome test message\r\n{\"session\":{\"id\":\"bq2fk4i3nhkgbj4eua964g5r63\"},\"user\":{\"id\":1}}\r\n",
"DEBUG:security\t2023-11-14 00:37:26\tChecking support on authenticator.\r\n{\"authenticator\":\"App\\\\Security\\\\AppAuthenticator\",\"firewall_name\":\"main\"}\r\n",
"",
"==> some/path/to\\file.log <==\n",
"INFO:default\t2023-10-23 11:07:47\tUser logged in \r\n{\"user\":{\"id\":\"54767261-98c6-4a57-9064-0d35fd06d1fc\"}}\r\n",
})
}

func runTests(t *testing.T, name string, expected []string) {
for id, line := range data.GetTestData() {
logLineItem := viewer.InitLogLine(line)
result := logLineItem.GetFormattedString()

if result != expected[id] {
t.Errorf("Invalid string value on '%s' item '%d'. Expected '%s', got '%s'", "testDefault", id, expected[id], result)
}
}
}
11 changes: 11 additions & 0 deletions src/data/test_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package data

func GetTestData() []string {
return []string{
`{"message":"Some test message","context":{"user":{"id":1},"session":{"id":"bq2fk4i3nhkgbj4eua964g5r63"}},"level":"NOTICE","channel":"default","timestamp":"1699540146122"}`,
`{"message":"Checking support on authenticator.","context":{"firewall_name":"main","authenticator":"App\\Security\\AppAuthenticator"},"level":100,"level_name":"DEBUG","channel":"security","datetime":"2023-11-14T00:37:26.623539+02:00","extra":{}}`,
`2023-10-23 11:03:16: [9a4e77e9afa8] [ERROR] [Whatever] Login Error`,
`==> some/path/to\file.log <==`,
`[2023-10-23T11:07:47.038324+00:00] default.INFO: User logged in {"user":{"id":"54767261-98c6-4a57-9064-0d35fd06d1fc"}} []`,
}
}

0 comments on commit 0d13e25

Please sign in to comment.