internal/testing/sample: add UnitForPackage

For golang/go#39629

Change-Id: Idcc525d1127a0e5bb67a758d8e049402997fc261
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/263214
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go
index 969ce87..5cd4793 100644
--- a/internal/testing/sample/sample.go
+++ b/internal/testing/sample/sample.go
@@ -93,6 +93,32 @@
 	return time.Now().Truncate(time.Microsecond)
 }
 
+// UnitForPackage constructs a unit with the given module path and suffix.
+//
+// If modulePath is the standard library, the package path is the
+// suffix, which must not be empty. Otherwise, the package path
+// is the concatenation of modulePath and suffix.
+//
+// The package name is last component of the package path.
+func UnitForPackage(modulePath, suffix string) *internal.Unit {
+	p := constructFullPath(modulePath, suffix)
+	return &internal.Unit{
+		UnitMeta: internal.UnitMeta{
+			Name:              path.Base(p),
+			Path:              p,
+			IsRedistributable: true,
+			Licenses:          LicenseMetadata,
+		},
+		Documentation: &internal.Documentation{
+			Synopsis: Synopsis,
+			HTML:     DocumentationHTML,
+			GOOS:     GOOS,
+			GOARCH:   GOARCH,
+		},
+		Imports: Imports,
+	}
+}
+
 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",