From f6f5076d4026eb90c4e6603f2b56b86402fc0bfb Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 26 Aug 2021 06:36:22 -0400 Subject: internal/fetchdatasource: handle build contexts properly GetUnit returns a Unit with its Documentation set to a matching BuildContext. This is the same behavior as postgres.DB.GetUnit. For golang/go#47780 Change-Id: I2bc23b7bc5a006e78bec54f6f3229e59ab5a03ef Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/345269 Trust: Jonathan Amsterdam Run-TryBot: Jonathan Amsterdam TryBot-Result: kokoro Reviewed-by: Jamal Carvalho Reviewed-by: Julie Qiu --- internal/fetchdatasource/fetchdatasource_test.go | 41 +++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'internal/fetchdatasource/fetchdatasource_test.go') diff --git a/internal/fetchdatasource/fetchdatasource_test.go b/internal/fetchdatasource/fetchdatasource_test.go index 995dfad9..e807ec7a 100644 --- a/internal/fetchdatasource/fetchdatasource_test.go +++ b/internal/fetchdatasource/fetchdatasource_test.go @@ -449,7 +449,7 @@ func TestLocalGetUnitMeta(t *testing.T) { } } -func TestLocalGetUnit(t *testing.T) { +func TestGetUnit(t *testing.T) { // This is a simple test to verify that data is fetched correctly. The // return value of FetchResult is tested in internal/fetch so no need // to repeat it. @@ -509,6 +509,45 @@ func TestLocalGetUnit(t *testing.T) { } } +func TestBuildConstraints(t *testing.T) { + // The Unit returned by GetUnit should have a single Documentation that + // matches the BuildContext argument. + ctx, ds, teardown := setup(t, defaultTestModules, true) + defer teardown() + + um := &internal.UnitMeta{ + Path: "example.com/build-constraints/cpu", + ModuleInfo: internal.ModuleInfo{ + ModulePath: "example.com/build-constraints", + Version: version.Latest, + }, + } + for _, test := range []struct { + in, want internal.BuildContext + }{ + {internal.BuildContext{}, internal.BuildContextLinux}, + {internal.BuildContextLinux, internal.BuildContextLinux}, + {internal.BuildContextDarwin, internal.BuildContextDarwin}, + {internal.BuildContext{GOOS: "LiverPaté", GOARCH: "DeTriomphe"}, internal.BuildContext{}}, + } { + t.Run(test.in.String(), func(t *testing.T) { + u, err := ds.GetUnit(ctx, um, internal.AllFields, test.in) + if err != nil { + t.Fatal(err) + } + if test.want == (internal.BuildContext{}) { + if len(u.Documentation) != 0 { + t.Error("got docs, want none") + } + } else if n := len(u.Documentation); n != 1 { + t.Errorf("got %d docs, want 1", n) + } else if got := u.Documentation[0].BuildContext(); got != test.want { + t.Errorf("got %s, want %s", got, test.want) + } + }) + } +} + func TestCache(t *testing.T) { ds := Options{}.New() m1 := &internal.Module{} -- cgit v1.3-5-g9baa