@@ -72,6 +72,101 @@ func TestVolumeSet(t *testing.T) {
7272 }
7373}
7474
75+ func TestByName (t * testing.T ) {
76+ n , err := ParseFile ("dockerclient/testdata/Dockerfile.target" )
77+ if err != nil {
78+ t .Fatal (err )
79+ }
80+ stages , err := NewStages (n , NewBuilder (nil ))
81+ if err != nil {
82+ t .Fatal (err )
83+ }
84+ if len (stages ) != 4 {
85+ t .Fatalf ("expected 4 stages, got %d" , len (stages ))
86+ }
87+ t .Logf ("stages: %#v" , stages )
88+
89+ stage1 , found := stages .ByName ("mytarget" )
90+ if ! found {
91+ t .Fatal ("First target not found" )
92+ }
93+ if stage1 .Position != 1 {
94+ t .Fatalf ("expected stage at position 1, got %d" , stage1 .Position )
95+ }
96+ t .Logf ("stage1: %#v" , stage1 )
97+
98+ stage2 , found := stages .ByName ("mytarget2" )
99+ if ! found {
100+ t .Fatal ("Second target not found" )
101+ }
102+ if stage2 .Position != 2 {
103+ t .Fatalf ("expected stage at position 2, got %d" , stage1 .Position )
104+ }
105+ t .Logf ("stage2: %#v" , stage2 )
106+
107+ stage3 , found := stages .ByName ("1" )
108+ if ! found {
109+ t .Fatal ("Third target not found" )
110+ }
111+ if stage3 .Position != 1 {
112+ t .Fatalf ("expected stage at position 1, got %d" , stage3 .Position )
113+ }
114+ t .Logf ("stage3: %#v" , stage3 )
115+ assert .Equal (t , stage3 , stage1 )
116+
117+ stage4 , found := stages .ByName ("2" )
118+ if ! found {
119+ t .Fatal ("Fourth target not found" )
120+ }
121+ if stage4 .Position != 2 {
122+ t .Fatalf ("expected stage at position 2, got %d" , stage4 .Position )
123+ }
124+ t .Logf ("stage4: %#v" , stage4 )
125+ assert .Equal (t , stage4 , stage2 )
126+
127+ stage5 , found := stages .ByName ("mytarget3" )
128+ if ! found {
129+ t .Fatal ("Fifth target not found" )
130+ }
131+ if stage5 .Position != 3 {
132+ t .Fatalf ("expected stage at position 3, got %d" , stage5 .Position )
133+ }
134+ t .Logf ("stage5: %#v" , stage5 )
135+
136+ stage6 , found := stages .ByName ("3" )
137+ if ! found {
138+ t .Fatal ("Sixth target not found" )
139+ }
140+ if stage6 .Position != 3 {
141+ t .Fatalf ("expected stage at position 3, got %d" , stage6 .Position )
142+ }
143+ t .Logf ("stage6: %#v" , stage6 )
144+ assert .Equal (t , stage6 , stage5 )
145+
146+ n , err = ParseFile ("dockerclient/testdata/Dockerfile.target" )
147+ if err != nil {
148+ t .Fatal (err )
149+ }
150+ stages , err = NewStages (n , NewBuilder (map [string ]string {"TARGET3" : "mytarget" }))
151+ if err != nil {
152+ t .Fatal (err )
153+ }
154+ if len (stages ) != 4 {
155+ t .Fatalf ("expected 4 stage, got %d" , len (stages ))
156+ }
157+ t .Logf ("stages: %#v" , stages )
158+
159+ stage7 , found := stages .ByName ("mytarget" )
160+ if ! found {
161+ t .Fatal ("Seventh target not found" )
162+ }
163+ if stage7 .Position != 3 {
164+ t .Fatalf ("expected stage at position 3, got %d" , stage7 .Position )
165+ }
166+ t .Logf ("stage7: %#v" , stage7 )
167+
168+ }
169+
75170func TestByTarget (t * testing.T ) {
76171 n , err := ParseFile ("dockerclient/testdata/Dockerfile.target" )
77172 if err != nil {
@@ -93,6 +188,9 @@ func TestByTarget(t *testing.T) {
93188 if len (stages1 ) != 1 {
94189 t .Fatalf ("expected 1 stages, got %d" , len (stages1 ))
95190 }
191+ if stages1 [0 ].Position != 1 {
192+ t .Fatalf ("expected stage at position 1, got %d" , stages1 [0 ].Position )
193+ }
96194 t .Logf ("stages1: %#v" , stages1 )
97195
98196 stages2 , found := stages .ByTarget ("mytarget2" )
@@ -138,10 +236,36 @@ func TestByTarget(t *testing.T) {
138236 t .Fatal ("Sixth target not found" )
139237 }
140238 if len (stages6 ) != 1 {
141- t .Fatalf ("expected 1 stages, got %d" , len (stages4 ))
239+ t .Fatalf ("expected 1 stages, got %d" , len (stages6 ))
142240 }
143241 t .Logf ("stages6: %#v" , stages6 )
144242 assert .Equal (t , stages6 , stages5 )
243+
244+ n , err = ParseFile ("dockerclient/testdata/Dockerfile.target" )
245+ if err != nil {
246+ t .Fatal (err )
247+ }
248+ stages , err = NewStages (n , NewBuilder (map [string ]string {"TARGET3" : "mytarget" }))
249+ if err != nil {
250+ t .Fatal (err )
251+ }
252+ if len (stages ) != 4 {
253+ t .Fatalf ("expected 4 stages, got %d" , len (stages ))
254+ }
255+ t .Logf ("stages: %#v" , stages )
256+
257+ stages7 , found := stages .ByTarget ("mytarget" )
258+ if ! found {
259+ t .Fatal ("Seventh target not found" )
260+ }
261+ if len (stages7 ) != 1 {
262+ t .Fatalf ("expected 1 stages, got %d" , len (stages7 ))
263+ }
264+ if stages7 [0 ].Position != 3 {
265+ t .Fatalf ("expected stage at position 3, got %d" , stages7 [0 ].Position )
266+ }
267+ t .Logf ("stages7: %#v" , stages7 )
268+
145269}
146270
147271func TestThroughTarget (t * testing.T ) {
@@ -214,6 +338,29 @@ func TestThroughTarget(t *testing.T) {
214338 }
215339 t .Logf ("stages6: %#v" , stages6 )
216340 assert .Equal (t , stages6 , stages5 )
341+
342+ n , err = ParseFile ("dockerclient/testdata/Dockerfile.target" )
343+ if err != nil {
344+ t .Fatal (err )
345+ }
346+
347+ stages , err = NewStages (n , NewBuilder (map [string ]string {"TARGET3" : "mytarget" }))
348+ if err != nil {
349+ t .Fatal (err )
350+ }
351+ if len (stages ) != 4 {
352+ t .Fatalf ("expected 4 stages, got %d" , len (stages ))
353+ }
354+ t .Logf ("stages: %#v" , stages )
355+
356+ stages7 , found := stages .ThroughTarget ("mytarget" )
357+ if ! found {
358+ t .Fatal ("Seventh target not found" )
359+ }
360+ if len (stages7 ) != 4 {
361+ t .Fatalf ("expected 4 stages, got %d" , len (stages7 ))
362+ }
363+ t .Logf ("stages7: %#v" , stages7 )
217364}
218365
219366func TestMultiStageParse (t * testing.T ) {
0 commit comments