github.com/yasushi-saito/gometalinter@v2.0.13-0.20190118091058-bb04f89050ef+incompatible/partition_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPartitionToMaxSize(t *testing.T) {
    14  	cmdArgs := []string{"/usr/bin/foo", "-c"}
    15  	paths := []string{"one", "two", "three", "four"}
    16  
    17  	parts := partitionToMaxSize(cmdArgs, paths, 24)
    18  	expected := [][]string{
    19  		append(cmdArgs, "one", "two"),
    20  		append(cmdArgs, "three"),
    21  		append(cmdArgs, "four"),
    22  	}
    23  	assert.Equal(t, expected, parts)
    24  }
    25  
    26  func TestPartitionToPackageFileGlobs(t *testing.T) {
    27  	tmpdir, err := ioutil.TempDir("", "test-expand-paths")
    28  	require.NoError(t, err)
    29  	defer os.RemoveAll(tmpdir)
    30  
    31  	cmdArgs := []string{"/usr/bin/foo", "-c"}
    32  	paths := []string{
    33  		filepath.Join(tmpdir, "one"),
    34  		filepath.Join(tmpdir, "two"),
    35  	}
    36  	for _, dir := range paths {
    37  		mkDir(t, dir)
    38  		mkGoFile(t, dir, "other.go")
    39  	}
    40  
    41  	parts, err := partitionPathsAsFilesGroupedByPackage(cmdArgs, paths)
    42  	require.NoError(t, err)
    43  	expected := [][]string{
    44  		append(cmdArgs, packagePaths(paths[0], "file.go", "other.go")...),
    45  		append(cmdArgs, packagePaths(paths[1], "file.go", "other.go")...),
    46  	}
    47  	assert.Equal(t, expected, parts)
    48  }
    49  
    50  func packagePaths(dir string, filenames ...string) []string {
    51  	paths := []string{}
    52  	for _, filename := range filenames {
    53  		paths = append(paths, filepath.Join(dir, filename))
    54  	}
    55  	return paths
    56  }
    57  
    58  func TestPartitionToPackageFileGlobsNoFiles(t *testing.T) {
    59  	tmpdir, err := ioutil.TempDir("", "test-expand-paths")
    60  	require.NoError(t, err)
    61  	defer os.RemoveAll(tmpdir)
    62  
    63  	cmdArgs := []string{"/usr/bin/foo", "-c"}
    64  	paths := []string{filepath.Join(tmpdir, "one"), filepath.Join(tmpdir, "two")}
    65  	parts, err := partitionPathsAsFilesGroupedByPackage(cmdArgs, paths)
    66  	require.NoError(t, err)
    67  	assert.Len(t, parts, 0)
    68  }
    69  
    70  func TestPartitionToMaxArgSizeWithFileGlobsNoFiles(t *testing.T) {
    71  	tmpdir, err := ioutil.TempDir("", "test-expand-paths")
    72  	require.NoError(t, err)
    73  	defer os.RemoveAll(tmpdir)
    74  
    75  	cmdArgs := []string{"/usr/bin/foo", "-c"}
    76  	paths := []string{filepath.Join(tmpdir, "one"), filepath.Join(tmpdir, "two")}
    77  	parts, err := partitionPathsAsFiles(cmdArgs, paths)
    78  	require.NoError(t, err)
    79  	assert.Len(t, parts, 0)
    80  }
    81  
    82  func TestPathsToPackagePaths(t *testing.T) {
    83  	root := "/fake/root"
    84  	defer fakeGoPath(t, root)()
    85  
    86  	packagePaths, err := pathsToPackagePaths([]string{
    87  		filepath.Join(root, "src", "example.com", "foo"),
    88  		"./relative/package",
    89  	})
    90  	require.NoError(t, err)
    91  	expected := []string{"example.com/foo", "./relative/package"}
    92  	assert.Equal(t, expected, packagePaths)
    93  }
    94  
    95  func fakeGoPath(t *testing.T, path string) func() {
    96  	oldpath := os.Getenv("GOPATH")
    97  	require.NoError(t, os.Setenv("GOPATH", path))
    98  	return func() { require.NoError(t, os.Setenv("GOPATH", oldpath)) }
    99  }
   100  
   101  func TestPartitionPathsByDirectory(t *testing.T) {
   102  	cmdArgs := []string{"/usr/bin/foo", "-c"}
   103  	paths := []string{"one", "two", "three"}
   104  
   105  	parts, err := partitionPathsByDirectory(cmdArgs, paths)
   106  	require.NoError(t, err)
   107  	expected := [][]string{
   108  		append(cmdArgs, "one"),
   109  		append(cmdArgs, "two"),
   110  		append(cmdArgs, "three"),
   111  	}
   112  	assert.Equal(t, expected, parts)
   113  
   114  }