Skip to content

Integrate Development to Master #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
59d5a58
Implement UseOutputTypeCorrectly
Apr 24, 2015
70a48d0
Fix comment and change severity to warning
Apr 24, 2015
44130ce
Add rule documentation for useoutputtypecorrectly
Apr 27, 2015
3c84251
Merge branch 'returntyperule' of https://github.com/PowerShell/PSScri…
Apr 27, 2015
4f9972e
Change severity of useoutputtypecorrectly to information
Apr 27, 2015
2af9cb8
Merge pull request #69 from PowerShell/returntyperule
Apr 27, 2015
ae0b374
Adding DscExamplesPresent rule scaffolding
KarolKaczmarek Apr 28, 2015
6ff3193
Fix typo
KarolKaczmarek Apr 28, 2015
d94733c
Adding implementation for class based resources
KarolKaczmarek May 5, 2015
823ac98
Adding tests for class based resource
KarolKaczmarek May 5, 2015
da42209
Changes in AnalyzeDSCClass to be called only for class based resource
KarolKaczmarek May 5, 2015
110b53c
Replacing string operations with functions
KarolKaczmarek May 5, 2015
96342d9
Populating extent
KarolKaczmarek May 5, 2015
0d539ee
Adding scaffolding for DscTestsPresent rule
KarolKaczmarek May 5, 2015
5ca074b
Adding implementation for non-class based resources
KarolKaczmarek May 5, 2015
d099687
Adding rule to project file
KarolKaczmarek May 5, 2015
ed2f291
Adding implementation for class based resources
KarolKaczmarek May 5, 2015
bf204bd
Adding tests
KarolKaczmarek May 5, 2015
8fd01d1
Create documentation in DscExamplesPresent.md
KarolKaczmarek May 6, 2015
3f6ebbe
Merge pull request #95 from KarolKaczmarek/DscExamplesPresentRule
May 6, 2015
8730151
Create documentation for DscTestsPresent rule
KarolKaczmarek May 6, 2015
8a6e394
Fixing tests
KarolKaczmarek May 6, 2015
2a986e0
Merge branch 'DscTestsPresentRule' of github.com:KarolKaczmarek/PSScr…
KarolKaczmarek May 6, 2015
4fbd842
Merge branch 'master' of https://github.com/PowerShell/PSScriptAnalyz…
KarolKaczmarek May 6, 2015
5892a67
Resolving conflicts
KarolKaczmarek May 6, 2015
0bade57
Merge pull request #106 from KarolKaczmarek/DscTestsPresentRule
yutingc May 7, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,11 @@ public object VisitConvertExpression(ConvertExpressionAst convAst)
{
if (convAst != null)
{
if (convAst.Type.TypeName.GetReflectionType() != null)
{
return convAst.Type.TypeName.GetReflectionType().FullName;
}

return convAst.Type.TypeName.FullName;
}

Expand Down Expand Up @@ -2135,6 +2140,11 @@ public object VisitTypeConstraint(TypeConstraintAst typeAst)
{
if (typeAst != null)
{
if (typeAst.TypeName.GetReflectionType() != null)
{
return typeAst.TypeName.GetReflectionType().FullName;
}

return typeAst.TypeName.FullName;
}

Expand All @@ -2160,6 +2170,11 @@ public object VisitTypeExpression(TypeExpressionAst typeExpressionAst)
{
if (typeExpressionAst != null)
{
if (typeExpressionAst.TypeName.GetReflectionType() != null)
{
return typeExpressionAst.TypeName.GetReflectionType().FullName;
}

return typeExpressionAst.TypeName.FullName;
}

Expand Down
55 changes: 55 additions & 0 deletions RuleDocumentation/DscExamplesPresent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#DscExamplesPresent
**Severity Level: Information**


##Description

Checks that DSC examples for given resource are present.

##How to Fix

To fix a violation of this rule, please make sure Examples directory is present:
* For non-class based resources it should exist at the same folder level as DSCResources folder.
* For class based resources it should be present at the same folder level as resource psm1 file.

Examples folder should contain sample configuration for given resource - file name should contain resource's name.

##Example

### Non-class based resource

Let's assume we have non-class based resource with a following file structure:

* xAzure
* DSCResources
* MSFT_xAzureSubscription
* MSFT_xAzureSubscription.psm1
* MSFT_xAzureSubscription.schema.mof

In this case, to fix this warning, we should add examples in a following way:

* xAzure
* DSCResources
* MSFT_xAzureSubscription
* MSFT_xAzureSubscription.psm1
* MSFT_xAzureSubscription.schema.mof
* Examples
* MSFT_xAzureSubscription_AddSubscriptionExample.ps1
* MSFT_xAzureSubscription_RemoveSubscriptionExample.ps1

### Class based resource

Let's assume we have class based resource with a following file structure:

* MyDscResource
* MyDscResource.psm1
* MyDscresource.psd1

In this case, to fix this warning, we should add examples in a following way:

* MyDscResource
* MyDscResource.psm1
* MyDscresource.psd1
* Tests
* MyDscResource_Example1.ps1
* MyDscResource_Example2.ps1
54 changes: 54 additions & 0 deletions RuleDocumentation/DscTestsPresent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#DscTestsPresent
**Severity Level: Information**


##Description

Checks that DSC tests for given resource are present.

##How to Fix

To fix a violation of this rule, please make sure Tests directory is present:
* For non-class based resources it should exist at the same folder level as DSCResources folder.
* For class based resources it should be present at the same folder level as resource psm1 file.

Tests folder should contain test script for given resource - file name should contain resource's name.

##Example

### Non-class based resource

Let's assume we have non-class based resource with a following file structure:

* xAzure
* DSCResources
* MSFT_xAzureSubscription
* MSFT_xAzureSubscription.psm1
* MSFT_xAzureSubscription.schema.mof

In this case, to fix this warning, we should add tests in a following way:

* xAzure
* DSCResources
* MSFT_xAzureSubscription
* MSFT_xAzureSubscription.psm1
* MSFT_xAzureSubscription.schema.mof
* Tests
* MSFT_xAzureSubscription_Tests.ps1

### Class based resource

Let's assume we have class based resource with a following file structure:

* MyDscResource
* MyDscResource.psm1
* MyDscresource.psd1

In this case, to fix this warning, we should add tests in a following way:

* MyDscResource
* MyDscResource.psm1
* MyDscresource.psd1
* Tests
* MyDscResource_Tests.ps1

37 changes: 37 additions & 0 deletions RuleDocumentation/UseOutputTypeCorrectly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#UseOutputTypeCorrectly
**Severity Level: Information**


##Description

If a return type is declared, the cmdlet must return that type. If a type is returned, a return type must be declared.

##How to Fix

To fix a violation of this rule, please check the OuputType attribute lists and the types that are returned in your code. You can get more details by running “Get-Help about_Functions_OutputTypeAttribute” command in Windows PowerShell.

##Example

##Example
Wrong:

function Get-Foo
{
[CmdletBinding()]
[OutputType([String])]
Param(
)
return "4
}

Correct:

function Get-Foo
{
[CmdletBinding()]
[OutputType([String])]
Param(
)

return "bar"
}
164 changes: 164 additions & 0 deletions Rules/DscExamplesPresent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//
// Copyright (c) Microsoft Corporation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Language;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Management.Automation;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// DscExamplesPresent: Checks that DSC examples for given resource are present.
/// Rule expects directory Examples to be present:
/// For non-class based resources it should exist at the same folder level as DSCResources folder.
/// For class based resources it should be present at the same folder level as resource psm1 file.
/// Examples folder should contain sample configuration for given resource - file name should contain resource's name.
/// </summary>
[Export(typeof(IDSCResourceRule))]
public class DscExamplesPresent : IDSCResourceRule
{
/// <summary>
/// AnalyzeDSCResource: Analyzes given DSC Resource
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The name of the script file being analyzed</param>
/// <returns>The results of the analysis</returns>
public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName)
{
String fileNameOnly = Path.GetFileName(fileName);
String resourceName = Path.GetFileNameWithoutExtension(fileNameOnly);
String examplesQuery = String.Format("*{0}*", resourceName);
Boolean examplesPresent = false;
String expectedExamplesPath = Path.Combine(new String[] {fileName, "..", "..", "..", "Examples"});

// Verify examples are present
if (Directory.Exists(expectedExamplesPath))
{
DirectoryInfo examplesFolder = new DirectoryInfo(expectedExamplesPath);
FileInfo[] exampleFiles = examplesFolder.GetFiles(examplesQuery);
if (exampleFiles.Length != 0)
{
examplesPresent = true;
}
}

// Return error if no examples present
if (!examplesPresent)
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentNoExamplesError, resourceName),
ast.Extent, GetName(), DiagnosticSeverity.Information, fileName);
}
}

/// <summary>
/// AnalyzeDSCClass: Analyzes given DSC class
/// </summary>
/// <param name="ast"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName)
{
String resourceName = null;

IEnumerable<Ast> dscClasses = ast.FindAll(item =>
item is TypeDefinitionAst
&& ((item as TypeDefinitionAst).IsClass)
&& (item as TypeDefinitionAst).Attributes.Any(attr => String.Equals("DSCResource", attr.TypeName.FullName, StringComparison.OrdinalIgnoreCase)), true);

foreach (TypeDefinitionAst dscClass in dscClasses)
{
resourceName = dscClass.Name;

String examplesQuery = String.Format("*{0}*", resourceName);
Boolean examplesPresent = false;
String expectedExamplesPath = Path.Combine(new String[] {fileName, "..", "Examples"});

// Verify examples are present
if (Directory.Exists(expectedExamplesPath))
{
DirectoryInfo examplesFolder = new DirectoryInfo(expectedExamplesPath);
FileInfo[] exampleFiles = examplesFolder.GetFiles(examplesQuery);
if (exampleFiles.Length != 0)
{
examplesPresent = true;
}
}

// Return error if no examples present
if (!examplesPresent)
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentNoExamplesError, resourceName),
dscClass.Extent, GetName(), DiagnosticSeverity.Information, fileName);
}
}
}

/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.DscExamplesPresent);
}

/// <summary>
/// GetCommonName: Retrieves the Common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentCommonName);
}

/// <summary>
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentDescription);
}

/// <summary>
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public SourceType GetSourceType()
{
return SourceType.Builtin;
}

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning or information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Information;
}

/// <summary>
/// GetSourceName: Retrieves the module/assembly name the rule is from.
/// </summary>
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.DSCSourceName);
}
}

}
Loading