github.com/jfrog/gocmd@v0.6.0/cmd/cmd_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
     5  	"github.com/jfrog/jfrog-client-go/utils/log"
     6  	"github.com/stretchr/testify/assert"
     7  	"os"
     8  	"path/filepath"
     9  	"reflect"
    10  	"runtime"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestListToMap(t *testing.T) {
    16  	content := `github.com/you/hello
    17  github.com/dsnet/compress@v0.0.0-20171208185109-cc9eb1d7ad76
    18  github.com/golang/snappy@v0.0.0-20180518054509-2e65f85255db
    19  github.com/mholt/archiver@v2.1.0+incompatible
    20  github.com/nwaples/rardecode@v0.0.0-20171029023500-e06696f847ae
    21  github.com/pierrec/lz4@v2.0.5+incompatible
    22  github.com/ulikunitz/xz@v0.5.4
    23  rsc.io/quote@v1.5.2
    24  rsc.io/sampler@v1.3.0
    25  	`
    26  
    27  	log.SetLogger(log.NewLogger(log.ERROR, nil))
    28  	actual := listToMap(content)
    29  	expected := map[string]bool{
    30  		"github.com/dsnet/compress@v0.0.0-20171208185109-cc9eb1d7ad76":    true,
    31  		"github.com/golang/snappy@v0.0.0-20180518054509-2e65f85255db":     true,
    32  		"github.com/mholt/archiver@v2.1.0+incompatible":                   true,
    33  		"github.com/nwaples/rardecode@v0.0.0-20171029023500-e06696f847ae": true,
    34  		"github.com/pierrec/lz4@v2.0.5+incompatible":                      true,
    35  		"github.com/ulikunitz/xz@v0.5.4":                                  true,
    36  		"rsc.io/quote@v1.5.2":                                             true,
    37  		"rsc.io/sampler@v1.3.0":                                           true,
    38  	}
    39  
    40  	if !reflect.DeepEqual(expected, actual) {
    41  		t.Errorf("Expecting: \n%v \nGot: \n%v", expected, actual)
    42  	}
    43  }
    44  
    45  func TestGetProjectDir(t *testing.T) {
    46  	wd, err := os.Getwd()
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  	defer os.Chdir(wd)
    51  
    52  	// CD into a directory with a go.mod file.
    53  	projectRoot := filepath.Join("testdata", "project")
    54  	err = os.Chdir(projectRoot)
    55  	if err != nil {
    56  		t.Error(err)
    57  	}
    58  
    59  	// Make projectRoot an absolute path.
    60  	projectRoot, err = os.Getwd()
    61  	if err != nil {
    62  		t.Error(err)
    63  	}
    64  
    65  	// Get the project root.
    66  	root, err := GetProjectRoot()
    67  	if err != nil {
    68  		t.Error(err)
    69  	}
    70  	if root != projectRoot {
    71  		t.Error("Expecting", projectRoot, "got:", root)
    72  	}
    73  
    74  	// CD back to the original directory.
    75  	if err := os.Chdir(wd); err != nil {
    76  		t.Error(err)
    77  	}
    78  
    79  	// CD into a sub directory in the same project, and expect to get the same project root.
    80  	os.Chdir(wd)
    81  	projectSubDirectory := filepath.Join("testdata", "project", "dir")
    82  	err = os.Chdir(projectSubDirectory)
    83  	if err != nil {
    84  		t.Error(err)
    85  	}
    86  	root, err = GetProjectRoot()
    87  	if err != nil {
    88  		t.Error(err)
    89  	}
    90  	if root != projectRoot {
    91  		t.Error("Expecting", projectRoot, "got:", root)
    92  	}
    93  
    94  	// CD back to the original directory.
    95  	if err := os.Chdir(wd); err != nil {
    96  		t.Error(err)
    97  	}
    98  
    99  	// Now CD into a directory outside the project, and expect to get a different project root.
   100  	noProjectRoot := filepath.Join("testdata", "noproject")
   101  	err = os.Chdir(noProjectRoot)
   102  	if err != nil {
   103  		t.Error(err)
   104  	}
   105  	root, err = GetProjectRoot()
   106  	if err != nil {
   107  		t.Error(err)
   108  	}
   109  	if root == projectRoot {
   110  		t.Error("Expecting a different value than", root)
   111  	}
   112  }
   113  
   114  func TestGetDependenciesList(t *testing.T) {
   115  	log.SetLogger(log.NewLogger(log.ERROR, nil))
   116  	gomodPath := filepath.Join("testdata", "mods", "testGoList")
   117  	err := fileutils.MoveFile(filepath.Join(gomodPath, "go.mod.txt"), filepath.Join(gomodPath, "go.mod"))
   118  	assert.NoError(t, err)
   119  	defer func() {
   120  		err := fileutils.MoveFile(filepath.Join(gomodPath, "go.mod"), filepath.Join(gomodPath, "go.mod.txt"))
   121  		assert.NoError(t, err)
   122  	}()
   123  	err = fileutils.MoveFile(filepath.Join(gomodPath, "go.sum.txt"), filepath.Join(gomodPath, "go.sum"))
   124  	assert.NoError(t, err)
   125  	defer func() {
   126  		err = fileutils.MoveFile(filepath.Join(gomodPath, "go.sum"), filepath.Join(gomodPath, "go.sum.txt"))
   127  		assert.NoError(t, err)
   128  	}()
   129  	originSumFileContent, _, err := GetGoSum(gomodPath)
   130  	err = fileutils.MoveFile(filepath.Join(gomodPath, "test.go.txt"), filepath.Join(gomodPath, "test.go"))
   131  	assert.NoError(t, err)
   132  	defer func() {
   133  		err := fileutils.MoveFile(filepath.Join(gomodPath, "test.go"), filepath.Join(gomodPath, "test.go.txt"))
   134  		assert.NoError(t, err)
   135  	}()
   136  	actual, err := GetDependenciesList(filepath.Join(gomodPath))
   137  	if err != nil {
   138  		t.Error(err)
   139  	}
   140  
   141  	// Since Go 1.16 'go list' command won't automatically update go.mod and go.sum.
   142  	// Check that we rollback changes properly.
   143  	newSumFileContent, _, err := GetGoSum(gomodPath)
   144  	if !reflect.DeepEqual(originSumFileContent, newSumFileContent) {
   145  		t.Errorf("go.sum has been modified and didn't rollback properly")
   146  	}
   147  
   148  	expected := map[string]bool{
   149  		"golang.org/x/text@v0.3.3": true,
   150  		"rsc.io/quote@v1.5.2":      true,
   151  		"rsc.io/sampler@v1.3.0":    true,
   152  		"testGoList@":              true,
   153  	}
   154  	if !reflect.DeepEqual(expected, actual) {
   155  		t.Errorf("Expecting: \n%v \nGot: \n%v", expected, actual)
   156  	}
   157  }
   158  
   159  func TestParseGoPathWindows(t *testing.T) {
   160  	log.SetLogger(log.NewLogger(log.DEBUG, nil))
   161  	if runtime.GOOS != "windows" {
   162  		log.Debug("Skipping the test since not running on Windows OS")
   163  		return
   164  	}
   165  	tests := []struct {
   166  		name     string
   167  		goPath   string
   168  		expected string
   169  	}{
   170  		{"One go path", "C:\\Users\\JFrog\\go", "C:\\Users\\JFrog\\go"},
   171  		{"Multiple go paths", "C:\\Users\\JFrog\\go;C:\\Users\\JFrog\\go2;C:\\Users\\JFrog\\go3", "C:\\Users\\JFrog\\go"},
   172  		{"Empty path", "", ""},
   173  	}
   174  
   175  	runGoPathTests(tests, t)
   176  }
   177  
   178  func TestParseGoPathUnix(t *testing.T) {
   179  	if runtime.GOOS == "windows" {
   180  		return
   181  	}
   182  	tests := []struct {
   183  		name     string
   184  		goPath   string
   185  		expected string
   186  	}{
   187  		{"One go path", "/Users/jfrog/go", "/Users/jfrog/go"},
   188  		{"Multiple go paths", "/Users/jfrog/go:/Users/jfrog/go2:/Users/jfrog/go3", "/Users/jfrog/go"},
   189  		{"Empty path", "", ""},
   190  	}
   191  
   192  	runGoPathTests(tests, t)
   193  }
   194  
   195  func runGoPathTests(tests []struct {
   196  	name     string
   197  	goPath   string
   198  	expected string
   199  }, t *testing.T) {
   200  	for _, test := range tests {
   201  		t.Run(test.name, func(t *testing.T) {
   202  			actual := parseGoPath(test.goPath)
   203  			if !strings.EqualFold(actual, test.expected) {
   204  				t.Errorf("Test name: %s: Expected: %v, Got: %v", test.name, test.expected, actual)
   205  			}
   206  		})
   207  	}
   208  }