github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/buildtools/yarn/parse_test.go (about)

     1  package yarn_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/fossas/fossa-cli/buildtools/yarn"
    10  	"github.com/fossas/fossa-cli/pkg"
    11  )
    12  
    13  /*
    14  └─┬ chai@4.1.2
    15    ├── assertion-error@1.1.0
    16    ├── check-error@1.0.2
    17    ├─┬ deep-eql@3.0.1
    18    │ └── type-detect@4.0.8 deduped
    19    ├── get-func-name@2.0.0
    20    ├── pathval@1.1.0
    21    └── type-detect@4.0.8
    22  */
    23  func TestSimpleLockfile(t *testing.T) {
    24  	deps, err := yarn.FromProject(filepath.Join("testdata", "package.json"), filepath.Join("testdata", "yarn.lock"))
    25  	assert.NoError(t, err)
    26  
    27  	assert.Len(t, deps.Direct, 2)
    28  	assert.Len(t, deps.Transitive, 8)
    29  
    30  	var chaiImport pkg.Import
    31  	var typeDetectImport pkg.Import
    32  
    33  	if deps.Direct[0].Target == "chai" {
    34  		chaiImport = deps.Direct[0]
    35  		typeDetectImport = deps.Direct[1]
    36  	} else {
    37  		chaiImport = deps.Direct[1]
    38  		typeDetectImport = deps.Direct[0]
    39  	}
    40  
    41  	assert.Equal(t, "chai", chaiImport.Target)
    42  	assert.Equal(t, "4.1.2", chaiImport.Resolved.Revision)
    43  	assert.Equal(t, "type-detect", typeDetectImport.Target)
    44  	assert.Equal(t, "3.0.0", typeDetectImport.Resolved.Revision)
    45  
    46  	AssertDeps(t, deps.Transitive, "assertion-error", "1.1.0")
    47  	AssertDeps(t, deps.Transitive, "check-error", "1.0.2")
    48  	AssertDeps(t, deps.Transitive, "deep-eql", "3.0.1")
    49  	AssertDeps(t, deps.Transitive, "get-func-name", "2.0.0")
    50  	AssertDeps(t, deps.Transitive, "pathval", "1.1.0")
    51  	AssertDeps(t, deps.Transitive, "type-detect", "4.0.8")
    52  	AssertDeps(t, deps.Transitive, "type-detect", "3.0.0")
    53  	AssertDeps(t, deps.Transitive, "chai", "4.1.2")
    54  }
    55  
    56  func TestTransitiveCollisionsWithDirectProdDeps(t *testing.T) {
    57  	t.Parallel()
    58  	for _, testNameDirName := range []string{"trans_devdep_name_and_rev_collision_with_dir_proddep", "trans_devdep_name_collision_with_dir_proddep"} {
    59  		testPathBase := filepath.Join("testdata", testNameDirName)
    60  		t.Run(testNameDirName, func(t *testing.T) {
    61  			t.Parallel()
    62  			deps, err := yarn.FromProject(filepath.Join(testPathBase, "package.json"), filepath.Join(testPathBase, "yarn.lock"))
    63  			assert.NoError(t, err)
    64  
    65  			assert.Len(t, deps.Direct, 1)
    66  			assert.Len(t, deps.Transitive, 1)
    67  
    68  			assert.Equal(t, "a", deps.Direct[0].Target)
    69  			assert.Equal(t, "1.0.1", deps.Direct[0].Resolved.Revision)
    70  
    71  			AssertDeps(t, deps.Transitive, "a", "1.0.1")
    72  		})
    73  	}
    74  }
    75  
    76  func TestTransitiveCollisionsWithTransProdDeps(t *testing.T) {
    77  	t.Parallel()
    78  	for _, testNameDirName := range []string{"trans_devdep_name_and_rev_collision_with_trans_proddep", "trans_devdep_name_collision_with_trans_proddep"} {
    79  		testPathBase := filepath.Join("testdata", testNameDirName)
    80  		t.Run(testNameDirName, func(t *testing.T) {
    81  			t.Parallel()
    82  			deps, err := yarn.FromProject(filepath.Join(testPathBase, "package.json"), filepath.Join(testPathBase, "yarn.lock"))
    83  			assert.NoError(t, err)
    84  
    85  			assert.Len(t, deps.Direct, 1)
    86  			assert.Len(t, deps.Transitive, 2)
    87  
    88  			assert.Equal(t, "a", deps.Direct[0].Target)
    89  			assert.Equal(t, "1.0.1", deps.Direct[0].Resolved.Revision)
    90  
    91  			AssertDeps(t, deps.Transitive, "a", "1.0.1")
    92  			AssertDeps(t, deps.Transitive, "b", "2.0.2")
    93  		})
    94  	}
    95  }
    96  
    97  func TestRevisionsResolvingNotToSuffix(t *testing.T) {
    98  	t.Parallel()
    99  	testPathBase := filepath.Join("testdata", "revision_resolving_not_to_suffix")
   100  	deps, err := yarn.FromProject(filepath.Join(testPathBase, "package.json"), filepath.Join(testPathBase, "yarn.lock"))
   101  	assert.NoError(t, err)
   102  
   103  	assert.Len(t, deps.Direct, 1)
   104  	assert.Len(t, deps.Transitive, 2)
   105  
   106  	assert.Equal(t, "a", deps.Direct[0].Target)
   107  	assert.Equal(t, "2.0.2", deps.Direct[0].Resolved.Revision)
   108  
   109  	AssertDeps(t, deps.Transitive, "a", "2.0.2")
   110  	AssertDeps(t, deps.Transitive, "b", "4.0.4")
   111  
   112  }
   113  
   114  func AssertDeps(t *testing.T, transitiveDeps map[pkg.ID]pkg.Package, name, revision string) {
   115  	for pkgID, _ := range transitiveDeps {
   116  		if pkgID.Name == name && pkgID.Revision == revision {
   117  			return
   118  		}
   119  	}
   120  
   121  	assert.Fail(t, "missing "+name+"@"+revision)
   122  }