Skip to content

Commit db1d42d

Browse files
committed
Exclude double ReadAllText() from EndProcessing
1 parent 0e84b4d commit db1d42d

2 files changed

Lines changed: 66 additions & 35 deletions

File tree

src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,14 @@ internal string GetUsingSet(Language language)
763763
/// </summary>
764764
protected override void EndProcessing()
765765
{
766+
// Prevent code compilation in ConstrainedLanguage mode
767+
if (SessionState.LanguageMode == PSLanguageMode.ConstrainedLanguage)
768+
{
769+
ThrowTerminatingError(
770+
new ErrorRecord(
771+
new PSNotSupportedException(AddTypeStrings.CannotDefineNewType), "CannotDefineNewType", ErrorCategory.PermissionDenied, null));
772+
}
773+
766774
// Generate an error if they've specified an output
767775
// assembly type without an output assembly
768776
if (String.IsNullOrEmpty(outputAssembly) && outputTypeSpecified)
@@ -779,36 +787,6 @@ protected override void EndProcessing()
779787
ThrowTerminatingError(errorRecord);
780788
return;
781789
}
782-
783-
PopulateSource();
784-
}
785-
786-
internal void PopulateSource()
787-
{
788-
// Prevent code compilation in ConstrainedLanguage mode
789-
if (SessionState.LanguageMode == PSLanguageMode.ConstrainedLanguage)
790-
{
791-
ThrowTerminatingError(
792-
new ErrorRecord(
793-
new PSNotSupportedException(AddTypeStrings.CannotDefineNewType), "CannotDefineNewType", ErrorCategory.PermissionDenied, null));
794-
}
795-
796-
// Load the source if they want to load from a file
797-
if (String.Equals(ParameterSetName, "FromPath", StringComparison.OrdinalIgnoreCase) ||
798-
String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase)
799-
)
800-
{
801-
sourceCode = "";
802-
foreach (string file in paths)
803-
{
804-
sourceCode += System.IO.File.ReadAllText(file) + "\n";
805-
}
806-
}
807-
808-
if (String.Equals(ParameterSetName, "FromMember", StringComparison.OrdinalIgnoreCase))
809-
{
810-
sourceCode = GenerateTypeSource(typeNamespace, Name, sourceCode, language);
811-
}
812790
}
813791

814792
internal void HandleCompilerErrors(AddTypeCompilerError[] compilerErrors)
@@ -932,13 +910,23 @@ protected override void EndProcessing()
932910
{
933911
// Load the source if they want to load from a file
934912
if (String.Equals(ParameterSetName, "FromPath", StringComparison.OrdinalIgnoreCase) ||
935-
String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase))
913+
String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase)
914+
)
936915
{
937-
this.sourceCode = "";
916+
StringBuilder sb = new StringBuilder(paths.Length);
917+
sourceCode = "";
918+
938919
foreach (string file in paths)
939920
{
940-
this.sourceCode += System.IO.File.ReadAllText(file) + "\n";
921+
sb.Append(System.IO.File.ReadAllText(file));
922+
sb.Append("\n");
941923
}
924+
925+
sourceCode = sb.ToString();
926+
}
927+
else if (String.Equals(ParameterSetName, "FromMember", StringComparison.OrdinalIgnoreCase))
928+
{
929+
sourceCode = GenerateTypeSource(typeNamespace, Name, sourceCode, language);
942930
}
943931

944932
CompileSourceToAssembly(this.sourceCode);

test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1-
$guid = [Guid]::NewGuid().ToString().Replace("-","")
2-
31
Describe "Add-Type" -Tags "CI" {
2+
BeforeAll {
3+
$guid = [Guid]::NewGuid().ToString().Replace("-","")
4+
5+
$code1 = @"
6+
namespace Test.AddType
7+
{
8+
public class BasicTest1
9+
{
10+
public static int Add1(int a, int b)
11+
{
12+
return (a + b);
13+
}
14+
}
15+
}
16+
"@
17+
$code2 = @"
18+
namespace Test.AddType
19+
{
20+
public class BasicTest2
21+
{
22+
public static int Add2(int a, int b)
23+
{
24+
return (a + b);
25+
}
26+
}
27+
}
28+
"@
29+
$codeFile1 = Join-Path -Path $TestDrive -ChildPath "codeFile1.cs"
30+
$codeFile2 = Join-Path -Path $TestDrive -ChildPath "codeFile2.cs"
31+
32+
Set-Content -Path $codeFile1 -Value $code1 -Force
33+
Set-Content -Path $codeFile2 -Value $code2 -Force
34+
}
35+
436
It "Public 'Language' enumeration contains all members" {
537
[Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should Be "CSharp,CSharpVersion7,CSharpVersion6,CSharpVersion5,CSharpVersion4,CSharpVersion3,CSharpVersion2,CSharpVersion1,VisualBasic,JScript"
638
}
@@ -20,4 +52,15 @@ public class AttributeTest$guid {}
2052
It "Can load TPA assembly System.Runtime.Serialization.Primitives.dll" {
2153
Add-Type -AssemblyName 'System.Runtime.Serialization.Primitives' -PassThru | Should Not Be $null
2254
}
55+
56+
It "Can compile C# files" {
57+
58+
{ [Test.AddType.BasicTest1]::Add1(1, 2) } | Should Throw
59+
{ [Test.AddType.BasicTest2]::Add2(3, 4) } | Should Throw
60+
61+
Add-Type -Path $codeFile1,$codeFile2
62+
63+
{ [Test.AddType.BasicTest1]::Add1(1, 2) } | Should Not Throw
64+
{ [Test.AddType.BasicTest2]::Add2(3, 4) } | Should Not Throw
65+
}
2366
}

0 commit comments

Comments
 (0)