blob: 209bee68073bf6eee8f004619c632fd6e1a53d48 [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
42 buildDir := "build"
43 factory := NewFixtureFactory(&buildDir, preparer1, preparer2, preparer1, preparer1Then2)
44
45 extension := factory.Extend(preparer4, preparer2)
46
47 extension.Fixture(t, preparer1, preparer2, preparer2Then1, preparer3)
48
Paul Duffin3d0ddff2021-03-12 12:20:59 +000049 AssertDeepEquals(t, "preparers called in wrong order",
Paul Duffin35816122021-02-24 01:49:52 +000050 []string{"preparer1", "preparer2", "preparer4", "preparer3"}, list)
51}
Paul Duffin80f4cea2021-03-16 14:08:00 +000052
53func TestFixtureValidateMockFS(t *testing.T) {
54 buildDir := "<unused>"
55 factory := NewFixtureFactory(&buildDir)
56
57 t.Run("absolute path", func(t *testing.T) {
58 AssertPanicMessageContains(t, "source path validation failed", "Path is outside directory: /abs/path/Android.bp", func() {
59 factory.Fixture(t, FixtureAddFile("/abs/path/Android.bp", nil))
60 })
61 })
62 t.Run("not canonical", func(t *testing.T) {
63 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() {
64 factory.Fixture(t, FixtureAddFile("path/with/../in/it/Android.bp", nil))
65 })
66 })
67 t.Run("FixtureAddFile", func(t *testing.T) {
68 AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
69 factory.Fixture(t, FixtureAddFile("out/Android.bp", nil))
70 })
71 })
72 t.Run("FixtureMergeMockFs", func(t *testing.T) {
73 AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
74 factory.Fixture(t, FixtureMergeMockFs(MockFS{
75 "out/Android.bp": nil,
76 }))
77 })
78 })
79 t.Run("FixtureModifyMockFS", func(t *testing.T) {
80 AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
81 factory.Fixture(t, FixtureModifyMockFS(func(fs MockFS) {
82 fs["out/Android.bp"] = nil
83 }))
84 })
85 })
86}