From fb97fdee6b0cc26602d1cb1eaa02c2c9bb30613d Mon Sep 17 00:00:00 2001 From: Rupert Shuttleworth Date: Thu, 11 Feb 2021 03:40:05 +0000 Subject: [PATCH] Move sh_binary tests for bp2build into their own file. Test: go test passes for refactored tests. Change-Id: Ie094031f4b1f190aa711a37d40bacb182e277b26 --- bp2build/Android.bp | 1 + bp2build/build_conversion_test.go | 24 ------ bp2build/sh_conversion_test.go | 134 ++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 24 deletions(-) create mode 100644 bp2build/sh_conversion_test.go diff --git a/bp2build/Android.bp b/bp2build/Android.bp index fdac88dfd..6570b163c 100644 --- a/bp2build/Android.bp +++ b/bp2build/Android.bp @@ -22,6 +22,7 @@ bootstrap_go_package { "build_conversion_test.go", "bzl_conversion_test.go", "conversion_test.go", + "sh_conversion_test.go", "testing.go", ], pluginFor: [ diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go index df554a09a..27212d135 100644 --- a/bp2build/build_conversion_test.go +++ b/bp2build/build_conversion_test.go @@ -17,7 +17,6 @@ package bp2build import ( "android/soong/android" "android/soong/genrule" - "android/soong/sh" "strings" "testing" ) @@ -358,12 +357,6 @@ load("//build/bazel/rules:java.bzl", "java_binary")`, ruleClass: "genrule", // Note: no bzlLoadLocation for native rules }, - BazelTarget{ - name: "sh_binary_target", - ruleClass: "sh_binary", - // Note: no bzlLoadLocation for native rules - // TODO(ruperts): Could open source the existing, experimental Starlark sh_ rules? - }, }, expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") load("//build/bazel/rules:java.bzl", "java_binary")`, @@ -860,23 +853,6 @@ genrule { )`, }, }, - { - description: "sh_binary test", - moduleTypeUnderTest: "sh_binary", - moduleTypeUnderTestFactory: sh.ShBinaryFactory, - moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build, - bp: `sh_binary { - name: "foo", - src: "foo.sh", - bazel_module: { bp2build_available: true }, -}`, - expectedBazelTargets: []string{`sh_binary( - name = "foo", - srcs = [ - "foo.sh", - ], -)`}, - }, } dir := "." diff --git a/bp2build/sh_conversion_test.go b/bp2build/sh_conversion_test.go new file mode 100644 index 000000000..dcc75bd5c --- /dev/null +++ b/bp2build/sh_conversion_test.go @@ -0,0 +1,134 @@ +// 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 + +import ( + "android/soong/android" + "android/soong/sh" + "strings" + "testing" +) + +func TestShBinaryLoadStatement(t *testing.T) { + testCases := []struct { + bazelTargets BazelTargets + expectedLoadStatements string + }{ + { + bazelTargets: BazelTargets{ + BazelTarget{ + name: "sh_binary_target", + ruleClass: "sh_binary", + // Note: no bzlLoadLocation for native rules + // TODO(ruperts): Could open source the existing, experimental Starlark sh_ rules? + }, + }, + expectedLoadStatements: ``, + }, + } + + for _, testCase := range testCases { + actual := testCase.bazelTargets.LoadStatements() + expected := testCase.expectedLoadStatements + if actual != expected { + t.Fatalf("Expected load statements to be %s, got %s", expected, actual) + } + } + +} + +func TestShBinaryBp2Build(t *testing.T) { + testCases := []struct { + description string + moduleTypeUnderTest string + moduleTypeUnderTestFactory android.ModuleFactory + moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) + preArchMutators []android.RegisterMutatorFunc + depsMutators []android.RegisterMutatorFunc + bp string + expectedBazelTargets []string + filesystem map[string]string + dir string + }{ + { + description: "sh_binary test", + moduleTypeUnderTest: "sh_binary", + moduleTypeUnderTestFactory: sh.ShBinaryFactory, + moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build, + bp: `sh_binary { + name: "foo", + src: "foo.sh", + bazel_module: { bp2build_available: true }, +}`, + expectedBazelTargets: []string{`sh_binary( + name = "foo", + srcs = [ + "foo.sh", + ], +)`}, + }, + } + + dir := "." + for _, testCase := range testCases { + filesystem := make(map[string][]byte) + toParse := []string{ + "Android.bp", + } + for f, content := range testCase.filesystem { + if strings.HasSuffix(f, "Android.bp") { + toParse = append(toParse, f) + } + filesystem[f] = []byte(content) + } + config := android.TestConfig(buildDir, nil, testCase.bp, filesystem) + ctx := android.NewTestContext(config) + ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) + for _, m := range testCase.depsMutators { + ctx.DepsBp2BuildMutators(m) + } + ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) + ctx.RegisterForBazelConversion() + + _, errs := ctx.ParseFileList(dir, toParse) + if Errored(t, testCase.description, errs) { + continue + } + _, errs = ctx.ResolveDependencies(config) + if Errored(t, testCase.description, errs) { + continue + } + + checkDir := dir + if testCase.dir != "" { + checkDir = testCase.dir + } + bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[checkDir] + if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { + t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) + } else { + for i, target := range bazelTargets { + if w, g := testCase.expectedBazelTargets[i], target.content; w != g { + t.Errorf( + "%s: Expected generated Bazel target to be '%s', got '%s'", + testCase.description, + w, + g, + ) + } + } + } + } +}