github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/cmd/swagger/commands/diff/spec_analyser_test.go (about) 1 package diff 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/go-openapi/loads" 11 "github.com/go-swagger/go-swagger/cmd/swagger/commands/internal/cmdtest" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func fixturePath(file string, parts ...string) string { 16 return filepath.Join("..", "..", "..", "..", "fixtures", "diff", strings.Join(append([]string{file}, parts...), "")) 17 } 18 19 type testCaseData struct { 20 name string 21 oldSpec string 22 newSpec string 23 expectedLines io.Reader 24 expectedFile string 25 } 26 27 func fixturePart(file string) string { 28 base := filepath.Base(file) 29 parts := strings.Split(base, ".diff.txt") 30 return parts[0] 31 } 32 33 // TestDiffForVariousCombinations - computes the diffs for a number 34 // of scenarios and compares the computed diff with expected diffs 35 func TestDiffForVariousCombinations(t *testing.T) { 36 pattern := fixturePath("*.diff.txt") 37 allTests, err := filepath.Glob(pattern) 38 require.NoError(t, err) 39 require.True(t, len(allTests) > 0) 40 41 // To filter cases for debugging poke an individual case here eg "path", "enum" etc 42 // see the test cases in fixtures/diff 43 // Don't forget to remove it once you're done. 44 // (There's a test at the end to check all cases were run) 45 matches := allTests 46 // matches := []string{"enum"} 47 48 testCases := makeTestCases(t, matches) 49 50 for i, tc := range testCases { 51 tc := tc 52 t.Run(tc.name, func(t *testing.T) { 53 diffs, err := getDiffs(tc.oldSpec, tc.newSpec) 54 require.NoError(t, err) 55 56 out, err, warn := diffs.ReportAllDiffs(false) 57 require.NoError(t, err) 58 59 if !cmdtest.AssertReadersContent(t, true, tc.expectedLines, out) { 60 t.Logf("unexpected content for fixture %q[%d] (file: %s)", tc.name, i, tc.expectedFile) 61 } 62 63 if diffs.BreakingChangeCount() > 0 { 64 require.Error(t, warn) 65 } 66 }) 67 } 68 69 require.Equalf(t, len(allTests), len(matches), "All test cases were not run. Remove filter") 70 } 71 72 func getDiffs(oldSpecPath, newSpecPath string) (SpecDifferences, error) { 73 swaggerDoc1 := oldSpecPath 74 specDoc1, err := loads.Spec(swaggerDoc1) 75 76 if err != nil { 77 return nil, err 78 } 79 80 swaggerDoc2 := newSpecPath 81 specDoc2, err := loads.Spec(swaggerDoc2) 82 if err != nil { 83 return nil, err 84 } 85 86 return Compare(specDoc1.Spec(), specDoc2.Spec()) 87 } 88 89 func makeTestCases(t testing.TB, matches []string) []testCaseData { 90 testCases := make([]testCaseData, 0, len(matches)) 91 for _, eachFile := range matches { 92 namePart := fixturePart(eachFile) 93 if _, err := os.Stat(fixturePath(namePart, ".v1.json")); err == nil { 94 testCases = append( 95 testCases, testCaseData{ 96 name: namePart, 97 oldSpec: fixturePath(namePart, ".v1.json"), 98 newSpec: fixturePath(namePart, ".v2.json"), 99 expectedLines: linesInFile(t, fixturePath(namePart, ".diff.txt")), 100 }) 101 } 102 if _, err := os.Stat(fixturePath(namePart, ".v1.yml")); err == nil { 103 testCases = append( 104 testCases, testCaseData{ 105 name: namePart, 106 oldSpec: fixturePath(namePart, ".v1.yml"), 107 newSpec: fixturePath(namePart, ".v2.yml"), 108 expectedLines: linesInFile(t, fixturePath(namePart, ".diff.txt")), 109 }) 110 } 111 } 112 return testCases 113 } 114 115 func linesInFile(t testing.TB, fileName string) io.ReadCloser { 116 file, err := os.Open(fileName) 117 require.NoError(t, err) 118 return file 119 }