build_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 compose
  14. import (
  15. "slices"
  16. "testing"
  17. "github.com/compose-spec/compose-go/v2/types"
  18. "gotest.tools/v3/assert"
  19. )
  20. func Test_dockerFilePath(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. ctxName string
  24. dockerfile string
  25. expected string
  26. }{
  27. {
  28. name: "empty dockerfile",
  29. ctxName: "/some/local/dir",
  30. dockerfile: "",
  31. expected: "",
  32. },
  33. {
  34. name: "local dir with relative dockerfile",
  35. ctxName: "/some/local/dir",
  36. dockerfile: "Dockerfile",
  37. expected: "/some/local/dir/Dockerfile",
  38. },
  39. {
  40. name: "local dir with absolute dockerfile",
  41. ctxName: "/some/local/dir",
  42. dockerfile: "/absolute/path/Dockerfile",
  43. expected: "/absolute/path/Dockerfile",
  44. },
  45. {
  46. name: "ssh URL preserves double slash",
  47. ctxName: "ssh://[email protected]:22/docker/welcome-to-docker.git",
  48. dockerfile: "Dockerfile",
  49. expected: "Dockerfile",
  50. },
  51. {
  52. name: "git:// URL returns dockerfile as-is",
  53. ctxName: "git://github.com/docker/compose.git",
  54. dockerfile: "Dockerfile",
  55. expected: "Dockerfile",
  56. },
  57. {
  58. name: "https git URL returns dockerfile as-is",
  59. ctxName: "https://github.com/docker/compose.git",
  60. dockerfile: "Dockerfile",
  61. expected: "Dockerfile",
  62. },
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. result := dockerFilePath(tt.ctxName, tt.dockerfile)
  67. assert.Equal(t, tt.expected, result)
  68. })
  69. }
  70. }
  71. func Test_addBuildDependencies(t *testing.T) {
  72. project := &types.Project{Services: types.Services{
  73. "test": types.ServiceConfig{
  74. Build: &types.BuildConfig{
  75. AdditionalContexts: map[string]string{
  76. "foo": "service:foo",
  77. "bar": "service:bar",
  78. },
  79. },
  80. },
  81. "foo": types.ServiceConfig{
  82. Build: &types.BuildConfig{
  83. AdditionalContexts: map[string]string{
  84. "zot": "service:zot",
  85. },
  86. },
  87. },
  88. "bar": types.ServiceConfig{
  89. Build: &types.BuildConfig{},
  90. },
  91. "zot": types.ServiceConfig{
  92. Build: &types.BuildConfig{},
  93. },
  94. }}
  95. services := addBuildDependencies([]string{"test"}, project)
  96. expected := []string{"test", "foo", "bar", "zot"}
  97. slices.Sort(services)
  98. slices.Sort(expected)
  99. assert.DeepEqual(t, services, expected)
  100. }