@@ -14,34 +14,246 @@ import (
1414 "github.com/google/go-cmp/cmp"
1515)
1616
17- func TestCollectWeakDependencies (t * testing.T ) {
18- // Helper to create test packages
19- newPkg := func (name string , pkgType PackageType , packaging interface {}) * Package {
20- pkg := & Package {
21- C : & Component {
22- W : & Workspace {Packages : make (map [string ]* Package )},
23- Origin : "test" ,
24- Name : "test" ,
25- },
26- PackageInternal : PackageInternal {Name : name , Type : pkgType },
27- }
28- switch pkgType {
29- case GoPackage :
30- if p , ok := packaging .(GoPackaging ); ok {
31- pkg .Config = GoPkgConfig {Packaging : p }
32- }
33- case GenericPackage :
34- pkg .Config = GenericPkgConfig {}
17+ // Helper to create test packages for weak dep tests
18+ func newTestPkg (name string , pkgType PackageType , packaging interface {}) * Package {
19+ pkg := & Package {
20+ C : & Component {
21+ W : & Workspace {Packages : make (map [string ]* Package )},
22+ Origin : "test" ,
23+ Name : "test" ,
24+ },
25+ PackageInternal : PackageInternal {Name : name , Type : pkgType },
26+ }
27+ switch pkgType {
28+ case GoPackage :
29+ if p , ok := packaging .(GoPackaging ); ok {
30+ pkg .Config = GoPkgConfig {Packaging : p }
3531 }
36- return pkg
32+ case GenericPackage :
33+ pkg .Config = GenericPkgConfig {}
3734 }
35+ return pkg
36+ }
37+
38+ func TestGetEffectiveDependencies (t * testing.T ) {
39+ t .Run ("with flag enabled returns only hard deps" , func (t * testing.T ) {
40+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
41+ libA .dependencies = []* Package {}
42+ libA .weakDependencies = []* Package {}
43+
44+ libB := newTestPkg ("lib-b" , GoPackage , GoLibrary )
45+ libB .dependencies = []* Package {}
46+ libB .weakDependencies = []* Package {}
47+
48+ app := newTestPkg ("app" , GoPackage , GoApp )
49+ app .dependencies = []* Package {libA } // hard dep
50+ app .weakDependencies = []* Package {libB } // weak dep
51+
52+ ctx := & buildContext {
53+ buildOptions : buildOptions {GoLibraryWeakDeps : true },
54+ }
55+
56+ result := ctx .GetEffectiveDependencies (app )
57+
58+ if len (result ) != 1 {
59+ t .Errorf ("expected 1 dep (only hard), got %d" , len (result ))
60+ }
61+ if result [0 ].Name != "lib-a" {
62+ t .Errorf ("expected lib-a, got %s" , result [0 ].Name )
63+ }
64+ })
65+
66+ t .Run ("with flag disabled returns hard + weak deps" , func (t * testing.T ) {
67+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
68+ libA .dependencies = []* Package {}
69+ libA .weakDependencies = []* Package {}
70+
71+ libB := newTestPkg ("lib-b" , GoPackage , GoLibrary )
72+ libB .dependencies = []* Package {}
73+ libB .weakDependencies = []* Package {}
74+
75+ app := newTestPkg ("app" , GoPackage , GoApp )
76+ app .dependencies = []* Package {libA } // hard dep
77+ app .weakDependencies = []* Package {libB } // weak dep
78+
79+ ctx := & buildContext {
80+ buildOptions : buildOptions {GoLibraryWeakDeps : false },
81+ }
82+
83+ result := ctx .GetEffectiveDependencies (app )
84+
85+ if len (result ) != 2 {
86+ t .Errorf ("expected 2 deps (hard + weak), got %d" , len (result ))
87+ }
88+
89+ names := make (map [string ]bool )
90+ for _ , p := range result {
91+ names [p .Name ] = true
92+ }
93+ if ! names ["lib-a" ] || ! names ["lib-b" ] {
94+ t .Errorf ("expected lib-a and lib-b, got %v" , names )
95+ }
96+ })
97+
98+ t .Run ("with flag disabled and no weak deps returns only hard deps" , func (t * testing.T ) {
99+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
100+ libA .dependencies = []* Package {}
101+ libA .weakDependencies = []* Package {}
102+
103+ app := newTestPkg ("app" , GoPackage , GoApp )
104+ app .dependencies = []* Package {libA }
105+ app .weakDependencies = []* Package {}
106+
107+ ctx := & buildContext {
108+ buildOptions : buildOptions {GoLibraryWeakDeps : false },
109+ }
110+
111+ result := ctx .GetEffectiveDependencies (app )
112+
113+ if len (result ) != 1 {
114+ t .Errorf ("expected 1 dep, got %d" , len (result ))
115+ }
116+ if result [0 ].Name != "lib-a" {
117+ t .Errorf ("expected lib-a, got %s" , result [0 ].Name )
118+ }
119+ })
120+ }
121+
122+ func TestGetEffectiveTransitiveDependencies (t * testing.T ) {
123+ t .Run ("with flag enabled returns only hard transitive deps" , func (t * testing.T ) {
124+ // app -> libA (hard) -> libB (weak)
125+ libB := newTestPkg ("lib-b" , GoPackage , GoLibrary )
126+ libB .dependencies = []* Package {}
127+ libB .weakDependencies = []* Package {}
128+
129+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
130+ libA .dependencies = []* Package {}
131+ libA .weakDependencies = []* Package {libB }
132+
133+ app := newTestPkg ("app" , GoPackage , GoApp )
134+ app .dependencies = []* Package {libA }
135+ app .weakDependencies = []* Package {}
136+
137+ ctx := & buildContext {
138+ buildOptions : buildOptions {GoLibraryWeakDeps : true },
139+ }
38140
141+ result := ctx .GetEffectiveTransitiveDependencies (app )
142+
143+ // Should only include libA (hard dep), not libB (weak dep of libA)
144+ if len (result ) != 1 {
145+ t .Errorf ("expected 1 transitive dep, got %d: %v" , len (result ), result )
146+ }
147+ if result [0 ].Name != "lib-a" {
148+ t .Errorf ("expected lib-a, got %s" , result [0 ].Name )
149+ }
150+ })
151+
152+ t .Run ("with flag disabled returns hard + weak transitive deps" , func (t * testing.T ) {
153+ // app -> libA (hard) -> libB (weak)
154+ libB := newTestPkg ("lib-b" , GoPackage , GoLibrary )
155+ libB .dependencies = []* Package {}
156+ libB .weakDependencies = []* Package {}
157+
158+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
159+ libA .dependencies = []* Package {}
160+ libA .weakDependencies = []* Package {libB }
161+
162+ app := newTestPkg ("app" , GoPackage , GoApp )
163+ app .dependencies = []* Package {libA }
164+ app .weakDependencies = []* Package {}
165+
166+ ctx := & buildContext {
167+ buildOptions : buildOptions {GoLibraryWeakDeps : false },
168+ }
169+
170+ result := ctx .GetEffectiveTransitiveDependencies (app )
171+
172+ // Should include both libA and libB
173+ if len (result ) != 2 {
174+ t .Errorf ("expected 2 transitive deps, got %d: %v" , len (result ), result )
175+ }
176+
177+ names := make (map [string ]bool )
178+ for _ , p := range result {
179+ names [p .Name ] = true
180+ }
181+ if ! names ["lib-a" ] || ! names ["lib-b" ] {
182+ t .Errorf ("expected lib-a and lib-b, got %v" , names )
183+ }
184+ })
185+
186+ t .Run ("with flag disabled includes direct weak deps" , func (t * testing.T ) {
187+ // app -> libA (weak)
188+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
189+ libA .dependencies = []* Package {}
190+ libA .weakDependencies = []* Package {}
191+
192+ app := newTestPkg ("app" , GoPackage , GoApp )
193+ app .dependencies = []* Package {}
194+ app .weakDependencies = []* Package {libA }
195+
196+ ctx := & buildContext {
197+ buildOptions : buildOptions {GoLibraryWeakDeps : false },
198+ }
199+
200+ result := ctx .GetEffectiveTransitiveDependencies (app )
201+
202+ // Should include libA even though it's a weak dep
203+ if len (result ) != 1 {
204+ t .Errorf ("expected 1 transitive dep, got %d: %v" , len (result ), result )
205+ }
206+ if result [0 ].Name != "lib-a" {
207+ t .Errorf ("expected lib-a, got %s" , result [0 ].Name )
208+ }
209+ })
210+
211+ t .Run ("with flag disabled handles deep transitive weak deps" , func (t * testing.T ) {
212+ // app -> libA (weak) -> libB (weak) -> libC (weak)
213+ libC := newTestPkg ("lib-c" , GoPackage , GoLibrary )
214+ libC .dependencies = []* Package {}
215+ libC .weakDependencies = []* Package {}
216+
217+ libB := newTestPkg ("lib-b" , GoPackage , GoLibrary )
218+ libB .dependencies = []* Package {}
219+ libB .weakDependencies = []* Package {libC }
220+
221+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
222+ libA .dependencies = []* Package {}
223+ libA .weakDependencies = []* Package {libB }
224+
225+ app := newTestPkg ("app" , GoPackage , GoApp )
226+ app .dependencies = []* Package {}
227+ app .weakDependencies = []* Package {libA }
228+
229+ ctx := & buildContext {
230+ buildOptions : buildOptions {GoLibraryWeakDeps : false },
231+ }
232+
233+ result := ctx .GetEffectiveTransitiveDependencies (app )
234+
235+ // Should include all: libA, libB, libC
236+ if len (result ) != 3 {
237+ t .Errorf ("expected 3 transitive deps, got %d: %v" , len (result ), result )
238+ }
239+
240+ names := make (map [string ]bool )
241+ for _ , p := range result {
242+ names [p .Name ] = true
243+ }
244+ if ! names ["lib-a" ] || ! names ["lib-b" ] || ! names ["lib-c" ] {
245+ t .Errorf ("expected lib-a, lib-b, and lib-c, got %v" , names )
246+ }
247+ })
248+ }
249+
250+ func TestCollectWeakDependencies (t * testing.T ) {
39251 t .Run ("collects direct weak deps" , func (t * testing.T ) {
40- libA := newPkg ("lib-a" , GoPackage , GoLibrary )
252+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
41253 libA .dependencies = []* Package {}
42254 libA .weakDependencies = []* Package {}
43255
44- app := newPkg ("app" , GoPackage , GoApp )
256+ app := newTestPkg ("app" , GoPackage , GoApp )
45257 app .dependencies = []* Package {}
46258 app .weakDependencies = []* Package {libA }
47259
@@ -57,15 +269,15 @@ func TestCollectWeakDependencies(t *testing.T) {
57269
58270 t .Run ("collects nested weak deps (weak dep of weak dep)" , func (t * testing.T ) {
59271 // lib-b depends on lib-a (both are Go libraries)
60- libA := newPkg ("lib-a" , GoPackage , GoLibrary )
272+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
61273 libA .dependencies = []* Package {}
62274 libA .weakDependencies = []* Package {}
63275
64- libB := newPkg ("lib-b" , GoPackage , GoLibrary )
276+ libB := newTestPkg ("lib-b" , GoPackage , GoLibrary )
65277 libB .dependencies = []* Package {}
66278 libB .weakDependencies = []* Package {libA } // lib-a is weak dep of lib-b
67279
68- app := newPkg ("app" , GoPackage , GoApp )
280+ app := newTestPkg ("app" , GoPackage , GoApp )
69281 app .dependencies = []* Package {}
70282 app .weakDependencies = []* Package {libB }
71283
@@ -87,15 +299,15 @@ func TestCollectWeakDependencies(t *testing.T) {
87299
88300 t .Run ("collects weak deps from hard deps" , func (t * testing.T ) {
89301 // app -> hard-dep -> lib-a (weak)
90- libA := newPkg ("lib-a" , GoPackage , GoLibrary )
302+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
91303 libA .dependencies = []* Package {}
92304 libA .weakDependencies = []* Package {}
93305
94- hardDep := newPkg ("hard-dep" , GoPackage , GoApp )
306+ hardDep := newTestPkg ("hard-dep" , GoPackage , GoApp )
95307 hardDep .dependencies = []* Package {}
96308 hardDep .weakDependencies = []* Package {libA }
97309
98- app := newPkg ("app" , GoPackage , GoApp )
310+ app := newTestPkg ("app" , GoPackage , GoApp )
99311 app .dependencies = []* Package {hardDep }
100312 app .weakDependencies = []* Package {}
101313
@@ -112,15 +324,15 @@ func TestCollectWeakDependencies(t *testing.T) {
112324
113325 t .Run ("no duplicates" , func (t * testing.T ) {
114326 // Both app and hard-dep depend on lib-a
115- libA := newPkg ("lib-a" , GoPackage , GoLibrary )
327+ libA := newTestPkg ("lib-a" , GoPackage , GoLibrary )
116328 libA .dependencies = []* Package {}
117329 libA .weakDependencies = []* Package {}
118330
119- hardDep := newPkg ("hard-dep" , GoPackage , GoApp )
331+ hardDep := newTestPkg ("hard-dep" , GoPackage , GoApp )
120332 hardDep .dependencies = []* Package {}
121333 hardDep .weakDependencies = []* Package {libA }
122334
123- app := newPkg ("app" , GoPackage , GoApp )
335+ app := newTestPkg ("app" , GoPackage , GoApp )
124336 app .dependencies = []* Package {hardDep }
125337 app .weakDependencies = []* Package {libA } // also directly depends on lib-a
126338
0 commit comments