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
15package android
16
17import (
18	"testing"
19)
20
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")
35	preparer4 := OptionalFixturePreparer(appendToList("preparer4"))
36	nilPreparer := OptionalFixturePreparer(nil)
37
38	preparer1Then2 := GroupFixturePreparers(preparer1, preparer2, nilPreparer)
39
40	preparer2Then1 := GroupFixturePreparers(preparer2, preparer1)
41
42	group := GroupFixturePreparers(preparer1, preparer2, preparer1, preparer1Then2)
43
44	extension := GroupFixturePreparers(group, preparer4, preparer2)
45
46	GroupFixturePreparers(extension, preparer1, preparer2, preparer2Then1, preparer3).Fixture(t)
47
48	AssertDeepEquals(t, "preparers called in wrong order",
49		[]string{"preparer1", "preparer2", "preparer4", "preparer3"}, list)
50}
51
52func TestFixtureValidateMockFS(t *testing.T) {
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() {
55			FixtureAddFile("/abs/path/Android.bp", nil).Fixture(t)
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() {
60			FixtureAddFile("path/with/../in/it/Android.bp", nil).Fixture(t)
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() {
65			FixtureAddFile("out/Android.bp", nil).Fixture(t)
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() {
70			FixtureMergeMockFs(MockFS{
71				"out/Android.bp": nil,
72			}).Fixture(t)
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() {
77			FixtureModifyMockFS(func(fs MockFS) {
78				fs["out/Android.bp"] = nil
79			}).Fixture(t)
80		})
81	})
82}
83