Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 4, 2019. It is now read-only.
/PashPublic archive

Commit18499d3

Browse files
committed
Clear-Variable - Unknown variable name reports an error.
1 parent05fe9de commit18499d3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

‎Source/Microsoft.PowerShell.Commands.Utility/ClearVariableCommand.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ protected override void ProcessRecord()
4141
{
4242
PSVariablevariable=SessionState.PSVariable.Get(name);
4343

44+
if(variable==null)
45+
{
46+
WriteVariableNotFoundError(name);
47+
continue;
48+
}
49+
4450
try
4551
{
4652
SessionState.PSVariable.Set(variable.Name,null);
@@ -56,5 +62,14 @@ protected override void ProcessRecord()
5662
}
5763
}
5864
}
65+
66+
privatevoidWriteVariableNotFoundError(stringname)
67+
{
68+
varexception=newItemNotFoundException(String.Format("Cannot find a variable with name '{0}'.",name));
69+
stringerrorId="VariableNotFound,"+typeof(ClearVariableCommand).FullName;
70+
varerror=newErrorRecord(exception,errorId,ErrorCategory.ObjectNotFound,name);
71+
error.CategoryInfo.Activity="Clear-Variable";
72+
WriteError(error);
73+
}
5974
}
6075
}

‎Source/ReferenceTests/Commands/ClearVariableTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,40 @@ public void PassThruTwoVariablesCleared()
7070

7171
Assert.AreEqual("Names=foo,bar Values are null=True,True Type=Object[] PSVariable"+Environment.NewLine,result);
7272
}
73+
74+
[Test]
75+
publicvoidUnknownNameCausesError()
76+
{
77+
Assert.Throws<ExecutionWithErrorsException>(delegate
78+
{
79+
ReferenceHost.Execute("Clear-Variable unknownvariable");
80+
});
81+
82+
ErrorRecorderror=ReferenceHost.GetLastRawErrorRecords().Single();
83+
Assert.AreEqual("Cannot find a variable with name 'unknownvariable'.",error.Exception.Message);
84+
Assert.AreEqual("VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand",error.FullyQualifiedErrorId);
85+
Assert.AreEqual("unknownvariable",error.TargetObject);
86+
Assert.IsInstanceOf<ItemNotFoundException>(error.Exception);
87+
Assert.AreEqual("Clear-Variable",error.CategoryInfo.Activity);
88+
Assert.AreEqual(ErrorCategory.ObjectNotFound,error.CategoryInfo.Category);
89+
Assert.AreEqual("ItemNotFoundException",error.CategoryInfo.Reason);
90+
Assert.AreEqual("unknownvariable",error.CategoryInfo.TargetName);
91+
Assert.AreEqual("String",error.CategoryInfo.TargetType);
92+
}
93+
94+
[Test]
95+
publicvoidTwoUnknownNamesCausesTwoErrors()
96+
{
97+
Assert.Throws<ExecutionWithErrorsException>(delegate
98+
{
99+
ReferenceHost.Execute("Clear-Variable unknownvariable1,unknownvariable2");
100+
});
101+
102+
ErrorRecorderror1=ReferenceHost.GetLastRawErrorRecords().First();
103+
ErrorRecorderror2=ReferenceHost.GetLastRawErrorRecords().Last();
104+
Assert.AreEqual(2,ReferenceHost.GetLastRawErrorRecords().Count());
105+
Assert.AreEqual("Cannot find a variable with name 'unknownvariable1'.",error1.Exception.Message);
106+
Assert.AreEqual("Cannot find a variable with name 'unknownvariable2'.",error2.Exception.Message);
107+
}
73108
}
74109
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp