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

Commitc7acd5e

Browse files
committed
Clear-Variable - Support Scope parameter.
Also fix a bug in the PSVariableIntrinsics.SetAtScope which was notusing the correct scope.
1 parent2291fe1 commitc7acd5e

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public sealed class ClearVariableCommand : VariableCommandBase
2424
[Parameter]
2525
publicSwitchParameterPassThru{get;set;}
2626

27+
[ParameterAttribute]
28+
publicstringScope{get;set;}
29+
2730
publicClearVariableCommand()
2831
{
2932
// MUST: take these out into the base
@@ -33,13 +36,11 @@ public ClearVariableCommand()
3336

3437
protectedoverridevoidProcessRecord()
3538
{
36-
// TODO: deal with scope
3739
// TODO: deal with ShouldProcess
38-
// TODO: deal with read-only variables
3940

4041
foreach(stringnameinName)
4142
{
42-
PSVariablevariable=SessionState.PSVariable.Get(name);
43+
PSVariablevariable=SessionState.PSVariable.GetAtScope(name,Scope);
4344

4445
if(variable==null)
4546
{
@@ -51,7 +52,7 @@ protected override void ProcessRecord()
5152
{
5253
CheckVariableCanBeChanged(variable);
5354
variable.Value=null;
54-
SessionState.PSVariable.Set(variable,Force);
55+
SessionState.PSVariable.SetAtScope(variable,Scope,Force);
5556
}
5657
catch(SessionStateExceptionex)
5758
{

‎Source/ReferenceTests/Commands/ClearVariableTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,31 @@ public void TryToClearConstantVariableWithForce()
196196
ErrorRecorderror=ReferenceHost.GetLastRawErrorRecords().Single();
197197
Assert.AreEqual("Cannot overwrite variable foo because it is read-only or constant.",error.Exception.Message);
198198
}
199+
200+
[Test]
201+
publicvoidClearLocalScopeVariable()
202+
{
203+
stringresult=ReferenceHost.Execute(newstring[]{
204+
"$test = 'test-global'",
205+
"function foo { $test = 'test-local'; Clear-Variable test -Scope local; return $test; }",
206+
"$a = foo",
207+
"($a -eq $null).ToString() + ', ' + $test"
208+
});
209+
210+
Assert.AreEqual("True, test-global"+Environment.NewLine,result);
211+
}
212+
213+
[Test]
214+
publicvoidClearGlobalScopeVariable()
215+
{
216+
stringresult=ReferenceHost.Execute(newstring[]{
217+
"$test = 'test-global'",
218+
"function foo { $test = 'test-local'; Clear-Variable test -Scope global; return $test; }",
219+
"$a = foo",
220+
"$a + ', ' + ($test -eq $null).ToString()"
221+
});
222+
223+
Assert.AreEqual("test-local, True"+Environment.NewLine,result);
224+
}
199225
}
200226
}

‎Source/System.Management/Automation/PSVariableIntrinsics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal void SetAtScope(PSVariable variable, string scope, bool force = false)
7979
thrownewArgumentNullException("The variable is null.");
8080
}
8181

82-
varoriginal=_intrinsics.Scope.GetLocal(variable.Name);
82+
varoriginal=_intrinsics.Scope.GetAtScope(variable.Name,scope);
8383
if(original==null)
8484
{
8585
_intrinsics.Scope.SetAtScope(variable,scope,true);
@@ -89,7 +89,7 @@ internal void SetAtScope(PSVariable variable, string scope, bool force = false)
8989
original.Value=variable.Value;
9090
original.Description=variable.Description;
9191
original.Options=variable.Options;
92-
_intrinsics.Scope.SetLocal(original,true,force);
92+
_intrinsics.Scope.SetAtScope(original,scope,true,force);
9393
}
9494

9595
publicvoidSet(stringname,objectvalue)

‎Source/System.Management/Pash/Implementation/SessionStateScope.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ public void SetAtScope(T value, string scope, bool overwrite)
229229
affectedScope.SetLocal(value,overwrite);
230230
}
231231

232+
internalvoidSetAtScope(Tvalue,stringscope,booloverwrite,boolforce)
233+
{
234+
varaffectedScope=GetScope(scope,true,this);
235+
affectedScope.SetLocal(value,overwrite,force);
236+
}
237+
232238
publicvoidRemoveAtScope(stringname,stringscope)
233239
{
234240
varaffectedScope=GetScope(scope,true,this);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp