dockerignore_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 watch
  14. import (
  15. "testing"
  16. )
  17. func TestNewDockerPatternMatcher(t *testing.T) {
  18. tests := []struct {
  19. name string
  20. repoRoot string
  21. patterns []string
  22. expectedErr bool
  23. expectedRoot string
  24. expectedPat []string
  25. }{
  26. {
  27. name: "Basic patterns without wildcard",
  28. repoRoot: "/repo",
  29. patterns: []string{"dir1/", "file.txt"},
  30. expectedErr: false,
  31. expectedRoot: "/repo",
  32. expectedPat: []string{"/repo/dir1", "/repo/file.txt"},
  33. },
  34. {
  35. name: "Patterns with exclusion",
  36. repoRoot: "/repo",
  37. patterns: []string{"dir1/", "!file.txt"},
  38. expectedErr: false,
  39. expectedRoot: "/repo",
  40. expectedPat: []string{"/repo/dir1", "!/repo/file.txt"},
  41. },
  42. {
  43. name: "Wildcard with exclusion",
  44. repoRoot: "/repo",
  45. patterns: []string{"*", "!file.txt"},
  46. expectedErr: false,
  47. expectedRoot: "/repo",
  48. expectedPat: []string{"!/repo/file.txt"},
  49. },
  50. {
  51. name: "No patterns",
  52. repoRoot: "/repo",
  53. patterns: []string{},
  54. expectedErr: false,
  55. expectedRoot: "/repo",
  56. expectedPat: nil,
  57. },
  58. {
  59. name: "Only exclusion pattern",
  60. repoRoot: "/repo",
  61. patterns: []string{"!file.txt"},
  62. expectedErr: false,
  63. expectedRoot: "/repo",
  64. expectedPat: []string{"!/repo/file.txt"},
  65. },
  66. }
  67. for _, tt := range tests {
  68. t.Run(tt.name, func(t *testing.T) {
  69. // Call the function with the test data
  70. matcher, err := NewDockerPatternMatcher(tt.repoRoot, tt.patterns)
  71. // Check if we expect an error
  72. if (err != nil) != tt.expectedErr {
  73. t.Fatalf("expected error: %v, got: %v", tt.expectedErr, err)
  74. }
  75. // If no error is expected, check the output
  76. if !tt.expectedErr {
  77. if matcher.repoRoot != tt.expectedRoot {
  78. t.Errorf("expected root: %v, got: %v", tt.expectedRoot, matcher.repoRoot)
  79. }
  80. // Compare patterns
  81. actualPatterns := matcher.matcher.Patterns()
  82. if len(actualPatterns) != len(tt.expectedPat) {
  83. t.Errorf("expected patterns length: %v, got: %v", len(tt.expectedPat), len(actualPatterns))
  84. }
  85. for i, expectedPat := range tt.expectedPat {
  86. actualPatternStr := actualPatterns[i].String()
  87. if actualPatterns[i].Exclusion() {
  88. actualPatternStr = "!" + actualPatternStr
  89. }
  90. if actualPatternStr != expectedPat {
  91. t.Errorf("expected pattern: %v, got: %v", expectedPat, actualPatterns[i])
  92. }
  93. }
  94. }
  95. })
  96. }
  97. }