Build/Add-TestAppsToAppJson.ps1

function Add-TestAppsToAppJson{
    Param(
        #Path to determine the environment or app .json file location
        [Parameter(Mandatory=$false)]
        [String]
        $SourcePath = (Get-Location)
    )

    #Get test apps property from environment.json
    $EnvironmentJson = ConvertFrom-Json (Get-Content (Join-Path $SourcePath 'environment.json') -Raw)
    $TestappsJsonContent =  $EnvironmentJson.PSObject.Properties.Item('testapps').Value

    if ($TestappsJsonContent.Length -gt 0){
        #app has no current dependencies, add the test test dependencies directly
        $AppJsonContent = Get-Content (Join-Path $SourcePath 'app.json') -Raw
        $AppJson = ConvertFrom-Json $AppJsonContent
        $Dependencies = Get-AppKeyValue -SourcePath $SourcePath -KeyName 'dependencies'
        if ($Dependencies -eq ''){
            $AppJson | Add-Member -Name 'dependencies' -value $TestappsJsonContent -MemberType NoteProperty
        }
        else{
            #Check the test app dependency isnt already in the app.json
            [System.Collections.ArrayList]$DependenciesArrayList = $Dependencies
            foreach ($TestDependency in $TestappsJsonContent){
                $SkipDependency = $false
                foreach ($Dependency in $Dependencies){
                    if (!$SkipDependency){
                        if ($Dependency.appId -eq $TestDependency.appId){
                            $SkipDependency = $true
                        }
                    }
                }   
                if (!$SkipDependency){
                    $DependenciesArrayList.Add($TestDependency) | Out-Null
                }
            }
            $AppJson.PSObject.Properties.Remove('dependencies')
            $AppJson | Add-Member -Name 'dependencies' -value $DependenciesArrayList -MemberType NoteProperty
        }
        Set-Content -Path (Join-Path $SourcePath 'app.json') -Value (ConvertTo-Json $AppJson)
    }
}

Export-ModuleMember -Function Add-TestAppsToAppJson