internal/testing/sample: add AddPackage
For golang/go#39629
Trust: Julie Qiu
Run-TryBot: Julie Qiu
TryBot-Result: kokoro
@@ -93,6 +93,41 @@ func NowTruncated() time.Time {
return time.Now().Truncate(time.Microsecond)
}
+func AddPackage(m *internal.Module, fullPath string) *internal.Module {
+ if m.ModulePath != stdlib.ModulePath && !strings.HasPrefix(fullPath, m.ModulePath) {
+ panic(fmt.Sprintf("package path %q not a prefix of module path %q",
+ fullPath, m.ModulePath))
+ }
+ AddUnit(m, &internal.Unit{
+ UnitMeta: *UnitMeta(fullPath, m.ModulePath, m.Version, path.Base(fullPath), true),
+ Imports: Imports,
+ LicenseContents: Licenses,
+ Documentation: &internal.Documentation{
+ Synopsis: Synopsis,
+ HTML: DocumentationHTML,
+ GOOS: GOOS,
+ GOARCH: GOARCH,
+ },
+ })
+ minLen := len(m.ModulePath)
+ if m.ModulePath == stdlib.ModulePath {
+ minLen = 1
+ }
+ for pth := fullPath; len(pth) > minLen; pth = path.Dir(pth) {
+ found := false
+ for _, u := range m.Units {
+ if u.Path == pth {
+ found = true
+ break
+ }
+ }
+ if !found {
+ AddUnit(m, UnitEmpty(pth, m.ModulePath, m.Version))
+ }
+ }
+ return m
+}
+
func PackageMeta(fullPath string) *internal.PackageMeta {
return &internal.PackageMeta{
Path: fullPath,
