Tests/Set-EnvironmentKeyValue.Tests.ps1

Describe "Set-EnvironmentKeyValue" {
    function New-EnvironmentJsonFile {
        Param(
            # path to create the new file in
            [Parameter(Mandatory=$true)]
            [string]
            $Path
        )

        Set-Content -Path (Join-Path $Path 'environment.json') -Value '{"user": "testuser", "password": "testpassword"}'
    }

    Context "When setting the user key" {
        New-EnvironmentJsonFile -Path $TestDrive
        It "should maintain the original order of the keys" {
            Set-EnvironmentKeyValue -SourcePath $TestDrive -KeyName user -KeyValue 'testuser2'
            $EnvironmentJson = ConvertFrom-Json (Get-Content (Join-Path $TestDrive 'environment.json') -Raw)
            $PropertyNo = 1
            foreach ($Property in $EnvironmentJson.PSObject.Properties) {
                if ($PropertyNo -eq 1) {
                    $Property.Name | should be 'user'
                    $Property.Value | should be 'testuser2'
                }
                else {
                    $Property.Name | should be 'password'
                    $Property.Value | should be 'testpassword'
                }
                $PropertyNo++
            }
        }
    }
}