github.com/djarvur/go-swagger@v0.18.0/generator/support_test.go (about)

     1  package generator
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	goruntime "runtime"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  var checkprefixandfetchrelativepathtests = []struct {
    12  	childpath  string
    13  	parentpath string
    14  	ok         bool
    15  	path       string
    16  }{
    17  	// Positive
    18  	{"/", "/", true, "."},
    19  	{"/User/Gopher", "/", true, "User/Gopher"},
    20  	{"/User/Gopher/Go", "/User/Gopher/Go", true, "."},
    21  	{"/User/../User/Gopher", "/", true, "User/Gopher"},
    22  	// Negative cases
    23  	{"/", "/var", false, ""},
    24  	{"/User/Gopher", "/User/SomethingElse", false, ""},
    25  	{"/var", "/etc", false, ""},
    26  	{"/mnt/dev3", "/mnt/dev3/dir", false, ""},
    27  }
    28  
    29  var tempdir = os.TempDir()
    30  
    31  var checkbaseimporttest = []struct {
    32  	path         []string
    33  	gopath       string
    34  	targetpath   string
    35  	symlinksrc   string
    36  	symlinkdest  string // symlink is the last dir in targetpath
    37  	expectedpath string
    38  }{
    39  	// No sym link. Positive Test Case
    40  	{[]string{tempdir + "/root/go/src/github.com/go-swagger"}, tempdir + "/root/go/", tempdir + "/root/go/src/github.com/go-swagger", "", "", "github.com/go-swagger"},
    41  	// Symlink points inside GOPATH
    42  	{[]string{tempdir + "/root/go/src/github.com/go-swagger"}, tempdir + "/root/go/", tempdir + "/root/symlink", tempdir + "/root/symlink", tempdir + "/root/go/src/", "."},
    43  	// Symlink points inside GOPATH
    44  	{[]string{tempdir + "/root/go/src/github.com/go-swagger"}, tempdir + "/root/go/", tempdir + "/root/symlink", tempdir + "/root/symlink", tempdir + "/root/go/src/github.com", "github.com"},
    45  	// Symlink point outside GOPATH : Targets Case 1: in baseImport implementation.
    46  	{[]string{tempdir + "/root/go/src/github.com/go-swagger", tempdir + "/root/gopher/go/"}, tempdir + "/root/go/", tempdir + "/root/go/src/github.com/gopher", tempdir + "/root/go/src/github.com/gopher", tempdir + "/root/gopher/go", "github.com/gopher"},
    47  }
    48  
    49  func TestCheckPrefixFetchRelPath(t *testing.T) {
    50  
    51  	for _, item := range checkprefixandfetchrelativepathtests {
    52  		actualok, actualpath := checkPrefixAndFetchRelativePath(item.childpath, item.parentpath)
    53  
    54  		if goruntime.GOOS == "windows" {
    55  			item.path = strings.Replace(item.path, "/", "\\", -1)
    56  		}
    57  
    58  		if actualok != item.ok {
    59  			t.Errorf("checkPrefixAndFetchRelativePath(%s, %s): expected %v, actual %v", item.childpath, item.parentpath, item.ok, actualok)
    60  		} else if actualpath != item.path {
    61  			t.Errorf("checkPrefixAndFetchRelativePath(%s, %s): expected %s, actual %s", item.childpath, item.parentpath, item.path, actualpath)
    62  		} else {
    63  			continue
    64  		}
    65  	}
    66  
    67  }
    68  
    69  func TestBaseImport(t *testing.T) {
    70  
    71  	// 1. Create a root folder /tmp/root
    72  	// 2. Simulate scenario
    73  	//	2.a No Symlink
    74  	//	2.b Symlink from outside of GOPATH to inside
    75  	//  2.c Symlink from inside of GOPATH to outside.
    76  	// 3. Check results.
    77  
    78  	oldgopath := os.Getenv("GOPATH")
    79  	defer func() {
    80  		_ = os.Setenv("GOPATH", oldgopath)
    81  		_ = os.RemoveAll(filepath.Join(tempdir, "root"))
    82  	}()
    83  
    84  	for _, item := range checkbaseimporttest {
    85  
    86  		// Create Paths
    87  		for _, paths := range item.path {
    88  			_ = os.MkdirAll(paths, 0777)
    89  		}
    90  
    91  		// Change GOPATH
    92  		_ = os.Setenv("GOPATH", item.gopath)
    93  
    94  		if item.symlinksrc != "" {
    95  			// Create Symlink
    96  			if err := os.Symlink(item.symlinkdest, item.symlinksrc); err == nil {
    97  
    98  				// Test
    99  				actualpath := golang.baseImport(item.targetpath)
   100  
   101  				if goruntime.GOOS == "windows" {
   102  					item.expectedpath = strings.Replace(item.expectedpath, "/", "\\", -1)
   103  				}
   104  
   105  				if actualpath != item.expectedpath {
   106  					t.Errorf("baseImport(%s): expected %s, actual %s", item.targetpath, item.expectedpath, actualpath)
   107  				}
   108  
   109  				_ = os.RemoveAll(filepath.Join(tempdir, "root"))
   110  
   111  			} else {
   112  				t.Logf("WARNING:TestBaseImport with symlink could not be carried on. Symlink creation failed for %s -> %s: %v", item.symlinksrc, item.symlinkdest, err)
   113  				t.Logf("WARNING:TestBaseImport with symlink on Windows requires extended privileges (admin or a user with SeCreateSymbolicLinkPrivilege)")
   114  			}
   115  		}
   116  	}
   117  
   118  }