@@ -323,3 +323,114 @@ func TestMustAnyClientWithEmptyDatabricksCfg(t *testing.T) {
323323 _ , err = MustAnyClient (cmd , []string {})
324324 require .ErrorContains (t , err , "does not contain account profiles" )
325325}
326+
327+ func TestMustWorkspaceClientDefaultProfilePrecedence (t * testing.T ) {
328+ testutil .CleanupEnvironment (t )
329+
330+ configFile := filepath .Join (t .TempDir (), ".databrickscfg" )
331+ err := os .WriteFile (configFile , []byte (`
332+ [__databricks-settings__]
333+ default_profile = settings-profile
334+
335+ [DEFAULT]
336+ host = https://default.cloud.databricks.com
337+ token = default-token
338+
339+ [settings-profile]
340+ host = https://settings.cloud.databricks.com
341+ token = settings-token
342+
343+ [env-profile]
344+ host = https://env.cloud.databricks.com
345+ token = env-token
346+
347+ [flag-profile]
348+ host = https://flag.cloud.databricks.com
349+ token = flag-token
350+ ` ), 0o600 )
351+ require .NoError (t , err )
352+
353+ testCases := []struct {
354+ name string
355+ profileFlag string
356+ envProfile string
357+ wantProfile string
358+ wantHost string
359+ }{
360+ {
361+ name : "settings default is used when flag and env are unset" ,
362+ wantProfile : "settings-profile" ,
363+ wantHost : "https://settings.cloud.databricks.com" ,
364+ },
365+ {
366+ name : "env var takes precedence over settings default" ,
367+ envProfile : "env-profile" ,
368+ wantProfile : "env-profile" ,
369+ wantHost : "https://env.cloud.databricks.com" ,
370+ },
371+ {
372+ name : "profile flag takes precedence over env var" ,
373+ profileFlag : "flag-profile" ,
374+ envProfile : "env-profile" ,
375+ wantProfile : "flag-profile" ,
376+ wantHost : "https://flag.cloud.databricks.com" ,
377+ },
378+ }
379+
380+ for _ , tc := range testCases {
381+ t .Run (tc .name , func (t * testing.T ) {
382+ testutil .CleanupEnvironment (t )
383+ t .Setenv ("DATABRICKS_CONFIG_FILE" , configFile )
384+ if tc .envProfile != "" {
385+ t .Setenv ("DATABRICKS_CONFIG_PROFILE" , tc .envProfile )
386+ }
387+
388+ ctx := cmdio .MockDiscard (context .Background ())
389+ ctx = SkipLoadBundle (ctx )
390+ cmd := New (ctx )
391+
392+ if tc .profileFlag != "" {
393+ err := cmd .Flag ("profile" ).Value .Set (tc .profileFlag )
394+ require .NoError (t , err )
395+ }
396+
397+ err := MustWorkspaceClient (cmd , []string {})
398+ require .NoError (t , err )
399+
400+ w := cmdctx .WorkspaceClient (cmd .Context ())
401+ require .NotNil (t , w )
402+ assert .Equal (t , tc .wantProfile , w .Config .Profile )
403+ assert .Equal (t , tc .wantHost , w .Config .Host )
404+ })
405+ }
406+ }
407+
408+ func TestMustWorkspaceClientWithoutConfiguredDefaultFallsBackToDefaultSection (t * testing.T ) {
409+ testutil .CleanupEnvironment (t )
410+
411+ configFile := filepath .Join (t .TempDir (), ".databrickscfg" )
412+ err := os .WriteFile (configFile , []byte (`
413+ [DEFAULT]
414+ host = https://default.cloud.databricks.com
415+ token = default-token
416+
417+ [named-profile]
418+ host = https://named.cloud.databricks.com
419+ token = named-token
420+ ` ), 0o600 )
421+ require .NoError (t , err )
422+
423+ t .Setenv ("DATABRICKS_CONFIG_FILE" , configFile )
424+
425+ ctx := cmdio .MockDiscard (context .Background ())
426+ ctx = SkipLoadBundle (ctx )
427+ cmd := New (ctx )
428+
429+ err = MustWorkspaceClient (cmd , []string {})
430+ require .NoError (t , err )
431+
432+ w := cmdctx .WorkspaceClient (cmd .Context ())
433+ require .NotNil (t , w )
434+ assert .Equal (t , "" , w .Config .Profile )
435+ assert .Equal (t , "https://default.cloud.databricks.com" , w .Config .Host )
436+ }
0 commit comments