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

Commit 175073c4 authored by Joe Onorato's avatar Joe Onorato
Browse files

Make aconfig flags generate a library instead of a srcjar.

Also add unit tests for the rest of device_config

Bug: 283475679
Test: m nothing (soong unit tests)
Change-Id: Iee18a1f2f2cbb23e8c8d84c54e903b32be29a693
parent f1d37b35
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -200,6 +200,22 @@ func AssertArrayString(t *testing.T, message string, expected, actual []string)
	}
}

// Asserts that each of the Paths in actual end with the corresponding string
// from expected. Useful to test that output paths contain expected items without
// hard-coding where intermediate files might be located.
func AssertPathsEndWith(t *testing.T, message string, expected []string, actual []Path) {
	t.Helper()
	if len(expected) != len(actual) {
		t.Errorf("%s (length): expected %d, actual %d", message, len(expected), len(actual))
		return
	}
	for i := range expected {
		if !strings.HasSuffix(actual[i].String(), expected[i]) {
			t.Errorf("%s (item %d): expected '%s', actual '%s'", message, i, expected[i], actual[i].String())
		}
	}
}

// AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
// if they are not then it reports an error prefixed with the supplied message and including a
// reason for why it failed.
+7 −5
Original line number Diff line number Diff line
@@ -12,19 +12,21 @@ bootstrap_go_package {
        "soong",
        "soong-android",
        "soong-bazel",
        "soong-shared",
        "soong-android",
        "soong-java",
    ],
    srcs: [
        "device_config_definitions.go",
        "device_config_values.go",
        "device_config_value_set.go",
        "init.go",
        //"testing.go"
        "java_device_config_definitions_library.go",
        "testing.go",
    ],
    /*
    testSrcs: [
        "device_config_test.go",
        "device_config_definitions_test.go",
        "device_config_values_test.go",
        "device_config_value_set_test.go",
    ],
    */
    pluginFor: ["soong_build"],
}
+13 −20
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ type DefinitionsModule struct {
	}

	intermediatePath android.WritablePath
	srcJarPath       android.WritablePath
}

func DefinitionsFactory() android.Module {
@@ -78,8 +77,6 @@ func (module *DefinitionsModule) DepsMutator(ctx android.BottomUpMutatorContext)

func (module *DefinitionsModule) OutputFiles(tag string) (android.Paths, error) {
	switch tag {
	case ".srcjar":
		return []android.Path{module.srcJarPath}, nil
	case "":
		// The default output of this module is the intermediates format, which is
		// not installable and in a private format that no other rules can handle
@@ -99,6 +96,14 @@ func joinAndPrefix(prefix string, values []string) string {
	return sb.String()
}

// Provider published by device_config_value_set
type definitionsProviderData struct {
	namespace        string
	intermediatePath android.WritablePath
}

var definitionsProviderKey = blueprint.NewProvider(definitionsProviderData{})

func (module *DefinitionsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	// Get the values that came from the global RELEASE_DEVICE_CONFIG_VALUE_SETS flag
	ctx.VisitDirectDeps(func(dep android.Module) {
@@ -117,11 +122,11 @@ func (module *DefinitionsModule) GenerateAndroidBuildActions(ctx android.ModuleC

	// Intermediate format
	inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
	module.intermediatePath = android.PathForModuleOut(ctx, "intermediate.json")
	intermediatePath := android.PathForModuleOut(ctx, "intermediate.json")
	ctx.Build(pctx, android.BuildParams{
		Rule:        aconfigRule,
		Inputs:      inputFiles,
		Output:      module.intermediatePath,
		Output:      intermediatePath,
		Description: "device_config_definitions",
		Args: map[string]string{
			"release_version": ctx.Config().ReleaseVersion(),
@@ -130,21 +135,9 @@ func (module *DefinitionsModule) GenerateAndroidBuildActions(ctx android.ModuleC
		},
	})

	// Generated java inside a srcjar
	module.srcJarPath = android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
	ctx.Build(pctx, android.BuildParams{
		Rule:        srcJarRule,
		Input:       module.intermediatePath,
		Output:      module.srcJarPath,
		Description: "device_config.srcjar",
	ctx.SetProvider(definitionsProviderKey, definitionsProviderData{
		namespace:        module.properties.Namespace,
		intermediatePath: intermediatePath,
	})

	// TODO: C++

	// Phony target for debugging convenience
	ctx.Build(pctx, android.BuildParams{
		Rule:   blueprint.Phony,
		Output: android.PathForPhony(ctx, ctx.ModuleName()),
		Inputs: []android.Path{module.srcJarPath}, // TODO: C++
	})
}
+42 −0
Original line number Diff line number Diff line
// Copyright (C) 2019 The Android Open Source Project
// Copyright 2018 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.
@@ -15,47 +15,28 @@
package device_config

import (
	"os"
	"strings"
	"testing"

	"android/soong/android"
	"android/soong/java"
)

func TestMain(m *testing.M) {
	os.Exit(m.Run())
func TestDeviceConfigDefinitions(t *testing.T) {
	bp := `
		device_config_definitions {
			name: "module_name",
			namespace: "com.example.package",
			srcs: ["foo.aconfig"],
		}
	`
	result := runTest(t, android.FixtureExpectsNoErrors, bp)

func test(t *testing.T, bp string) *android.TestResult {
	t.Helper()
	module := result.ModuleForTests("module_name", "").Module().(*DefinitionsModule)

	mockFS := android.MockFS{
		"config.aconfig": nil,
	// Check that the provider has the right contents
	depData := result.ModuleProvider(module, definitionsProviderKey).(definitionsProviderData)
	android.AssertStringEquals(t, "namespace", depData.namespace, "com.example.package")
	if !strings.HasSuffix(depData.intermediatePath.String(), "/intermediate.json") {
		t.Errorf("Missing intermediates path in provider: %s", depData.intermediatePath.String())
	}

	result := android.GroupFixturePreparers(
		java.PrepareForTestWithJavaDefaultModules,
		PrepareForTestWithSyspropBuildComponents,
		// TODO: Consider values files, although maybe in its own test
		// android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
		//	variables.ReleaseConfigValuesBasePaths = ...
		//})
		mockFS.AddToFixture(),
		android.FixtureWithRootAndroidBp(bp),
	).RunTest(t)

	return result
}

func TestOutputs(t *testing.T) {
	/*result := */ test(t, `
        device_config {
            name: "my_device_config",
            srcs: ["config.aconfig"],
        }
	`)

	// TODO: Make sure it exports a .srcjar, which is used by java libraries
	// TODO: Make sure it exports an intermediates file
	// TODO: Make sure the intermediates file is propagated to the Android.mk file
}
+43 −0
Original line number Diff line number Diff line
// Copyright 2018 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 device_config

import (
	"testing"

	"android/soong/android"
)

func TestDeviceConfigValueSet(t *testing.T) {
	bp := `
				device_config_values {
					name: "one",
					srcs: [ "blah.aconfig_values" ],
					namespace: "foo.namespace"
				}

				device_config_value_set {
					name: "module_name",
          values: [ "one" ],
				}
			`
	result := runTest(t, android.FixtureExpectsNoErrors, bp)

	module := result.ModuleForTests("module_name", "").Module().(*ValueSetModule)

	// Check that the provider has the right contents
	depData := result.ModuleProvider(module, valueSetProviderKey).(valueSetProviderData)
	android.AssertStringEquals(t, "AvailableNamespaces", "blah.aconfig_values", depData.AvailableNamespaces["foo.namespace"][0].String())
}
Loading