Add unit test for `command.ConfigUsed` (#2446)

## Changes
I forgot to add a unit test in the original PR, so adding it here:
https://github.com/databricks/cli/pull/2440

## Why
Unit testing code is a good practice. I believe we all should unit test
code.

## Tests
N/A

NO_CHANGELOG=true
This commit is contained in:
shreyas-goenka 2025-03-07 17:12:43 +05:30 committed by GitHub
parent 1a02422d56
commit 5948339f92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package command
import (
"context"
"testing"
"github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
)
func TestCommandConfigUsed(t *testing.T) {
cfg := &config.Config{
Host: "https://test.com",
}
ctx := context.Background()
// Panic if ConfigUsed is called before SetConfigUsed.
assert.Panics(t, func() {
ConfigUsed(ctx)
})
ctx = SetConfigUsed(ctx, cfg)
// Multiple calls should return a pointer to the same config.
c := ConfigUsed(ctx)
assert.Same(t, c, ConfigUsed(ctx))
// The config should have the correct configuration.
assert.Equal(t, "https://test.com", ConfigUsed(ctx).Host)
// Second call should update the config used.
cfg2 := &config.Config{
Host: "https://test2.com",
}
ctx = SetConfigUsed(ctx, cfg2)
assert.Equal(t, "https://test2.com", ConfigUsed(ctx).Host)
}