diff --git a/.gitignore b/.gitignore index 9b9052d6..66f38ea8 100755 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ debug.test # Coverage coverage.* + +# Linter +golangci-report.xml diff --git a/cfg/default.go b/cfg/default.go old mode 100755 new mode 100644 index ce96f15c..fb1367f4 --- a/cfg/default.go +++ b/cfg/default.go @@ -15,7 +15,7 @@ package cfg // DefaultConfig is the default configuration for golium. -// This default configuration can be overriden with environment variables. +// This default configuration can be overridden with environment variables. var DefaultConfig = Config{ Suite: "golium", Environment: "local", diff --git a/context.go b/context.go index e4434c90..ee8c078b 100755 --- a/context.go +++ b/context.go @@ -21,7 +21,7 @@ import ( // ContextKey defines a type to store the Context in context.Context. type ContextKey string -var contextKey ContextKey = "contextKey" +const contextKey ContextKey = "contextKey" // Context contains the context required for common utilities. // It contains a map[string]interface{} to store global values and find them with [CTXT:xxx] tag. diff --git a/mock/http/model.go b/mock/http/model.go index e074241e..3ab3e361 100644 --- a/mock/http/model.go +++ b/mock/http/model.go @@ -16,7 +16,7 @@ package http import "encoding/json" -// MockRequest contains the instruction to configure the behaviour of the HTTP mock server. +// MockRequest contains the instruction to configure the behavior of the HTTP mock server. // The document configures which request is going to be attended (e.g. the path and method) // and the response to be generated by the mock. type MockRequest struct { diff --git a/sonar-project.properties b/sonar-project.properties index 98f12546..b42e06bb 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -4,6 +4,7 @@ sonar.organization=telefonicatc2tech sonar.go.coverage.reportPaths=coverage.txt sonar.go.golangci-lint.reportPaths=golangci-report.xml +sonar.coverage.exclusions=**/*context.go sonar.sources=. sonar.exclusions=**/*_test.go diff --git a/steps/dns/context.go b/steps/dns/context.go index 2d99ce2f..7adaf7c1 100755 --- a/steps/dns/context.go +++ b/steps/dns/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the Context in context.Context. type ContextKey string -var contextKey ContextKey = "dnsSession" +const contextKey ContextKey = "dnsSession" // InitializeContext adds the DNS Session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - session := Session{} - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the DNS session stored in context. diff --git a/steps/dns/session.go b/steps/dns/session.go old mode 100755 new mode 100644 index 5ef272da..b01c916c --- a/steps/dns/session.go +++ b/steps/dns/session.go @@ -106,7 +106,7 @@ func (s *Session) SendUDPQuery(ctx context.Context, qtype uint16, qdomain string func (s *Session) SendDoHQuery(ctx context.Context, method string, qtype uint16, qdomain string, recursive bool) error { logger := GetLogger() corr := uuid.New().String() - //Set DNS query + // Set DNS query m := &dns.Msg{} m.SetQuestion(dns.Fqdn(qdomain), qtype) m.RecursionDesired = recursive @@ -177,7 +177,7 @@ func (s *Session) SendDoHQuery(ctx context.Context, method string, qtype uint16, func (s *Session) SendDoTQuery(ctx context.Context, qtype uint16, qdomain string, recursive bool) error { logger := GetLogger() corr := uuid.New().String() - //Set DNS query + // Set DNS query opts := upstream.Options{ Timeout: s.Timeout, InsecureSkipVerify: true, diff --git a/steps/elasticsearch/context.go b/steps/elasticsearch/context.go index 430231cf..03236bcc 100755 --- a/steps/elasticsearch/context.go +++ b/steps/elasticsearch/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the elasticsearch session in context.Context. type ContextKey string -var contextKey ContextKey = "elasticsearchSession" +const contextKey ContextKey = "elasticsearchSession" // InitializeContext adds the elasticsearch session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the elasticsearch session stored in context. diff --git a/steps/http/context.go b/steps/http/context.go index a683c6e8..938f43de 100755 --- a/steps/http/context.go +++ b/steps/http/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the HTTP session in context.Context. type ContextKey string -var contextKey ContextKey = "httpSession" +const contextKey ContextKey = "httpSession" // InitializeContext adds the HTTP session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the HTTP session stored in context. diff --git a/steps/http/json_test.go b/steps/http/json_test.go index 079c78a3..5858c483 100644 --- a/steps/http/json_test.go +++ b/steps/http/json_test.go @@ -3,6 +3,7 @@ package http import ( "context" "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -15,7 +16,6 @@ import ( func TestGetParamFromJSON(t *testing.T) { t.Run("Should return selected value from JSON file", func(t *testing.T) { - var JSONhttpFileValues = ` [ { @@ -61,7 +61,7 @@ func TestGetParamFromJSON(t *testing.T) { ] }` var expectedParam interface{} - if err := json.Unmarshal([]byte(fmt.Sprint(JSON)), &expectedParam); err != nil { + if err := json.Unmarshal([]byte(JSON), &expectedParam); err != nil { t.Error("error Unmarshaling expected response body: %w", err) } @@ -114,12 +114,12 @@ func TestFindValueByCode(t *testing.T) { ] }` var expectedValue interface{} - if err := json.Unmarshal([]byte(fmt.Sprint(JSON)), &expectedValue); err != nil { + if err := json.Unmarshal([]byte(JSON), &expectedValue); err != nil { t.Error("error Unmarshaling expected response body: %w", err) } dataStruct := []map[string]interface{}{} - if err := json.Unmarshal([]byte(fmt.Sprint(JSONhttpFileValues)), &dataStruct); err != nil { + if err := json.Unmarshal([]byte(JSONhttpFileValues), &dataStruct); err != nil { t.Error("error Unmarshaling expected response body: %w", err) } @@ -162,7 +162,6 @@ func TestFindValueByCode(t *testing.T) { } }) } - } func TestLoadJSONData(t *testing.T) { @@ -201,7 +200,7 @@ func TestUnmarshalJSONData(t *testing.T) { }` var message = "error unmarshalling JSON data due to error: json: cannot unmarshal object into Go value of type []map[string]interface {}" - formatError := fmt.Errorf(message) + formatError := errors.New(message) var expected interface{} if err := json.Unmarshal([]byte(expectedString), &expected); err != nil { t.Error("error Unmarshaling expected response body: %w", err) @@ -237,7 +236,6 @@ func TestUnmarshalJSONData(t *testing.T) { if JSONEquals(tc.expected, unmarshalled) { t.Errorf("expected unmarshalled data error:\n%v", err) } - }) } } @@ -273,17 +271,17 @@ func TestJSONEquals(t *testing.T) { ] }` var expected interface{} - if err := json.Unmarshal([]byte(fmt.Sprint(expectedString)), &expected); err != nil { + if err := json.Unmarshal([]byte(expectedString), &expected); err != nil { t.Error("error Unmarshaling expected response body: %w", err) } var different interface{} - if err := json.Unmarshal([]byte(fmt.Sprint(differentString)), &different); err != nil { + if err := json.Unmarshal([]byte(differentString), &different); err != nil { t.Error("error Unmarshaling expected response body: %w", err) } var current interface{} - if err := json.Unmarshal([]byte(fmt.Sprint(currentString)), ¤t); err != nil { + if err := json.Unmarshal([]byte(currentString), ¤t); err != nil { t.Error("error Unmarshaling expected response body: %w", err) } @@ -312,8 +310,6 @@ func TestJSONEquals(t *testing.T) { t.Errorf("expected JSON comparison should be %t \n%v\n vs \n%v", tc.equals, tc.expected, tc.current) } - }) } - } diff --git a/steps/jwt/context.go b/steps/jwt/context.go index 94467e88..0adfe8a1 100755 --- a/steps/jwt/context.go +++ b/steps/jwt/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the JWT session in context.Context. type ContextKey string -var contextKey ContextKey = "jwtSession" +const contextKey ContextKey = "jwtSession" // InitializeContext adds the JWT session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the HTTP session stored in context. diff --git a/steps/rabbit/context.go b/steps/rabbit/context.go index 3d6d2568..c7ae3d54 100755 --- a/steps/rabbit/context.go +++ b/steps/rabbit/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the rabbit session in context.Context. type ContextKey string -var contextKey ContextKey = "rabbitSession" +const contextKey ContextKey = "rabbitSession" // InitializeContext adds the rabbit session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the rabbit session stored in context. diff --git a/steps/rabbit/session.go b/steps/rabbit/session.go old mode 100755 new mode 100644 index 135910dc..445eb4e3 --- a/steps/rabbit/session.go +++ b/steps/rabbit/session.go @@ -139,7 +139,7 @@ func (s *Session) SubscribeTopic(ctx context.Context, topic string) error { return nil } -// Unsubscribe unsubscribes from rabbit closing the channel asociated. +// Unsubscribe unsubscribes from rabbit closing the channel associated. // If this method is not invoked, then the goroutine created with SubscribeTopic is never closed // and will permanently processing messages from the topic until the program is finished. func (s *Session) Unsubscribe(ctx context.Context) error { diff --git a/steps/redis/context.go b/steps/redis/context.go index 0ba2c12c..bffb6678 100755 --- a/steps/redis/context.go +++ b/steps/redis/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the redis session in context.Context. type ContextKey string -var contextKey ContextKey = "redisSession" +const contextKey ContextKey = "redisSession" // InitializeContext adds the redis session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the redis session stored in context. diff --git a/steps/s3/context.go b/steps/s3/context.go index 5bdaea07..8c1f1bc3 100755 --- a/steps/s3/context.go +++ b/steps/s3/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the s3 session in context.Context. type ContextKey string -var contextKey ContextKey = "s3Session" +const contextKey ContextKey = "s3Session" // InitializeContext adds the s3 session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the s3 session stored in context. diff --git a/test/acceptance/steps/aggregated/context.go b/test/acceptance/steps/aggregated/context.go index 95973657..660bb876 100755 --- a/test/acceptance/steps/aggregated/context.go +++ b/test/acceptance/steps/aggregated/context.go @@ -23,7 +23,7 @@ import ( // ContextKey defines a type to store the aggregated session in context.Context. type ContextKey string -var contextKey ContextKey = "aggregatedSession" +const contextKey ContextKey = "aggregatedSession" // InitializeContext adds the Aggregated session to the context. // The new context is returned because context is immutable. diff --git a/test/acceptance/steps/aggregated/context_test.go b/test/acceptance/steps/aggregated/context_test.go index 24803e80..93b5f804 100644 --- a/test/acceptance/steps/aggregated/context_test.go +++ b/test/acceptance/steps/aggregated/context_test.go @@ -10,7 +10,6 @@ import ( ) func TestInitializeContext(t *testing.T) { - var ctx = context.Background() var contextKeyValue = "aggregatedSession" sharedContext := shared.InitializeContext(ctx) diff --git a/test/acceptance/steps/shared/context.go b/test/acceptance/steps/shared/context.go index 41de07ab..df8b36a6 100755 --- a/test/acceptance/steps/shared/context.go +++ b/test/acceptance/steps/shared/context.go @@ -21,13 +21,12 @@ import ( // ContextKey defines a type to store the shared session in context.Context. type ContextKey string -var contextKey ContextKey = "sharedSession" +const contextKey ContextKey = "sharedSession" // InitializeContext adds the shared session to the context. // The new context is returned because context is immutable. func InitializeContext(ctx context.Context) context.Context { - var session Session - return context.WithValue(ctx, contextKey, &session) + return context.WithValue(ctx, contextKey, &Session{}) } // GetSession returns the shared session stored in context.