Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Menghua1 committed Nov 14, 2024
1 parent 276471d commit 0f3c2fc
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cli/azd/pkg/environment/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,58 @@ func Test_SaveAndReload(t *testing.T) {
require.Equal(t, "http://api.example.com/updated", value)
}

func Test_SaveSpecialCharacters_NoEscaping(t *testing.T) {
tempDir := t.TempDir()
ostest.Chdir(t, tempDir)

mockContext := mocks.NewMockContext(context.Background())
envManager, azdCtx := createEnvManager(mockContext, t.TempDir())

env := New("test")
require.NotNil(t, env)

// Set environment variables with special characters that should NOT be escaped
env.DotenvSet("TEST1", "!")
env.DotenvSet("TEST2", "$")
env.DotenvSet("TEST3", "`")

// Set environment variables using common and escaped characters
env.DotenvSet("TEST4", "01")
env.DotenvSet("TEST5", "/n")
env.DotenvSet("TEST6", "abc")

// Save the environment
err := envManager.Save(*mockContext.Context, env)
require.NoError(t, err)

// Reload the environment to verify the values are correctly saved
env2, err := envManager.Get(*mockContext.Context, "test")
require.NoError(t, err)

// Verify that the values are correctly set
require.Equal(t, "!", env2.dotenv["TEST1"])
require.Equal(t, "$", env2.dotenv["TEST2"])
require.Equal(t, "`", env2.dotenv["TEST3"])
require.Equal(t, "01", env2.dotenv["TEST4"])
require.Equal(t, "/n", env2.dotenv["TEST5"])
require.Equal(t, "abc", env2.dotenv["TEST6"])

// Optionally, read the .env file content to verify special character escaping
envRoot := azdCtx.EnvironmentRoot("test")
envPath := filepath.Join(envRoot, azdcontext.DotEnvFileName)

envMap, err := godotenv.Read(envPath)
require.NoError(t, err)

// Verify the .env file content directly for expected escaping
require.Equal(t, "!", envMap["TEST1"])
require.Equal(t, "$", envMap["TEST2"])
require.Equal(t, "`", envMap["TEST3"])
require.Equal(t, "01", envMap["TEST4"])
require.Equal(t, "/n", envMap["TEST5"])
require.Equal(t, "abc", envMap["TEST6"])
}

func TestCleanName(t *testing.T) {
require.Equal(t, "already-clean-name", CleanName("already-clean-name"))
require.Equal(t, "was-CLEANED-with--bad--things-(123)", CleanName("was CLEANED with *bad* things (123)"))
Expand Down

0 comments on commit 0f3c2fc

Please sign in to comment.