Skip to content

Commit 7526030

Browse files
author
Kapil Borle
authored
Merge pull request #777 from PowerShell/kapilmb/fix-formatter
Fix minor issues with Invoke-Formatter
2 parents 6a2a9d6 + 97dd4cb commit 7526030

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

Engine/Commands/InvokeFormatterCommand.cs

+21-14
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
2525
public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
2626
{
2727
private const string defaultSettingsPreset = "CodeFormatting";
28-
private Settings defaultSettings;
2928
private Settings inputSettings;
3029

3130
/// <summary>
3231
/// The script text to be formated.
3332
///
3433
/// *NOTE*: Unlike ScriptBlock parameter, the ScriptDefinition parameter require a string value.
3534
/// </summary>
36-
[ParameterAttribute(Mandatory = true)]
35+
[ParameterAttribute(Mandatory = true, Position = 1)]
3736
[ValidateNotNull]
3837
public string ScriptDefinition { get; set; }
3938

4039
/// <summary>
4140
/// A settings hashtable or a path to a PowerShell data file (.psd1) file that contains the settings.
4241
/// </summary>
43-
[Parameter(Mandatory = false)]
42+
[Parameter(Mandatory = false, Position = 2)]
4443
[ValidateNotNull]
45-
public object Settings { get; set; }
44+
public object Settings { get; set; } = defaultSettingsPreset;
4645

4746
#if DEBUG
4847
[Parameter(Mandatory = false)]
@@ -88,18 +87,26 @@ protected override void BeginProcessing()
8887

8988
try
9089
{
91-
inputSettings = PSSASettings.Create(Settings, null, this);
92-
if (inputSettings == null)
93-
{
94-
inputSettings = new PSSASettings(
95-
defaultSettingsPreset,
96-
PSSASettings.GetSettingPresetFilePath);
97-
}
90+
inputSettings = PSSASettings.Create(Settings, this.MyInvocation.PSScriptRoot, this);
9891
}
99-
catch
92+
catch (Exception e)
93+
{
94+
this.ThrowTerminatingError(new ErrorRecord(
95+
e,
96+
"SETTNGS_ERROR",
97+
ErrorCategory.InvalidData,
98+
Settings));
99+
}
100+
101+
if (inputSettings == null)
100102
{
101-
this.WriteWarning(String.Format(CultureInfo.CurrentCulture, Strings.SettingsNotParsable));
102-
return;
103+
this.ThrowTerminatingError(new ErrorRecord(
104+
new ArgumentException(String.Format(
105+
CultureInfo.CurrentCulture,
106+
Strings.SettingsNotParsable)),
107+
"SETTINGS_ERROR",
108+
ErrorCategory.InvalidArgument,
109+
Settings));
103110
}
104111
}
105112

Tests/Engine/InvokeFormatter.tests.ps1

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ Import-Module PSScriptAnalyzer
55
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")
66

77
Describe "Invoke-Formatter Cmdlet" {
8+
Context "When positional parameters are given" {
9+
It "Should use the positional parameters" {
10+
$def = @"
11+
function foo {
12+
"abc"
13+
}
14+
"@
15+
16+
$expected = @"
17+
function foo {
18+
"abc"
19+
}
20+
"@
21+
22+
$settings = @{
23+
IncludeRules = @('PSUseConsistentIndentation')
24+
Rules = @{
25+
PSUseConsistentIndentation = @{
26+
Enable = $true
27+
}
28+
}
29+
}
30+
31+
Invoke-Formatter $def $settings | Should Be $expected
32+
}
33+
}
834
Context "When no settings are given" {
935
It "Should format using default settings" {
1036
$def = @'

docs/markdown/Invoke-Formatter.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ schema: 2.0.0
44
---
55

66
# Invoke-Formatter
7-
87
## SYNOPSIS
98
Formats a script text based on the input settings or default settings.
109

@@ -61,7 +60,7 @@ This command formats the input script text using the settings defined in the `$s
6160

6261
### -------------------------- EXAMPLE 3 --------------------------
6362
```
64-
S> Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings /path/to/settings.psd1
63+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings /path/to/settings.psd1
6564
```
6665

6766
This command formats the input script text using the settings defined in the `settings.psd1` file.
@@ -75,11 +74,11 @@ The script text to be formated.
7574

7675
```yaml
7776
Type: String
78-
Parameter Sets:
77+
Parameter Sets: (All)
7978
Aliases:
8079

8180
Required: True
82-
Position: Named
81+
Position: 1
8382
Default value:
8483
Accept pipeline input: False
8584
Accept wildcard characters: False
@@ -90,11 +89,11 @@ A settings hashtable or a path to a PowerShell data file (.psd1) file that conta
9089
9190
```yaml
9291
Type: Object
93-
Parameter Sets:
92+
Parameter Sets: (All)
9493

9594
Required: False
96-
Position: Named
97-
Default value:
95+
Position: 2
96+
Default value: CodeFormatting
9897
Accept pipeline input: False
9998
Accept wildcard characters: False
10099
```

0 commit comments

Comments
 (0)