@@ -3,6 +3,8 @@ package apps
33import (
44 "context"
55 "errors"
6+ "os"
7+ "path/filepath"
68 "testing"
79
810 "github.com/databricks/databricks-sdk-go/service/apps"
@@ -106,6 +108,80 @@ func TestFormatAppStatusMessage(t *testing.T) {
106108 })
107109}
108110
111+ func TestInferAppNameHint (t * testing.T ) {
112+ t .Run ("returns empty when no app config exists" , func (t * testing.T ) {
113+ dir := t .TempDir ()
114+ testChdir (t , dir )
115+
116+ assert .Equal (t , "" , inferAppNameHint ())
117+ })
118+
119+ t .Run ("returns dir name when app.yml exists" , func (t * testing.T ) {
120+ dir := t .TempDir ()
121+ testChdir (t , dir )
122+ os .WriteFile (filepath .Join (dir , "app.yml" ), []byte ("command: [\" python\" ]" ), 0o644 )
123+
124+ assert .Equal (t , filepath .Base (dir ), inferAppNameHint ())
125+ })
126+
127+ t .Run ("returns dir name when app.yaml exists" , func (t * testing.T ) {
128+ dir := t .TempDir ()
129+ testChdir (t , dir )
130+ os .WriteFile (filepath .Join (dir , "app.yaml" ), []byte ("command: [\" python\" ]" ), 0o644 )
131+
132+ assert .Equal (t , filepath .Base (dir ), inferAppNameHint ())
133+ })
134+
135+ t .Run ("returns empty when cwd has been deleted" , func (t * testing.T ) {
136+ dir := t .TempDir ()
137+ testChdir (t , dir )
138+ os .Remove (dir )
139+
140+ assert .Equal (t , "" , inferAppNameHint ())
141+ })
142+ }
143+
144+ func TestMissingAppNameError (t * testing.T ) {
145+ t .Run ("includes APP_NAME and usage info" , func (t * testing.T ) {
146+ dir := t .TempDir ()
147+ testChdir (t , dir )
148+
149+ err := missingAppNameError ()
150+ assert .Contains (t , err .Error (), "APP_NAME" )
151+ assert .Contains (t , err .Error (), "databricks.yml" )
152+ assert .NotContains (t , err .Error (), "Did you mean" )
153+ })
154+
155+ t .Run ("includes hint when app.yml exists" , func (t * testing.T ) {
156+ dir := t .TempDir ()
157+ testChdir (t , dir )
158+ os .WriteFile (filepath .Join (dir , "app.yml" ), []byte ("command: [\" python\" ]" ), 0o644 )
159+
160+ err := missingAppNameError ()
161+ assert .Contains (t , err .Error (), "Did you mean" )
162+ assert .Contains (t , err .Error (), filepath .Base (dir ))
163+ })
164+
165+ t .Run ("gracefully handles deleted cwd" , func (t * testing.T ) {
166+ dir := t .TempDir ()
167+ testChdir (t , dir )
168+ os .Remove (dir )
169+
170+ err := missingAppNameError ()
171+ assert .Contains (t , err .Error (), "APP_NAME" )
172+ assert .NotContains (t , err .Error (), "Did you mean" )
173+ })
174+ }
175+
176+ // testChdir changes to the given directory for the duration of the test.
177+ func testChdir (t * testing.T , dir string ) {
178+ t .Helper ()
179+ orig , err := os .Getwd ()
180+ assert .NoError (t , err )
181+ assert .NoError (t , os .Chdir (dir ))
182+ t .Cleanup (func () { os .Chdir (orig ) })
183+ }
184+
109185func TestMakeArgsOptionalWithBundle (t * testing.T ) {
110186 t .Run ("updates command usage" , func (t * testing.T ) {
111187 cmd := & cobra.Command {}
0 commit comments