Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 17 | import ( |
| 18 | "testing" |
| 19 | ) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 20 | |
| 21 | // Make sure that FixturePreparer instances are only called once per fixture and in the order in |
| 22 | // which they were added. |
| 23 | func TestFixtureDedup(t *testing.T) { |
| 24 | list := []string{} |
| 25 | |
| 26 | appendToList := func(s string) FixturePreparer { |
| 27 | return FixtureModifyConfig(func(_ Config) { |
| 28 | list = append(list, s) |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | preparer1 := appendToList("preparer1") |
| 33 | preparer2 := appendToList("preparer2") |
| 34 | preparer3 := appendToList("preparer3") |
Paul Duffin | 50deaae | 2021-03-16 17:46:12 +0000 | [diff] [blame] | 35 | preparer4 := OptionalFixturePreparer(appendToList("preparer4")) |
| 36 | nilPreparer := OptionalFixturePreparer(nil) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 37 | |
Paul Duffin | 50deaae | 2021-03-16 17:46:12 +0000 | [diff] [blame] | 38 | preparer1Then2 := GroupFixturePreparers(preparer1, preparer2, nilPreparer) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 39 | |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 40 | preparer2Then1 := GroupFixturePreparers(preparer2, preparer1) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 41 | |
Paul Duffin | ff2aa69 | 2021-03-19 18:20:59 +0000 | [diff] [blame] | 42 | group := GroupFixturePreparers(preparer1, preparer2, preparer1, preparer1Then2) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 43 | |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 44 | extension := GroupFixturePreparers(group, preparer4, preparer2) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 45 | |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 46 | GroupFixturePreparers(extension, preparer1, preparer2, preparer2Then1, preparer3).Fixture(t) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 47 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame] | 48 | AssertDeepEquals(t, "preparers called in wrong order", |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 49 | []string{"preparer1", "preparer2", "preparer4", "preparer3"}, list) |
| 50 | } |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 51 | |
| 52 | func TestFixtureValidateMockFS(t *testing.T) { |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 53 | t.Run("absolute path", func(t *testing.T) { |
| 54 | AssertPanicMessageContains(t, "source path validation failed", "Path is outside directory: /abs/path/Android.bp", func() { |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 55 | FixtureAddFile("/abs/path/Android.bp", nil).Fixture(t) |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 56 | }) |
| 57 | }) |
| 58 | t.Run("not canonical", func(t *testing.T) { |
| 59 | AssertPanicMessageContains(t, "source path validation failed", `path "path/with/../in/it/Android.bp" is not a canonical path, use "path/in/it/Android.bp" instead`, func() { |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 60 | FixtureAddFile("path/with/../in/it/Android.bp", nil).Fixture(t) |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 61 | }) |
| 62 | }) |
| 63 | t.Run("FixtureAddFile", func(t *testing.T) { |
| 64 | AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() { |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 65 | FixtureAddFile("out/Android.bp", nil).Fixture(t) |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 66 | }) |
| 67 | }) |
| 68 | t.Run("FixtureMergeMockFs", func(t *testing.T) { |
| 69 | AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() { |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 70 | FixtureMergeMockFs(MockFS{ |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 71 | "out/Android.bp": nil, |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 72 | }).Fixture(t) |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 73 | }) |
| 74 | }) |
| 75 | t.Run("FixtureModifyMockFS", func(t *testing.T) { |
| 76 | AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() { |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 77 | FixtureModifyMockFS(func(fs MockFS) { |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 78 | fs["out/Android.bp"] = nil |
Paul Duffin | 34a7fff | 2021-03-30 22:11:24 +0100 | [diff] [blame] | 79 | }).Fixture(t) |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame] | 80 | }) |
| 81 | }) |
| 82 | } |