Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5272a412 authored by Alex Márquez Pérez Muñíz Díaz Púras Thaureaux's avatar Alex Márquez Pérez Muñíz Díaz Púras Thaureaux Committed by Gerrit Code Review
Browse files

Merge changes from topic "share-runBp2BuildTestCase+runPythonTestCase"

* changes:
  Add License decl to testing.go
  Document testing & build_conversion purposes
  Rename runPythonTestCase
  Share runBp2BuildTestCase & runPythonTestCase
parents 373e71c5 f5a3eacc
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -14,14 +14,20 @@

package bp2build

/*
For shareable/common functionality for conversion from soong-module to build files
for queryview/bp2build
*/

import (
	"android/soong/android"
	"android/soong/bazel"
	"fmt"
	"reflect"
	"sort"
	"strings"

	"android/soong/android"
	"android/soong/bazel"

	"github.com/google/blueprint"
	"github.com/google/blueprint/proptools"
)
+2 −55
Original line number Diff line number Diff line
@@ -15,10 +15,10 @@
package bp2build

import (
	"testing"

	"android/soong/android"
	"android/soong/cc"
	"strings"
	"testing"
)

const (
@@ -54,59 +54,6 @@ func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
	ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
}

func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
	t.Helper()
	dir := "."
	filesystem := make(map[string][]byte)
	toParse := []string{
		"Android.bp",
	}
	for f, content := range tc.filesystem {
		if strings.HasSuffix(f, "Android.bp") {
			toParse = append(toParse, f)
		}
		filesystem[f] = []byte(content)
	}
	config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
	ctx := android.NewTestContext(config)

	registerModuleTypes(ctx)
	ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
	ctx.RegisterBp2BuildConfig(bp2buildConfig)
	ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
	ctx.RegisterForBazelConversion()

	_, errs := ctx.ParseFileList(dir, toParse)
	if errored(t, tc.description, errs) {
		return
	}
	_, errs = ctx.ResolveDependencies(config)
	if errored(t, tc.description, errs) {
		return
	}

	checkDir := dir
	if tc.dir != "" {
		checkDir = tc.dir
	}
	codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
	bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
	if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
		t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
	} else {
		for i, target := range bazelTargets {
			if w, g := tc.expectedBazelTargets[i], target.content; w != g {
				t.Errorf(
					"%s: Expected generated Bazel target to be '%s', got '%s'",
					tc.description,
					w,
					g,
				)
			}
		}
	}
}

func TestCcLibrarySimple(t *testing.T) {
	runCcLibraryTestCase(t, bp2buildTestCase{
		description:                        "cc_library - simple example",
+0 −11
Original line number Diff line number Diff line
@@ -40,17 +40,6 @@ toolchain_library {
}`
)

type bp2buildTestCase struct {
	description                        string
	moduleTypeUnderTest                string
	moduleTypeUnderTestFactory         android.ModuleFactory
	moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
	blueprint                          string
	expectedBazelTargets               []string
	filesystem                         map[string]string
	dir                                string
}

func TestCcLibraryHeadersLoadStatement(t *testing.T) {
	testCases := []struct {
		bazelTargets           BazelTargets
+3 −9
Original line number Diff line number Diff line
@@ -3,17 +3,11 @@ package bp2build
import (
	"testing"

	"android/soong/android"
	"android/soong/python"
)

func runPythonTestCase(t *testing.T, tc bp2buildTestCase) {
	t.Helper()
	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
}

func TestPythonBinaryHostSimple(t *testing.T) {
	runPythonTestCase(t, bp2buildTestCase{
	runBp2BuildTestCaseSimple(t, bp2buildTestCase{
		description:                        "simple python_binary_host converts to a native py_binary",
		moduleTypeUnderTest:                "python_binary_host",
		moduleTypeUnderTestFactory:         python.PythonBinaryHostFactory,
@@ -49,7 +43,7 @@ func TestPythonBinaryHostSimple(t *testing.T) {
}

func TestPythonBinaryHostPy2(t *testing.T) {
	runPythonTestCase(t, bp2buildTestCase{
	runBp2BuildTestCaseSimple(t, bp2buildTestCase{
		description:                        "py2 python_binary_host",
		moduleTypeUnderTest:                "python_binary_host",
		moduleTypeUnderTestFactory:         python.PythonBinaryHostFactory,
@@ -79,7 +73,7 @@ func TestPythonBinaryHostPy2(t *testing.T) {
}

func TestPythonBinaryHostPy3(t *testing.T) {
	runPythonTestCase(t, bp2buildTestCase{
	runBp2BuildTestCaseSimple(t, bp2buildTestCase{
		description:                        "py3 python_binary_host",
		moduleTypeUnderTest:                "python_binary_host",
		moduleTypeUnderTestFactory:         python.PythonBinaryHostFactory,
+100 −11
Original line number Diff line number Diff line
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bp2build

/*
For shareable/common bp2build testing functionality and dumping ground for
specific-but-shared functionality among tests in package
*/

import (
	"strings"
	"testing"

	"android/soong/android"
@@ -16,6 +36,86 @@ var (
	buildDir string
)

func errored(t *testing.T, desc string, errs []error) bool {
	t.Helper()
	if len(errs) > 0 {
		for _, err := range errs {
			t.Errorf("%s: %s", desc, err)
		}
		return true
	}
	return false
}

func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) {
	t.Helper()
	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
}

type bp2buildTestCase struct {
	description                        string
	moduleTypeUnderTest                string
	moduleTypeUnderTestFactory         android.ModuleFactory
	moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
	blueprint                          string
	expectedBazelTargets               []string
	filesystem                         map[string]string
	dir                                string
}

func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
	t.Helper()
	dir := "."
	filesystem := make(map[string][]byte)
	toParse := []string{
		"Android.bp",
	}
	for f, content := range tc.filesystem {
		if strings.HasSuffix(f, "Android.bp") {
			toParse = append(toParse, f)
		}
		filesystem[f] = []byte(content)
	}
	config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
	ctx := android.NewTestContext(config)

	registerModuleTypes(ctx)
	ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
	ctx.RegisterBp2BuildConfig(bp2buildConfig)
	ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
	ctx.RegisterForBazelConversion()

	_, errs := ctx.ParseFileList(dir, toParse)
	if errored(t, tc.description, errs) {
		return
	}
	_, errs = ctx.ResolveDependencies(config)
	if errored(t, tc.description, errs) {
		return
	}

	checkDir := dir
	if tc.dir != "" {
		checkDir = tc.dir
	}
	codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
	bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
	if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
		t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
	} else {
		for i, target := range bazelTargets {
			if w, g := tc.expectedBazelTargets[i], target.content; w != g {
				t.Errorf(
					"%s: Expected generated Bazel target to be '%s', got '%s'",
					tc.description,
					w,
					g,
				)
			}
		}
	}
}

type nestedProps struct {
	Nested_prop string
}
@@ -44,17 +144,6 @@ type customModule struct {
	props customProps
}

func errored(t *testing.T, desc string, errs []error) bool {
	t.Helper()
	if len(errs) > 0 {
		for _, err := range errs {
			t.Errorf("%s: %s", desc, err)
		}
		return true
	}
	return false
}

// OutputFiles is needed because some instances of this module use dist with a
// tag property which requires the module implements OutputFileProducer.
func (m *customModule) OutputFiles(tag string) (android.Paths, error) {