github.com/emreu/go-swagger@v0.22.1/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  		switch {
    59  		case actualok != item.ok:
    60  			t.Errorf("checkPrefixAndFetchRelativePath(%s, %s): expected %v, actual %v", item.childpath, item.parentpath, item.ok, actualok)
    61  		case actualpath != item.path:
    62  			t.Errorf("checkPrefixAndFetchRelativePath(%s, %s): expected %s, actual %s", item.childpath, item.parentpath, item.path, actualpath)
    63  		default:
    64  			continue
    65  		}
    66  	}
    67  
    68  }
    69  
    70  func TestBaseImport(t *testing.T) {
    71  
    72  	// 1. Create a root folder /tmp/root
    73  	// 2. Simulate scenario
    74  	//	2.a No Symlink
    75  	//	2.b Symlink from outside of GOPATH to inside
    76  	//  2.c Symlink from inside of GOPATH to outside.
    77  	// 3. Check results.
    78  
    79  	oldgopath := os.Getenv("GOPATH")
    80  	defer func() {
    81  		_ = os.Setenv("GOPATH", oldgopath)
    82  		_ = os.RemoveAll(filepath.Join(tempdir, "root"))
    83  	}()
    84  
    85  	for _, item := range checkbaseimporttest {
    86  
    87  		// Create Paths
    88  		for _, paths := range item.path {
    89  			_ = os.MkdirAll(paths, 0777)
    90  		}
    91  
    92  		// Change GOPATH
    93  		_ = os.Setenv("GOPATH", item.gopath)
    94  
    95  		if item.symlinksrc != "" {
    96  			// Create Symlink
    97  			if err := os.Symlink(item.symlinkdest, item.symlinksrc); err == nil {
    98  
    99  				// Test
   100  				actualpath := golang.baseImport(item.targetpath)
   101  
   102  				if goruntime.GOOS == "windows" {
   103  					item.expectedpath = strings.Replace(item.expectedpath, "/", "\\", -1)
   104  				}
   105  
   106  				if actualpath != item.expectedpath {
   107  					t.Errorf("baseImport(%s): expected %s, actual %s", item.targetpath, item.expectedpath, actualpath)
   108  				}
   109  
   110  				_ = os.RemoveAll(filepath.Join(tempdir, "root"))
   111  
   112  			} else {
   113  				t.Logf("WARNING:TestBaseImport with symlink could not be carried on. Symlink creation failed for %s -> %s: %v", item.symlinksrc, item.symlinkdest, err)
   114  				t.Logf("WARNING:TestBaseImport with symlink on Windows requires extended privileges (admin or a user with SeCreateSymbolicLinkPrivilege)")
   115  			}
   116  		}
   117  	}
   118  
   119  }