github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/testing/helpers/helpers.go (about)

     1  package helpers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/fossas/fossa-cli/pkg"
     9  )
    10  
    11  // AssertDosFile parses the file and ensures that every line ending is formatted
    12  // for DOS operating systems with a carriage return and line feed ("\r\n").
    13  func AssertDosFile(t *testing.T, file []byte) {
    14  	fixture := string(file)
    15  	for i := range fixture {
    16  		if i == 0 {
    17  			continue
    18  		}
    19  		if fixture[i] == '\n' {
    20  			assert.Equal(t, uint8('\r'), fixture[i-1])
    21  		}
    22  	}
    23  }
    24  
    25  // AssertUnixFile parses the file and ensures that every line ending is formatted
    26  // for Unix/Linux operating systems with only line feed ("\n").
    27  func AssertUnixFile(t *testing.T, file []byte) {
    28  	fixture := string(file)
    29  	for i := range fixture {
    30  		if i == 0 {
    31  			continue
    32  		}
    33  		if fixture[i] == '\n' {
    34  			assert.NotEqual(t, uint8('\r'), fixture[i-1])
    35  		}
    36  	}
    37  }
    38  
    39  // PackageInTransitiveGraph searches a map (typically from Graph.Deps.Transitive)
    40  // for a package and returns it if it exists.
    41  func PackageInTransitiveGraph(packages map[pkg.ID]pkg.Package, name, revision string) pkg.Package {
    42  	for id := range packages {
    43  		if id.Name == name && id.Revision == revision {
    44  			return packages[id]
    45  		}
    46  	}
    47  	return pkg.Package{}
    48  }
    49  
    50  // AssertPackageImport searches a list of imports (typically from pkg.Package.Imports)
    51  // for a package and asserts on its existence.
    52  func AssertPackageImport(t *testing.T, imports pkg.Imports, name, revision string) {
    53  	for _, importedProj := range imports {
    54  		if importedProj.Resolved.Name == name {
    55  			if importedProj.Resolved.Revision == revision {
    56  				return
    57  			}
    58  			assert.Fail(t, "found "+name+"@"+importedProj.Resolved.Revision+" instead of "+revision)
    59  		}
    60  	}
    61  	assert.Fail(t, "missing "+name+"@"+revision)
    62  }