fixture_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package android
  15. import (
  16. "testing"
  17. )
  18. // Make sure that FixturePreparer instances are only called once per fixture and in the order in
  19. // which they were added.
  20. func TestFixtureDedup(t *testing.T) {
  21. list := []string{}
  22. appendToList := func(s string) FixturePreparer {
  23. return FixtureModifyConfig(func(_ Config) {
  24. list = append(list, s)
  25. })
  26. }
  27. preparer1 := appendToList("preparer1")
  28. preparer2 := appendToList("preparer2")
  29. preparer3 := appendToList("preparer3")
  30. preparer4 := OptionalFixturePreparer(appendToList("preparer4"))
  31. nilPreparer := OptionalFixturePreparer(nil)
  32. preparer1Then2 := GroupFixturePreparers(preparer1, preparer2, nilPreparer)
  33. preparer2Then1 := GroupFixturePreparers(preparer2, preparer1)
  34. group := GroupFixturePreparers(preparer1, preparer2, preparer1, preparer1Then2)
  35. extension := GroupFixturePreparers(group, preparer4, preparer2)
  36. GroupFixturePreparers(extension, preparer1, preparer2, preparer2Then1, preparer3).Fixture(t)
  37. AssertDeepEquals(t, "preparers called in wrong order",
  38. []string{"preparer1", "preparer2", "preparer4", "preparer3"}, list)
  39. }
  40. func TestFixtureValidateMockFS(t *testing.T) {
  41. t.Run("absolute path", func(t *testing.T) {
  42. AssertPanicMessageContains(t, "source path validation failed", "Path is outside directory: /abs/path/Android.bp", func() {
  43. FixtureAddFile("/abs/path/Android.bp", nil).Fixture(t)
  44. })
  45. })
  46. t.Run("not canonical", func(t *testing.T) {
  47. 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() {
  48. FixtureAddFile("path/with/../in/it/Android.bp", nil).Fixture(t)
  49. })
  50. })
  51. t.Run("FixtureAddFile", func(t *testing.T) {
  52. AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
  53. FixtureAddFile("out/Android.bp", nil).Fixture(t)
  54. })
  55. })
  56. t.Run("FixtureMergeMockFs", func(t *testing.T) {
  57. AssertPanicMessageContains(t, "source path validation failed", `cannot add output path "out/Android.bp" to the mock file system`, func() {
  58. FixtureMergeMockFs(MockFS{
  59. "out/Android.bp": nil,
  60. }).Fixture(t)
  61. })
  62. })
  63. t.Run("FixtureModifyMockFS", 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. FixtureModifyMockFS(func(fs MockFS) {
  66. fs["out/Android.bp"] = nil
  67. }).Fixture(t)
  68. })
  69. })
  70. }