11package databrickscfg
22
33import (
4- "runtime"
4+ "context"
5+ "path/filepath"
56 "testing"
67
8+ "github.com/databricks/cli/libs/env"
79 "github.com/stretchr/testify/assert"
810 "github.com/stretchr/testify/require"
911)
@@ -27,27 +29,50 @@ func TestProfilesSearchCaseInsensitive(t *testing.T) {
2729}
2830
2931func TestLoadProfilesReturnsHomedirAsTilde (t * testing.T ) {
30- if runtime .GOOS == "windows" {
31- t .Setenv ("USERPROFILE" , "./testdata" )
32- } else {
33- t .Setenv ("HOME" , "./testdata" )
34- }
35- t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/databrickscfg" )
36- file , _ , err := LoadProfiles (func (p Profile ) bool { return true })
32+ ctx := context .Background ()
33+ ctx = env .WithUserHomeDir (ctx , "testdata" )
34+ ctx = env .Set (ctx , "DATABRICKS_CONFIG_FILE" , "./testdata/databrickscfg" )
35+ file , _ , err := LoadProfiles (ctx , func (p Profile ) bool { return true })
3736 require .NoError (t , err )
38- assert .Equal (t , "~/databrickscfg" , file )
37+ require .Equal (t , filepath .Clean ("~/databrickscfg" ), file )
38+ }
39+
40+ func TestLoadProfilesReturnsHomedirAsTildeExoticFile (t * testing.T ) {
41+ ctx := context .Background ()
42+ ctx = env .WithUserHomeDir (ctx , "testdata" )
43+ ctx = env .Set (ctx , "DATABRICKS_CONFIG_FILE" , "~/databrickscfg" )
44+ file , _ , err := LoadProfiles (ctx , func (p Profile ) bool { return true })
45+ require .NoError (t , err )
46+ require .Equal (t , filepath .Clean ("~/databrickscfg" ), file )
47+ }
48+
49+ func TestLoadProfilesReturnsHomedirAsTildeDefaultFile (t * testing.T ) {
50+ ctx := context .Background ()
51+ ctx = env .WithUserHomeDir (ctx , "testdata/sample-home" )
52+ file , _ , err := LoadProfiles (ctx , func (p Profile ) bool { return true })
53+ require .NoError (t , err )
54+ require .Equal (t , filepath .Clean ("~/.databrickscfg" ), file )
55+ }
56+
57+ func TestLoadProfilesNoConfiguration (t * testing.T ) {
58+ ctx := context .Background ()
59+ ctx = env .WithUserHomeDir (ctx , "testdata" )
60+ _ , _ , err := LoadProfiles (ctx , func (p Profile ) bool { return true })
61+ require .ErrorIs (t , err , ErrNoConfiguration )
3962}
4063
4164func TestLoadProfilesMatchWorkspace (t * testing.T ) {
42- t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/databrickscfg" )
43- _ , profiles , err := LoadProfiles (MatchWorkspaceProfiles )
65+ ctx := context .Background ()
66+ ctx = env .Set (ctx , "DATABRICKS_CONFIG_FILE" , "./testdata/databrickscfg" )
67+ _ , profiles , err := LoadProfiles (ctx , MatchWorkspaceProfiles )
4468 require .NoError (t , err )
4569 assert .Equal (t , []string {"DEFAULT" , "query" , "foo1" , "foo2" }, profiles .Names ())
4670}
4771
4872func TestLoadProfilesMatchAccount (t * testing.T ) {
49- t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/databrickscfg" )
50- _ , profiles , err := LoadProfiles (MatchAccountProfiles )
73+ ctx := context .Background ()
74+ ctx = env .Set (ctx , "DATABRICKS_CONFIG_FILE" , "./testdata/databrickscfg" )
75+ _ , profiles , err := LoadProfiles (ctx , MatchAccountProfiles )
5176 require .NoError (t , err )
5277 assert .Equal (t , []string {"acc" }, profiles .Names ())
5378}
0 commit comments