github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/content_analyzer_test.go (about) 1 // Copyright 2024 Google LLC 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 15 package yamlfmt_test 16 17 import ( 18 "path/filepath" 19 "testing" 20 21 "github.com/google/yamlfmt" 22 "github.com/google/yamlfmt/internal/collections" 23 "github.com/google/yamlfmt/internal/tempfile" 24 ) 25 26 const testdataBase = "testdata/content_analyzer" 27 28 func TestBasicContentAnalyzer(t *testing.T) { 29 testCases := []struct { 30 name string 31 testdataDir string 32 excludePatterns []string 33 expectedPaths collections.Set[string] 34 expectedExcluded collections.Set[string] 35 }{ 36 { 37 name: "has ignore metadata", 38 testdataDir: "has_ignore", 39 excludePatterns: []string{}, 40 expectedPaths: collections.Set[string]{ 41 "y.yaml": {}, 42 }, 43 expectedExcluded: collections.Set[string]{ 44 "x.yaml": {}, 45 }, 46 }, 47 { 48 name: "matches regex pattern", 49 testdataDir: "regex_ignore", 50 excludePatterns: []string{ 51 ".*generated by.*", 52 }, 53 expectedPaths: collections.Set[string]{ 54 "y.yaml": {}, 55 }, 56 expectedExcluded: collections.Set[string]{ 57 "x.yaml": {}, 58 }, 59 }, 60 } 61 62 for _, tc := range testCases { 63 tc := tc 64 t.Run(tc.name, func(t *testing.T) { 65 t.Parallel() 66 tempPath := t.TempDir() 67 testdataDir := filepath.Join(testdataBase, tc.testdataDir) 68 paths, err := tempfile.ReplicateDirectory(testdataDir, tempPath) 69 if err != nil { 70 t.Fatalf("could not replicate testdata directory %s: %v", tc.testdataDir, err) 71 } 72 err = paths.CreateAll() 73 if err != nil { 74 t.Fatalf("could not create full test directory: %v", err) 75 } 76 contentAnalyzer, err := yamlfmt.NewBasicContentAnalyzer(tc.excludePatterns) 77 if err != nil { 78 t.Fatalf("could not create content analyzer: %v", err) 79 } 80 collector := &yamlfmt.FilepathCollector{ 81 Include: []string{tempPath}, 82 Exclude: []string{}, 83 Extensions: []string{"yaml", "yml"}, 84 } 85 collectedPaths, err := collector.CollectPaths() 86 if err != nil { 87 t.Fatalf("CollectPaths failed: %v", err) 88 } 89 resultPaths, excludedPaths, err := contentAnalyzer.ExcludePathsByContent(collectedPaths) 90 if err != nil { 91 t.Fatalf("expected content analyzer to work, got error: %v", err) 92 } 93 resultPathsTrimmed, err := pathsTempdirTrimmed(resultPaths, tempPath) 94 if err != nil { 95 t.Fatalf("expected trimming tempdir from result not to have error: %v", err) 96 } 97 if !tc.expectedPaths.Equals(collections.SliceToSet(resultPathsTrimmed)) { 98 t.Fatalf("expected files:\n%v\ngot:\n%v", tc.expectedPaths, resultPaths) 99 } 100 excludePathsTrimmed, err := pathsTempdirTrimmed(excludedPaths, tempPath) 101 if err != nil { 102 t.Fatalf("expected trimming tempdir from excluded not to have error: %v", err) 103 } 104 if !tc.expectedExcluded.Equals(collections.SliceToSet(excludePathsTrimmed)) { 105 t.Fatalf("expected exclusions:\n%v\ngot:\n%v", tc.expectedExcluded, excludedPaths) 106 } 107 }) 108 } 109 } 110 111 func TestBadNewContentAnalyzer(t *testing.T) { 112 // Illegal because no closing ) 113 badPattern := "%^3412098(]fj" 114 _, err := yamlfmt.NewBasicContentAnalyzer([]string{badPattern}) 115 if err == nil { 116 t.Fatalf("expected there to be an error") 117 } 118 } 119 120 func pathsTempdirTrimmed(paths []string, tempDir string) ([]string, error) { 121 trimmedPaths := []string{} 122 for _, path := range paths { 123 trimmedPath, err := filepath.Rel(tempDir, path) 124 if err != nil { 125 return nil, err 126 } 127 trimmedPaths = append(trimmedPaths, trimmedPath) 128 } 129 return trimmedPaths, nil 130 }