replace_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transform
  14. import (
  15. "reflect"
  16. "testing"
  17. "gotest.tools/v3/assert"
  18. )
  19. func TestReplace(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. in string
  23. want string
  24. }{
  25. {
  26. name: "simple",
  27. in: `services:
  28. test:
  29. extends:
  30. file: foo.yaml
  31. service: foo
  32. `,
  33. want: `services:
  34. test:
  35. extends:
  36. file: REPLACED
  37. service: foo
  38. `,
  39. },
  40. {
  41. name: "last line",
  42. in: `services:
  43. test:
  44. extends:
  45. service: foo
  46. file: foo.yaml
  47. `,
  48. want: `services:
  49. test:
  50. extends:
  51. service: foo
  52. file: REPLACED
  53. `,
  54. },
  55. {
  56. name: "last line no CR",
  57. in: `services:
  58. test:
  59. extends:
  60. service: foo
  61. file: foo.yaml`,
  62. want: `services:
  63. test:
  64. extends:
  65. service: foo
  66. file: REPLACED`,
  67. },
  68. }
  69. for _, tt := range tests {
  70. t.Run(tt.name, func(t *testing.T) {
  71. got, err := ReplaceExtendsFile([]byte(tt.in), "test", "REPLACED")
  72. assert.NilError(t, err)
  73. if !reflect.DeepEqual(got, []byte(tt.want)) {
  74. t.Errorf("ReplaceExtendsFile() got = %v, want %v", got, tt.want)
  75. }
  76. })
  77. }
  78. }