github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/buildtools/composer/composer_test.go (about) 1 package composer_test 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/fossas/fossa-cli/buildtools/composer" 11 ) 12 13 // The mockRunner type implements composer.Composer. 14 type mockRunner struct { 15 showTreeOutput, showOutput, installOutput string 16 } 17 18 func (mr mockRunner) Show(dir string, args ...string) (stdout string, stderr string, err error) { 19 for _, arg := range args { 20 if arg == "--tree" { 21 return mr.showTreeOutput, "", nil 22 } 23 } 24 return mr.showOutput, "", nil 25 } 26 27 func (mr mockRunner) Install(dir string, args ...string) (stdout string, stderr string, err error) { 28 return mr.installOutput, "", nil 29 } 30 31 func TestDependencies(t *testing.T) { 32 const tree = "twig/twig v2.5.0 Twig, the flexible, fast, and secure template language for PHP\n" + 33 "|--php ^7.0\n" + 34 "|--symfony/polyfill-ctype ^1.8\n" + 35 "| `--php >=5.3.3\n" + 36 "`--symfony/polyfill-mbstring ~1.0\n" + 37 "`--php >=5.3.3\n" 38 39 const show = `{ 40 "installed": [ 41 { 42 "name": "symfony/polyfill-ctype", 43 "version": "v1.9.0", 44 "description": "Symfony polyfill for ctype functions" 45 }, 46 { 47 "name": "symfony/polyfill-mbstring", 48 "version": "v1.9.0", 49 "description": "Polyfill for Mbstring" 50 }, 51 { 52 "name": "twig/twig", 53 "version": "v2.5.0", 54 "description": "Twig, the flexible, fast, and secure template language for PHP" 55 } 56 ] 57 }` 58 59 type pkg = composer.Package 60 61 p1 := pkg{"twig/twig", "v2.5.0", "Twig, the flexible, fast, and secure template language for PHP"} 62 p2 := pkg{"symfony/polyfill-ctype", "v1.9.0", "Symfony polyfill for ctype functions"} 63 p3 := pkg{"symfony/polyfill-mbstring", "v1.9.0", "Polyfill for Mbstring"} 64 65 runner := mockRunner{ 66 showTreeOutput: tree, 67 showOutput: show, 68 } 69 70 imports, deps, err := composer.Dependencies("", runner) 71 assert.NoError(t, err) 72 73 // Check for all expected dependencies, and ensure that "php" is not one of them. 74 expectedImports := []pkg{p1} 75 76 assert.Equal(t, len(expectedImports), len(imports), fmt.Sprintf("Got: %v", imports)) 77 assert.Equal(t, expectedImports, imports) 78 79 expectedDeps := map[string][]pkg{ 80 "twig/twig": {p2, p3}, 81 "symfony/polyfill-ctype": {}, 82 "symfony/polyfill-mbstring": {}, 83 } 84 85 for pkgName, pkgDeps := range expectedDeps { 86 var found *pkg 87 for k := range deps { 88 if k.Name == pkgName { 89 found = &k 90 break 91 } 92 } 93 assert.NotNil(t, found) 94 95 // The pkgDeps slice and the corresponding slice in deps may have all the same data but with the 96 // elements not in the same order because they're taken from iteration over a map. 97 for _, dep := range pkgDeps { 98 // Find a Package with the same data. 99 sameFound := false 100 for _, gotDep := range deps[*found] { 101 if reflect.DeepEqual(dep, gotDep) { 102 sameFound = true 103 break 104 } 105 } 106 assert.True(t, sameFound) 107 } 108 } 109 } 110 111 func TestShow_empty(t *testing.T) { 112 // A special case with just "[]" output by `composer show --format=json` when no dependencies are found. 113 // The point of this test is that Show should be able to handle the JSON. 114 /* 115 { 116 "name": "something/something", 117 "description": "Something something.", 118 "type": "something-something", 119 "keywords": [], 120 "license": "GPL-2.0-or-later", 121 "require": { 122 "php": ">=7.2.0" 123 } 124 } 125 */ 126 127 runner := mockRunner{} 128 129 runner.showOutput = "[]" 130 show, err := composer.Show("", runner) 131 assert.NoError(t, err) 132 assert.Empty(t, show.Installed) 133 134 runner.showOutput = "[]\n" 135 show, err = composer.Show("", runner) 136 assert.NoError(t, err) 137 assert.Empty(t, show.Installed) 138 }