Skip to content

Commit

Permalink
modulegen: generate sonar configuration (testcontainers#1644)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Sep 19, 2023
1 parent aa3be2d commit e9fba55
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
21 changes: 21 additions & 0 deletions modulegen/_template/sonar-project.properties.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is autogenerated by the 'modulegen' tool.
# Github organization linked to sonarcloud
sonar.organization=testcontainers

# Project key from sonarcloud dashboard for Github Action, otherwise pick a project key you like
sonar.projectKey=testcontainers_testcontainers-go

sonar.projectName=testcontainers-go

sonar.projectVersion={{ .ProjectVersion }}

sonar.sources=.

sonar.exclusions=**/*_test.go,**/vendor/**,**/testdata/**

sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**

sonar.go.coverage.reportPaths=**/coverage.out
sonar.go.tests.reportPaths={{ .Go.Tests.ReportPaths }}
4 changes: 4 additions & 0 deletions modulegen/internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (ctx Context) MkdocsConfigFile() string {
return filepath.Join(ctx.RootDir, "mkdocs.yml")
}

func (ctx Context) SonarProjectFile() string {
return filepath.Join(ctx.RootDir, "sonar-project.properties")
}

func (ctx Context) VSCodeWorkspaceFile() string {
return filepath.Join(ctx.RootDir, ".vscode", ".testcontainers-go.code-workspace")
}
Expand Down
2 changes: 2 additions & 0 deletions modulegen/internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/testcontainers/testcontainers-go/modulegen/internal/make"
"github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs"
"github.com/testcontainers/testcontainers-go/modulegen/internal/module"
"github.com/testcontainers/testcontainers-go/modulegen/internal/sonar"
"github.com/testcontainers/testcontainers-go/modulegen/internal/tools"
"github.com/testcontainers/testcontainers-go/modulegen/internal/vscode"
"github.com/testcontainers/testcontainers-go/modulegen/internal/workflow"
Expand Down Expand Up @@ -80,6 +81,7 @@ func GenerateFiles(ctx context.Context, tcModule context.TestcontainersModule) e
projectGenerators := []ProjectGenerator{
workflow.Generator{}, // update github ci workflow
vscode.Generator{}, // update vscode workspace
sonar.Generator{}, // update sonar-project.properties
}

for _, generator := range projectGenerators {
Expand Down
43 changes: 43 additions & 0 deletions modulegen/internal/sonar/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sonar

import (
"fmt"
"path/filepath"
"text/template"

"github.com/testcontainers/testcontainers-go/modulegen/internal/context"
"github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs"
internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template"
)

type Generator struct{}

// Generate updates sonar-project.properties
func (g Generator) Generate(ctx context.Context) error {
rootCtx, err := context.GetRootContext()
if err != nil {
return err
}
examples, err := rootCtx.GetExamples()
if err != nil {
return err
}
modules, err := rootCtx.GetModules()
if err != nil {
return err
}
mkdocsConfig, err := mkdocs.ReadConfig(rootCtx.MkdocsConfigFile())
if err != nil {
fmt.Printf(">> could not read MkDocs config: %v\n", err)
return err
}
tcVersion := mkdocsConfig.Extra.LatestVersion
config := newConfig(tcVersion, examples, modules)
name := "sonar-project.properties.tmpl"
t, err := template.New(name).ParseFiles(filepath.Join("_template", name))
if err != nil {
return err
}

return internal_template.GenerateFile(t, ctx.SonarProjectFile(), name, config)
}
38 changes: 38 additions & 0 deletions modulegen/internal/sonar/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sonar

import (
"sort"
"strings"
)

type Config struct {
Go Go
ProjectVersion string
}

type Go struct {
Tests Tests
}

type Tests struct {
ReportPaths string
}

func newConfig(tcVersion string, examples []string, modules []string) *Config {
reportPaths := []string{"TEST-unit.xml", "modulegen/TEST-unit.xml"}
for _, example := range examples {
reportPaths = append(reportPaths, "examples/"+example+"/TEST-unit.xml")
}
for _, module := range modules {
reportPaths = append(reportPaths, "modules/"+module+"/TEST-unit.xml")
}
sort.Strings(reportPaths)
return &Config{
Go: Go{
Tests: Tests{
ReportPaths: strings.Join(reportPaths, ","),
},
},
ProjectVersion: tcVersion,
}
}
3 changes: 2 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file is autogenerated by the 'modulegen' tool.
# Github organization linked to sonarcloud
sonar.organization=testcontainers

Expand All @@ -17,4 +18,4 @@ sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**

sonar.go.coverage.reportPaths=**/coverage.out
sonar.go.tests.reportPaths=**/TEST-unit.xml
sonar.go.tests.reportPaths=TEST-unit.xml,examples/bigtable/TEST-unit.xml,examples/cockroachdb/TEST-unit.xml,examples/consul/TEST-unit.xml,examples/datastore/TEST-unit.xml,examples/firestore/TEST-unit.xml,examples/nginx/TEST-unit.xml,examples/pubsub/TEST-unit.xml,examples/spanner/TEST-unit.xml,examples/toxiproxy/TEST-unit.xml,modulegen/TEST-unit.xml,modules/artemis/TEST-unit.xml,modules/clickhouse/TEST-unit.xml,modules/compose/TEST-unit.xml,modules/couchbase/TEST-unit.xml,modules/elasticsearch/TEST-unit.xml,modules/k3s/TEST-unit.xml,modules/kafka/TEST-unit.xml,modules/localstack/TEST-unit.xml,modules/mariadb/TEST-unit.xml,modules/mongodb/TEST-unit.xml,modules/mysql/TEST-unit.xml,modules/nats/TEST-unit.xml,modules/neo4j/TEST-unit.xml,modules/postgres/TEST-unit.xml,modules/pulsar/TEST-unit.xml,modules/redis/TEST-unit.xml,modules/redpanda/TEST-unit.xml,modules/vault/TEST-unit.xml

0 comments on commit e9fba55

Please sign in to comment.