starlark_import_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2023 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 starlark_import
  15. import (
  16. "strings"
  17. "testing"
  18. "go.starlark.net/starlark"
  19. )
  20. func TestBasic(t *testing.T) {
  21. globals, _, err := runStarlarkFileWithFilesystem("a.bzl", "", map[string]string{
  22. "a.bzl": `
  23. my_string = "hello, world!"
  24. `})
  25. if err != nil {
  26. t.Error(err)
  27. return
  28. }
  29. if globals["my_string"].(starlark.String) != "hello, world!" {
  30. t.Errorf("Expected %q, got %q", "hello, world!", globals["my_string"].String())
  31. }
  32. }
  33. func TestLoad(t *testing.T) {
  34. globals, _, err := runStarlarkFileWithFilesystem("a.bzl", "", map[string]string{
  35. "a.bzl": `
  36. load("//b.bzl", _b_string = "my_string")
  37. my_string = "hello, " + _b_string
  38. `,
  39. "b.bzl": `
  40. my_string = "world!"
  41. `})
  42. if err != nil {
  43. t.Error(err)
  44. return
  45. }
  46. if globals["my_string"].(starlark.String) != "hello, world!" {
  47. t.Errorf("Expected %q, got %q", "hello, world!", globals["my_string"].String())
  48. }
  49. }
  50. func TestLoadRelative(t *testing.T) {
  51. globals, ninjaDeps, err := runStarlarkFileWithFilesystem("a.bzl", "", map[string]string{
  52. "a.bzl": `
  53. load(":b.bzl", _b_string = "my_string")
  54. load("//foo/c.bzl", _c_string = "my_string")
  55. my_string = "hello, " + _b_string
  56. c_string = _c_string
  57. `,
  58. "b.bzl": `
  59. my_string = "world!"
  60. `,
  61. "foo/c.bzl": `
  62. load(":d.bzl", _d_string = "my_string")
  63. my_string = "hello, " + _d_string
  64. `,
  65. "foo/d.bzl": `
  66. my_string = "world!"
  67. `})
  68. if err != nil {
  69. t.Error(err)
  70. return
  71. }
  72. if globals["my_string"].(starlark.String) != "hello, world!" {
  73. t.Errorf("Expected %q, got %q", "hello, world!", globals["my_string"].String())
  74. }
  75. expectedNinjaDeps := []string{
  76. "a.bzl",
  77. "b.bzl",
  78. "foo/c.bzl",
  79. "foo/d.bzl",
  80. }
  81. if !slicesEqual(ninjaDeps, expectedNinjaDeps) {
  82. t.Errorf("Expected %v ninja deps, got %v", expectedNinjaDeps, ninjaDeps)
  83. }
  84. }
  85. func TestLoadCycle(t *testing.T) {
  86. _, _, err := runStarlarkFileWithFilesystem("a.bzl", "", map[string]string{
  87. "a.bzl": `
  88. load(":b.bzl", _b_string = "my_string")
  89. my_string = "hello, " + _b_string
  90. `,
  91. "b.bzl": `
  92. load(":a.bzl", _a_string = "my_string")
  93. my_string = "hello, " + _a_string
  94. `})
  95. if err == nil || !strings.Contains(err.Error(), "cycle in load graph") {
  96. t.Errorf("Expected cycle in load graph, got: %v", err)
  97. return
  98. }
  99. }
  100. func slicesEqual[T comparable](a []T, b []T) bool {
  101. if len(a) != len(b) {
  102. return false
  103. }
  104. for i := range a {
  105. if a[i] != b[i] {
  106. return false
  107. }
  108. }
  109. return true
  110. }