Skip to content

Commit

Permalink
CI: Enable and fix checks - Pt3 (#33)
Browse files Browse the repository at this point in the history
* fix: funlen

* fix: gochecknoglobals

* fix: nonamedreturns

* fix: testpackage
  • Loading branch information
DownerCase authored Dec 18, 2024
1 parent ddd4cd5 commit 55898aa
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 73 deletions.
22 changes: 12 additions & 10 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,31 @@ linters:
- godox # Yes. The project is in progress
- depguard # Not desired
- varnamelen # Not desired
- gochecknoglobals
- nonamedreturns
- testpackage
- cyclop
- funlen
- cyclop # Flags big simple switch statements

linters-settings:
gci:
sections:
- standard
- default
- localmodule
ireturn:
allow:
- tea.Model
- error
- stdlib
- empty
- generic
gci:
sections:
- standard
- default
- localmodule
mnd:
ignored-files:
- 'monitor/.*.go' # UI code has many magic layout numbers...
- 'main.go' # Main file allowed, as it is example code
paralleltest:
# eCAL uses global state, any test using Initialize can't be run in parallel
ignore-missing: true

issues:
exclude-rules:
- path: monitor/styles.go
linters:
- gochecknoglobals
2 changes: 1 addition & 1 deletion cmd/monitor/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewTable(columns []table.Column) table.Model {

type NavKeyMap map[string]func() tea.Cmd

func (navKeys NavKeyMap) HandleMsg(msg tea.Msg) (cmd tea.Cmd, navigated bool) {
func (navKeys NavKeyMap) HandleMsg(msg tea.Msg) (tea.Cmd, bool) {
if msg, ok := msg.(tea.KeyMsg); ok {
if f, ok := navKeys[msg.String()]; ok {
return f(), true
Expand Down
4 changes: 3 additions & 1 deletion cmd/monitor/config_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ func NewConfigModel() *ModelConfig {

func (m *ModelConfig) Refresh() {}

func (m *ModelConfig) Update(msg tea.Msg) (cmd tea.Cmd) {
func (m *ModelConfig) Update(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)

return cmd
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/monitor/hosts_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (m *ModelHosts) updateTable(msg tea.Msg) {
m.table, _ = m.table.Update(msg)
}

func hostsToRows(hosts map[string]hostInfo) (rows []table.Row) {
func hostsToRows(hosts map[string]hostInfo) []table.Row {
rows := make([]table.Row, 0)
for hostName, hostInfo := range hosts {
rows = append(rows, table.Row{
hostName,
Expand All @@ -99,5 +100,5 @@ func hostsToRows(hosts map[string]hostInfo) (rows []table.Row) {
})
}

return
return rows
}
6 changes: 4 additions & 2 deletions cmd/monitor/page_processes_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ func (m *ModelProcessesMain) getSelectedPid() (int32, error) {
return int32(pid), err
}

func (m *ModelProcessesMain) updateTable(msg tea.Msg) (cmd tea.Cmd) {
func (m *ModelProcessesMain) updateTable(msg tea.Msg) tea.Cmd {
rows := []table.Row{}

mon := monitoring.GetMonitoring(monitoring.MonitorProcess)
for _, proc := range mon.Processes {
rows = append(rows, procToRow(proc))
}

var cmd tea.Cmd

m.table.SetRows(rows)
m.table, cmd = m.table.Update(msg)

return
return cmd
}

func procToRow(proc monitoring.ProcessMon) table.Row {
Expand Down
6 changes: 4 additions & 2 deletions cmd/monitor/page_services_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *ModelServicesMain) GetSelectedID() (string, bool, error) {
return row[0], row[1] == "S", nil
}

func (m *ModelServicesMain) updateTable(msg tea.Msg) (cmd tea.Cmd) {
func (m *ModelServicesMain) updateTable(msg tea.Msg) tea.Cmd {
rows := []table.Row{}

mon := monitoring.GetMonitoring(monitoring.MonitorClient | monitoring.MonitorServer)
Expand All @@ -60,10 +60,12 @@ func (m *ModelServicesMain) updateTable(msg tea.Msg) (cmd tea.Cmd) {
rows = append(rows, serverToRow(server))
}

var cmd tea.Cmd

m.table.SetRows(rows)
m.table, cmd = m.table.Update(msg)

return
return cmd
}

func serviceToRow(service monitoring.ServiceBase) table.Row {
Expand Down
32 changes: 17 additions & 15 deletions ecal/core_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ecal
package ecal_test

import (
"testing"

"github.com/DownerCase/ecal-go/ecal"
)

func TestVersionString(t *testing.T) {
t.Parallel()

version := GetVersionString()
version := ecal.GetVersionString()
if version == "" {
t.Error("GetVersionString returned empty string")
}
Expand All @@ -18,7 +20,7 @@ func TestVersionString(t *testing.T) {
func TestVersionDateString(t *testing.T) {
t.Parallel()

buildDate := GetVersionDateString()
buildDate := ecal.GetVersionDateString()
if buildDate == "" {
t.Error("GetVersionDateString returned empty string")
}
Expand All @@ -29,61 +31,61 @@ func TestVersionDateString(t *testing.T) {
func TestGetVersion(t *testing.T) {
t.Parallel()

version := GetVersion()
version := ecal.GetVersion()
t.Log(version)
}

func TestInitializeFinalize(t *testing.T) {
if IsInitialized() {
if ecal.IsInitialized() {
t.Error("eCAL pre-initialized...")
}

initResult := Initialize(NewConfig(), "go_test", CDefault)
initResult := ecal.Initialize(ecal.NewConfig(), "go_test", ecal.CDefault)
if initResult == 1 {
t.Fatal("eCAL already initialized")
} else if initResult != 0 {
t.Fatalf("eCAL failed to initialize with error %v", initResult)
}

// Test double initialization
secondInit := Initialize(NewConfig(), "go_test2", CPublisher)
secondInit := ecal.Initialize(ecal.NewConfig(), "go_test2", ecal.CPublisher)
if secondInit != 1 {
t.Errorf("Second initialize returned %v", secondInit)
}

if !IsInitialized() {
if !ecal.IsInitialized() {
t.Error("IsInitialized return false, expected true")
}

if !IsComponentInitialized(CPublisher) {
if !ecal.IsComponentInitialized(ecal.CPublisher) {
t.Error("Expected publisheCPublisher to be initialised")
}

if !SetUnitName("go_test_set_name") {
if !ecal.SetUnitName("go_test_set_name") {
t.Error("Failed to set unit name")
}

if !Ok() {
if !ecal.Ok() {
t.Error("eCAL not Ok")
}

finalizeResult := Finalize()
finalizeResult := ecal.Finalize()
if finalizeResult != 0 {
t.Errorf("Failed to finalize with error %v", finalizeResult)
}

secondFinalize := Finalize()
secondFinalize := ecal.Finalize()
// We've called Initialize twice so 2 calls to Finalize are needed
if secondFinalize != 0 {
t.Errorf("Second finalize returned %v", secondFinalize)
}

thirdFinalize := Finalize()
thirdFinalize := ecal.Finalize()
if thirdFinalize != 1 {
t.Errorf("Expected Finalize to already be done, recevied %v", thirdFinalize)
}

if Ok() {
if ecal.Ok() {
t.Error("eCAL Ok after being finalized")
}
}
13 changes: 7 additions & 6 deletions ecal/logging/logging_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package logging
package logging_test

import (
"os"
"testing"
"time"

"github.com/DownerCase/ecal-go/ecal"
"github.com/DownerCase/ecal-go/ecal/logging"
_ "github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/internal/ecaltest"
)

func expectMessageIsFromHost(t *testing.T, msg LogMessage) {
func expectMessageIsFromHost(t *testing.T, msg logging.LogMessage) {
t.Helper()

host, err := os.Hostname()
Expand All @@ -23,10 +24,10 @@ func expectMessageIsFromHost(t *testing.T, msg LogMessage) {
}
}

func receiveMessage(t *testing.T, msg string, level Level) bool {
func receiveMessage(t *testing.T, msg string, level logging.Level) bool {
t.Helper()

logs := GetLogging()
logs := logging.GetLogging()

for _, rmsg := range logs.Messages {
if rmsg.Content == msg {
Expand All @@ -50,8 +51,8 @@ func TestGetLogging(t *testing.T) {

// When: Logging a message
testMessage := "This is a test log message"
level := LevelError
Log(level, testMessage)
level := logging.LevelError
logging.Log(level, testMessage)

// Expect: To receieve that message
time.Sleep(5 * time.Millisecond)
Expand Down
12 changes: 6 additions & 6 deletions ecal/monitoring/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,26 @@ func copyToServiceBase(cbase C.struct_CServiceCommon) ServiceBase {
}
}

func copyToServerMons(cservers []C.struct_CServerMon) (servers []ServerMon) {
servers = make([]ServerMon, len(cservers))
func copyToServerMons(cservers []C.struct_CServerMon) []ServerMon {
servers := make([]ServerMon, len(cservers))
for idx, cserver := range cservers {
servers[idx] = ServerMon{
ServiceBase: copyToServiceBase(cserver.base),
PortV0: uint32(cserver.port_v0),
PortV1: uint32(cserver.port_v1),
}
}
return
return servers
}

func copyToClientMons(cclients []C.struct_CClientMon) (clients []ClientMon) {
clients = make([]ClientMon, len(cclients))
func copyToClientMons(cclients []C.struct_CClientMon) []ClientMon {
clients := make([]ClientMon, len(cclients))
for idx, cclient := range cclients {
clients[idx] = ClientMon{
ServiceBase: copyToServiceBase(cclient.base),
}
}
return
return clients
}

//export goCopyMonitoring
Expand Down
17 changes: 9 additions & 8 deletions ecal/monitoring/monitoring_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package monitoring
package monitoring_test

import (
"os"
"testing"
"time"

"github.com/DownerCase/ecal-go/ecal"
"github.com/DownerCase/ecal-go/ecal/monitoring"
"github.com/DownerCase/ecal-go/ecal/registration"
"github.com/DownerCase/ecal-go/internal/ecaltest"
"github.com/DownerCase/ecal-go/internal/ecaltest/regtest"
testutilpublisher "github.com/DownerCase/ecal-go/internal/ecaltest/testutil_publisher"
testutilsubscriber "github.com/DownerCase/ecal-go/internal/ecaltest/testutil_subscriber"
)

func expectTopicPresent(t *testing.T, ts []TopicMon, topicName string) {
func expectTopicPresent(t *testing.T, ts []monitoring.TopicMon, topicName string) {
t.Helper()

if len(ts) == 0 {
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestPublisherMonitoring(t *testing.T) {
pub := testutilpublisher.NewGenericPublisher(t, topic)
defer pub.Delete()

mon := GetMonitoring(MonitorHost)
mon := monitoring.GetMonitoring(monitoring.MonitorHost)
if len(mon.Publishers) > 0 {
t.Error("Monitoring returned publishers when not requested")
}
Expand All @@ -50,11 +51,11 @@ func TestPublisherMonitoring(t *testing.T) {
regtest.ExpectNew(t, topic, channel)

// Get its info
mon = GetMonitoring(MonitorPublisher)
mon = monitoring.GetMonitoring(monitoring.MonitorPublisher)
expectTopicPresent(t, mon.Publishers, topic)
}

func expectPid(t *testing.T, pid int, procs []ProcessMon) {
func expectPid(t *testing.T, pid int, procs []monitoring.ProcessMon) {
t.Helper()

hostname, err := os.Hostname()
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestSubscriberMonitoring(t *testing.T) {
sub := testutilsubscriber.NewGenericSubscriber(t, topic)
defer sub.Delete()

mon := GetMonitoring(MonitorHost)
mon := monitoring.GetMonitoring(monitoring.MonitorHost)
if len(mon.Publishers) > 0 {
t.Error("Monitoring returned publishers when not requested")
}
Expand All @@ -96,7 +97,7 @@ func TestSubscriberMonitoring(t *testing.T) {
regtest.ExpectNew(t, topic, channel)

// Get its info
mon = GetMonitoring(MonitorSubscriber)
mon = monitoring.GetMonitoring(monitoring.MonitorSubscriber)
expectTopicPresent(t, mon.Subscribers, topic)
}

Expand All @@ -109,7 +110,7 @@ func TestProcessMonitoring(t *testing.T) {
time.Sleep(1500 * time.Millisecond) // Propagation delay...

// When: Requesting the processes
mon := GetMonitoring(MonitorProcess)
mon := monitoring.GetMonitoring(monitoring.MonitorProcess)

// Expect: This program
expectPid(t, os.Getpid(), mon.Processes)
Expand Down
Loading

0 comments on commit 55898aa

Please sign in to comment.