@@ -2,10 +2,11 @@ package v5
22
33import (
44 "encoding/json"
5- "github.com/murphysecurity/murphysec/utils/must"
6- "github.com/stretchr/testify/assert"
75 "io"
86 "testing"
7+
8+ "github.com/murphysecurity/murphysec/utils/must"
9+ "github.com/stretchr/testify/assert"
910)
1011
1112func TestLockfile (t * testing.T ) {
@@ -33,3 +34,61 @@ func TestBuildDepTree(t *testing.T) {
3334 assert .NotNil (t , tree )
3435 t .Log (string (must .A (json .MarshalIndent (tree , "| " , " " ))))
3536}
37+
38+ func TestParseLockfile_ConsistentAcrossMultipleRuns (t * testing.T ) {
39+ files , _ := testFiles .ReadDir ("testdata" )
40+ assert .NotEmpty (t , files )
41+ for _ , s := range files {
42+ t .Run (s .Name (), func (t * testing.T ) {
43+ f , e := testFiles .Open ("testdata/" + s .Name ())
44+ assert .NoError (t , e )
45+ defer func () { assert .NoError (t , f .Close ()) }()
46+ data , e := io .ReadAll (f )
47+ assert .NoError (t , e )
48+
49+ var baseline []byte
50+ for i := 0 ; i < 10 ; i ++ {
51+ lockfile , e := ParseLockfile (data )
52+ assert .NoError (t , e )
53+ assert .NotNil (t , lockfile )
54+ got , e := json .Marshal (lockfile )
55+ assert .NoError (t , e )
56+ if i == 0 {
57+ baseline = got
58+ continue
59+ }
60+ assert .Equal (t , string (baseline ), string (got ), "parse result changed at run %d" , i + 1 )
61+ }
62+ })
63+ }
64+ }
65+
66+ func TestBuildDepTree_AllTestdata (t * testing.T ) {
67+ files , _ := testFiles .ReadDir ("testdata" )
68+ assert .NotEmpty (t , files )
69+ for _ , s := range files {
70+ t .Run (s .Name (), func (t * testing.T ) {
71+ f , e := testFiles .Open ("testdata/" + s .Name ())
72+ assert .NoError (t , e )
73+ defer func () { assert .NoError (t , f .Close ()) }()
74+ data , e := io .ReadAll (f )
75+ assert .NoError (t , e )
76+
77+ lockfile , e := ParseLockfile (data )
78+ assert .NoError (t , e )
79+ assert .NotNil (t , lockfile )
80+
81+ tree := BuildDepTree (lockfile , nil , "" )
82+ if len (lockfile .Dependencies ) > 0 || len (lockfile .DevDependencies ) > 0 {
83+ assert .NotNil (t , tree )
84+ }
85+
86+ for importerName , importer := range lockfile .Importers {
87+ importerTree := BuildDepTree (lockfile , importer , importerName )
88+ if importerTree != nil {
89+ assert .Equal (t , importerName , importerTree .Name )
90+ }
91+ }
92+ })
93+ }
94+ }
0 commit comments