<commit_before>Describe "Update-TypeData" {

    Context "Validate Update-Type updates correctly" {

	It "Should not throw upon reloading previous formatting file" {
	    { Update-TypeData } | Should Not throw
	}

	It "Should validly load formatting data" {
	    { Get-TypeData -typename System.Diagnostics.Process | Export-TypeData -Path "outputfile.ps1xml" }
	    { Update-TypeData -prependPath "outputfile.ps1xml" | Should Not throw }
	    { Remove-Item "outputfile.ps1xml" -ErrorAction SilentlyContinue }
	}
    }
}

Describe "Update-TypeData basic functionality" -Tags DRT{
	$tmpDirectory = $TestDrive
    $testfilename = "testfile.ps1xml"
    $testfile = Join-Path -Path $tmpDirectory -ChildPath $testfilename
	$invalidFileExtensionFile = Join-Path -Path $tmpDirectory -ChildPath "notmshxml"
	$filelist = Join-Path -Path $tmpDirectory -ChildPath "fileList.ps1xml"
	
	#Pester bug:https://github.com/PowerShell/psl-pester/issues/6
	It "Update-TypeData with Invalid TypesXml should throw Exception" -Pending{
		$xmlContent=@"
				<Types>
                    <Type>
                        <Name>AnyName</Name>
                        <MembersTypo>
                            <PropertySet>
                                <Name>PropertySetName</Name>
                                <ReferencedProperties>
                                    <Name>FirstName</Name> 
                                    <Name>LastName</Name> 
                                </ReferencedProperties>
                            </PropertySet>
                        </MembersTypo>
                        <MembersTypo2>
                            <PropertySet>
                                <Name>PropertySetName</Name>
                                <ReferencedProperties>
                                    <Name>FirstName</Name> 
                                    <Name>LastName</Name> 
                                </ReferencedProperties>
                            </PropertySet>
                        </MembersTypo2>
                    </Type>
				</Types>
"@
		$xmlContent>$testfile
		try
		{
			Update-TypeData -PrependPath $testfile -EA Stop
			Throw "Execution OK"
		}
		catch 
		{
			$error[0] | Should Match "Error: The node MembersTypo2 is not allowed."
			$_.CategoryInfo | Should Match "RuntimeException"
			$_.FullyQualifiedErrorId | Should be "TypesXmlUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand"
		}
		finally
		{
			Remove-TypeData -Path $testfile
			rm $testfile
		}
	}
	
	It "Update-TypeData with Invalid File Extension should throw Exception"{
		$xmlContent="not really an xml file, but we will not use it"
		$xmlContent>$invalidFileExtensionFile
		try
		{
			Update-TypeData -PrependPath $invalidFileExtensionFile -EA Stop
			Throw "Execution OK"
		}
		catch 
		{
			$error[0] | Should Match "because it does not have the file name extension"
			$_.CategoryInfo | Should Match "PSInvalidOperationException"
			$_.FullyQualifiedErrorId | Should be "WrongExtension,Microsoft.PowerShell.Commands.UpdateTypeDataCommand"
		}
		finally
		{
			rm $invalidFileExtensionFile
		}
	}
	
	It "Update-TypeData with Invalid File List Extension should throw Exception"{
		$xmlContent="not really an xml file, but we will not use it"
		$xmlContent>$invalidFileExtensionFile
		$filelistContent = "<Files><File>" + $invalidFileExtensionFile + "</File></Files>"
		$filelistContent>$filelist
		try
		{
			Update-TypeData -AppendPath $filelist -EA Stop
			Throw "Execution OK"
		}
		catch 
		{
			$error[0] | Should Match "ps1xml"
			$_.CategoryInfo | Should Match "RuntimeException"
			$_.FullyQualifiedErrorId | Should be "TypesXmlUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand"
		}
		finally
		{
			Remove-TypeData -Path $filelist
			rm $invalidFileExtensionFile
			rm $filelist
		}
	}
	
	It "Update-TypeData with Valid Dynamic Type NoteProperty with Force should work"{
		Update-TypeData -MemberType NoteProperty -MemberName TestNote -Value 2 -TypeName System.String
		"string".TestNote | Should Be 2
		Update-TypeData -MemberType NoteProperty -MemberName TestNote -Value "test" -TypeName System.String -Force
		"string".TestNote | Should Be "test"
		Update-TypeData -MemberType NoteProperty -MemberName TestNote -Value $null -TypeName System.String -Force
		"string".TestNote | Should BeNullOrEmpty
	}
	
	It "Update-TypeData with Valid Dynamic Type AliasProperty with Force should work"{
		Update-TypeData -MemberType AliasProperty -MemberName TestAlias -Value "Length" -TypeName System.String
		"string".TestAlias.GetType().Name | Should Be "Int32"
		"string".TestAlias | Should Be 6
		Update-TypeData -MemberType AliasProperty -MemberName TestAlias -Value "Length" -SecondValue "string" -TypeName System.String -Force
		"string".TestAlias.GetType().Name | Should Be "String"
		"string".TestAlias | Should Be 6
	}
	
	It "Update-TypeData with Valid Dynamic Type ScriptMethod with Force should work"{
		$script1={"script method"}
		Update-TypeData -MemberType ScriptMethod -MemberName TestScriptMethod -Value $script1 -TypeName System.String
		"string".TestScriptMethod() | Should Be "script method"
		$script2={"new script method"}
		Update-TypeData -MemberType ScriptMethod -MemberName TestScriptMethod -Value $script2 -TypeName System.String -Force
		"string".TestScriptMethod() | Should Be "new script method"
	}
	<commit_msg>Update fixes based on comments for Update-TypeData Pester Test<commit_after>Describe "Update-TypeData" {

    Context "Validate Update-Type updates correctly" {

	It "Should not throw upon reloading previous formatting file" {
	    { Update-TypeData } | Should Not throw
	}

	It "Should validly load formatting data" {
	    { Get-TypeData -typename System.Diagnostics.Process | Export-TypeData -Path "outputfile.ps1xml" }
	    { Update-TypeData -prependPath "outputfile.ps1xml" | Should Not throw }
	    { Remove-Item "outputfile.ps1xml" -ErrorAction SilentlyContinue }
	}
    }
}

Describe "Update-TypeData basic functionality" -Tags DRT{
    $testfilename = "testfile.ps1xml"
    $testfile = Join-Path -Path $TestDrive -ChildPath $testfilename
	$invalidFileExtensionFile = Join-Path -Path $TestDrive -ChildPath "notmshxml"
	$filelist = Join-Path -Path $TestDrive -ChildPath "fileList.ps1xml"
	
	#Pester bug:https://github.com/PowerShell/psl-pester/issues/6
	It "Update-TypeData with Invalid TypesXml should throw Exception" -Pending{
		$xmlContent=@"
				<Types>
                    <Type>
                        <Name>AnyName</Name>
                        <MembersTypo>
                            <PropertySet>
                                <Name>PropertySetName</Name>
                                <ReferencedProperties>
                                    <Name>FirstName</Name> 
                                    <Name>LastName</Name> 
                                </ReferencedProperties>
                            </PropertySet>
                        </MembersTypo>
                        <MembersTypo2>
                            <PropertySet>
                                <Name>PropertySetName</Name>
                                <ReferencedProperties>
                                    <Name>FirstName</Name> 
                                    <Name>LastName</Name> 
                                </ReferencedProperties>
                            </PropertySet>
                        </MembersTypo2>
                    </Type>
				</Types>
"@
		$xmlContent>$testfile
		try
		{
			Update-TypeData -PrependPath $testfile -EA Stop
			Throw "Execution OK"
		}
		catch 
		{
			$error[0] | Should Match "Error: The node MembersTypo2 is not allowed."
			$_.CategoryInfo | Should Match "RuntimeException"
			$_.FullyQualifiedErrorId | Should be "TypesXmlUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand"
		}
		finally
		{
			Remove-TypeData -Path $testfile
			rm $testfile
		}
	}
	
	It "Update-TypeData with Invalid File Extension should throw Exception"{
		$xmlContent="not really an xml file, but we will not use it"
		$xmlContent>$invalidFileExtensionFile
		try
		{
			Update-TypeData -PrependPath $invalidFileExtensionFile -EA Stop
			Throw "Execution OK"
		}
		catch 
		{
			$error[0] | Should Match "because it does not have the file name extension"
			$_.CategoryInfo | Should Match "PSInvalidOperationException"
			$_.FullyQualifiedErrorId | Should be "WrongExtension,Microsoft.PowerShell.Commands.UpdateTypeDataCommand"
		}
		finally
		{
			rm $invalidFileExtensionFile
		}
	}
	
	It "Update-TypeData with Invalid File List Extension should throw Exception"{
		$xmlContent="not really an xml file, but we will not use it"
		$xmlContent>$invalidFileExtensionFile
		$filelistContent = "<Files><File>" + $invalidFileExtensionFile + "</File></Files>"
		$filelistContent>$filelist
		try
		{
			Update-TypeData -AppendPath $filelist -EA Stop
			Throw "Execution OK"
		}
		catch 
		{
			$error[0] | Should Match "ps1xml"
			$_.CategoryInfo | Should Match "RuntimeException"
			$_.FullyQualifiedErrorId | Should be "TypesXmlUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand"
		}
		finally
		{
			Remove-TypeData -Path $filelist
			rm $invalidFileExtensionFile
			rm $filelist
		}
	}
	
	It "Update-TypeData with Valid Dynamic Type NoteProperty with Force should work"{
		try
		{
			Update-TypeData -MemberType NoteProperty -MemberName TestNote -Value 2 -TypeName System.String
			"string".TestNote | Should Be 2
			Update-TypeData -MemberType NoteProperty -MemberName TestNote -Value "test" -TypeName System.String -Force
			"string".TestNote | Should Be "test"
			Update-TypeData -MemberType NoteProperty -MemberName TestNote -Value $null -TypeName System.String -Force
			"string".TestNote | Should BeNullOrEmpty
		}
		finally
		{
			Remove-TypeData System.String
		}
	}
	
	It "Update-TypeData with Valid Dynamic Type AliasProperty with Force should work"{
		try
		{
			Update-TypeData -MemberType AliasProperty -MemberName TestAlias -Value "Length" -TypeName System.String
			"string".TestAlias.GetType().Name | Should Be "Int32"
			"string".TestAlias | Should Be 6
			Update-TypeData -MemberType AliasProperty -MemberName TestAlias -Value "Length" -SecondValue "string" -TypeName System.String -Force
			"string".TestAlias.GetType().Name | Should Be "String"
			"string".TestAlias | Should Be 6
		}
		finally
		{
			Remove-TypeData System.String
		}
	}
	
	It "Update-TypeData with Valid Dynamic Type ScriptMethod with Force should work"{
		try
		{
			$script1={"script method"}
			Update-TypeData -MemberType ScriptMethod -MemberName TestScriptMethod -Value $script1 -TypeName System.String
			"string".TestScriptMethod() | Should Be "script method"
			$script2={"new script method"}
			Update-TypeData -MemberType ScriptMethod -MemberName TestScriptMethod -Value $script2 -TypeName System.String -Force
			"string".TestScriptMethod() | Should Be "new script method"
		}
		finally
		{
			Remove-TypeData System.String
		}
	}
	
{'fraction_non_alphanumeric': 0.09050350541746335, 'fraction_numerical': 0.00418829099517436, 'mean_word_length': 2.5883698137863442, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 79, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3858130', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3040, 'n_words': 810}