Tests/Get-BranchNameForDependencies.Tests.ps1

Describe 'Get-BranchNameForDependencies' {
    InModuleScope Tecman.Tfs.Tools {
        Context 'Branch dependency specified in environment.json' {
            Mock Get-EnvironmentKeyValue {return 'ThisBranch'}
            It 'should return the name of the branch in environment.json' {
                Get-BranchNameForDependencies -Path $TestDrive -DependencyProject 'dependency app' | should be 'ThisBranch'
            }
        }

        Context 'Branch dependencies specified in config file, platform within a specified range' {
            Mock Get-TFSConfigKeyValue {return ConvertFrom-Json ('[{"from": "13.0.0.0", "to": "13.999.0.0", "branch": "BC13"},{"from": "14.0.0.0", "to": "14.999.0.0", "branch": "BC14"}]')}
            Mock Get-AppKeyValue {return "14.3.0.0"}
            Mock Get-RefIsInRepository {return $true}
            It 'should return the branch from the corresponding range' {
                Mock Get-EnvironmentKeyValue {return $null}
                Get-EnvironmentKeyValue -KeyName 'dependencyBranch' | should be $null
                Get-BranchNameForDependencies -Path $TestDrive -DependencyProject 'dependency app' | should be 'BC14' 
            }
        }

        Context 'Platform within a specified range, but branch does not exist in repo' {
            Mock Get-TFSConfigKeyValue {return ConvertFrom-Json ('[{"from": "13.0.0.0", "to": "13.999.0.0", "branch": "BC13"},{"from": "14.0.0.0", "to": "14.999.0.0", "branch": "BC14"}]')}
            Mock Get-AppKeyValue {return "14.3.0.0"}
            Mock Get-RefIsInRepository {return $false}
            It 'should return blank' {
                Mock Get-EnvironmentKeyValue {return $null}
                Get-EnvironmentKeyValue -KeyName 'dependencyBranch' | should be $null
                Get-BranchNameForDependencies -Path $TestDrive -DependencyProject 'dependency app' | should be '' 
            }
        }

        Context 'Traget branch does not exist in repo, return next available one up' {
            Mock Get-TFSConfigKeyValue {return ConvertFrom-Json ('[{"from": "1.0.0.0", "to": "13.999.0.0", "branch": "BC13"},{"from": "14.0.0.0", "to": "14.999.0.0", "branch": "BC14"},{"from": "15.0.0.0", "to": "15.999.0.0", "branch": "BC15"}]')}
            Mock Get-AppKeyValue {return "13.3.0.0"}
            Mock Get-RefIsInRepository {return $false} -ParameterFilter{$RefName -eq 'BC13'}
            Mock Get-RefIsInRepository {return $false} -ParameterFilter{$RefName -eq 'BC14'}
            Mock Get-RefIsInRepository {return $true} -ParameterFilter{$RefName -eq 'BC15'}
            It 'should return the branch BC15' {
                Mock Get-EnvironmentKeyValue {return $null}
                Get-EnvironmentKeyValue -KeyName 'dependencyBranch' | should be $null
                Get-BranchNameForDependencies -Path $TestDrive -DependencyProject 'dependency app' | should be 'BC15' 
            }
        }

        Context 'Branch dependencies specified in config file, platform not within a specified range' {
            Mock Get-TFSConfigKeyValue {return ConvertFrom-Json ('[{"from": "13.0.0.0", "to": "13.999.0.0", "branch": "BC13"},{"from": "14.0.0.0", "to": "14.999.0.0", "branch": "BC14"}]')}
            Mock Get-AppKeyValue {return "15.0.0.0"}
            It 'should return the branch from the corresponding range' {
                Get-BranchNameForDependencies -Path $TestDrive -DependencyProject 'dependency app' | should be '' 
            }
        }

        Context 'Branch dependencies specified in config file, platform within a specified range, environment file takes precedence' {
            Mock Get-TFSConfigKeyValue {return ConvertFrom-Json ('[{"from": "13.0.0.0", "to": "13.999.0.0", "branch": "BC13"},{"from": "14.0.0.0", "to": "14.999.0.0", "branch": "BC14"}]')}
            Mock Get-EnvironmentKeyValue {return 'ThisBranch'}
            Mock Get-AppKeyValue {return "14.0.0.0"}
            It 'should return the branch from the corresponding range' {
                Get-BranchNameForDependencies -Path $TestDrive -DependencyProject 'dependency app' | should be 'ThisBranch'
            }
        }
    }
}