Skip to content

Commit a467aff

Browse files
author
Kapil Borle
committed
Merge pull request #509 from PowerShell/AddFullPath
Add ScriptPath property to DiagnosticRecord
2 parents cddf24f + bcaff26 commit a467aff

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

Engine/Generic/DiagnosticRecord.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// THE SOFTWARE.
1111
//
1212

13+
using System;
1314
using System.Management.Automation.Language;
1415

1516
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
@@ -24,7 +25,7 @@ public class DiagnosticRecord
2425
private IScriptExtent extent;
2526
private string ruleName;
2627
private DiagnosticSeverity severity;
27-
private string scriptName;
28+
private string scriptPath;
2829
private string ruleSuppressionId;
2930

3031
/// <summary>
@@ -33,7 +34,7 @@ public class DiagnosticRecord
3334
public string Message
3435
{
3536
get { return message; }
36-
set { message = value; }
37+
protected set { message = string.IsNullOrEmpty(value) ? string.Empty : value; }
3738
}
3839

3940
/// <summary>
@@ -42,7 +43,7 @@ public string Message
4243
public IScriptExtent Extent
4344
{
4445
get { return extent; }
45-
set { extent = value; }
46+
protected set { extent = value; }
4647
}
4748

4849
/// <summary>
@@ -51,7 +52,7 @@ public IScriptExtent Extent
5152
public string RuleName
5253
{
5354
get { return ruleName; }
54-
set { ruleName = value; }
55+
protected set { ruleName = string.IsNullOrEmpty(value) ? string.Empty : value; }
5556
}
5657

5758
/// <summary>
@@ -68,18 +69,16 @@ public DiagnosticSeverity Severity
6869
/// </summary>
6970
public string ScriptName
7071
{
71-
get { return scriptName; }
72-
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
73-
set {
74-
if (!string.IsNullOrWhiteSpace(value))
75-
{
76-
scriptName = System.IO.Path.GetFileName(value);
77-
}
78-
else
79-
{
80-
scriptName = string.Empty;
81-
}
82-
}
72+
get { return string.IsNullOrEmpty(scriptPath) ? string.Empty : System.IO.Path.GetFileName(scriptPath);}
73+
}
74+
75+
/// <summary>
76+
/// Returns the path of the script.
77+
/// </summary>
78+
public string ScriptPath
79+
{
80+
get { return scriptPath; }
81+
protected set { scriptPath = string.IsNullOrEmpty(value) ? string.Empty : value; }
8382
}
8483

8584
/// <summary>
@@ -106,16 +105,28 @@ public DiagnosticRecord()
106105
/// <param name="extent">The place in the script this diagnostic refers to</param>
107106
/// <param name="ruleName">The name of the rule that created this diagnostic</param>
108107
/// <param name="severity">The severity of this diagnostic</param>
109-
/// <param name="scriptName">The name of the script file being analyzed</param>
110-
public DiagnosticRecord(string message, IScriptExtent extent, string ruleName, DiagnosticSeverity severity, string scriptName, string ruleId = null)
108+
/// <param name="scriptName">The path of the script file being analyzed</param>
109+
public DiagnosticRecord(string message, IScriptExtent extent, string ruleName, DiagnosticSeverity severity, string scriptPath, string ruleId = null)
111110
{
112-
Message = string.IsNullOrEmpty(message) ? string.Empty : message;
113-
RuleName = string.IsNullOrEmpty(ruleName) ? string.Empty : ruleName;
111+
Message = message;
112+
RuleName = ruleName;
114113
Extent = extent;
115114
Severity = severity;
116-
ScriptName = string.IsNullOrEmpty(scriptName) ? string.Empty : scriptName;
115+
ScriptPath = scriptPath;
117116
ruleSuppressionId = ruleId;
118117
}
118+
119+
/// <summary>
120+
/// Copy Constructor
121+
/// </summary>
122+
/// <param name="record"></param>
123+
public DiagnosticRecord(DiagnosticRecord diagnosticRecord)
124+
{
125+
if (diagnosticRecord == null)
126+
{
127+
throw new ArgumentNullException("diagnosticRecord");
128+
}
129+
}
119130
}
120131

121132

Engine/Generic/SuppressedRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public SuppressedRecord(DiagnosticRecord record, RuleSuppression suppression)
4040
Message = record.Message;
4141
Extent = record.Extent;
4242
Severity = record.Severity;
43-
ScriptName = record.ScriptName;
43+
ScriptPath = record.ScriptPath;
4444
RuleSuppressionID = record.RuleSuppressionID;
4545
}
4646
}

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ Describe "Test Path" {
124124
}
125125
}
126126

127+
Context "DiagnosticRecord " {
128+
It "has valid ScriptPath and ScriptName properties when an input file is given" {
129+
$scriptName = "TestScript.ps1"
130+
$scriptPath = Join-Path $directory $scriptName
131+
$expectedScriptPath = Resolve-Path $directory\TestScript.ps1
132+
$diagnosticRecords = Invoke-ScriptAnalyzer $scriptPath -IncludeRule "PSAvoidUsingEmptyCatchBlock"
133+
$diagnosticRecords[0].ScriptPath | Should Be $expectedScriptPath.Path
134+
$diagnosticRecords[0].ScriptName | Should Be $scriptName
135+
}
136+
137+
It "has empty ScriptPath and ScriptName properties when a script definition is given" {
138+
$diagnosticRecords = Invoke-ScriptAnalyzer -ScriptDefinition gci -IncludeRule "PSAvoidUsingCmdletAliases"
139+
$diagnosticRecords[0].ScriptPath | Should Be ([System.String]::Empty)
140+
$diagnosticRecords[0].ScriptName | Should Be ([System.String]::Empty)
141+
}
142+
}
143+
127144
if (!$testingLibraryUsage)
128145
{
129146
#There is probably a more concise way to do this but for now we will settle for this!
@@ -177,7 +194,6 @@ Describe "Test Path" {
177194
Write-Output $writeHostViolation.Count
178195
$globalVarsViolation.Count -eq 1 -and $writeHostViolation.Count -eq 1 | Should Be $true
179196
}
180-
181197
}
182198
}
183199

0 commit comments

Comments
 (0)