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

     1  package gradle_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/fossas/fossa-cli/analyzers/gradle"
    10  	"github.com/fossas/fossa-cli/graph"
    11  	"github.com/fossas/fossa-cli/module"
    12  	"github.com/fossas/fossa-cli/pkg"
    13  )
    14  
    15  var DepAPIID = pkg.ID{Type: pkg.Maven, Name: "API", Revision: "1"}
    16  var DepImplmentationID = pkg.ID{Type: pkg.Maven, Name: "Implementation", Revision: "2"}
    17  var DepCompileID = pkg.ID{Type: pkg.Maven, Name: "Compile", Revision: "3"}
    18  var DepCustomID = pkg.ID{Type: pkg.Maven, Name: "Custom", Revision: "4"}
    19  var ImportsAPI = []pkg.Import{pkg.Import{Resolved: DepAPIID}}
    20  var ImportsImplementation = []pkg.Import{pkg.Import{Resolved: DepImplmentationID}}
    21  var ImportsCompile = []pkg.Import{pkg.Import{Resolved: DepCompileID}}
    22  var ImportsCustom = []pkg.Import{pkg.Import{Resolved: DepCustomID}}
    23  var DependenciesAPI = map[pkg.ID]pkg.Package{DepAPIID: pkg.Package{ID: DepAPIID}}
    24  var DependenciesImplementation = map[pkg.ID]pkg.Package{DepImplmentationID: pkg.Package{ID: DepImplmentationID}}
    25  var DependenciesCompile = map[pkg.ID]pkg.Package{DepCompileID: pkg.Package{ID: DepCompileID}}
    26  var DependenciesCustom = map[pkg.ID]pkg.Package{DepCustomID: pkg.Package{ID: DepCustomID}}
    27  
    28  var testMap = map[string]graph.Deps{
    29  	"api":            graph.Deps{Direct: ImportsAPI, Transitive: DependenciesAPI},
    30  	"implementation": graph.Deps{Direct: ImportsImplementation, Transitive: DependenciesImplementation},
    31  	"compile":        graph.Deps{Direct: ImportsCompile, Transitive: DependenciesCompile},
    32  	"custom":         graph.Deps{Direct: ImportsCustom, Transitive: DependenciesCustom},
    33  }
    34  
    35  func TestGradleDependencies(t *testing.T) {
    36  	mock := MockInput(testMap)
    37  	a := gradle.Analyzer{Input: mock}
    38  	graph, err := a.Analyze()
    39  	assert.NoError(t, err)
    40  	assert.Equal(t, 3, len(graph.Transitive))
    41  	assert.Equal(t, graph.Transitive[DepAPIID].ID, DepAPIID)
    42  	assert.Equal(t, graph.Transitive[DepImplmentationID].ID, DepImplmentationID)
    43  	assert.Equal(t, graph.Transitive[DepCompileID].ID, DepCompileID)
    44  	assert.Empty(t, graph.Transitive[DepCustomID].ID)
    45  }
    46  
    47  func TestGradleDependenciesAllConfigurations(t *testing.T) {
    48  	mock := MockGradle{ConfigMap: testMap}
    49  	a := gradle.Analyzer{
    50  		Input: mock,
    51  		Options: gradle.Options{
    52  			AllConfigurations: true,
    53  		},
    54  	}
    55  	graph, err := a.Analyze()
    56  	assert.NoError(t, err)
    57  	assert.Equal(t, 4, len(graph.Transitive))
    58  	assert.Equal(t, graph.Transitive[DepAPIID].ID, DepAPIID)
    59  	assert.Equal(t, graph.Transitive[DepImplmentationID].ID, DepImplmentationID)
    60  	assert.Equal(t, graph.Transitive[DepCompileID].ID, DepCompileID)
    61  	assert.Equal(t, graph.Transitive[DepCustomID].ID, DepCustomID)
    62  }
    63  
    64  func TestGradleDependenciesCustomConfigurations(t *testing.T) {
    65  	mock := MockInput(testMap)
    66  	a := gradle.Analyzer{
    67  		Input: mock,
    68  		Options: gradle.Options{
    69  			Configuration: "compile,custom",
    70  		},
    71  	}
    72  	graph, err := a.Analyze()
    73  	assert.NoError(t, err)
    74  	assert.Equal(t, 2, len(graph.Transitive))
    75  	assert.Empty(t, graph.Transitive[DepAPIID].ID)
    76  	assert.Empty(t, graph.Transitive[DepImplmentationID].ID)
    77  	assert.Equal(t, graph.Transitive[DepCompileID].ID, DepCompileID)
    78  	assert.Equal(t, graph.Transitive[DepCustomID].ID, DepCustomID)
    79  }
    80  
    81  func TestGradleDiscovery(t *testing.T) {
    82  	modules, err := gradle.DiscoverWithCommand("testdata", make(map[string]interface{}), mockCommand("testdata/gradle-tasks-all"))
    83  	assert.NoError(t, err)
    84  	assert.Equal(t, 2, len(modules))
    85  	assert.True(t, moduleExists("testdata/grpc-netty", modules))
    86  	assert.True(t, moduleExists("testdata/grpc-xds", modules))
    87  }
    88  
    89  func mockCommand(mockOutput string) func(string, string, int, ...string) (string, error) {
    90  	return func(string, string, int, ...string) (string, error) {
    91  		output, err := ioutil.ReadFile(mockOutput)
    92  		return string(output), err
    93  	}
    94  }
    95  
    96  type MockGradle struct {
    97  	ConfigMap map[string]graph.Deps
    98  }
    99  
   100  func (m MockGradle) ProjectDependencies(args ...string) (map[string]graph.Deps, error) {
   101  	return m.ConfigMap, nil
   102  }
   103  
   104  func (m MockGradle) DependencyTasks() ([]string, error) {
   105  	return []string{}, nil
   106  }
   107  
   108  func MockInput(config map[string]graph.Deps) MockGradle {
   109  	return MockGradle{
   110  		ConfigMap: config,
   111  	}
   112  }
   113  
   114  func moduleExists(name string, modules []module.Module) bool {
   115  	for _, module := range modules {
   116  		if module.Name == name {
   117  			return true
   118  		}
   119  	}
   120  	return false
   121  }