blob: 681a034276cd44decf7f3ba9aff4da5ff35728aa [file] [log] [blame]
Paul Duffin35816122021-02-24 01:49:52 +00001// 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
15package android
16
Paul Duffin80f4cea2021-03-16 14:08:00 +000017import (
18 "testing"
19)
Paul Duffin35816122021-02-24 01:49:52 +000020
21// Make sure that FixturePreparer instances are only called once per fixture and in the order in
22// which they were added.
23func 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 Duffin50deaae2021-03-16 17:46:12 +000035 preparer4 := OptionalFixturePreparer(appendToList("preparer4"))
36 nilPreparer := OptionalFixturePreparer(nil)
Paul Duffin35816122021-02-24 01:49:52 +000037
Paul Duffin50deaae2021-03-16 17:46:12 +000038 preparer1Then2 := GroupFixturePreparers(preparer1, preparer2, nilPreparer)
Paul Duffin35816122021-02-24 01:49:52 +000039
Paul Duffina560d5a2021-02-28 01:38:51 +000040 preparer2Then1 := GroupFixturePreparers(preparer2, preparer1)
Paul Duffin35816122021-02-24 01:49:52 +000041
Paul Duffinff2aa692021-03-19 18:20:59 +000042 group := GroupFixturePreparers(preparer1, preparer2, preparer1, preparer1Then2)
Paul Duffin35816122021-02-24 01:49:52 +000043
Paul Duffinff2aa692021-03-19 18:20:59 +000044 extension := group.Extend(preparer4, preparer2)
Paul Duffin35816122021-02-24 01:49:52 +000045
46 extension.Fixture(t, preparer1, preparer2, preparer2Then1, preparer3)
47
Paul Duffin3d0ddff2021-03-12 12:20:59 +000048 AssertDeepEquals(t, "preparers called in wrong order",
Paul Duffin35816122021-02-24 01:49:52 +000049 []string{"preparer1", "preparer2", "preparer4", "preparer3"}, list)
50}
Paul Duffin80f4cea2021-03-16 14:08:00 +000051
52func TestFixtureValidateMockFS(t *testing.T) {
53 buildDir := "<unused>"
54 factory := NewFixtureFactory(&buildDir)
55
56 t.Run("absolute path", func(t *testing.T) {
57 AssertPanicMessageContains(t, "source path validation failed", "Path is outside directory: /abs/path/Android.bp", func() {
58 factory.Fixture(t, FixtureAddFile("/abs/path/Android.bp", nil))
59 })
60 })
61 t.Run("not canonical", func(t *testing.T) {
62 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() {
63 factory.Fixture(t, FixtureAddFile("path/with/../in/it/Android.bp", nil))
64 })
65 })
66 t.Run("FixtureAddFile", func(t *testing.T) {
67 AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
68 factory.Fixture(t, FixtureAddFile("out/Android.bp", nil))
69 })
70 })
71 t.Run("FixtureMergeMockFs", func(t *testing.T) {
72 AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
73 factory.Fixture(t, FixtureMergeMockFs(MockFS{
74 "out/Android.bp": nil,
75 }))
76 })
77 })
78 t.Run("FixtureModifyMockFS", func(t *testing.T) {
79 AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
80 factory.Fixture(t, FixtureModifyMockFS(func(fs MockFS) {
81 fs["out/Android.bp"] = nil
82 }))
83 })
84 })
85}