winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

xE2ETestResource.psm1 (7540B)


      1 # E2E module with resources.
      2 
      3 enum Ensure
      4 {
      5     Absent
      6     Present
      7 }
      8 
      9 # This resource just checks if a file is there or not with and if its with the specified content.
     10 [DscResource()]
     11 class E2EFileResource
     12 {
     13     [DscProperty(Key)]
     14     [string] $Path
     15 
     16     [DscProperty()]
     17     [Ensure] $Ensure = [Ensure]::Present
     18 
     19     [DscProperty()]
     20     [string] $Content = $null
     21 
     22     [E2EFileResource] Get()
     23     {
     24         if ([string]::IsNullOrEmpty($this.Path))
     25         {
     26             throw
     27         }
     28 
     29         $fileContent = $null
     30         if (Test-Path -Path $this.Path -PathType Leaf)
     31         {
     32             $fileContent = Get-Content $this.Path -Raw
     33         }
     34 
     35         $result = @{
     36             Path = $this.Path
     37             Content = $fileContent
     38         }
     39 
     40         return $result
     41     }
     42 
     43     [bool] Test()
     44     {
     45         $get = $this.Get()
     46 
     47         if (Test-Path -Path $this.Path -PathType Leaf)
     48         {
     49             if ($this.Ensure -eq [Ensure]::Present)
     50             {
     51                 return $this.Content -eq $get.Content
     52             }
     53         }
     54         elseif ($this.Ensure -eq [Ensure]::Absent)
     55         {
     56             return $true
     57         }
     58 
     59         return $false
     60     }
     61 
     62     [void] Set()
     63     {
     64         if (-not $this.Test())
     65         {
     66             if (Test-Path -Path $this.Path -PathType Leaf)
     67             {
     68                 if ($this.Ensure -eq [Ensure]::Present)
     69                 {
     70                     Set-Content $this.Path $this.Content -NoNewline
     71                 }
     72                 else
     73                 {
     74                     Remove-Item $this.Path
     75                 }
     76             }
     77             else
     78             {
     79                 if ($this.Ensure -eq [Ensure]::Present)
     80                 {
     81                     Set-Content $this.Path $this.Content -NoNewline
     82                 }
     83             }
     84         }
     85     }
     86 }
     87 
     88 [DscResource()]
     89 class E2ETestResource
     90 {
     91     [DscProperty(Key)]
     92     [string] $key
     93 
     94     [DscProperty(Mandatory)]
     95     [string] $secretCode
     96 
     97     [E2ETestResource] Get()
     98     {
     99         $result = @{
    100             key = "E2ETestResourceKey"
    101         }
    102         return $result
    103     }
    104 
    105     [bool] Test()
    106     {
    107         return $this.secretCode -eq "4815162342"
    108     }
    109 
    110     [void] Set()
    111     {
    112         if (-not $this.Test())
    113         {
    114             $global:DSCMachineStatus = 1
    115         }
    116     }
    117 }
    118 
    119 [DscResource()]
    120 class E2ETestResourceThrows
    121 {
    122     [DscProperty(Key)]
    123     [string] $key
    124 
    125     [E2ETestResourceThrows] Get()
    126     {
    127         $result = @{
    128             key = "E2ETestResourceThrowsKey"
    129         }
    130         throw "throws in Get"
    131         return $result
    132     }
    133 
    134     [bool] Test()
    135     {
    136         throw "throws in Test"
    137         return $false
    138     }
    139 
    140     [void] Set()
    141     {
    142         throw "throws in Set"
    143     }
    144 }
    145 
    146 [DscResource()]
    147 class E2ETestResourceError
    148 {
    149     [DscProperty(Key)]
    150     [string] $key
    151 
    152     [E2ETestResourceError] Get()
    153     {
    154         $result = @{
    155             key = "E2ETestResourceErrorKey"
    156         }
    157         Write-Error "Error in Get"
    158         return $result
    159     }
    160 
    161     [bool] Test()
    162     {
    163         Write-Error "Error in Test"
    164         return $true
    165     }
    166 
    167     [void] Set()
    168     {
    169         Write-Error "Error in Set"
    170     }
    171 }
    172 
    173 [DscResource()]
    174 class E2ETestResourceTypes
    175 {
    176     [DscProperty(Key)]
    177     [string] $key
    178 
    179     [DscProperty()]
    180     [boolean] $boolProperty
    181 
    182     [DscProperty()]
    183     [int] $intProperty;
    184 
    185     [DscProperty()]
    186     [double] $doubleProperty;
    187 
    188     [DscProperty()]
    189     [char] $charProperty;
    190 
    191     [DscProperty()]
    192     [Hashtable] $hashtableProperty;
    193 
    194     [E2ETestResourceTypes] Get()
    195     {
    196         $result = @{
    197             key = "E2ETestResourceTypesKey"
    198             boolProperty = $false
    199             intProperty = 0
    200             doubleProperty = 0.0
    201             charProperty = 'z'
    202             hashtableProperty = @{}
    203         }
    204         return $result
    205     }
    206 
    207     [bool] Test()
    208     {
    209         # Because we can't get the error stream from a class based resource, I throw so is easier to know if
    210         # there's something wrong.
    211         if ($this.boolProperty -ne $true)
    212         {
    213             throw "Failed boolProperty"
    214         }
    215 
    216         if ($this.intProperty -ne 3)
    217         {
    218             throw "Failed intProperty. Got $($this.intProperty)"
    219         }
    220 
    221         if ($this.doubleProperty -ne -9.876)
    222         {
    223             throw "Failed doubleProperty Got $($this.doubleProperty)"
    224         }
    225 
    226         if ($this.charProperty -ne 'f')
    227         {
    228             throw "Failed charProperty Got $($this.charProperty)"
    229         }
    230 
    231         if ($this.hashtableProperty.ContainsKey("secretStringKey"))
    232         {
    233             if ($this.hashtableProperty["secretStringKey"] -ne "secretCode")
    234             {
    235                 throw "Failed comparing value of `$hashtableProperty.secretStringKey Got $($this.hashtableProperty["secretStringKey"])"
    236             }
    237         }
    238         else
    239         {
    240             throw "Failed finding secretStringKey in hashtableProperty"
    241         }
    242 
    243         if ($this.hashtableProperty.ContainsKey("secretIntKey"))
    244         {
    245             if ($this.hashtableProperty["secretIntKey"] -ne 123456)
    246             {
    247                 throw "Failed comparing value of `$hashtableProperty.secretIntKey Got $($this.hashtableProperty["secretIntKey"])"
    248             }
    249         }
    250         else
    251         {
    252             throw "Failed finding secretIntKey in hashtableProperty"
    253         }
    254 
    255         return $true
    256     }
    257 
    258     [void] Set()
    259     {
    260         # no-op
    261     }
    262 }
    263 
    264 # This resource "crashes" the containing process (really it just exits)
    265 [DscResource()]
    266 class E2ETestResourceCrash
    267 {
    268     [DscProperty(Key)]
    269     [string] $key
    270 
    271     [E2ETestResourceCrash] Get()
    272     {
    273         $result = @{
    274             key = "E2ETestResourceCrashKey"
    275         }
    276         [System.Environment]::Exit(0)
    277         return $result
    278     }
    279 
    280     [bool] Test()
    281     {
    282         [System.Environment]::Exit(0)
    283         return $true
    284     }
    285 
    286     [void] Set()
    287     {
    288         [System.Environment]::Exit(0)
    289     }
    290 }
    291 
    292 # This resource writes the current PID to the provided file path.
    293 [DscResource()]
    294 class E2ETestResourcePID
    295 {
    296     [DscProperty(Key)]
    297     [string] $key
    298 
    299     [DscProperty(Mandatory)]
    300     [string] $directoryPath
    301 
    302     [E2ETestResourcePID] Get()
    303     {
    304         $result = @{
    305             key = "E2ETestResourcePID"
    306             directoryPath = $this.directoryPath
    307         }
    308 
    309         return $result
    310     }
    311 
    312     [bool] Test()
    313     {
    314         return $false
    315     }
    316 
    317     [void] Set()
    318     {
    319         if (Test-Path -Path $this.directoryPath)
    320         {
    321             $processId = [System.Diagnostics.Process]::GetCurrentProcess().Id
    322             $filePath = Join-Path -Path $this.directoryPath -ChildPath "$processId.txt"
    323             New-Item -Path $filePath -ItemType File -Force  
    324         }
    325     }
    326 }
    327 
    328 # This resource writes the current PSModulePath to the provided file path.
    329 [DscResource()]
    330 class E2ETestResourcePSModulePath
    331 {
    332     [DscProperty(Key)]
    333     [string] $key
    334 
    335     [DscProperty(Mandatory)]
    336     [string] $outputPath
    337 
    338     [E2ETestResourcePSModulePath] Get()
    339     {
    340         $result = @{
    341             key = "E2ETestResourcePSModulePath"
    342             outputPath = $this.outputPath
    343         }
    344 
    345         return $result
    346     }
    347 
    348     [bool] Test()
    349     {
    350         return $false
    351     }
    352 
    353     [void] Set()
    354     {
    355         Set-Content -Path $this.outputPath -Value $env:PSModulePath -Force
    356     }
    357 }