Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 1 | package sh |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 4 | "io/ioutil" |
| 5 | "os" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 6 | "reflect" |
| 7 | "testing" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 8 | |
| 9 | "android/soong/android" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 12 | var buildDir string |
| 13 | |
| 14 | func setUp() { |
| 15 | var err error |
| 16 | buildDir, err = ioutil.TempDir("", "soong_sh_test") |
| 17 | if err != nil { |
| 18 | panic(err) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func tearDown() { |
| 23 | os.RemoveAll(buildDir) |
| 24 | } |
| 25 | |
| 26 | func TestMain(m *testing.M) { |
| 27 | run := func() int { |
| 28 | setUp() |
| 29 | defer tearDown() |
| 30 | |
| 31 | return m.Run() |
| 32 | } |
| 33 | |
| 34 | os.Exit(run()) |
| 35 | } |
| 36 | |
| 37 | func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 38 | fs := map[string][]byte{ |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 39 | "test.sh": nil, |
| 40 | "testdata/data1": nil, |
| 41 | "testdata/sub/data2": nil, |
| 42 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 43 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 44 | config := android.TestArchConfig(buildDir, nil, bp, fs) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 45 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 46 | ctx := android.NewTestArchContext() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 47 | ctx.RegisterModuleType("sh_test", ShTestFactory) |
| 48 | ctx.RegisterModuleType("sh_test_host", ShTestHostFactory) |
| 49 | ctx.Register(config) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 50 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 51 | android.FailIfErrored(t, errs) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 52 | _, errs = ctx.PrepareBuildActions(config) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 53 | android.FailIfErrored(t, errs) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 54 | |
| 55 | return ctx, config |
| 56 | } |
| 57 | |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame^] | 58 | func TestShTestSubDir(t *testing.T) { |
| 59 | ctx, config := testShBinary(t, ` |
| 60 | sh_test { |
| 61 | name: "foo", |
| 62 | src: "test.sh", |
| 63 | sub_dir: "foo_test" |
| 64 | } |
| 65 | `) |
| 66 | |
| 67 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
| 68 | |
| 69 | entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] |
| 70 | |
| 71 | expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo_test" |
| 72 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
| 73 | if expectedPath != actualPath { |
| 74 | t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestShTest(t *testing.T) { |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 79 | ctx, config := testShBinary(t, ` |
| 80 | sh_test { |
| 81 | name: "foo", |
| 82 | src: "test.sh", |
| 83 | filename: "test.sh", |
| 84 | data: [ |
| 85 | "testdata/data1", |
| 86 | "testdata/sub/data2", |
| 87 | ], |
| 88 | } |
| 89 | `) |
| 90 | |
| 91 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
| 92 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 93 | entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame^] | 94 | |
| 95 | expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo" |
| 96 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
| 97 | if expectedPath != actualPath { |
| 98 | t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) |
| 99 | } |
| 100 | |
| 101 | expectedData := []string{":testdata/data1", ":testdata/sub/data2"} |
| 102 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 103 | if !reflect.DeepEqual(expectedData, actualData) { |
| 104 | t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 107 | |
| 108 | func TestShTestHost(t *testing.T) { |
| 109 | ctx, _ := testShBinary(t, ` |
| 110 | sh_test_host { |
| 111 | name: "foo", |
| 112 | src: "test.sh", |
| 113 | filename: "test.sh", |
| 114 | data: [ |
| 115 | "testdata/data1", |
| 116 | "testdata/sub/data2", |
| 117 | ], |
| 118 | } |
| 119 | `) |
| 120 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 121 | buildOS := android.BuildOs.String() |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 122 | mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest) |
| 123 | if !mod.Host() { |
| 124 | t.Errorf("host bit is not set for a sh_test_host module.") |
| 125 | } |
| 126 | } |