From 5948f62559b473747b124bd4288d38bb988af033 Mon Sep 17 00:00:00 2001 From: Nikolay Eskov Date: Fri, 15 Nov 2024 12:19:45 +0300 Subject: [PATCH] Fix lint. (#304) * Fix lint. * Fix semgrep issue (using of 'text/template'). * Fix issue. --- cmd/bots/internal/common/environment.go | 24 +++++-------------- pkg/analysis/analyzer_test.go | 5 ++-- pkg/analysis/criteria/base_target_test.go | 8 ++++--- .../criteria/challenged_block_test.go | 5 ++-- pkg/analysis/criteria/height_test.go | 5 ++-- pkg/analysis/criteria/incomplete_test.go | 5 ++-- pkg/analysis/criteria/state_hash_test.go | 5 ++-- pkg/analysis/criteria/unreachable_test.go | 5 ++-- 8 files changed, 29 insertions(+), 33 deletions(-) diff --git a/cmd/bots/internal/common/environment.go b/cmd/bots/internal/common/environment.go index e1ea1a7..1262c68 100644 --- a/cmd/bots/internal/common/environment.go +++ b/cmd/bots/internal/common/environment.go @@ -6,12 +6,11 @@ import ( "embed" "encoding/json" "fmt" - htmlTemplate "html/template" + "html/template" "sort" "strconv" "strings" "sync" - textTemplate "text/template" "nodemon/cmd/bots/internal/common/messaging" "nodemon/pkg/entities" @@ -404,7 +403,7 @@ func nodesToUrls(nodes []entities.Node) []string { func (tgEnv *TelegramBotEnvironment) NodesListMessage(nodes []entities.Node) (string, error) { urls := nodesToUrls(nodes) - tmpl, err := htmlTemplate.ParseFS(templateFiles, "templates/nodes_list.html") + tmpl, err := template.ParseFS(templateFiles, "templates/nodes_list.html") if err != nil { tgEnv.zap.Error("failed to parse nodes list template", zap.Error(err)) return "", err @@ -493,7 +492,7 @@ type subscriptionsList struct { } func (tgEnv *TelegramBotEnvironment) SubscriptionsList() (string, error) { - tmpl, err := htmlTemplate.ParseFS(templateFiles, "templates/subscriptions.html") + tmpl, err := template.ParseFS(templateFiles, "templates/subscriptions.html") if err != nil { tgEnv.zap.Error("failed to parse subscriptions template", zap.Error(err)) @@ -613,21 +612,10 @@ func sortNodesStatuses(statuses []NodeStatus) { }) } -func executeTemplate(template string, data any, extension ExpectedExtension) (string, error) { +func executeTemplate(templateName string, data any, extension ExpectedExtension) (string, error) { switch extension { - case HTML: - tmpl, err := htmlTemplate.ParseFS(templateFiles, template+string(extension)) - if err != nil { - return "", err - } - buffer := &bytes.Buffer{} - err = tmpl.Execute(buffer, data) - if err != nil { - return "", err - } - return buffer.String(), nil - case Markdown: - tmpl, err := textTemplate.ParseFS(templateFiles, template+string(extension)) + case HTML, Markdown: + tmpl, err := template.ParseFS(templateFiles, templateName+string(extension)) if err != nil { return "", err } diff --git a/pkg/analysis/analyzer_test.go b/pkg/analysis/analyzer_test.go index 1d01923..be452c9 100644 --- a/pkg/analysis/analyzer_test.go +++ b/pkg/analysis/analyzer_test.go @@ -179,8 +179,9 @@ func runTestCase(zap *zap.Logger, test testCase) func(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - stateHashAlert := *actualAlert.(*entities.StateHashAlert) - require.Contains(t, test.expectedAlerts, stateHashAlert, "test case #%d", j+1) + stateHashAlert, ok := actualAlert.(*entities.StateHashAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *stateHashAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") } diff --git a/pkg/analysis/criteria/base_target_test.go b/pkg/analysis/criteria/base_target_test.go index a67752c..e3b0a6a 100644 --- a/pkg/analysis/criteria/base_target_test.go +++ b/pkg/analysis/criteria/base_target_test.go @@ -78,8 +78,9 @@ func TestBaseTargetCriterion_Analyze(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - baseTargetAlert := *actualAlert.(*entities.BaseTargetAlert) - require.Contains(t, test.expectedAlerts, baseTargetAlert, "test case #%d", j+1) + baseTargetAlert, ok := actualAlert.(*entities.BaseTargetAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *baseTargetAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") } @@ -125,7 +126,8 @@ func TestNoBaseTargetAlertCriterion_Analyze(t *testing.T) { select { case actualAlert, ok := <-alerts: if ok { - baseTargetAlert := *actualAlert.(*entities.BaseTargetAlert) + baseTargetAlert, btOk := actualAlert.(*entities.BaseTargetAlert) + require.True(t, btOk, "unexpected alert type: %T", actualAlert) require.Fail(t, "unexpected alert: %v", baseTargetAlert) } return diff --git a/pkg/analysis/criteria/challenged_block_test.go b/pkg/analysis/criteria/challenged_block_test.go index 08e4bb2..ddeb1a1 100644 --- a/pkg/analysis/criteria/challenged_block_test.go +++ b/pkg/analysis/criteria/challenged_block_test.go @@ -92,8 +92,9 @@ func TestChallengedBlockCriterion_Analyze(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - challengedBlockAlert := *actualAlert.(*entities.ChallengedBlockAlert) - require.Contains(t, test.expectedAlerts, challengedBlockAlert, "test case #%d", j+1) + challengedBlockAlert, ok := actualAlert.(*entities.ChallengedBlockAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *challengedBlockAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") } diff --git a/pkg/analysis/criteria/height_test.go b/pkg/analysis/criteria/height_test.go index ad9d330..ae49dc1 100644 --- a/pkg/analysis/criteria/height_test.go +++ b/pkg/analysis/criteria/height_test.go @@ -101,8 +101,9 @@ func TestHeightCriterion_Analyze(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - heightAlert := *actualAlert.(*entities.HeightAlert) - require.Contains(t, test.expectedAlerts, heightAlert, "test case #%d", j+1) + heightAlert, ok := actualAlert.(*entities.HeightAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *heightAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") } diff --git a/pkg/analysis/criteria/incomplete_test.go b/pkg/analysis/criteria/incomplete_test.go index 3c3f927..9f632f4 100644 --- a/pkg/analysis/criteria/incomplete_test.go +++ b/pkg/analysis/criteria/incomplete_test.go @@ -132,8 +132,9 @@ func TestIncompleteCriterion_Analyze(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - incompleteAlert := *actualAlert.(*entities.IncompleteAlert) - require.Contains(t, test.expectedAlerts, incompleteAlert, "test case #%d", j+1) + incompleteAlert, ok := actualAlert.(*entities.IncompleteAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *incompleteAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") } diff --git a/pkg/analysis/criteria/state_hash_test.go b/pkg/analysis/criteria/state_hash_test.go index c254784..c1cdf13 100644 --- a/pkg/analysis/criteria/state_hash_test.go +++ b/pkg/analysis/criteria/state_hash_test.go @@ -245,8 +245,9 @@ func TestStateHashCriterion_Analyze(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - stateHashAlert := *actualAlert.(*entities.StateHashAlert) - require.Contains(t, test.expectedAlerts, stateHashAlert, "test case #%d", j+1) + stateHashAlert, ok := actualAlert.(*entities.StateHashAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *stateHashAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") } diff --git a/pkg/analysis/criteria/unreachable_test.go b/pkg/analysis/criteria/unreachable_test.go index c4bd40e..cec7d68 100644 --- a/pkg/analysis/criteria/unreachable_test.go +++ b/pkg/analysis/criteria/unreachable_test.go @@ -96,8 +96,9 @@ func TestUnreachableCriterion_Analyze(t *testing.T) { for j := range test.expectedAlerts { select { case actualAlert := <-alerts: - unreachableAlert := *actualAlert.(*entities.UnreachableAlert) - require.Contains(t, test.expectedAlerts, unreachableAlert, "test case #%d", j+1) + unreachableAlert, ok := actualAlert.(*entities.UnreachableAlert) + require.True(t, ok, "unexpected alert type: %T", actualAlert) + require.Contains(t, test.expectedAlerts, *unreachableAlert, "test case #%d", j+1) case <-time.After(5 * time.Second): require.Fail(t, "timeout exceeded") }