github.com/go-swagger/go-swagger@v0.31.0/generator/support_test.go (about) 1 package generator 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 type relativePathTest struct { 14 childpath string 15 parentpath string 16 ok bool 17 path string 18 } 19 20 func prefixAndFetchRelativePathFixtures() []relativePathTest { 21 return []relativePathTest{ 22 // Positive 23 {"/", "/", true, "."}, 24 {"/User/Gopher", "/", true, "User/Gopher"}, 25 {"/User/Gopher/Go", "/User/Gopher/Go", true, "."}, 26 {"/User/../User/Gopher", "/", true, "User/Gopher"}, 27 // Negative cases 28 {"/", "/var", false, ""}, 29 {"/User/Gopher", "/User/SomethingElse", false, ""}, 30 {"/var", "/etc", false, ""}, 31 {"/mnt/dev3", "/mnt/dev3/dir", false, ""}, 32 } 33 } 34 35 type baseImportTest struct { 36 title string 37 path []string 38 gopath string 39 targetpath string 40 symlinksrc string 41 symlinkdest string // symlink is the last dir in targetpath 42 expectedpath string 43 } 44 45 func baseImportTestFixtures(tempdir string) []baseImportTest { 46 return []baseImportTest{ 47 { 48 title: "No sym link. Positive Test Case", 49 path: []string{tempdir + "/root/go/src/github.com/go-swagger"}, 50 gopath: tempdir + "/root/go/", 51 targetpath: tempdir + "/root/go/src/github.com/go-swagger", 52 symlinksrc: "", 53 symlinkdest: "", 54 expectedpath: "github.com/go-swagger", 55 }, 56 { 57 title: "Symlink points inside GOPATH", 58 path: []string{tempdir + "/root/go/src/github.com/go-swagger"}, 59 gopath: tempdir + "/root/go/", 60 targetpath: tempdir + "/root/symlink", 61 symlinksrc: tempdir + "/root/symlink", 62 symlinkdest: tempdir + "/root/go/src/", 63 expectedpath: ".", 64 }, 65 { 66 title: "Symlink points inside GOPATH (2)", 67 path: []string{tempdir + "/root/go/src/github.com/go-swagger"}, 68 gopath: tempdir + "/root/go/", 69 targetpath: tempdir + "/root/symlink", 70 symlinksrc: tempdir + "/root/symlink", 71 symlinkdest: tempdir + "/root/go/src/github.com", 72 expectedpath: "github.com", 73 }, 74 { 75 title: "Symlink point outside GOPATH : Targets Case 1: in baseImport implementation", 76 path: []string{tempdir + "/root/go/src/github.com/go-swagger", tempdir + "/root/gopher/go/"}, 77 gopath: tempdir + "/root/go/", 78 targetpath: tempdir + "/root/go/src/github.com/gopher", 79 symlinksrc: tempdir + "/root/go/src/github.com/gopher", 80 symlinkdest: tempdir + "/root/gopher/go", 81 expectedpath: "github.com/gopher", 82 }, 83 } 84 } 85 86 func TestCheckPrefixFetchRelPath(t *testing.T) { 87 for _, item := range prefixAndFetchRelativePathFixtures() { 88 actualok, actualpath := checkPrefixAndFetchRelativePath(item.childpath, item.parentpath) 89 90 item.path = filepath.FromSlash(item.path) 91 92 assert.Equalf(t, item.ok, actualok, "checkPrefixAndFetchRelativePath(%s, %s): expected %v, actual %v", item.childpath, item.parentpath, item.ok, actualok) 93 assert.Equal(t, item.path, actualpath, "checkPrefixAndFetchRelativePath(%s, %s): expected %s, actual %s", item.childpath, item.parentpath, item.path, actualpath) 94 } 95 } 96 97 func TestBaseImport(t *testing.T) { 98 // 1. Create a root folder /tmp/root 99 // 2. Simulate scenario 100 // 2.a No Symlink 101 // 2.b Symlink from outside of GOPATH to inside 102 // 2.c Symlink from inside of GOPATH to outside. 103 // 3. Check results. 104 105 tempdir := t.TempDir() 106 107 golang := GoLangOpts() 108 109 for _, item := range baseImportTestFixtures(tempdir) { 110 t.Logf("TestBaseImport(%q)", item.title) 111 112 // Create Paths 113 for _, paths := range item.path { 114 require.NoError(t, os.MkdirAll(paths, 0o700)) 115 } 116 117 if item.symlinksrc == "" { 118 continue 119 } 120 121 // Create Symlink 122 require.NoErrorf(t, os.Symlink(item.symlinkdest, item.symlinksrc), 123 "WARNING:TestBaseImport with symlink could not be carried on. Symlink creation failed for %s -> %s\n%s", 124 item.symlinksrc, item.symlinkdest, 125 "NOTE:TestBaseImport with symlink on Windows requires extended privileges (admin or a user with SeCreateSymbolicLinkPrivilege)", 126 ) 127 128 // Change GOPATH 129 t.Setenv("GOPATH", item.gopath) 130 131 // Test (baseImport always with /) 132 actualpath := golang.baseImport(item.targetpath) 133 134 require.Equalf(t, item.expectedpath, actualpath, "baseImport(%s): expected %s, actual %s", item.targetpath, item.expectedpath, actualpath) 135 136 _ = os.RemoveAll(filepath.Join(tempdir, "root")) 137 } 138 } 139 140 func TestGenerateMarkdown(t *testing.T) { 141 defer discardOutput()() 142 143 t.Run("should generate doc for demo fixture", func(t *testing.T) { 144 opts := testGenOpts() 145 opts.Spec = "../fixtures/enhancements/184/fixture-184.yaml" 146 output := filepath.Join(t.TempDir(), "markdown.md") 147 148 require.NoError(t, GenerateMarkdown(output, nil, nil, opts)) 149 expectedCode := []string{ 150 "# Markdown generator demo", 151 } 152 153 code, err := os.ReadFile(output) 154 require.NoError(t, err) 155 156 for line, codeLine := range expectedCode { 157 if !assertInCode(t, strings.TrimSpace(codeLine), string(code)) { 158 t.Logf("Code expected did not match in codegenfile %s for expected line %d: %q", output, line, expectedCode[line]) 159 } 160 } 161 }) 162 163 t.Run("should handle new lines in descriptions", func(t *testing.T) { 164 opts := testGenOpts() 165 opts.Spec = "../fixtures/bugs/2700/2700.yaml" 166 output := filepath.Join(t.TempDir(), "markdown.md") 167 168 require.NoError(t, GenerateMarkdown(output, nil, nil, opts)) 169 expectedCode := []string{ 170 `| Filesystem type of the volume that you want to mount.</br>Tip: Ensure that the filesystem type is supported by the host operating system.</br>Examples: "ext4", "xfs", "ntfs". Implicitly inferred to be "ext4" if unspecified.</br>More info: https://kubernetes.io/docs/concepts/storage/volumes#awselasticblockstore</br></br>TODO: how do we prevent errors in the filesystem from compromising the machine |`, 171 } 172 173 code, err := os.ReadFile(output) 174 require.NoError(t, err) 175 176 for line, codeLine := range expectedCode { 177 if !assertInCode(t, strings.TrimSpace(codeLine), string(code)) { 178 t.Logf("Code expected did not match in codegenfile %s for expected line %d: %q", output, line, expectedCode[line]) 179 } 180 } 181 }) 182 }