@@ -35,6 +35,7 @@ import (
3535)
3636
3737var _ uniast.Writer = (* Writer )(nil )
38+ var testPkgPathRegex = regexp .MustCompile (`^(.+?) \[(.+)\]$` )
3839
3940type Options struct {
4041 // RepoDir string
@@ -81,6 +82,22 @@ func (w *Writer) WriteRepo(repo *uniast.Repository, outDir string) error {
8182 return nil
8283}
8384
85+ // sanitizePkgPath sanitize the package path, remove the suffix in brackets
86+ func sanitizePkgPath (pkgPath string ) string {
87+ matches := testPkgPathRegex .FindStringSubmatch (pkgPath )
88+ // matches should be 3 elements:
89+ // 1. The full string
90+ // 2. The package name
91+ // 3. The content inside the brackets
92+ if len (matches ) == 3 {
93+ packageName := matches [1 ]
94+ testName := matches [2 ]
95+ if testName == packageName + ".test" {
96+ return packageName
97+ }
98+ }
99+ return pkgPath
100+ }
84101func (w * Writer ) WriteModule (repo * uniast.Repository , modPath string , outDir string ) error {
85102 mod := repo .Modules [modPath ]
86103 if mod == nil {
@@ -94,7 +111,9 @@ func (w *Writer) WriteModule(repo *uniast.Repository, modPath string, outDir str
94111
95112 outdir := filepath .Join (outDir , mod .Dir )
96113 for dir , pkg := range w .visited {
97- rel := strings .TrimPrefix (dir , mod .Name )
114+ // sanitize the package path
115+ cleanDir := sanitizePkgPath (dir )
116+ rel := strings .TrimPrefix (cleanDir , mod .Name )
98117 pkgDir := filepath .Join (outdir , rel )
99118 if err := os .MkdirAll (pkgDir , 0755 ); err != nil {
100119 return fmt .Errorf ("mkdir %s failed: %v" , pkgDir , err )
0 commit comments