Skip to content

Commit ca0f0e5

Browse files
committed
Add unit tests
1 parent e7cae0e commit ca0f0e5

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

cmd/workspace/apps/errors.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9+
const tailLinesSuggestedValue = 100
10+
911
// AppDeploymentError wraps deployment errors with a helpful logs command suggestion.
1012
type AppDeploymentError struct {
11-
Err error
12-
AppName string
13-
Profile string
13+
Underlying error
14+
appName string
15+
profile string
1416
}
1517

1618
func (e *AppDeploymentError) Error() string {
17-
suggestion := "\n\nTo view app logs, run:\n databricks workspace apps logs " + e.AppName
18-
if e.Profile != "" {
19-
suggestion = fmt.Sprintf("%s --profile %s", suggestion, e.Profile)
19+
suggestion := fmt.Sprintf("\n\nTo view app logs, run:\n databricks apps logs %s --tail-lines %d",
20+
e.appName,
21+
tailLinesSuggestedValue,
22+
)
23+
if e.profile != "" {
24+
suggestion = fmt.Sprintf("%s --profile %s", suggestion, e.profile)
2025
}
21-
return e.Err.Error() + suggestion
26+
return e.Underlying.Error() + suggestion
2227
}
2328

2429
func (e *AppDeploymentError) Unwrap() error {
25-
return e.Err
30+
return e.Underlying
2631
}
2732

2833
// newAppDeploymentError creates an AppDeploymentError with profile info from the command.
@@ -33,8 +38,8 @@ func newAppDeploymentError(cmd *cobra.Command, appName string, err error) error
3338
profile = profileFlag.Value.String()
3439
}
3540
return &AppDeploymentError{
36-
Err: err,
37-
AppName: appName,
38-
Profile: profile,
41+
Underlying: err,
42+
appName: appName,
43+
profile: profile,
3944
}
4045
}

cmd/workspace/apps/errors_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package apps
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestAppDeploymentError_Error_WithoutProfile(t *testing.T) {
12+
originalErr := errors.New("deployment failed: timeout")
13+
appErr := &AppDeploymentError{
14+
Underlying: originalErr,
15+
appName: "my-app",
16+
profile: "",
17+
}
18+
19+
result := appErr.Error()
20+
21+
assert.Contains(t, result, "deployment failed: timeout")
22+
assert.Contains(t, result, "To view app logs, run:")
23+
assert.Contains(t, result, "databricks apps logs my-app --tail-lines 100")
24+
assert.NotContains(t, result, "--profile")
25+
}
26+
27+
func TestAppDeploymentError_Error_WithProfile(t *testing.T) {
28+
originalErr := errors.New("deployment failed: timeout")
29+
appErr := &AppDeploymentError{
30+
Underlying: originalErr,
31+
appName: "my-app",
32+
profile: "production",
33+
}
34+
35+
result := appErr.Error()
36+
37+
assert.Contains(t, result, "deployment failed: timeout")
38+
assert.Contains(t, result, "To view app logs, run:")
39+
assert.Contains(t, result, "databricks apps logs my-app --tail-lines 100 --profile production")
40+
}
41+
42+
func TestAppDeploymentError_Unwrap(t *testing.T) {
43+
originalErr := errors.New("original error")
44+
appErr := &AppDeploymentError{
45+
Underlying: originalErr,
46+
appName: "my-app",
47+
profile: "",
48+
}
49+
50+
unwrapped := appErr.Unwrap()
51+
52+
require.Equal(t, originalErr, unwrapped)
53+
assert.ErrorIs(t, appErr, originalErr, "errors.Is should work with wrapped error")
54+
}

experimental/apps-mcp/lib/prompts/target_apps.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This is battle-tested to catch common issues before deployment. Prefer using thi
2222

2323
### View App Logs
2424
To troubleshoot deployed apps, view their logs:
25-
invoke_databricks_cli 'workspace apps logs <app-name> --tail-lines 100'
25+
invoke_databricks_cli 'apps logs <app-name> --tail-lines 100'
2626

2727
Add --follow to stream logs continuously. Useful for debugging deployment failures or runtime issues.
2828

0 commit comments

Comments
 (0)