internal/fetch: test local fetching with empty module path
For golang/go#47780
Change-Id: Ib32d9b56540ce6786e5951829fcf2b8999303057
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/343211
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/fetch/fetchlocal_test.go b/internal/fetch/fetchlocal_test.go
new file mode 100644
index 0000000..25d7252
--- /dev/null
+++ b/internal/fetch/fetchlocal_test.go
@@ -0,0 +1,32 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fetch
+
+import (
+ "context"
+ "errors"
+ "testing"
+
+ "golang.org/x/pkgsite/internal/derrors"
+ "golang.org/x/pkgsite/internal/source"
+)
+
+func TestLocalEmptyModulePath(t *testing.T) {
+ // Test local fetching when the module path is empty (corresponding to the
+ // main module of a directory). Other cases are tested in TestFetchModule.
+ ctx := context.Background()
+ got := FetchLocalModule(ctx, "", "testdata/has_go_mod", source.NewClientForTesting())
+ if got.Error != nil {
+ t.Fatal(got.Error)
+ }
+ if want := "testmod"; got.ModulePath != want {
+ t.Errorf("got %q, want %q", got.ModulePath, want)
+ }
+
+ got = FetchLocalModule(ctx, "", "testdata/no_go_mod", source.NewClientForTesting())
+ if !errors.Is(got.Error, derrors.BadModule) {
+ t.Errorf("got %v, want BadModule", got.Error)
+ }
+}
diff --git a/internal/fetch/testdata/has_go_mod/a.go b/internal/fetch/testdata/has_go_mod/a.go
new file mode 100644
index 0000000..f13289e
--- /dev/null
+++ b/internal/fetch/testdata/has_go_mod/a.go
@@ -0,0 +1,5 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
diff --git a/internal/fetch/testdata/has_go_mod/go.mod b/internal/fetch/testdata/has_go_mod/go.mod
new file mode 100644
index 0000000..c5553d5
--- /dev/null
+++ b/internal/fetch/testdata/has_go_mod/go.mod
@@ -0,0 +1 @@
+module testmod
diff --git a/internal/fetch/testdata/no_go_mod/a.go b/internal/fetch/testdata/no_go_mod/a.go
new file mode 100644
index 0000000..f13289e
--- /dev/null
+++ b/internal/fetch/testdata/no_go_mod/a.go
@@ -0,0 +1,5 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a