Skip to content

Commit 2cc32a1

Browse files
test: add tests for inferAppNameHint and missingAppNameError
Cover normal cases (app.yml, app.yaml, no config) and edge cases (deleted cwd) to verify graceful degradation. Co-authored-by: Isaac Signed-off-by: James Broadhead <jamesbroadhead@gmail.com>
1 parent a57135b commit 2cc32a1

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

cmd/apps/bundle_helpers_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package apps
33
import (
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+
109185
func TestMakeArgsOptionalWithBundle(t *testing.T) {
110186
t.Run("updates command usage", func(t *testing.T) {
111187
cmd := &cobra.Command{}

0 commit comments

Comments
 (0)