diff options
Diffstat (limited to 'sh/sh_binary_test.go')
-rw-r--r-- | sh/sh_binary_test.go | 125 |
1 files changed, 58 insertions, 67 deletions
diff --git a/sh/sh_binary_test.go b/sh/sh_binary_test.go index 232a28133..9e7e59462 100644 --- a/sh/sh_binary_test.go +++ b/sh/sh_binary_test.go @@ -1,66 +1,64 @@ package sh import ( - "io/ioutil" "os" "path/filepath" - "reflect" "testing" "android/soong/android" "android/soong/cc" ) -var buildDir string - -func setUp() { - var err error - buildDir, err = ioutil.TempDir("", "soong_sh_test") - if err != nil { - panic(err) - } -} - -func tearDown() { - os.RemoveAll(buildDir) -} - func TestMain(m *testing.M) { - run := func() int { - setUp() - defer tearDown() - - return m.Run() - } - - os.Exit(run()) + os.Exit(m.Run()) } -func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { - fs := map[string][]byte{ +var prepareForShTest = android.GroupFixturePreparers( + cc.PrepareForTestWithCcBuildComponents, + PrepareForTestWithShBuildComponents, + android.FixtureMergeMockFs(android.MockFS{ "test.sh": nil, "testdata/data1": nil, "testdata/sub/data2": nil, - } + }), +) - config := android.TestArchConfig(buildDir, nil, bp, fs) +// testShBinary runs tests using the prepareForShTest +// +// Do not add any new usages of this, instead use the prepareForShTest directly as it makes it much +// easier to customize the test behavior. +// +// If it is necessary to customize the behavior of an existing test that uses this then please first +// convert the test to using prepareForShTest first and then in a following change add the +// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify +// that it did not change the test behavior unexpectedly. +// +// deprecated +func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { + result := prepareForShTest.RunTestWithBp(t, bp) + + return result.TestContext, result.Config +} - ctx := android.NewTestArchContext() - ctx.RegisterModuleType("sh_test", ShTestFactory) - ctx.RegisterModuleType("sh_test_host", ShTestHostFactory) +func TestShTestSubDir(t *testing.T) { + ctx, config := testShBinary(t, ` + sh_test { + name: "foo", + src: "test.sh", + sub_dir: "foo_test" + } + `) - cc.RegisterRequiredBuildComponentsForTest(ctx) + mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) - ctx.Register(config) - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) + entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] - return ctx, config + expectedPath := "out/target/product/test_device/data/nativetest64/foo_test" + actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] + android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath) } -func TestShTestTestData(t *testing.T) { +func TestShTest(t *testing.T) { ctx, config := testShBinary(t, ` sh_test { name: "foo", @@ -75,12 +73,15 @@ func TestShTestTestData(t *testing.T) { mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) - entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] - expected := []string{":testdata/data1", ":testdata/sub/data2"} - actual := entries.EntryMap["LOCAL_TEST_DATA"] - if !reflect.DeepEqual(expected, actual) { - t.Errorf("Unexpected test data expected: %q, actual: %q", expected, actual) - } + entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] + + expectedPath := "out/target/product/test_device/data/nativetest64/foo" + actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] + android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath) + + expectedData := []string{":testdata/data1", ":testdata/sub/data2"} + actualData := entries.EntryMap["LOCAL_TEST_DATA"] + android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData) } func TestShTest_dataModules(t *testing.T) { @@ -123,22 +124,17 @@ func TestShTest_dataModules(t *testing.T) { libExt = ".dylib" } relocated := variant.Output("relocated/lib64/libbar" + libExt) - expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt) - if relocated.Input.String() != expectedInput { - t.Errorf("Unexpected relocation input, expected: %q, actual: %q", - expectedInput, relocated.Input.String()) - } + expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt + android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) mod := variant.Module().(*ShTest) - entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] + entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] expectedData := []string{ - filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"), - filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt), + filepath.Join("out/soong/.intermediates/bar", arch, ":bar"), + filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt), } actualData := entries.EntryMap["LOCAL_TEST_DATA"] - if !reflect.DeepEqual(expectedData, actualData) { - t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData) - } + android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData) } } @@ -193,21 +189,16 @@ func TestShTestHost_dataDeviceModules(t *testing.T) { variant := ctx.ModuleForTests("foo", buildOS+"_x86_64") relocated := variant.Output("relocated/lib64/libbar.so") - expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so") - if relocated.Input.String() != expectedInput { - t.Errorf("Unexpected relocation input, expected: %q, actual: %q", - expectedInput, relocated.Input.String()) - } + expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so" + android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) mod := variant.Module().(*ShTest) - entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] + entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] expectedData := []string{ - filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"), + "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar", // libbar has been relocated, and so has a variant that matches the host arch. - filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"), + "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so", } actualData := entries.EntryMap["LOCAL_TEST_DATA"] - if !reflect.DeepEqual(expectedData, actualData) { - t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData) - } + android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData) } |