1717package compose
1818
1919import (
20+ "io"
2021 "net"
2122 "net/netip"
2223 "os"
2324 "path/filepath"
2425 "sort"
26+ "strings"
2527 "testing"
2628
2729 composeloader "github.com/compose-spec/compose-go/v2/loader"
@@ -35,6 +37,8 @@ import (
3537 "gotest.tools/v3/assert"
3638 "gotest.tools/v3/assert/cmp"
3739
40+ "github.com/docker/cli/cli/streams"
41+ "github.com/docker/compose/v5/cmd/prompt"
3842 "github.com/docker/compose/v5/pkg/api"
3943)
4044
@@ -484,3 +488,81 @@ volumes:
484488 })
485489 }
486490}
491+
492+ func Test_composeService_confirmVolumeRecreate (t * testing.T ) {
493+ tests := []struct {
494+ name string
495+ labels map [string ]string
496+ input string
497+ want bool
498+ wantErr bool
499+ }{
500+ {
501+ name : "no labels no input" ,
502+ labels : nil ,
503+ input : "" ,
504+ want : false ,
505+ wantErr : false ,
506+ },
507+ {
508+ name : "no labels and input is y" ,
509+ labels : nil ,
510+ input : "y" ,
511+ want : true ,
512+ wantErr : false ,
513+ },
514+ {
515+ name : "no labels and input is true" ,
516+ labels : nil ,
517+ input : "true" ,
518+ want : true ,
519+ wantErr : false ,
520+ },
521+ {
522+ name : "no labels and input is no" ,
523+ labels : nil ,
524+ input : "no" ,
525+ want : false ,
526+ wantErr : false ,
527+ },
528+ {
529+ name : "no input, has labels recreate true" ,
530+ labels : map [string ]string {api .VolumeRecreateWhenSpecUpdatedLabel : "true" },
531+ want : true ,
532+ wantErr : false ,
533+ },
534+ {
535+ name : "no input, has labels recreate TRUE" ,
536+ labels : map [string ]string {api .VolumeRecreateWhenSpecUpdatedLabel : "TRUE" },
537+ want : true ,
538+ wantErr : false ,
539+ },
540+ {
541+ name : "no input, has labels recreate false" ,
542+ labels : map [string ]string {api .VolumeRecreateWhenSpecUpdatedLabel : "false" },
543+ want : false ,
544+ wantErr : false ,
545+ },
546+ }
547+ for _ , tt := range tests {
548+ t .Run (tt .name , func (t * testing.T ) {
549+ prompt := prompt .NewPrompt (
550+ streams .NewIn (io .NopCloser (strings .NewReader (tt .input ))),
551+ streams .NewOut (t .Output ())).Confirm
552+
553+ got , gotErr := confirmVolumeRecreate (tt .labels , prompt , "promptMsg" )
554+ if gotErr != nil {
555+ if ! tt .wantErr {
556+ t .Errorf ("confirmVolumeRecreate() failed: %v" , gotErr )
557+ }
558+ return
559+ }
560+ if tt .wantErr {
561+ t .Fatal ("confirmVolumeRecreate() succeeded unexpectedly" )
562+ }
563+ if tt .want != got {
564+ t .Errorf ("confirmVolumeRecreate() = %v, want %v" , got , tt .want )
565+ }
566+ })
567+ }
568+ }
0 commit comments