<commit_before>$script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
$script:commonTestHelperFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'CommonTestHelper.psm1'
Import-Module -Name $commonTestHelperFilePath

$script:dscModuleName = 'xPSDesiredStateConfiguration'
$script:dscResourceFriendlyName = 'xRemoteFile'
$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)"

if (Test-SkipContinuousIntegrationTask -Type 'Unit')
{
    return
}

#region HEADER
# Unit Test Template Version: 1.1.0
[System.String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
     (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:dscModuleName `
    -DSCResourceName $script:dscResourceName `
    -TestType Unit
#endregion HEADER

# Begin Testing
try
{
    InModuleScope $script:dscResourceName {
        function Get-InvalidDataException
        {
            param(
                [parameter(Mandatory = $true)]
                [System.String]
                $errorId,

                [parameter(Mandatory = $true)]
                [System.String]
                $errorMessage
            )

            $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData
            $exception = New-Object `
                -TypeName System.InvalidOperationException `
                -ArgumentList $errorMessage
            $errorRecord = New-Object `
                -TypeName System.Management.Automation.ErrorRecord `
                -ArgumentList $exception, $errorId, $errorCategory, $null
            return $errorRecord
        }

        try
        {
            # Create a working folder that all files will be created in
            $script:workingFolder = Join-Path -Path $ENV:Temp -ChildPath 'xRemoteFile.Temp'
            if (-not (Test-Path -Path $script:workingFolder))
            {
                $null = New-Item -Path $script:workingFolder -ItemType Directory
            }

            #region Pester Tests
            $testURIFile = 'test.xml'
            $testURI = "http://contoso.com/$testURIFile"
            $testURIFileNotExist = 'testnotexist.xml'
            $testURINotExist = "http://contoso.com/$testURIFileNotExist"

            $testDestinationFolder = Join-Path `
                -Path $script:workingFolder -ChildPath 'UnitTest_Folder'
            $testDestinationFolderFile = Join-Path `
                -Path $testDestinationFolder -ChildPath $testURIFile
            $testDestinationFile = Join-Path `
                -Path $script:workingFolder -ChildPath 'UnitTest_File.xml'
            $testDestinationNotExist = Join-Path `
                -Path $script:workingFolder -ChildPath 'UnitTest_NotExist'

            # Create the splats
            $testSplatFile = @{
                DestinationPath = $testDestinationFile;
                Uri = $testURI;
            }
            $testSplatFolderFileExists = @{
                DestinationPath = $testDestinationFolder;
                Uri = $testURI;
            }
            $testSplatFolderFileNotExist = @{
                DestinationPath = $testDestinationFolder;
                Uri = $testURINotExist;
            }

            # Create the test files/folders by clearing the working folder
            # if it exists and building a set of expected test files
            if (Test-Path -Path $script:workingFolder)
            {
                $null = Remove-Item -Path $script:workingFolder -Force -Recurse
            }
            $null = New-Item -Path $testDestinationFolder -ItemType Directory
            $null = Set-Content -Path $testDestinationFile -Value 'Dummy Content'
            $null = Set-Content -Path $testDestinationFolderFile -Value 'Dummy Content'

            Describe "$($script:dscResourceName)\Get-TargetResource" {
                $result = Get-TargetResource @testSplatFile
                It 'Returns "Present" when DestinationPath is a File and exists' {
                    $Result.Ensure | Should -Be 'Present'
                }

                $result = Get-TargetResource @testSplatFolderFileExists
                It 'Returns "Present" when DestinationPath is a Directory and exists and URI file exists' {
                    $Result.Ensure | Should -Be 'Present'
                }

                $result = Get-TargetResource @testSplatFolderFileNotExist
                It 'Returns "Absent" when DestinationPath is a Directory and exists but URI file does not' {
                    $Result.Ensure | Should -Be 'Absent'
                }

                Mock Get-PathItemType -MockWith { return 'Other' }
                $result = Get-TargetResource @testSplatFile
                It 'Returns "Absent" when DestinationPath is Other' {
                    $Result.Ensure | Should -Be 'Absent'
                }
            } #end Describe "$($script:dscResourceName)\Get-TargetResource"

            Describe "$($script:dscResourceName)\Set-TargetResource" {
                Context 'URI is "bad://.."' {
                    It 'Throws a UriValidationFailure exeception' {
                        $splat = $testSplatFile.Clone()
                        $splat.Uri = 'bad://contoso.com/test.xml'
                        $errorMessage = $($LocalizedData.InvalidWebUriError) `
                                    -f $splat.Uri
                        $errorRecord = Get-InvalidDataException `<commit_msg>Added Get tests to get a checksum<commit_after>$script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
$script:commonTestHelperFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'CommonTestHelper.psm1'
Import-Module -Name $commonTestHelperFilePath

$script:dscModuleName = 'xPSDesiredStateConfiguration'
$script:dscResourceFriendlyName = 'xRemoteFile'
$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)"

if (Test-SkipContinuousIntegrationTask -Type 'Unit')
{
    return
}

#region HEADER
# Unit Test Template Version: 1.1.0
[System.String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
     (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:dscModuleName `
    -DSCResourceName $script:dscResourceName `
    -TestType Unit
#endregion HEADER

# Begin Testing
try
{
    InModuleScope $script:dscResourceName {
        function Get-InvalidDataException
        {
            param (
                [Parameter(Mandatory = $true)]
                [System.String]
                $errorId,

                [Parameter(Mandatory = $true)]
                [System.String]
                $errorMessage
            )

            $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData
            $exception = New-Object `
                -TypeName System.InvalidOperationException `
                -ArgumentList $errorMessage
            $errorRecord = New-Object `
                -TypeName System.Management.Automation.ErrorRecord `
                -ArgumentList $exception, $errorId, $errorCategory, $null
            return $errorRecord
        }

        try
        {
            # Create a working folder that all files will be created in
            $script:workingFolder = Join-Path -Path $ENV:Temp -ChildPath 'xRemoteFile.Temp'
            if (-not (Test-Path -Path $script:workingFolder))
            {
                $null = New-Item -Path $script:workingFolder -ItemType Directory
            }

            #region Pester Tests
            $testURIFile = 'test.xml'
            $testURI = "http://contoso.com/$testURIFile"
            $testURIFileNotExist = 'testnotexist.xml'
            $testURINotExist = "http://contoso.com/$testURIFileNotExist"

            $testDestinationFolder = Join-Path `
                -Path $script:workingFolder -ChildPath 'UnitTest_Folder'
            $testDestinationFolderFile = Join-Path `
                -Path $testDestinationFolder -ChildPath $testURIFile
            $testDestinationFile = Join-Path `
                -Path $script:workingFolder -ChildPath 'UnitTest_File.xml'
            $testDestinationNotExist = Join-Path `
                -Path $script:workingFolder -ChildPath 'UnitTest_NotExist'

            # Create the splats
            $testSplatFile = @{
                DestinationPath = $testDestinationFile;
                Uri = $testURI;
            }
            $testSplatFileChecksum = $testSplatFile.clone()
            $testSplatFileChecksum.ChecksumType = 'MD5'

            $testSplatFolderFileExists = @{
                DestinationPath = $testDestinationFolder;
                Uri = $testURI;
            }
            $testSplatFolderFileExistsChecksum = $testSplatFolderFileExists.clone()
            $testSplatFolderFileExistsChecksum.ChecksumType = 'MD5'

            $testSplatFolderFileNotExist = @{
                DestinationPath = $testDestinationFolder;
                Uri = $testURINotExist;
            }

            $testFileHash = @{
                Hash = 'abc12345'
            }
            # Create the test files/folders by clearing the working folder
            # if it exists and building a set of expected test files
            if (Test-Path -Path $script:workingFolder)
            {
                $null = Remove-Item -Path $script:workingFolder -Force -Recurse
            }
            $null = New-Item -Path $testDestinationFolder -ItemType Directory
            $null = Set-Content -Path $testDestinationFile -Value 'Dummy Content'
            $null = Set-Content -Path $testDestinationFolderFile -Value 'Dummy Content'

            Describe "$($script:dscResourceName)\Get-TargetResource" {
                $result = Get-TargetResource @testSplatFile
                It 'Returns "Present" when DestinationPath is a File and exists' {
                    $Result.Ensure | Should -Be 'Present'
                }

                Mock Get-FileHash -MockWith { return $testFileHash }
                $result = Get-TargetResource @testSplatFileChecksum
                It 'Returns "Present" and file checksum value when DestinationPath is a File and exists' {
                    $Result.Ensure | Should -Be 'Present'
                    $Result.Checksum | Should -Be $testFileHash.Hash
                }

                $result = Get-TargetResource @testSplatFolderFileExists
                It 'Returns "Present" when DestinationPath is a Directory and exists and URI file exists' {
                    $Result.Ensure | Should -Be 'Present'
                }

                Mock Get-FileHash -MockWith { return $testFileHash }
                $result = Get-TargetResource @testSplatFolderFileExistsChecksum
                It 'Returns "Present" and a file checksum when DestinationPath is a Directory and exists and URI file exists' {
                    $Result.Ensure | Should -Be 'Present'
                    $Result.Checksum | Should -Be $testFileHash.Hash
                }

                $result = Get-TargetResource @testSplatFolderFileNotExist
                It 'Returns "Absent" when DestinationPath is a Directory and exists but URI file does not' {
                    $Result.Ensure | Should -Be 'Absent'
                }

                Mock Get-PathItemType -MockWith { return 'Other' }
                $result = Get-TargetResource @testSplatFile
                It 'Returns "Absent" when DestinationPath is Other' {
                    $Result.Ensure | Should -Be 'Absent'
                }
            } #end Describe "$($script:dscResourceName)\Get-TargetResource"

            Describe "$($script:dscResourceName)\Set-TargetResource" {
                Context 'URI is "bad://.."' {
                    It 'Throws a UriValidationFailure exeception' {
                        $splat = $testSplatFile.Clone()
                        $splat.Uri = 'bad://contoso.com/test.xml'
                        $errorMessage = $($LocalizedData.InvalidWebUriError) `
                                    -f $splat.Uri
                        $errorRecord = Get-InvalidDataException `
{'fraction_non_alphanumeric': 0.09252559464244477, 'fraction_numerical': 0.0014625509968439689, 'mean_word_length': 1.9736781872281988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6523671', 'n_tokens_mistral': 3528, 'n_tokens_neox': 3405, 'n_words': 791}