@@ -136,20 +136,21 @@ func TestBuildCliCommands(t *testing.T) {
136136 )
137137
138138 testCases := []struct {
139- name string
140- cfg * Config
141- wantCmd []string
142- wantHostCmd []string
139+ name string
140+ cfg * Config
141+ wantForceCmd []string
142+ wantProfileCmd []string
143+ wantHostCmd []string
143144 }{
144145 {
145- name : "workspace host" ,
146- cfg : & Config {Host : host },
147- wantCmd : []string {cliPath , "auth" , "token" , "--host" , host },
146+ name : "workspace host only " ,
147+ cfg : & Config {Host : host },
148+ wantHostCmd : []string {cliPath , "auth" , "token" , "--host" , host },
148149 },
149150 {
150- name : "account host" ,
151- cfg : & Config {Host : accountHost , AccountID : accountID },
152- wantCmd : []string {cliPath , "auth" , "token" , "--host" , accountHost , "--account-id" , accountID },
151+ name : "account host only " ,
152+ cfg : & Config {Host : accountHost , AccountID : accountID },
153+ wantHostCmd : []string {cliPath , "auth" , "token" , "--host" , accountHost , "--account-id" , accountID },
153154 },
154155 {
155156 name : "former unified host treated as workspace" ,
@@ -158,26 +159,31 @@ func TestBuildCliCommands(t *testing.T) {
158159 AccountID : accountID ,
159160 WorkspaceID : workspaceID ,
160161 },
161- wantCmd : []string {cliPath , "auth" , "token" , "--host" , unifiedHost },
162+ wantHostCmd : []string {cliPath , "auth" , "token" , "--host" , unifiedHost },
162163 },
163164 {
164- name : "profile uses --profile with --host fallback" ,
165- cfg : & Config {Profile : "my-profile" , Host : host },
166- wantCmd : []string {cliPath , "auth" , "token" , "--profile" , "my-profile" },
167- wantHostCmd : []string {cliPath , "auth" , "token" , "--host" , host },
165+ name : "profile with host — all three commands" ,
166+ cfg : & Config {Profile : "my-profile" , Host : host },
167+ wantForceCmd : []string {cliPath , "auth" , "token" , "--profile" , "my-profile" , "--force-refresh" },
168+ wantProfileCmd : []string {cliPath , "auth" , "token" , "--profile" , "my-profile" },
169+ wantHostCmd : []string {cliPath , "auth" , "token" , "--host" , host },
168170 },
169171 {
170- name : "profile without host — no fallback" ,
171- cfg : & Config {Profile : "my-profile" },
172- wantCmd : []string {cliPath , "auth" , "token" , "--profile" , "my-profile" },
172+ name : "profile without host — no host fallback" ,
173+ cfg : & Config {Profile : "my-profile" },
174+ wantForceCmd : []string {cliPath , "auth" , "token" , "--profile" , "my-profile" , "--force-refresh" },
175+ wantProfileCmd : []string {cliPath , "auth" , "token" , "--profile" , "my-profile" },
173176 },
174177 }
175178
176179 for _ , tc := range testCases {
177180 t .Run (tc .name , func (t * testing.T ) {
178- gotCmd , gotHostCmd := buildCliCommands (cliPath , tc .cfg )
179- if ! slices .Equal (gotCmd , tc .wantCmd ) {
180- t .Errorf ("primary cmd = %v, want %v" , gotCmd , tc .wantCmd )
181+ gotForceCmd , gotProfileCmd , gotHostCmd := buildCliCommands (cliPath , tc .cfg )
182+ if ! slices .Equal (gotForceCmd , tc .wantForceCmd ) {
183+ t .Errorf ("force cmd = %v, want %v" , gotForceCmd , tc .wantForceCmd )
184+ }
185+ if ! slices .Equal (gotProfileCmd , tc .wantProfileCmd ) {
186+ t .Errorf ("profile cmd = %v, want %v" , gotProfileCmd , tc .wantProfileCmd )
181187 }
182188 if ! slices .Equal (gotHostCmd , tc .wantHostCmd ) {
183189 t .Errorf ("host cmd = %v, want %v" , gotHostCmd , tc .wantHostCmd )
@@ -204,9 +210,8 @@ func TestNewCliTokenSource(t *testing.T) {
204210 if err != nil {
205211 t .Fatalf ("NewCliTokenSource() unexpected error: %v" , err )
206212 }
207- // Verify CLI path was resolved and used
208- if ts .cmd [0 ] != validCliPath {
209- t .Errorf ("cmd[0] = %q, want %q" , ts .cmd [0 ], validCliPath )
213+ if ts .hostCmd [0 ] != validCliPath {
214+ t .Errorf ("hostCmd[0] = %q, want %q" , ts .hostCmd [0 ], validCliPath )
210215 }
211216 })
212217
@@ -262,7 +267,7 @@ func TestCliTokenSource_Token(t *testing.T) {
262267 t .Fatalf ("failed to create mock script: %v" , err )
263268 }
264269
265- ts := & CliTokenSource {cmd : []string {mockScript }}
270+ ts := & CliTokenSource {hostCmd : []string {mockScript }}
266271 token , err := ts .Token (context .Background ())
267272
268273 if tc .wantErrMsg != "" {
@@ -281,42 +286,122 @@ func TestCliTokenSource_Token(t *testing.T) {
281286 }
282287}
283288
284- func TestCliTokenSource_Token_FallbackOnUnknownFlag (t * testing.T ) {
289+ func TestCliTokenSource_Token_ForceRefreshFallbackToProfile (t * testing.T ) {
290+ if runtime .GOOS == "windows" {
291+ t .Skip ("Skipping shell script test on Windows" )
292+ }
293+
294+ expiry := time .Now ().Add (1 * time .Hour ).Format (time .RFC3339 )
295+ validResponse , _ := json .Marshal (cliTokenResponse {
296+ AccessToken : "profile-token" ,
297+ TokenType : "Bearer" ,
298+ Expiry : expiry ,
299+ })
300+
301+ tempDir := t .TempDir ()
302+
303+ forceScript := filepath .Join (tempDir , "force_cli.sh" )
304+ if err := os .WriteFile (forceScript , []byte ("#!/bin/sh\n echo 'Error: unknown flag: --force-refresh' >&2\n exit 1" ), 0755 ); err != nil {
305+ t .Fatalf ("failed to create force script: %v" , err )
306+ }
307+
308+ profileScript := filepath .Join (tempDir , "profile_cli.sh" )
309+ if err := os .WriteFile (profileScript , []byte ("#!/bin/sh\n echo '" + string (validResponse )+ "'" ), 0755 ); err != nil {
310+ t .Fatalf ("failed to create profile script: %v" , err )
311+ }
312+
313+ ts := & CliTokenSource {
314+ forceCmd : []string {forceScript },
315+ profileCmd : []string {profileScript },
316+ }
317+ token , err := ts .Token (context .Background ())
318+ if err != nil {
319+ t .Fatalf ("Token() error = %v, want fallback to profileCmd to succeed" , err )
320+ }
321+ if token .AccessToken != "profile-token" {
322+ t .Errorf ("AccessToken = %q, want %q" , token .AccessToken , "profile-token" )
323+ }
324+ }
325+
326+ func TestCliTokenSource_Token_ProfileFallbackToHost (t * testing.T ) {
285327 if runtime .GOOS == "windows" {
286328 t .Skip ("Skipping shell script test on Windows" )
287329 }
288330
289331 expiry := time .Now ().Add (1 * time .Hour ).Format (time .RFC3339 )
290332 validResponse , _ := json .Marshal (cliTokenResponse {
291- AccessToken : "fallback -token" ,
333+ AccessToken : "host -token" ,
292334 TokenType : "Bearer" ,
293335 Expiry : expiry ,
294336 })
295337
296338 tempDir := t .TempDir ()
297339
298- // Primary script simulates an old CLI that doesn't know --profile.
299340 profileScript := filepath .Join (tempDir , "profile_cli.sh" )
300341 if err := os .WriteFile (profileScript , []byte ("#!/bin/sh\n echo 'Error: unknown flag: --profile' >&2\n exit 1" ), 0755 ); err != nil {
301342 t .Fatalf ("failed to create profile script: %v" , err )
302343 }
303344
304- // Fallback script succeeds with --host.
305345 hostScript := filepath .Join (tempDir , "host_cli.sh" )
306346 if err := os .WriteFile (hostScript , []byte ("#!/bin/sh\n echo '" + string (validResponse )+ "'" ), 0755 ); err != nil {
307347 t .Fatalf ("failed to create host script: %v" , err )
308348 }
309349
310350 ts := & CliTokenSource {
311- cmd : []string {profileScript },
312- hostCmd : []string {hostScript },
351+ profileCmd : []string {profileScript },
352+ hostCmd : []string {hostScript },
313353 }
314354 token , err := ts .Token (context .Background ())
315355 if err != nil {
316- t .Fatalf ("Token() error = %v, want fallback to succeed" , err )
356+ t .Fatalf ("Token() error = %v, want fallback to hostCmd to succeed" , err )
317357 }
318- if token .AccessToken != "fallback-token" {
319- t .Errorf ("AccessToken = %q, want %q" , token .AccessToken , "fallback-token" )
358+ if token .AccessToken != "host-token" {
359+ t .Errorf ("AccessToken = %q, want %q" , token .AccessToken , "host-token" )
360+ }
361+ }
362+
363+ func TestCliTokenSource_Token_ForceRefreshFallbackToHostOnProfileError (t * testing.T ) {
364+ if runtime .GOOS == "windows" {
365+ t .Skip ("Skipping shell script test on Windows" )
366+ }
367+
368+ expiry := time .Now ().Add (1 * time .Hour ).Format (time .RFC3339 )
369+ validResponse , _ := json .Marshal (cliTokenResponse {
370+ AccessToken : "host-token" ,
371+ TokenType : "Bearer" ,
372+ Expiry : expiry ,
373+ })
374+
375+ tempDir := t .TempDir ()
376+
377+ // forceCmd fails with --profile unknown (very old CLI).
378+ forceScript := filepath .Join (tempDir , "force_cli.sh" )
379+ if err := os .WriteFile (forceScript , []byte ("#!/bin/sh\n echo 'Error: unknown flag: --profile' >&2\n exit 1" ), 0755 ); err != nil {
380+ t .Fatalf ("failed to create force script: %v" , err )
381+ }
382+
383+ // profileCmd also fails with --profile unknown.
384+ profileScript := filepath .Join (tempDir , "profile_cli.sh" )
385+ if err := os .WriteFile (profileScript , []byte ("#!/bin/sh\n echo 'Error: unknown flag: --profile' >&2\n exit 1" ), 0755 ); err != nil {
386+ t .Fatalf ("failed to create profile script: %v" , err )
387+ }
388+
389+ hostScript := filepath .Join (tempDir , "host_cli.sh" )
390+ if err := os .WriteFile (hostScript , []byte ("#!/bin/sh\n echo '" + string (validResponse )+ "'" ), 0755 ); err != nil {
391+ t .Fatalf ("failed to create host script: %v" , err )
392+ }
393+
394+ ts := & CliTokenSource {
395+ forceCmd : []string {forceScript },
396+ profileCmd : []string {profileScript },
397+ hostCmd : []string {hostScript },
398+ }
399+ token , err := ts .Token (context .Background ())
400+ if err != nil {
401+ t .Fatalf ("Token() error = %v, want fallback through to hostCmd to succeed" , err )
402+ }
403+ if token .AccessToken != "host-token" {
404+ t .Errorf ("AccessToken = %q, want %q" , token .AccessToken , "host-token" )
320405 }
321406}
322407
@@ -327,21 +412,27 @@ func TestCliTokenSource_Token_NoFallbackOnRealError(t *testing.T) {
327412
328413 tempDir := t .TempDir ()
329414
330- // Primary script fails with a real auth error (not unknown flag).
415+ // forceCmd fails with a real auth error (not unknown flag).
416+ forceScript := filepath .Join (tempDir , "force_cli.sh" )
417+ if err := os .WriteFile (forceScript , []byte ("#!/bin/sh\n echo 'cache: databricks OAuth is not configured for this host' >&2\n exit 1" ), 0755 ); err != nil {
418+ t .Fatalf ("failed to create force script: %v" , err )
419+ }
420+
421+ // profileCmd and hostCmd should not be called.
331422 profileScript := filepath .Join (tempDir , "profile_cli.sh" )
332- if err := os .WriteFile (profileScript , []byte ("#!/bin/sh\n echo 'cache: databricks OAuth is not configured for this host ' >&2\n exit 1" ), 0755 ); err != nil {
423+ if err := os .WriteFile (profileScript , []byte ("#!/bin/sh\n echo 'should not reach here ' >&2\n exit 1" ), 0755 ); err != nil {
333424 t .Fatalf ("failed to create profile script: %v" , err )
334425 }
335426
336- // Fallback script would succeed, but should not be called.
337427 hostScript := filepath .Join (tempDir , "host_cli.sh" )
338428 if err := os .WriteFile (hostScript , []byte ("#!/bin/sh\n echo 'should not reach here' >&2\n exit 1" ), 0755 ); err != nil {
339429 t .Fatalf ("failed to create host script: %v" , err )
340430 }
341431
342432 ts := & CliTokenSource {
343- cmd : []string {profileScript },
344- hostCmd : []string {hostScript },
433+ forceCmd : []string {forceScript },
434+ profileCmd : []string {profileScript },
435+ hostCmd : []string {hostScript },
345436 }
346437 _ , err := ts .Token (context .Background ())
347438 if err == nil {
0 commit comments