From 5948339f92b6613a66ccbba93bedfd70379ecca0 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Fri, 7 Mar 2025 17:12:43 +0530 Subject: [PATCH] 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 --- libs/command/config_used_test.go | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 libs/command/config_used_test.go diff --git a/libs/command/config_used_test.go b/libs/command/config_used_test.go new file mode 100644 index 000000000..ad2c383c9 --- /dev/null +++ b/libs/command/config_used_test.go @@ -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) +}