Tests/Get-ALDependenciesFromAppJson.Tests.ps1

Describe Get-ALDependenciesFromAppJson {            
    InModuleScope Tecman.Tfs.Tools {
        
        function New-SampleAppJson {
            param(
                # path to create the new json file in
                [Parameter(Mandatory=$false)]
                [string]
                $Path,
                # the type of dependencies to create
                [Parameter(Mandatory=$false)]
                [ValidateSet('None','CleverDynamics','Microsoft','Both')]
                [string]
                $DependenciesToInclude
            )
    
            switch ($DependenciesToInclude) {
                'None' {}
                'CleverDynamics' {$Dependencies = '{"id": "71e01521-10a7-4ce5-b172-59100577b37b", name: "CleverDynamics App", "publisher": "Clever Dynamics", "version": "1.0.0.0"}'}
                'Microsoft' {$Dependencies = '{"id": "556630ef-27cc-404e-9380-8495676ad0bf", name: "Microsoft App", "publisher": "Microsoft", "version": "1.0.0.0"}'}
                'Both' {$Dependencies = '{"id": "71e01521-10a7-4ce5-b172-59100577b37b", name: "CleverDynamics App", "publisher": "Clever Dynamics", "version": "1.0.0.0"},{"id": "556630ef-27cc-404e-9380-8495676ad0bf", name: "Microsoft App", "publisher": "Microsoft", "version": "1.0.0.0"}'}
            }
            $AppJsonContent = "{`"id`": `"c31c6a51-053e-40d2-a70e-c837b4c4af3a`", `"name`": `"test app`", `"publisher`": `"test publisher`", `"platform`": `"14.0.0.0`", `"dependencies`": [$Dependencies]}"
    
            $AppJson = ConvertFrom-Json $AppJsonContent
            Set-Content -Path (Join-Path $Path 'app.json') -Value $AppJsonContent
            return $AppJson
        }

        Mock Get-AppFromLastSuccessfulBuild {}

        Context 'App has no dependencies' {
            Mock Get-ProjectName {return 'Test App'}
            Mock Get-BranchNameForDependencies {return ''}
            It 'Doesn''t attempt to download anything' {
                Get-ALDependenciesFromAppJson -AppJson (New-SampleAppJson -Path $TestDrive -DependenciesToInclude 'None') -ContainerName 'test' -SourcePath $TestDrive
                Assert-MockCalled Get-AppFromLastSuccessfulBuild -Times 0
            }
        }
        
        Context 'App has a Clever Dynamics dependency' {
            Mock Get-ProjectName {return 'Test App'}
            Mock Get-BranchNameForDependencies {return ''}
            It 'Calls Get-AppFromLastSuccessfulBuild once' {
                Mock Get-AppJsonForProjectAndRepo {'{"name": "an app"}'}
                {Get-ALDependenciesFromAppJson -AppJson (New-SampleAppJson -Path $TestDrive -DependenciesToInclude 'CleverDynamics') -ContainerName 'test' -SourcePath $TestDrive} | Should Throw "could not be downloaded"
                Assert-MockCalled Get-AppFromLastSuccessfulBuild -Times 1
            }
        }

        Context 'App has a Microsoft dependency' {
            Mock Get-ProjectName {return 'Test App'}
            Mock Get-BranchNameForDependencies {return ''}
            It 'Doesn''t call Get-AppFromLastSuccessfulBuild' {
                Mock Get-AppJsonForProjectAndRepo {
                    ConvertFrom-Json '{"id": "556630ef-27cc-404e-9380-8495676ad0bf", name: "Microsoft App", "publisher": "Microsoft", "version": "1.0.0.0"}'
                }

                Get-ALDependenciesFromAppJson -AppJson (New-SampleAppJson -Path $TestDrive -DependenciesToInclude 'Microsoft') -ContainerName 'test' -SourcePath $TestDrive
                Assert-MockCalled Get-AppFromLastSuccessfulBuild -Times 0
            }
        }

        Context 'App has a dependency on both' {
            Mock Get-ProjectName {return 'Test App'}
            Mock Get-BranchNameForDependencies {return ''}
            It 'Calls Get-AppFromLastSuccessfulBuild once' {
                Mock Get-AppJsonForProjectAndRepo {'{"name": "an app"}'}
                {Get-ALDependenciesFromAppJson -AppJson (New-SampleAppJson -Path $TestDrive -DependenciesToInclude 'Both') -ContainerName 'test' -SourcePath $TestDrive} | Should Throw "could not be downloaded"
                Assert-MockCalled Get-AppFromLastSuccessfulBuild -Times 1
            }
        }

        Context 'Dependency Branch is specified, ref exists in repo' {
            Mock Get-ProjectName {'test app'}
            Mock Get-AppFromLastSuccessfulBuild {} -ParameterFilter {$BranchName -eq 'ThisBranch'}
            Mock Get-AppFromLastSuccessfulBuild {Set-Content -Path (Join-Path $TestDrive 'CleverDynamics.app') -value 'app content'; return Get-ChildItem $TestDrive} -ParameterFilter {$ProjectName -eq 'CleverDynamics App'}
            Mock Get-BranchNameForDependencies {return 'ThisBranch'}
            Mock Get-RefsForRepository {return 'refs/heads/ThisBranch'}
            Mock Get-AppJsonForProjectAndRepo {return '{"name": "an app"}'}
            It 'should call Get-AppFromLastSuccessfulBuild with that branch filter' {
                New-Item (Join-Path $TestDrive '.alpackages') -ItemType Directory
                Get-ALDependenciesFromAppJson -AppJson (New-SampleAppJson -Path $TestDrive -DependenciesToInclude 'CleverDynamics') -ContainerName 'test' -SourcePath $TestDrive
                Assert-MockCalled Get-AppFromLastSuccessfulBuild -ParameterFilter {$BranchName -eq 'ThisBranch'}
            }
        }
    }
}