---
- name: ensure test package is uninstalled
  win_chocolatey:
    name: '{{ test_choco_packages }}'
    state: absent

- name: ensure testing dir is cleaned
  win_file:
    path: '{{ test_choco_path }}'
    state: '{{ item }}'
  with_items:
  - absent
  - directory

- name: copy template package files
  win_copy:
    src: files/
    dest: '{{ test_choco_path }}'

# run the setup in 1 shell script to save on test time
- name: set up packages
  win_shell: |
    $ErrorActionPreference = "Stop"
    $root_path = '{{ test_choco_path }}'
    $packages_path = '{{ test_choco_source }}'
    $packages_path_override = '{{ test_choco_source2 }}'
    $packages = @(
        @{ name = "ansible"; version = "0.0.1"; override = $false },
        @{ name = "ansible"; version = "0.1.0"; override = $false },
        @{ name = "ansible"; version = "0.1.0"; override = $true },
        @{ name = "ansible-test"; version = "1.0.0"; override = $false },
        @{ name = "ansible-test"; version = "1.0.1-beta1"; override = $false }
    )
    $nuspec_src = "$root_path\package.nuspec"
    $install_src = "$root_path\tools\chocolateyinstall.ps1"
    $uninstall_src = "$root_path\tools\chocolateyUninstall.ps1"

    New-Item -Path $packages_path -ItemType Directory > $null
    New-Item -Path $packages_path_override -ItemType Directory > $null

    foreach ($package in $packages) {
        $package_dir = "$root_path\$($package.name)-$($package.version)"
        New-Item -Path $package_dir -ItemType Directory > $null
        New-Item -Path "$package_dir\tools" -ItemType Directory > $null

        if ($package.override) {
            $out_path = $packages_path_override
            $source_value = "override"
        } else {
            $out_path = $packages_path
            $source_value = "normal"
        }

        $nuspec_text = ([System.IO.File]::ReadAllLines($nuspec_src) -join "`r`n")
        $nuspec_text = $nuspec_text.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version)

        $install_text = ([System.IO.File]::ReadAllLines($install_src) -join "`r`n")
        $install_text = $install_text.Replace('--- PATH ---', $root_path).Replace('--- SOURCE ---', $source_value)

        $uninstall_text = ([System.IO.File]::ReadAllLines($uninstall_src) -join "`r`n")
        $uninstall_text = $uninstall_text.Replace('--- PATH ---', $root_path)

        $utf8 = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $false
        $utf8_bom = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $true
        [System.IO.File]::WriteAllText("$package_dir\$($package.name).nuspec", $nuspec_text, $utf8)
        [System.IO.File]::WriteAllText("$package_dir\tools\chocolateyinstall.ps1", $install_text, $utf8_bom)
        [System.IO.File]::WriteAllText("$package_dir\tools\chocolateyUninstall.ps1", $uninstall_text, $utf8_bom)

        &choco.exe pack --out $out_path --no-progress --limit-output "$package_dir\$($package.name).nuspec"
        Remove-Item -Path $package_dir -Force -Recurse > $null
    }
    Remove-Item -Path "$root_path\tools" -Force -Recurse > $null
    Remove-Item -Path $nuspec_src > $null

- name: set up Chocolatey sources
  win_chocolatey_source:
    name: '{{ item.name }}'
    priority: '{{ item.priority }}'
    source: '{{ item.src }}'
    state: present
  with_items:
  - name: ansible-test
    priority: 1
    src: '{{ test_choco_source }}'
  - name: ansible-test-override
    priority: 2
    src: '{{ test_choco_source2 }}'

- block:
  - name: run tests
    include_tasks: tests.yml

  always:
  - name: ensure test package is uninstalled after tests
    win_chocolatey:
      name: '{{ test_choco_packages }}'
      state: absent

  - name: remove test sources
    win_chocolatey_source:
      name: '{{ item }}'
      state: absent
    with_items:
    - ansible-test
    - ansible-test-override

  - name: remove testing dir
    win_file:
      path: '{{ test_choco_path }}'
      state: absent
