github.com/6543-forks/go-swagger@v0.26.0/generator/support_test.go (about) 1 package generator 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 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 99 // 1. Create a root folder /tmp/root 100 // 2. Simulate scenario 101 // 2.a No Symlink 102 // 2.b Symlink from outside of GOPATH to inside 103 // 2.c Symlink from inside of GOPATH to outside. 104 // 3. Check results. 105 106 oldgopath := os.Getenv("GOPATH") 107 defer func() { 108 _ = os.Setenv("GOPATH", oldgopath) 109 }() 110 111 tempdir, err := ioutil.TempDir("", "test-baseimport") 112 require.NoError(t, err) 113 defer func() { 114 _ = os.RemoveAll(tempdir) 115 }() 116 117 golang := GoLangOpts() 118 119 for _, item := range baseImportTestFixtures(tempdir) { 120 t.Logf("TestBaseImport(%q)", item.title) 121 122 // Create Paths 123 for _, paths := range item.path { 124 err = os.MkdirAll(paths, 0700) 125 require.NoError(t, err) 126 } 127 128 if item.symlinksrc == "" { 129 continue 130 } 131 132 // Create Symlink 133 err := os.Symlink(item.symlinkdest, item.symlinksrc) 134 require.NoErrorf(t, err, 135 "WARNING:TestBaseImport with symlink could not be carried on. Symlink creation failed for %s -> %s: %v\n%s", 136 item.symlinksrc, item.symlinkdest, err, 137 "NOTE:TestBaseImport with symlink on Windows requires extended privileges (admin or a user with SeCreateSymbolicLinkPrivilege)", 138 ) 139 140 // Change GOPATH 141 _ = os.Setenv("GOPATH", item.gopath) 142 143 // Test (baseImport always with /) 144 actualpath := golang.baseImport(item.targetpath) 145 146 require.Equalf(t, item.expectedpath, actualpath, "baseImport(%s): expected %s, actual %s", item.targetpath, item.expectedpath, actualpath) 147 148 _ = os.RemoveAll(filepath.Join(tempdir, "root")) 149 } 150 151 }