1---
2- name: ensure test package is uninstalled
3  win_chocolatey:
4    name: '{{ test_choco_packages }}'
5    state: absent
6
7- name: ensure testing dir is cleaned
8  win_file:
9    path: '{{ test_choco_path }}'
10    state: '{{ item }}'
11  with_items:
12  - absent
13  - directory
14
15- name: copy template package files
16  win_copy:
17    src: files/
18    dest: '{{ test_choco_path }}'
19
20# run the setup in 1 shell script to save on test time
21- name: set up packages
22  win_shell: |
23    $ErrorActionPreference = "Stop"
24    $root_path = '{{ test_choco_path }}'
25    $packages_path = '{{ test_choco_source }}'
26    $packages_path_override = '{{ test_choco_source2 }}'
27    $packages = @(
28        @{ name = "ansible"; version = "0.0.1"; override = $false },
29        @{ name = "ansible"; version = "0.1.0"; override = $false },
30        @{ name = "ansible"; version = "0.1.0"; override = $true },
31        @{ name = "ansible-test"; version = "1.0.0"; override = $false },
32        @{ name = "ansible-test"; version = "1.0.1-beta1"; override = $false }
33    )
34    $nuspec_src = "$root_path\package.nuspec"
35    $install_src = "$root_path\tools\chocolateyinstall.ps1"
36    $uninstall_src = "$root_path\tools\chocolateyUninstall.ps1"
37
38    New-Item -Path $packages_path -ItemType Directory > $null
39    New-Item -Path $packages_path_override -ItemType Directory > $null
40
41    foreach ($package in $packages) {
42        $package_dir = "$root_path\$($package.name)-$($package.version)"
43        New-Item -Path $package_dir -ItemType Directory > $null
44        New-Item -Path "$package_dir\tools" -ItemType Directory > $null
45
46        if ($package.override) {
47            $out_path = $packages_path_override
48            $source_value = "override"
49        } else {
50            $out_path = $packages_path
51            $source_value = "normal"
52        }
53
54        $nuspec_text = ([System.IO.File]::ReadAllLines($nuspec_src) -join "`r`n")
55        $nuspec_text = $nuspec_text.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version)
56
57        $install_text = ([System.IO.File]::ReadAllLines($install_src) -join "`r`n")
58        $install_text = $install_text.Replace('--- PATH ---', $root_path).Replace('--- SOURCE ---', $source_value)
59
60        $uninstall_text = ([System.IO.File]::ReadAllLines($uninstall_src) -join "`r`n")
61        $uninstall_text = $uninstall_text.Replace('--- PATH ---', $root_path)
62
63        $utf8 = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $false
64        $utf8_bom = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $true
65        [System.IO.File]::WriteAllText("$package_dir\$($package.name).nuspec", $nuspec_text, $utf8)
66        [System.IO.File]::WriteAllText("$package_dir\tools\chocolateyinstall.ps1", $install_text, $utf8_bom)
67        [System.IO.File]::WriteAllText("$package_dir\tools\chocolateyUninstall.ps1", $uninstall_text, $utf8_bom)
68
69        &choco.exe pack --out $out_path --no-progress --limit-output "$package_dir\$($package.name).nuspec"
70        Remove-Item -Path $package_dir -Force -Recurse > $null
71    }
72    Remove-Item -Path "$root_path\tools" -Force -Recurse > $null
73    Remove-Item -Path $nuspec_src > $null
74
75- name: set up Chocolatey sources
76  win_chocolatey_source:
77    name: '{{ item.name }}'
78    priority: '{{ item.priority }}'
79    source: '{{ item.src }}'
80    state: present
81  with_items:
82  - name: ansible-test
83    priority: 1
84    src: '{{ test_choco_source }}'
85  - name: ansible-test-override
86    priority: 2
87    src: '{{ test_choco_source2 }}'
88
89- block:
90  - name: run tests
91    include_tasks: tests.yml
92
93  always:
94  - name: ensure test package is uninstalled after tests
95    win_chocolatey:
96      name: '{{ test_choco_packages }}'
97      state: absent
98
99  - name: remove test sources
100    win_chocolatey_source:
101      name: '{{ item }}'
102      state: absent
103    with_items:
104    - ansible-test
105    - ansible-test-override
106
107  - name: remove testing dir
108    win_file:
109      path: '{{ test_choco_path }}'
110      state: absent
111