Skip to content

Commit 5ca074b

Browse files
Adding implementation for non-class based resources
1 parent 0d539ee commit 5ca074b

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

Rules/DscTestsPresent.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
3030
/// Tests folder should contain test script for given resource - file name should contain resource's name.
3131
/// </summary>
3232
[Export(typeof(IDSCResourceRule))]
33-
public class DscExamplesPresent : IDSCResourceRule
33+
public class DscTestsPresent : IDSCResourceRule
3434
{
3535
/// <summary>
3636
/// AnalyzeDSCResource: Analyzes given DSC Resource
@@ -40,7 +40,29 @@ public class DscExamplesPresent : IDSCResourceRule
4040
/// <returns>The results of the analysis</returns>
4141
public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName)
4242
{
43-
43+
String fileNameOnly = Path.GetFileName(fileName);
44+
String resourceName = Path.GetFileNameWithoutExtension(fileNameOnly);
45+
String testsQuery = String.Format("*{0}*", resourceName);
46+
Boolean testsPresent = false;
47+
String expectedTestsPath = Path.Combine(new String[] { fileName, "..", "..", "..", "Tests" });
48+
49+
// Verify tests are present
50+
if (Directory.Exists(expectedTestsPath))
51+
{
52+
DirectoryInfo testsFolder = new DirectoryInfo(expectedTestsPath);
53+
FileInfo[] testFiles = testsFolder.GetFiles(testsQuery);
54+
if (testFiles.Length != 0)
55+
{
56+
testsPresent = true;
57+
}
58+
}
59+
60+
// Return error if no tests present
61+
if (!testsPresent)
62+
{
63+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentNoTestsError, resourceName),
64+
ast.Extent, GetName(), DiagnosticSeverity.Information, fileName);
65+
}
4466
}
4567

4668
/// <summary>
@@ -60,7 +82,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName)
6082
/// <returns>The name of this rule</returns>
6183
public string GetName()
6284
{
63-
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.DscExamplesPresent);
85+
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.DscTestsPresent);
6486
}
6587

6688
/// <summary>
@@ -69,7 +91,7 @@ public string GetName()
6991
/// <returns>The common name of this rule</returns>
7092
public string GetCommonName()
7193
{
72-
return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentCommonName);
94+
return string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentCommonName);
7395
}
7496

7597
/// <summary>
@@ -78,7 +100,7 @@ public string GetCommonName()
78100
/// <returns>The description of this rule</returns>
79101
public string GetDescription()
80102
{
81-
return string.Format(CultureInfo.CurrentCulture, Strings.DscExamplesPresentDescription);
103+
return string.Format(CultureInfo.CurrentCulture, Strings.DscTestsPresentDescription);
82104
}
83105

84106
/// <summary>

Rules/Strings.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,16 @@
690690
<data name="UseOutputTypeCorrectlyName" xml:space="preserve">
691691
<value>UseOutputTypeCorrectly</value>
692692
</data>
693+
<data name="DscTestsPresent" xml:space="preserve">
694+
<value>DscTestsPresent</value>
695+
</data>
696+
<data name="DscTestsPresentCommonName" xml:space="preserve">
697+
<value>Dsc tests are present</value>
698+
</data>
699+
<data name="DscTestsPresentDescription" xml:space="preserve">
700+
<value>Every DSC resource module should contain folder "Tests" with tests for every resource. Test scripts should have resource name they are testing in the file name.</value>
701+
</data>
702+
<data name="DscTestsPresentNoTestsError" xml:space="preserve">
703+
<value>No tests found for resource '{0}'</value>
704+
</data>
693705
</root>

0 commit comments

Comments
 (0)