@@ -8,9 +8,12 @@ import (
88 "path/filepath"
99 "testing"
1010
11+ "reflect"
12+
1113 "github.com/drycc/controller-sdk-go/api"
1214 "github.com/drycc/workflow-cli/pkg/testutil"
1315 "github.com/stretchr/testify/assert"
16+ "gopkg.in/yaml.v3"
1417)
1518
1619func TestParseProcfile (t * testing.T ) {
@@ -196,3 +199,140 @@ deploy:
196199 assert .Equal (t , testutil .StripProgress (b .String ()), "Creating build... done\n " , "output" )
197200
198201}
202+
203+ func TestBuildsFetch (t * testing.T ) {
204+ t .Parallel ()
205+
206+ // Mock server setup
207+ cf , server , err := testutil .NewTestServerAndClient ()
208+ if err != nil {
209+ t .Fatal (err )
210+ }
211+ defer server .Close ()
212+
213+ cmdr := DryccCmd {WOut : new (bytes.Buffer ), ConfigFile : cf }
214+
215+ // Mock build data
216+ server .Mux .HandleFunc ("/v2/apps/testapp/build/" , func (w http.ResponseWriter , _ * http.Request ) {
217+ testutil .SetHeaders (w )
218+ fmt .Fprintf (w , `{
219+ "app": "testapp",
220+ "procfile": {"web": "node server.js"},
221+ "dryccfile": {
222+ "pipeline": {
223+ "web.yaml": {
224+ "kind": "pipeline",
225+ "ptype": "web",
226+ "deploy": {
227+ "command": ["bash", "-c"],
228+ "args": ["echo hello"]
229+ }
230+ }
231+ },
232+ "config": {
233+ "env1.env": {"KEY1": "VALUE1"},
234+ "env2.env": {"KEY2": "VALUE2"}
235+ }
236+ }
237+ }` )
238+ })
239+
240+ // Helper function to compare YAML content ignoring field order
241+ isEqualYAML := func (actual , expected string ) bool {
242+ var actualMap , expectedMap map [string ]interface {}
243+ err := yaml .Unmarshal ([]byte (actual ), & actualMap )
244+ if err != nil {
245+ return false
246+ }
247+ err = yaml .Unmarshal ([]byte (expected ), & expectedMap )
248+ if err != nil {
249+ return false
250+ }
251+ return reflect .DeepEqual (actualMap , expectedMap )
252+ }
253+
254+ // Test case 1: Successful fetch with valid confirm
255+ t .Run ("Successful Fetch" , func (t * testing.T ) {
256+ tmpDir := t .TempDir () // Create a unique temporary directory for this test
257+ procfilePath := filepath .Join (tmpDir , "Procfile" )
258+ dryccpath := filepath .Join (tmpDir , ".drycc" )
259+
260+ // Mock user input for confirmation
261+ restoreStdin := os .Stdin
262+ r , w , _ := os .Pipe ()
263+ os .Stdin = r
264+ go func () {
265+ defer w .Close () // Ensure the pipe is closed after writing
266+ w .Write ([]byte ("yes\n " ))
267+ }()
268+
269+ err := cmdr .BuildsFetch ("testapp" , 0 , procfilePath , dryccpath , "" )
270+ if err != nil {
271+ t .Fatalf ("expected no error, got %v" , err )
272+ }
273+
274+ // Verify Procfile content
275+ content , err := os .ReadFile (procfilePath )
276+ if err != nil {
277+ t .Fatalf ("failed to read Procfile: %v" , err )
278+ }
279+ expectedProcfile := "web: node server.js\n "
280+ if string (content ) != expectedProcfile {
281+ t .Errorf ("expected Procfile content %q, got %q" , expectedProcfile , string (content ))
282+ }
283+
284+ // Verify .drycc/config/env1.env content
285+ env1Content , err := os .ReadFile (filepath .Join (dryccpath , "config" , "env1.env" ))
286+ if err != nil {
287+ t .Fatalf ("failed to read env1.env: %v" , err )
288+ }
289+ expectedEnv1 := "KEY1=VALUE1\n "
290+ if string (env1Content ) != expectedEnv1 {
291+ t .Errorf ("expected env1.env content %q, got %q" , expectedEnv1 , string (env1Content ))
292+ }
293+
294+ // Verify .drycc/web.yaml content
295+ webYamlContent , err := os .ReadFile (filepath .Join (dryccpath , "web.yaml" ))
296+ if err != nil {
297+ t .Fatalf ("failed to read web.yaml: %v" , err )
298+ }
299+ expectedWebYaml := "kind: pipeline\n ptype: web\n deploy:\n command:\n - bash\n - -c\n args:\n - echo hello\n "
300+ if ! isEqualYAML (string (webYamlContent ), expectedWebYaml ) {
301+ t .Errorf ("expected web.yaml content %q, got %q" , expectedWebYaml , string (webYamlContent ))
302+ }
303+
304+ os .Stdin = restoreStdin
305+ })
306+
307+ // Test case 2: User cancels the operation
308+ t .Run ("User Cancels Operation" , func (t * testing.T ) {
309+ tmpDir := t .TempDir () // Create a unique temporary directory for this test
310+ procfilePath := filepath .Join (tmpDir , "Procfile" )
311+ dryccpath := filepath .Join (tmpDir , ".drycc" )
312+
313+ // Mock user input for cancellation
314+ restoreStdin := os .Stdin
315+ r , w , _ := os .Pipe ()
316+ os .Stdin = r
317+ go func () {
318+ defer w .Close () // Ensure the pipe is closed after writing
319+ w .Write ([]byte ("no\n " ))
320+ }()
321+
322+ err := cmdr .BuildsFetch ("testapp" , 0 , procfilePath , dryccpath , "" )
323+ if err == nil || err .Error () != "cancel the build create fetch" {
324+ t .Fatalf ("expected cancellation error, got %v" , err )
325+ }
326+
327+ // Verify files were not created
328+ if _ , err := os .Stat (procfilePath ); ! os .IsNotExist (err ) {
329+ t .Errorf ("expected Procfile to not exist, but it does" )
330+ }
331+
332+ if _ , err := os .Stat (dryccpath ); ! os .IsNotExist (err ) {
333+ t .Errorf ("expected .drycc directory to not exist, but it does" )
334+ }
335+
336+ os .Stdin = restoreStdin
337+ })
338+ }
0 commit comments