blob: 5b810e0b7790867ba49bcd1b098a95deacd508a0 [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 Duffin79abe572021-03-29 02:16:14 +010044 extension := GroupFixturePreparers(group, preparer4, preparer2)
Paul Duffin35816122021-02-24 01:49:52 +000045
Paul Duffin34a7fff2021-03-30 22:11:24 +010046 GroupFixturePreparers(extension, preparer1, preparer2, preparer2Then1, preparer3).Fixture(t)
Paul Duffin35816122021-02-24 01:49:52 +000047
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) {
Paul Duffin80f4cea2021-03-16 14:08:00 +000053 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 Duffin34a7fff2021-03-30 22:11:24 +010055 FixtureAddFile("/abs/path/Android.bp", nil).Fixture(t)
Paul Duffin80f4cea2021-03-16 14:08:00 +000056 })
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 Duffin34a7fff2021-03-30 22:11:24 +010060 FixtureAddFile("path/with/../in/it/Android.bp", nil).Fixture(t)
Paul Duffin80f4cea2021-03-16 14:08:00 +000061 })
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 Duffin34a7fff2021-03-30 22:11:24 +010065 FixtureAddFile("out/Android.bp", nil).Fixture(t)
Paul Duffin80f4cea2021-03-16 14:08:00 +000066 })
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 Duffin34a7fff2021-03-30 22:11:24 +010070 FixtureMergeMockFs(MockFS{
Paul Duffin80f4cea2021-03-16 14:08:00 +000071 "out/Android.bp": nil,
Paul Duffin34a7fff2021-03-30 22:11:24 +010072 }).Fixture(t)
Paul Duffin80f4cea2021-03-16 14:08:00 +000073 })
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 Duffin34a7fff2021-03-30 22:11:24 +010077 FixtureModifyMockFS(func(fs MockFS) {
Paul Duffin80f4cea2021-03-16 14:08:00 +000078 fs["out/Android.bp"] = nil
Paul Duffin34a7fff2021-03-30 22:11:24 +010079 }).Fixture(t)
Paul Duffin80f4cea2021-03-16 14:08:00 +000080 })
81 })
82}