44 "bytes"
55 "context"
66 "testing"
7+ "testing/fstest"
78
89 "github.com/planetscale/cli/internal/cmdutil"
910 "github.com/planetscale/cli/internal/config"
@@ -33,11 +34,167 @@ func TestOrganization_ListCmd(t *testing.T) {
3334 },
3435 }
3536
37+ fs := fstest.MapFS {
38+ ".pscale.yml" : & fstest.MapFile {
39+ Data : []byte ("org: " + org + "\n " ),
40+ },
41+ }
42+
3643 ch := & cmdutil.Helper {
3744 Printer : p ,
3845 Config : & config.Config {
3946 Organization : org ,
4047 },
48+ ConfigFS : config .NewConfigFS (fs ),
49+ Client : func () (* ps.Client , error ) {
50+ return & ps.Client {
51+ Organizations : svc ,
52+ }, nil
53+ },
54+ }
55+
56+ cmd := ListCmd (ch )
57+ err := cmd .Execute ()
58+
59+ c .Assert (err , qt .IsNil )
60+ c .Assert (svc .ListFnInvoked , qt .IsTrue )
61+
62+ orgs := []* organization {
63+ {Name : "foo" , Current : false },
64+ {Name : "bar" , Current : false },
65+ }
66+ c .Assert (buf .String (), qt .JSONEquals , orgs )
67+ }
68+
69+ func TestOrganization_ListCmd_WithCurrentOrg (t * testing.T ) {
70+ c := qt .New (t )
71+
72+ var buf bytes.Buffer
73+ format := printer .JSON
74+ p := printer .NewPrinter (& format )
75+ p .SetResourceOutput (& buf )
76+
77+ currentOrg := "foo"
78+
79+ svc := & mock.OrganizationsService {
80+ ListFn : func (ctx context.Context ) ([]* ps.Organization , error ) {
81+ return []* ps.Organization {
82+ {Name : "foo" },
83+ {Name : "bar" },
84+ }, nil
85+ },
86+ }
87+
88+ fs := fstest.MapFS {
89+ ".pscale.yml" : & fstest.MapFile {
90+ Data : []byte ("org: " + currentOrg + "\n " ),
91+ },
92+ }
93+
94+ ch := & cmdutil.Helper {
95+ Printer : p ,
96+ Config : & config.Config {
97+ Organization : currentOrg ,
98+ },
99+ ConfigFS : config .NewConfigFS (fs ),
100+ Client : func () (* ps.Client , error ) {
101+ return & ps.Client {
102+ Organizations : svc ,
103+ }, nil
104+ },
105+ }
106+
107+ cmd := ListCmd (ch )
108+ err := cmd .Execute ()
109+
110+ c .Assert (err , qt .IsNil )
111+ c .Assert (svc .ListFnInvoked , qt .IsTrue )
112+
113+ orgs := []* organization {
114+ {Name : "foo" , Current : true },
115+ {Name : "bar" , Current : false },
116+ }
117+ c .Assert (buf .String (), qt .JSONEquals , orgs )
118+ }
119+
120+ func TestOrganization_ListCmd_HumanFormat (t * testing.T ) {
121+ c := qt .New (t )
122+
123+ var buf bytes.Buffer
124+ format := printer .Human
125+ p := printer .NewPrinter (& format )
126+ p .SetResourceOutput (& buf )
127+
128+ currentOrg := "foo"
129+
130+ svc := & mock.OrganizationsService {
131+ ListFn : func (ctx context.Context ) ([]* ps.Organization , error ) {
132+ return []* ps.Organization {
133+ {Name : "foo" },
134+ {Name : "bar" },
135+ }, nil
136+ },
137+ }
138+
139+ fs := fstest.MapFS {
140+ ".pscale.yml" : & fstest.MapFile {
141+ Data : []byte ("org: " + currentOrg + "\n " ),
142+ },
143+ }
144+
145+ ch := & cmdutil.Helper {
146+ Printer : p ,
147+ Config : & config.Config {
148+ Organization : currentOrg ,
149+ },
150+ ConfigFS : config .NewConfigFS (fs ),
151+ Client : func () (* ps.Client , error ) {
152+ return & ps.Client {
153+ Organizations : svc ,
154+ }, nil
155+ },
156+ }
157+
158+ cmd := ListCmd (ch )
159+ err := cmd .Execute ()
160+
161+ c .Assert (err , qt .IsNil )
162+ c .Assert (svc .ListFnInvoked , qt .IsTrue )
163+
164+ // For human format, the current org should have an asterisk prefix
165+ output := buf .String ()
166+ c .Assert (output , qt .Contains , "* foo" )
167+ c .Assert (output , qt .Contains , "bar" )
168+
169+ c .Assert (output , qt .Not (qt .Contains ), "* bar" )
170+ }
171+
172+ func TestOrganization_ListCmd_NoConfig (t * testing.T ) {
173+ c := qt .New (t )
174+
175+ var buf bytes.Buffer
176+ format := printer .JSON
177+ p := printer .NewPrinter (& format )
178+ p .SetResourceOutput (& buf )
179+
180+ svc := & mock.OrganizationsService {
181+ ListFn : func (ctx context.Context ) ([]* ps.Organization , error ) {
182+ return []* ps.Organization {
183+ {Name : "foo" },
184+ {Name : "bar" },
185+ }, nil
186+ },
187+ }
188+
189+ // Create an empty filesystem (no config file)
190+ fs := fstest.MapFS {}
191+
192+ ch := & cmdutil.Helper {
193+ Printer : p ,
194+ Config : & config.Config {
195+ Organization : "" ,
196+ },
197+ ConfigFS : config .NewConfigFS (fs ),
41198 Client : func () (* ps.Client , error ) {
42199 return & ps.Client {
43200 Organizations : svc ,
@@ -52,8 +209,8 @@ func TestOrganization_ListCmd(t *testing.T) {
52209 c .Assert (svc .ListFnInvoked , qt .IsTrue )
53210
54211 orgs := []* organization {
55- {Name : "foo" },
56- {Name : "bar" },
212+ {Name : "foo" , Current : false },
213+ {Name : "bar" , Current : false },
57214 }
58215 c .Assert (buf .String (), qt .JSONEquals , orgs )
59216}
0 commit comments