1515package main
1616
1717import (
18+ "flag"
1819 "fmt"
1920 "os"
21+ "slices"
2022 "sort"
2123 "strings"
2224 "testing"
@@ -26,6 +28,10 @@ import (
2628 "golang.org/x/tools/go/packages"
2729)
2830
31+ const dotFile = "packages.dot"
32+
33+ var updateF = flag .Bool ("update" , false , "update " + dotFile )
34+
2935/*
3036Commenting out these tests because not always we have a proper executable in the path.
3137These tests should be moved to QA testing framework.
@@ -105,7 +111,8 @@ func TestImports(t *testing.T) {
105111
106112 // agents code should be independent
107113 for _ , a := range []string {
108- "github.com/percona/pmm/agent/agents/mongodb" ,
114+ // mongodb has no package of its own, only subpackages
115+ "github.com/percona/pmm/agent/agents/mongodb/..." ,
109116 "github.com/percona/pmm/agent/agents/mysql/perfschema" ,
110117 "github.com/percona/pmm/agent/agents/mysql/slowlog" ,
111118 "github.com/percona/pmm/agent/agents/noop" ,
@@ -178,13 +185,16 @@ func TestImports(t *testing.T) {
178185 for path , c := range constraints {
179186 pkgs , err := packages .Load (config , path )
180187 require .NoError (t , err )
188+ require .NotEmpty (t , pkgs , "pattern %s matched no packages" , path )
189+ require .Zero (t , packages .PrintErrors (pkgs ), "failed to load %s" , path )
181190 allPkgs = append (allPkgs , pkgs ... )
182191
192+ own := strings .TrimSuffix (path , "/..." )
183193 for _ , p := range pkgs {
184194 for _ , d := range c .denyPrefixes {
185195 for i := range p .Imports {
186196 // allow own subpackages
187- if strings .HasPrefix (i , path ) {
197+ if strings .HasPrefix (i , own ) {
188198 continue
189199 }
190200
@@ -209,12 +219,6 @@ func TestImports(t *testing.T) {
209219 }
210220 }
211221
212- f , err := os .Create ("packages.dot" )
213- require .NoError (t , err )
214- t .Cleanup (func () {
215- assert .NoError (t , f .Close ())
216- })
217-
218222 var lines []string
219223 for _ , p := range allPkgs {
220224 pName := formatPkgName (t , p .PkgPath )
@@ -232,19 +236,25 @@ func TestImports(t *testing.T) {
232236 }
233237 }
234238 sort .Strings (lines )
239+ lines = slices .Compact (lines )
235240
236- _ , err = fmt .Fprintf (f , "digraph packages {\n " )
237- require .NoError (t , err )
238- duplicate := make (map [string ]struct {})
241+ var graph strings.Builder
242+ graph .WriteString ("digraph packages {\n " )
239243 for _ , line := range lines {
240- if _ , ok := duplicate [line ]; ! ok {
241- duplicate [line ] = struct {}{}
242- _ , err = fmt .Fprint (f , line )
243- require .NoError (t , err )
244- }
244+ graph .WriteString (line )
245+ }
246+ graph .WriteString ("}\n " )
247+
248+ if * updateF {
249+ require .NoError (t , os .WriteFile (dotFile , []byte (graph .String ()), 0o644 ))
250+ t .Logf ("%s updated." , dotFile )
251+ return
245252 }
246- _ , err = fmt .Fprintf (f , "}\n " )
253+
254+ expected , err := os .ReadFile (dotFile )
247255 require .NoError (t , err )
256+ assert .Equal (t , string (expected ), graph .String (),
257+ "%s is stale; regenerate it with 'go test ./agent -run TestImports -update'." , dotFile )
248258}
249259
250260func formatPkgName (t * testing.T , name string ) string {
0 commit comments