github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/analyzers/nuget/analyze_test.go (about)

     1  package nuget_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/fossas/fossa-cli/analyzers/nuget"
     8  	"github.com/fossas/fossa-cli/module"
     9  	"github.com/fossas/fossa-cli/pkg"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestIdealPath(t *testing.T) {
    14  	testAnalyzer := newAnalyzer("testdata/fallbacks/good-obj-directory/test.csproj", "testdata/fallbacks/good-obj-directory")
    15  	graph, err := testAnalyzer.Analyze()
    16  	assert.NoError(t, err)
    17  	assert.NotEmpty(t, graph)
    18  	assertImport(t, graph.Direct, "test-package-assets")
    19  }
    20  func TestFallbackToPackageReference(t *testing.T) {
    21  	testAnalyzer := newAnalyzer("testdata/fallbacks/no-obj-directory/test.csproj", "testdata/fallbacks/no-obj-directory")
    22  	graph, err := testAnalyzer.Analyze()
    23  	assert.NoError(t, err)
    24  	assert.NotEmpty(t, graph)
    25  	assertImport(t, graph.Direct, "not-package-assets")
    26  }
    27  func TestFallbackToNuspec(t *testing.T) {
    28  	testAnalyzer := newAnalyzer("testdata/fallbacks/bad-package-ref/temp.nuspec", "testdata/fallbacks/bad-package-ref")
    29  	graph, err := testAnalyzer.Analyze()
    30  	assert.NoError(t, err)
    31  	assert.NotEmpty(t, graph)
    32  	assertImport(t, graph.Direct, "nuspec-fallback")
    33  }
    34  
    35  func assertImport(t *testing.T, imports []pkg.Import, name string) {
    36  	for _, dep := range imports {
    37  		if dep.Resolved.Name == name {
    38  			return
    39  		}
    40  	}
    41  	assert.Fail(t, fmt.Sprintf("Imports list: %+v\nDoes not contain\nDependency: %s", imports, name))
    42  }
    43  
    44  func newAnalyzer(target, dir string) nuget.Analyzer {
    45  	return nuget.Analyzer{
    46  		Module: module.Module{
    47  			BuildTarget: target,
    48  			Dir:         dir,
    49  		},
    50  	}
    51  }