tests/Invoke-GeneratePackagesConfig.Tests.ps1

. ( Resolve-Path "$PSScriptRoot\..\src\Private\Invoke-GeneratePackagesConfig.ps1" )

$packages = [ordered] @{
  'Windows.ServiceBus'                                 = '6.0.0'  
  'System.Collections.Immutable'                       = '1.5.0'
  'System.Threading.Tasks.Dataflow'                    = '4.5.24'
  'Microsoft.VisualStudio.Setup.Configuration.Interop' = '1.16.30'
  'Microsoft.Build.Framework'                          = '16.0.461'
  'Microsoft.Build'                                    = '16.0.461'
  'Microsoft.Build.Utilities.Core'                     = '16.0.461'
}

$expectedOutput = @"
<?xml version="1.0" encoding="UTF-8"?>
<packages>
  <package id="Windows.ServiceBus" version="6.0.0" />
  <package id="System.Collections.Immutable" version="1.5.0" />
  <package id="System.Threading.Tasks.Dataflow" version="4.5.24" />
  <package id="Microsoft.VisualStudio.Setup.Configuration.Interop" version="1.16.30" />
  <package id="Microsoft.Build.Framework" version="16.0.461" />
  <package id="Microsoft.Build" version="16.0.461" />
  <package id="Microsoft.Build.Utilities.Core" version="16.0.461" />
</packages>
"@


$packagesPSObject = New-Object PSObject
$packagesPSObject | Add-Member NoteProperty Windows.ServiceBus '6.0.0'
$packagesPSObject | Add-Member NoteProperty System.Collections.Immutable '1.5.0'
$packagesPSObject | Add-Member NoteProperty System.Threading.Tasks.Dataflow '4.5.24'
$packagesPSObject | Add-Member NoteProperty Microsoft.VisualStudio.Setup.Configuration.Interop '1.16.30'
$packagesPSObject | Add-Member NoteProperty Microsoft.Build.Framework '16.0.461'
$packagesPSObject | Add-Member NoteProperty Microsoft.Build '16.0.461'
$packagesPSObject | Add-Member NoteProperty Microsoft.Build.Utilities.Core '16.0.461'

Describe "Invoke-GeneratePackagesConfig tests" {
  $tempPath = "$PSScriptRoot\tmp" + [System.Guid]::NewGuid()

  BeforeEach {
    mkdir $tempPath
  }

  AfterEach {
    Remove-Item -Path $tempPath -Recurse -Force
  }

  It "Should generate xml file in specified path with 'packages.config' name" {
    Invoke-GeneratePackagesConfig -OutputPath $tempPath -Packages $packages
    (Get-ChildItem -Path $tempPath -Filter "*.config" | Select-Object -First 1).Name | Should -Be 'packages.config'
  }

  It "Should generate xml file with given fields" {
    Invoke-GeneratePackagesConfig -OutputPath $tempPath -Packages $packages
    $written = Get-Content "$tempPath\packages.config" | Out-String
    $written = $written.Trim()
    $written | Should -BeExactly $expectedOutput
  }

  It "Should generate output for PSObject object type" {
    Invoke-GeneratePackagesConfig -OutputPath $tempPath -Packages $packagesPSObject
    $written = Get-Content "$tempPath\packages.config" | Out-String
    $written = $written.Trim()
    $written | Should -BeExactly $expectedOutput
  }
}