...

Source file src/github.com/go-openapi/swag/path_test.go

Documentation: github.com/go-openapi/swag

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package swag
    16  
    17  import (
    18  	"os"
    19  	"path"
    20  	"path/filepath"
    21  	"runtime"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func makeDirStructure(tgt string) (string, string, error) {
    29  	if tgt == "" {
    30  		tgt = "pkgpaths"
    31  	}
    32  	td, err := os.MkdirTemp("", tgt)
    33  	if err != nil {
    34  		return "", "", err
    35  	}
    36  	td2, err := os.MkdirTemp("", tgt+"-2")
    37  	if err != nil {
    38  		return "", "", err
    39  	}
    40  	realPath := filepath.Join(td, "src", "foo", "bar")
    41  	if err := os.MkdirAll(realPath, os.ModePerm); err != nil {
    42  		return "", "", err
    43  	}
    44  	linkPathBase := filepath.Join(td, "src", "baz")
    45  	if err := os.MkdirAll(linkPathBase, os.ModePerm); err != nil {
    46  		return "", "", err
    47  	}
    48  	linkPath := filepath.Join(linkPathBase, "das")
    49  	if err := os.Symlink(realPath, linkPath); err != nil {
    50  		return "", "", err
    51  	}
    52  
    53  	realPath = filepath.Join(td2, "src", "fuu", "bir")
    54  	if err := os.MkdirAll(realPath, os.ModePerm); err != nil {
    55  		return "", "", err
    56  	}
    57  	linkPathBase = filepath.Join(td2, "src", "biz")
    58  	if err := os.MkdirAll(linkPathBase, os.ModePerm); err != nil {
    59  		return "", "", err
    60  	}
    61  	linkPath = filepath.Join(linkPathBase, "dis")
    62  	if err := os.Symlink(realPath, linkPath); err != nil {
    63  		return "", "", err
    64  	}
    65  	return td, td2, nil
    66  }
    67  
    68  func TestFindPackage(t *testing.T) {
    69  	pth, pth2, err := makeDirStructure("")
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	defer func() {
    74  		os.RemoveAll(pth)
    75  		os.RemoveAll(pth2)
    76  	}()
    77  
    78  	searchPath := pth + string(filepath.ListSeparator) + pth2
    79  	// finds package when real name mentioned
    80  	pkg := FindInSearchPath(searchPath, "foo/bar")
    81  	assert.NotEmpty(t, pkg)
    82  	assertPath(t, path.Join(pth, "src", "foo", "bar"), pkg)
    83  	// finds package when real name is mentioned in secondary
    84  	pkg = FindInSearchPath(searchPath, "fuu/bir")
    85  	assert.NotEmpty(t, pkg)
    86  	assertPath(t, path.Join(pth2, "src", "fuu", "bir"), pkg)
    87  	// finds package when symlinked
    88  	pkg = FindInSearchPath(searchPath, "baz/das")
    89  	assert.NotEmpty(t, pkg)
    90  	assertPath(t, path.Join(pth, "src", "foo", "bar"), pkg)
    91  	// finds package when symlinked in secondary
    92  	pkg = FindInSearchPath(searchPath, "biz/dis")
    93  	assert.NotEmpty(t, pkg)
    94  	assertPath(t, path.Join(pth2, "src", "fuu", "bir"), pkg)
    95  	// return empty string when nothing is found
    96  	pkg = FindInSearchPath(searchPath, "not/there")
    97  	assert.Empty(t, pkg)
    98  }
    99  
   100  //nolint:unparam
   101  func assertPath(t testing.TB, expected, actual string) bool {
   102  	fp, err := filepath.EvalSymlinks(expected)
   103  	require.NoError(t, err)
   104  
   105  	return assert.Equal(t, fp, actual)
   106  }
   107  
   108  func TestFullGOPATH(t *testing.T) {
   109  	os.Unsetenv(GOPATHKey)
   110  	ngp := "/some/where:/other/place"
   111  	t.Setenv(GOPATHKey, ngp)
   112  
   113  	expected := ngp + ":" + runtime.GOROOT()
   114  	assert.Equal(t, expected, FullGoSearchPath())
   115  }
   116  

View as plain text