functions/Import-RegentSolution.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

Import-Module -Name Microsoft.Xrm.Data.Powershell
Import-Module "$PsScriptRoot\Get-Config.ps1"
$RepoPath = "$(Split-Path $here -Parent)\test-data\unpacked"
if (-not $global:cred) {
  $global:cred = Get-Credential -Message "Crm Credentials" -UserName $env:USERNAME
}
Describe "Import-RegentSolution" {
  Context "Given a config file specifying repository path TestDrive:/solutions" {
    New-Item TestDrive:\solutions -ItemType Directory
    Mock 'Get-Config' {
      [PSCustomObject]@{
        RepositoryPath = $RepoPath;
        DefaultBranch  = "master";
      }
    }

    It "Should Import the solution by default" {
      Import-RegentSolution `
        -SolutionName RecordLocked `
        -CrmInstance CRMRECRUITTEST `
        -Emails "dhines@regent.edu" `
        -Managed $false `
        -Credential $global:cred `
        -Verbose
    }
    It "Should throw any import errors and email the error message" {
      # In this example, I'm importing a managed version of solution into a CRM that
      # has the unmanged version intalled, which will cause an import error
      {
        Import-RegentSolution `
          -SolutionName RecordLocked `
          -CrmInstance CRMRECRUITTEST `
          -Managed $true `
          -Emails "dhines@regent.edu" `
          -Credential $global:cred `
          -Verbose 
      } | Should -Throw
    }
  }
}