github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/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  
    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 := os.MkdirTemp("", "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  			require.NoError(t, os.MkdirAll(paths, 0700))
   125  		}
   126  
   127  		if item.symlinksrc == "" {
   128  			continue
   129  		}
   130  
   131  		// Create Symlink
   132  		require.NoErrorf(t, os.Symlink(item.symlinkdest, item.symlinksrc),
   133  			"WARNING:TestBaseImport with symlink could not be carried on. Symlink creation failed for %s -> %s: %v\n%s",
   134  			item.symlinksrc, item.symlinkdest, err,
   135  			"NOTE:TestBaseImport with symlink on Windows requires extended privileges (admin or a user with SeCreateSymbolicLinkPrivilege)",
   136  		)
   137  
   138  		// Change GOPATH
   139  		_ = os.Setenv("GOPATH", item.gopath)
   140  
   141  		// Test (baseImport always with /)
   142  		actualpath := golang.baseImport(item.targetpath)
   143  
   144  		require.Equalf(t, item.expectedpath, actualpath, "baseImport(%s): expected %s, actual %s", item.targetpath, item.expectedpath, actualpath)
   145  
   146  		_ = os.RemoveAll(filepath.Join(tempdir, "root"))
   147  	}
   148  }
   149  
   150  func TestGenerateMarkdown(t *testing.T) {
   151  	defer discardOutput()()
   152  
   153  	tempdir, err := os.MkdirTemp("", "test-markdown")
   154  	require.NoError(t, err)
   155  	defer func() {
   156  		_ = os.RemoveAll(tempdir)
   157  	}()
   158  
   159  	opts := testGenOpts()
   160  	opts.Spec = "../fixtures/enhancements/184/fixture-184.yaml"
   161  	output := filepath.Join(tempdir, "markdown.md")
   162  
   163  	require.NoError(t, GenerateMarkdown(output, nil, nil, opts))
   164  	expectedCode := []string{
   165  		"# Markdown generator demo",
   166  	}
   167  
   168  	code, err := os.ReadFile(output)
   169  	require.NoError(t, err)
   170  
   171  	for line, codeLine := range expectedCode {
   172  		if !assertInCode(t, strings.TrimSpace(codeLine), string(code)) {
   173  			t.Logf("Code expected did not match in codegenfile %s for expected line %d: %q", output, line, expectedCode[line])
   174  		}
   175  	}
   176  }