Files
ent/cmd/entc/packages_test.go
Ariel Mashraki e6c6442c84 cmd/entc: load package path for arbitrary file paths (#186)
Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/186

This is an ongoing work for fixing #61 (Github issue)

Reviewed By: alexsn

Differential Revision: D18672982

fbshipit-source-id: 12051d7b06d87d73d37b68382566732e532193e3
2019-11-24 07:11:23 -08:00

41 lines
1.0 KiB
Go

package main
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/packages/packagestest"
)
func TestPkgPath(t *testing.T) { packagestest.TestAll(t, testPkgPath) }
func testPkgPath(t *testing.T, x packagestest.Exporter) {
e := packagestest.Export(t, x, []packagestest.Module{
{
Name: "golang.org/x",
Files: map[string]interface{}{
"x.go": "package x",
"y/y.go": "package y",
},
},
})
defer e.Cleanup()
e.Config.Dir = filepath.Dir(e.File("golang.org/x", "x.go"))
target := filepath.Join(e.Config.Dir, "ent")
pkgPath, err := PkgPath(e.Config, target)
require.NoError(t, err)
require.Equal(t, "golang.org/x/ent", pkgPath)
e.Config.Dir = filepath.Dir(e.File("golang.org/x", "y/y.go"))
target = filepath.Join(e.Config.Dir, "ent")
pkgPath, err = PkgPath(e.Config, target)
require.NoError(t, err)
require.Equal(t, "golang.org/x/y/ent", pkgPath)
target = filepath.Join(e.Config.Dir, "z/ent")
pkgPath, err = PkgPath(e.Config, target)
require.Error(t, err)
require.Empty(t, pkgPath)
}