Convert python package to fixtures

Bug: 182885307
Test: m nothing
Change-Id: I78506a10f0e6ba8ad4952e8305f47ffd5a33b359
This commit is contained in:
Paul Duffin
2021-03-17 22:38:23 +00:00
parent d0890452e3
commit 803876aaa6

View File

@@ -15,21 +15,15 @@
package python package python
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "regexp"
"sort"
"strings"
"testing" "testing"
"android/soong/android" "android/soong/android"
) )
var buildDir string
type pyModule struct { type pyModule struct {
name string name string
actualVersion string actualVersion string
@@ -56,7 +50,7 @@ var (
data = []struct { data = []struct {
desc string desc string
mockFiles map[string][]byte mockFiles android.MockFS
errors []string errors []string
expectedBinaries []pyModule expectedBinaries []pyModule
@@ -64,7 +58,6 @@ var (
{ {
desc: "module without any src files", desc: "module without any src files",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_library_host { `python_library_host {
name: "lib1", name: "lib1",
@@ -79,7 +72,6 @@ var (
{ {
desc: "module with bad src file ext", desc: "module with bad src file ext",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_library_host { `python_library_host {
name: "lib1", name: "lib1",
@@ -98,7 +90,6 @@ var (
{ {
desc: "module with bad data file ext", desc: "module with bad data file ext",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_library_host { `python_library_host {
name: "lib1", name: "lib1",
@@ -121,7 +112,6 @@ var (
{ {
desc: "module with bad pkg_path format", desc: "module with bad pkg_path format",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_library_host { `python_library_host {
name: "lib1", name: "lib1",
@@ -159,7 +149,6 @@ var (
{ {
desc: "module with bad runfile src path format", desc: "module with bad runfile src path format",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_library_host { `python_library_host {
name: "lib1", name: "lib1",
@@ -187,7 +176,6 @@ var (
{ {
desc: "module with duplicate runfile path", desc: "module with duplicate runfile path",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_library_host { `python_library_host {
name: "lib1", name: "lib1",
@@ -233,7 +221,6 @@ var (
{ {
desc: "module for testing dependencies", desc: "module for testing dependencies",
mockFiles: map[string][]byte{ mockFiles: map[string][]byte{
bpFile: []byte(`subdirs = ["dir"]`),
filepath.Join("dir", bpFile): []byte( filepath.Join("dir", bpFile): []byte(
`python_defaults { `python_defaults {
name: "default_lib", name: "default_lib",
@@ -326,10 +313,10 @@ var (
"e/default_py3.py", "e/default_py3.py",
"e/file4.py", "e/file4.py",
}, },
srcsZip: "@prefix@/.intermediates/dir/bin/PY3/bin.py.srcszip", srcsZip: "out/soong/.intermediates/dir/bin/PY3/bin.py.srcszip",
depsSrcsZips: []string{ depsSrcsZips: []string{
"@prefix@/.intermediates/dir/lib5/PY3/lib5.py.srcszip", "out/soong/.intermediates/dir/lib5/PY3/lib5.py.srcszip",
"@prefix@/.intermediates/dir/lib6/PY3/lib6.py.srcszip", "out/soong/.intermediates/dir/lib6/PY3/lib6.py.srcszip",
}, },
}, },
}, },
@@ -339,60 +326,37 @@ var (
func TestPythonModule(t *testing.T) { func TestPythonModule(t *testing.T) {
for _, d := range data { for _, d := range data {
if d.desc != "module with duplicate runfile path" {
continue
}
errorPatterns := make([]string, len(d.errors))
for i, s := range d.errors {
errorPatterns[i] = regexp.QuoteMeta(s)
}
t.Run(d.desc, func(t *testing.T) { t.Run(d.desc, func(t *testing.T) {
config := android.TestConfig(buildDir, nil, "", d.mockFiles) result := emptyFixtureFactory.
ctx := android.NewTestContext(config) ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(errorPatterns)).
ctx.PreDepsMutators(RegisterPythonPreDepsMutators) RunTest(t,
ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory) android.PrepareForTestWithDefaults,
ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) PrepareForTestWithPythonBuildComponents,
ctx.RegisterModuleType("python_defaults", defaultsFactory) d.mockFiles.AddToFixture(),
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) )
ctx.Register()
_, testErrs := ctx.ParseBlueprintsFiles(bpFile) if len(result.Errs) > 0 {
android.FailIfErrored(t, testErrs) return
_, actErrs := ctx.PrepareBuildActions(config) }
if len(actErrs) > 0 || len(d.errors) > 0 {
testErrs = append(testErrs, expectErrors(t, actErrs, d.errors)...) for _, e := range d.expectedBinaries {
} else { t.Run(e.name, func(t *testing.T) {
for _, e := range d.expectedBinaries { expectModule(t, result.TestContext, e.name, e.actualVersion, e.srcsZip, e.pyRunfiles, e.depsSrcsZips)
testErrs = append(testErrs, })
expectModule(t, ctx, buildDir, e.name,
e.actualVersion,
e.srcsZip,
e.pyRunfiles,
e.depsSrcsZips)...)
}
} }
android.FailIfErrored(t, testErrs)
}) })
} }
} }
func expectErrors(t *testing.T, actErrs []error, expErrs []string) (testErrs []error) { func expectModule(t *testing.T, ctx *android.TestContext, name, variant, expectedSrcsZip string, expectedPyRunfiles, expectedDepsSrcsZips []string) {
actErrStrs := []string{}
for _, v := range actErrs {
actErrStrs = append(actErrStrs, v.Error())
}
sort.Strings(actErrStrs)
if len(actErrStrs) != len(expErrs) {
t.Errorf("got (%d) errors, expected (%d) errors!", len(actErrStrs), len(expErrs))
for _, v := range actErrStrs {
testErrs = append(testErrs, errors.New(v))
}
} else {
sort.Strings(expErrs)
for i, v := range actErrStrs {
if !strings.Contains(v, expErrs[i]) {
testErrs = append(testErrs, errors.New(v))
}
}
}
return
}
func expectModule(t *testing.T, ctx *android.TestContext, buildDir, name, variant, expectedSrcsZip string,
expectedPyRunfiles, expectedDepsSrcsZips []string) (testErrs []error) {
module := ctx.ModuleForTests(name, variant) module := ctx.ModuleForTests(name, variant)
base, baseOk := module.Module().(*Module) base, baseOk := module.Module().(*Module)
@@ -405,56 +369,15 @@ func expectModule(t *testing.T, ctx *android.TestContext, buildDir, name, varian
actualPyRunfiles = append(actualPyRunfiles, path.dest) actualPyRunfiles = append(actualPyRunfiles, path.dest)
} }
if !reflect.DeepEqual(actualPyRunfiles, expectedPyRunfiles) { android.AssertDeepEquals(t, "pyRunfiles", expectedPyRunfiles, actualPyRunfiles)
testErrs = append(testErrs, errors.New(fmt.Sprintf(
`binary "%s" variant "%s" has unexpected pyRunfiles: %q! (expected: %q)`,
base.Name(),
base.properties.Actual_version,
actualPyRunfiles,
expectedPyRunfiles)))
}
if base.srcsZip.String() != strings.Replace(expectedSrcsZip, "@prefix@", buildDir, 1) { android.AssertPathRelativeToTopEquals(t, "srcsZip", expectedSrcsZip, base.srcsZip)
testErrs = append(testErrs, errors.New(fmt.Sprintf(
`binary "%s" variant "%s" has unexpected srcsZip: %q!`,
base.Name(),
base.properties.Actual_version,
base.srcsZip)))
}
for i, _ := range expectedDepsSrcsZips { android.AssertPathsRelativeToTopEquals(t, "depsSrcsZips", expectedDepsSrcsZips, base.depsSrcsZips)
expectedDepsSrcsZips[i] = strings.Replace(expectedDepsSrcsZips[i], "@prefix@", buildDir, 1)
}
if !reflect.DeepEqual(base.depsSrcsZips.Strings(), expectedDepsSrcsZips) {
testErrs = append(testErrs, errors.New(fmt.Sprintf(
`binary "%s" variant "%s" has unexpected depsSrcsZips: %q!`,
base.Name(),
base.properties.Actual_version,
base.depsSrcsZips)))
}
return
} }
func setUp() { var emptyFixtureFactory = android.NewFixtureFactory(nil)
var err error
buildDir, err = ioutil.TempDir("", "soong_python_test")
if err != nil {
panic(err)
}
}
func tearDown() {
os.RemoveAll(buildDir)
}
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
run := func() int { os.Exit(m.Run())
setUp()
defer tearDown()
return m.Run()
}
os.Exit(run())
} }