Skip to content

Commit

Permalink
fully define TestFromNodeModules
Browse files Browse the repository at this point in the history
  • Loading branch information
microsoftly committed Sep 10, 2018
1 parent 5644d87 commit 9b6bc04
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions buildtools/npm/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package npm

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,3 +15,53 @@ func TestFromManifest(t *testing.T) {
assert.NotEmpty(t, manifest.Dependencies)
assert.Equal(t, manifest.Dependencies["chai"], "4.1.2")
}

func TestFromNodeModules(t *testing.T) {
manifests, err := FromNodeModules("testdata/package.json")
assert.NoError(t, err)

/*
└─┬ chai@4.1.2
├── assertion-error@1.1.0
├── check-error@1.0.2
├─┬ deep-eql@3.0.1
│ └── type-detect@4.0.8 deduped
├── get-func-name@2.0.0
├── pathval@1.1.0
└── type-detect@4.0.8
*/

assert.NotEmpty(t, manifests)
assert.True(t, containtsDep(manifests, "assertion-error", "1.1.0"), "Manifests does not include dep assertion-error")
assert.True(t, containtsDep(manifests, "check-error", "1.0.2"), "Manifests does not include dep check-error")
assert.True(t, containtsDep(manifests, "get-func-name", "2.0.0"), "Manifests does not include dep get-func-name")
assert.True(t, containtsDep(manifests, "pathval", "1.1.0"), "Manifests does not include dep pathval")
assert.True(t, containtsDep(manifests, "type-detect", "4.0.8"), "Manifests does not include dep type-detect")

// check transitive dep's existance
deps, err := selectDep(manifests, "deep-eql", "3.0.1")
assert.NoError(t, err, "Manifests does not include dep deep-eql")

deepEql := deps[0]
assert.NotEmpty(t, deepEql.Dependencies)
assert.Len(t, deepEql.Dependencies, 1)
assert.Contains(t, deepEql.Dependencies, "type-detect")
}

func selectDep(manifests []Manifest, name string, version string) ([]Manifest, error) {
for _, v := range manifests {
if v.Name == name && v.Version == version {
return []Manifest{v}, nil
}
}

var emptyArr []Manifest

return emptyArr, errors.New("could not find manifest")
}

func containtsDep(manifests []Manifest, name string, version string) bool {
manifests, err := selectDep(manifests, name, version)

return err == nil
}

0 comments on commit 9b6bc04

Please sign in to comment.