MCPcopy Create free account
hub / github.com/stretchr/testify / parsePackageSource

Function parsePackageSource

_codegen/main.go:176–215  ·  view source on GitHub ↗

parsePackageSource returns the types scope and the package documentation from the package

(pkg string)

Source from the content-addressed store, hash-verified

174
175// parsePackageSource returns the types scope and the package documentation from the package
176func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
177 pd, err := build.Import(pkg, ".", 0)
178 if err != nil {
179 return nil, nil, err
180 }
181
182 fset := token.NewFileSet()
183 files := make(map[string]*ast.File)
184 fileList := make([]*ast.File, len(pd.GoFiles))
185 for i, fname := range pd.GoFiles {
186 src, err := ioutil.ReadFile(path.Join(pd.Dir, fname))
187 if err != nil {
188 return nil, nil, err
189 }
190 f, err := parser.ParseFile(fset, fname, src, parser.ParseComments|parser.AllErrors)
191 if err != nil {
192 return nil, nil, err
193 }
194 files[fname] = f
195 fileList[i] = f
196 }
197
198 cfg := types.Config{
199 Importer: importer.For("source", nil),
200 }
201 info := types.Info{
202 Defs: make(map[*ast.Ident]types.Object),
203 }
204 tp, err := cfg.Check(pkg, fset, fileList, &info)
205 if err != nil {
206 return nil, nil, err
207 }
208
209 scope := tp.Scope()
210
211 ap, _ := ast.NewPackage(fset, files, nil, nil)
212 docs := doc.New(ap, pkg, 0)
213
214 return scope, docs, nil
215}
216
217type testFunc struct {
218 CurrentPkg string

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected