github.com/circl-dev/go-swagger@v0.31.0/generator/support_test.go (about)

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