diff --git a/src/System.Management.Automation/engine/hostifaces/History.cs b/src/System.Management.Automation/engine/hostifaces/History.cs
index 785c34a89e3..7ab120d3065 100644
--- a/src/System.Management.Automation/engine/hostifaces/History.cs
+++ b/src/System.Management.Automation/engine/hostifaces/History.cs
@@ -19,8 +19,6 @@ namespace Microsoft.PowerShell.Commands
///
public class HistoryInfo
{
- #region constuctor
-
///
/// Constructor
///
@@ -34,11 +32,11 @@ internal HistoryInfo(long pipelineId, string cmdline, PipelineState status, Date
{
Dbg.Assert(cmdline != null, "caller should validate the parameter");
_pipelineId = pipelineId;
- _cmdline = cmdline;
- _status = status;
- _startTime = startTime;
- _endTime = endTime;
- _cleared = false;
+ CommandLine = cmdline;
+ ExecutionStatus = status;
+ StartExecutionTime = startTime;
+ EndExecutionTime = endTime;
+ Cleared = false;
}
///
@@ -47,77 +45,49 @@ internal HistoryInfo(long pipelineId, string cmdline, PipelineState status, Date
///
private HistoryInfo(HistoryInfo history)
{
- _id = history._id;
+ Id = history.Id;
_pipelineId = history._pipelineId;
- _cmdline = history._cmdline;
- _status = history._status;
- _startTime = history._startTime;
- _endTime = history._endTime;
- _cleared = history._cleared;
+ CommandLine = history.CommandLine;
+ ExecutionStatus = history.ExecutionStatus;
+ StartExecutionTime = history.StartExecutionTime;
+ EndExecutionTime = history.EndExecutionTime;
+ Cleared = history.Cleared;
}
- #endregion constructor
-
- #region public
///
/// Id of this history entry.
///
///
- public long Id
- {
- get
- {
- return _id;
- }
- }
+ public long Id { get; private set; }
///
/// CommandLine string
///
///
- public string CommandLine
- {
- get
- {
- return _cmdline;
- }
- }
+ public string CommandLine { get; private set; }
///
/// Execution status of associated pipeline
///
///
- public PipelineState ExecutionStatus
- {
- get
- {
- return _status;
- }
- }
+ public PipelineState ExecutionStatus { get; private set; }
///
/// Start time of execution of associated pipeline
///
///
- public DateTime StartExecutionTime
- {
- get
- {
- return _startTime;
- }
- }
+ public DateTime StartExecutionTime { get; }
///
/// End time of execution of associated pipeline
///
///
- public DateTime EndExecutionTime
- {
- get
- {
- return _endTime;
- }
- }
+ public DateTime EndExecutionTime { get; private set; }
+
+ ///
+ /// The time it took to execute the associeated pipeline
+ ///
+ public TimeSpan Duration => EndExecutionTime - StartExecutionTime;
///
/// Override for ToString() method
@@ -125,115 +95,51 @@ public DateTime EndExecutionTime
///
public override string ToString()
{
- if (string.IsNullOrEmpty(_cmdline))
+ if (string.IsNullOrEmpty(CommandLine))
{
return base.ToString();
}
else
{
- return _cmdline;
+ return CommandLine;
}
}
- #endregion public
-
- #region internal
-
///
/// Cleared status of an entry
///
- internal bool Cleared
- {
- get
- {
- return _cleared;
- }
- set
- {
- _cleared = value;
- }
- }
+ internal bool Cleared { get; set; } = false;
///
/// Sets Id
///
///
- internal void SetId(long id)
- {
- _id = id;
- }
+ internal void SetId(long id) => Id = id;
///
/// Set status
///
///
- internal void SetStatus(PipelineState status)
- {
- _status = status;
- }
+ internal void SetStatus(PipelineState status) => ExecutionStatus = status;
///
/// Set endtime
///
///
- internal void SetEndTime(DateTime endTime)
- {
- _endTime = endTime;
- }
+ internal void SetEndTime(DateTime endTime) => EndExecutionTime = endTime;
///
/// Sets command
///
///
- internal void SetCommand(string command)
- {
- _cmdline = command;
- }
-
- #endregion internal
-
- #region private
+ internal void SetCommand(string command) => CommandLine = command;
///
/// Id of the pipeline corresponding to this history entry
///
private long _pipelineId;
- ///
- /// Id of the history entry
- ///
- private long _id;
-
- ///
- /// CommandLine string
- ///
- private string _cmdline;
-
- ///
- /// ExecutionStatus of execution
- ///
- private PipelineState _status;
-
- ///
- /// Start time of execution
- ///
- private DateTime _startTime;
-
- ///
- ///End time of execution
- ///
- private DateTime _endTime;
-
- ///
- /// Flag indicating an entry is present/cleared
- ///
-
- private bool _cleared = false;
-
- #endregion private
-
- #region ICloneable Members
///
/// Returns a clone of this object
@@ -243,8 +149,6 @@ public HistoryInfo Clone()
{
return new HistoryInfo(this);
}
-
- #endregion
}
///
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/History.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/History.Tests.ps1
index 199c6f11a2d..4e2f1004bd1 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Core/History.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Core/History.Tests.ps1
@@ -8,11 +8,11 @@ Describe "History cmdlet test cases" -Tags "CI" {
$ps = [PowerShell]::Create("NewRunspace")
# we need to be sure that history is added, so use the proper
# Invoke variant
- $null = $ps.addcommand("Get-Date").Invoke($null,$setting)
+ $null = $ps.addcommand("Get-Date").Invoke($null, $setting)
$ps.commands.clear()
- $null = $ps.addscript("1+1").Invoke($null,$setting)
+ $null = $ps.addscript("1+1").Invoke($null, $setting)
$ps.commands.clear()
- $null = $ps.addcommand("Get-Location").Invoke($null,$setting)
+ $null = $ps.addcommand("Get-Location").Invoke($null, $setting)
$ps.commands.clear()
}
AfterEach {
@@ -38,18 +38,18 @@ Describe "History cmdlet test cases" -Tags "CI" {
}
It "Add-History actually adds to history" {
# add this invocation to history
- $ps.AddScript("Get-History|Add-History").Invoke($null,$setting)
+ $ps.AddScript("Get-History|Add-History").Invoke($null, $setting)
# that's 4 history lines * 2
$ps.Commands.Clear()
$result = $ps.AddCommand("Get-History").Invoke()
$result.Count | Should -Be 8
- for($i = 0; $i -lt 4; $i++) {
- $result[$i+4].CommandLine | Should -BeExactly $result[$i].CommandLine
+ for ($i = 0; $i -lt 4; $i++) {
+ $result[$i + 4].CommandLine | Should -BeExactly $result[$i].CommandLine
}
}
}
- It "Tests Invoke-History on a cmdlet that generates output on all streams" {
+ It "Tests Invoke-History on a cmdlet that generates output on all streams" {
$streamSpammer = '
function StreamSpammer
{
@@ -93,7 +93,7 @@ Describe "History cmdlet test cases" -Tags "CI" {
$outputCount | Should -Be 12
}
- It "Tests Invoke-History on a private command" {
+ It "Tests Invoke-History on a private command" {
$invocationSettings = New-Object System.Management.Automation.PSInvocationSettings
$invocationSettings.AddToHistory = $true
@@ -110,4 +110,19 @@ Describe "History cmdlet test cases" -Tags "CI" {
$errorResult | Should -BeExactly 'CommandNotFoundException'
}
+
+ It "HistoryInfo calculates Duration" {
+ $start = [datetime]::new(2001, 01, 01, 10, 01, 01)
+ $duration = [timespan] "1:2:21"
+ $end = $start + $duration
+ $history = [PSCustomObject] @{
+ CommandLine = "command"
+ ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::Completed
+ StartExecutionTime = $start
+ EndExecutionTime = $end
+ }
+ $history | Add-History
+ $h = Get-History -count 1
+ $h.Duration | Should -Be $duration
+ }
}