AL/Test-AppJsonFile.ps1

function Test-AppJsonFile {
    param (
        # Source path of app code
        [Parameter(Mandatory = $false)]
        [string]
        $SourcePath = (Get-Location),
        # Do not throw an error
        [Parameter(Mandatory = $false)]
        [switch]
        $SuppressError,
        [Parameter(Mandatory=$false)]
        $BuildSourceBranch
    )
    
    $TestDependencyExists = $null -ne (Get-AppKeyValue $SourcePath 'test')
    foreach ($testapp in (Get-EnvironmentKeyValue $SourcePath 'testapps')) {
        if ($null -ne $testapp.appId) {
            if ($null -ne (Get-AppKeyValue $SourcePath 'dependencies' | Where-Object appId -eq $testapp.appId)) {
                $TestDependencyExists = $true
                break
            }
        }
        else {
            if ($null -ne (Get-AppKeyValue $SourcePath 'dependencies' | Where-Object id -eq $testapp.id)) {
                $TestDependencyExists = $true
                break
            }
        }
    }
    
    if (!$TestDependencyExists) {
        $TestDependencyExists = $null -ne (Get-AppKeyValue $SourcePath 'dependencies' | Where-Object {
                ($_.publisher -eq 'Microsoft') -and ($_.name -eq 'Tests-TestLibraries' -or $_.name -eq 'Library Assert') })
    }

    if ($TestDependencyExists) {
        if ($SuppressError.IsPresent) {
            return $false
        }
        else {
            throw 'app.json contains test dependencies'
        }
    }

    if ($null -ne $BuildSourceBranch) {
        if ($BuildSourceBranch.contains('refs/heads/release'))
        {
            $version = Get-AppKeyValue $SourcePath 'version' 
            $BranchVersion = $BuildSourceBranch.SubString($BuildSourceBranch.LastIndexOf('/')+1)

            if (!$version.StartsWith($BranchVersion)) {
                if ($SuppressError.IsPresent) {
                    return $false
                }
                else {
                    throw 'Version in App.json does not match branch version'
                }
            }
        }
    }

    return $true
}