Skip to content

Commit b51cf13

Browse files
authored
Merge pull request #47 from PavelBansky/master
Master
2 parents 5af7997 + de87276 commit b51cf13

File tree

7 files changed

+181
-12
lines changed

7 files changed

+181
-12
lines changed

src/Microsoft.DevSkim/Microsoft.DevSkim.CLI.Tests/AnalyzeTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Microsoft.DevSkim.CLI.Tests
44
{
55
[TestClass]
6-
public class UnitTest1
6+
public class AnalyzeTest
77
{
88
[TestMethod]
9-
public void TestMethod1()
9+
public void AnalyzeGoodRunTest()
1010
{
1111
}
1212
}

src/Microsoft.DevSkim/Microsoft.DevSkim.CLI/Commands/AnalyzeCommand.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public static void Configure(CommandLineApplication command)
4545
"Ignore rules bundled with DevSkim",
4646
CommandOptionType.NoValue);
4747

48+
var errorOption = command.Option("-e|--suppress-standard-error",
49+
"Suppress output to standard error",
50+
CommandOptionType.NoValue);
51+
4852
command.ExtendedHelpText = "\nOutput format options:\n%F\tfile path\n%L\tstart line number\n" +
4953
"%C\tstart column\n%l\tend line number\n%c\tend column\n%I\tlocation inside file\n" +
5054
"%i\tmatch length\n%m\tmatch\n%R\trule id\n%N\trule name\n%S\tseverity\n%D\tissue description\n%T\ttags(comma-separated)";
@@ -56,7 +60,8 @@ public static void Configure(CommandLineApplication command)
5660
outputTextFormat.Value(),
5761
severityOption.Values,
5862
rulesOption.Values,
59-
ignoreOption.HasValue())).Run();
63+
ignoreOption.HasValue(),
64+
errorOption.HasValue())).Run();
6065
});
6166
}
6267

@@ -66,7 +71,8 @@ public AnalyzeCommand(string path,
6671
string outputTextFormat,
6772
List<string> severities,
6873
List<string> rules,
69-
bool ignoreDefault)
74+
bool ignoreDefault,
75+
bool suppressError)
7076
{
7177
_path = path;
7278
_outputFile = output;
@@ -75,10 +81,16 @@ public AnalyzeCommand(string path,
7581
_severities = severities.ToArray();
7682
_rulespath = rules.ToArray();
7783
_ignoreDefaultRules = ignoreDefault;
84+
_suppressError = suppressError;
7885
}
7986

8087
public int Run()
8188
{
89+
if (_suppressError)
90+
{
91+
Console.SetError(StreamWriter.Null);
92+
}
93+
8294
if (!Directory.Exists(_path) && !File.Exists(_path))
8395
{
8496
Console.Error.WriteLine("Error: Not a valid file or directory {0}", _path);
@@ -150,8 +162,19 @@ public int Run()
150162
int filesAffected = 0;
151163
int issuesCount = 0;
152164

165+
// We can pass either a file or a directory; if it's a file, make an IEnumerable out of it.
166+
IEnumerable <string> fileListing;
167+
if (!Directory.Exists(_path))
168+
{
169+
fileListing = new List<string>() { _path };
170+
}
171+
else
172+
{
173+
fileListing = Directory.EnumerateFiles(_path, "*.*", SearchOption.AllDirectories);
174+
}
175+
153176
// Iterate through all files
154-
foreach (string filename in Directory.EnumerateFiles(_path, "*.*", SearchOption.AllDirectories))
177+
foreach (string filename in fileListing)
155178
{
156179
string language = Language.FromFileName(filename);
157180

@@ -244,5 +267,6 @@ private bool ParseSeverity(string severityText, out Severity severity)
244267
private string[] _rulespath;
245268
private string[] _severities;
246269
private bool _ignoreDefaultRules;
270+
private bool _suppressError;
247271
}
248272
}

src/Microsoft.DevSkim/Microsoft.DevSkim.CLI/Microsoft.DevSkim.CLI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ApplicationIcon />
1010
<PackageId>Microsoft.DevSkim.CLI</PackageId>
1111
<Product>Microsoft DevSkim Command Line Interface</Product>
12-
<Version>0.1.8</Version>
12+
<Version>0.1.9</Version>
1313
<Authors>Microsoft</Authors>
1414
<Company>Microsoft</Company>
1515
<Copyright>(c) Microsoft Corporation. All rights reserved</Copyright>

src/Microsoft.DevSkim/Microsoft.DevSkim.Tests/SuppressorTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,62 @@ public void Constructor1FailTest()
1717
{
1818
Suppression sup = new Suppression(null);
1919
}
20+
21+
[TestMethod]
22+
public void IsNotSuppressedTest()
23+
{
24+
// Is supressed test
25+
string testString = "md5.new()";
26+
Suppression sup = new Suppression(testString);
27+
Assert.IsTrue(sup.Index < 0, "Suppression should not be flagged");
28+
}
29+
30+
[TestMethod]
31+
public void IsSuppressedTest()
32+
{
33+
// Is supressed test
34+
string testString = "md5.new() #DevSkim: ignore DS196098";
35+
Suppression sup = new Suppression(testString);
36+
Assert.IsTrue(sup.GetIssues().Length == 1, "Suppression should be flagged");
37+
}
38+
39+
public void SuppressedIndexTest()
40+
{
41+
// Is supressed test
42+
string testString = "md5.new() #DevSkim: ignore DS196098";
43+
Suppression sup = new Suppression(testString);
44+
Assert.IsTrue(sup.Index == 12, "Suppression should start in ondex 12");
45+
}
46+
47+
[TestMethod]
48+
public void SuppresseedAll_Test()
49+
{
50+
string testString = "var hash=MD5.Create(); /*DevSkim: ignore all*/";
51+
Suppression sup = new Suppression(testString);
52+
// Suppress All test
53+
Assert.IsTrue(sup.GetIssues().Length == 1, "Supress All failed");
54+
}
55+
56+
[TestMethod]
57+
public void GetSuppressedTest()
58+
{
59+
string testString = "MD5 hash = new MD5CryptoServiceProvider(); //DevSkim: ignore DS126858,DS168931";
60+
Suppression sup = new Suppression(testString);
61+
SuppressedIssue iss = sup.GetSuppressedIssue("DS126858");
62+
63+
Assert.IsNotNull(sup.GetSuppressedIssue("DS126858"), "Is suppressed DS126858 should be instance");
64+
Assert.IsNotNull(sup.GetSuppressedIssue("DS168931"), "Is suppressed DS168931 should be instance");
65+
}
66+
67+
[TestMethod]
68+
public void GetNotSuppressedTest()
69+
{
70+
string testString = "MD5 hash = new MD5CryptoServiceProvider(); //DevSkim: ignore DS126858,DS168931 until 1980-07-15";
71+
Suppression sup = new Suppression(testString);
72+
SuppressedIssue iss = sup.GetSuppressedIssue("DS126858");
73+
74+
Assert.IsNull(sup.GetSuppressedIssue("DS126858"), "Is suppressed DS126858 should be Null");
75+
Assert.IsNull(sup.GetSuppressedIssue("DS168931"), "Is suppressed DS168931 should be Null");
76+
}
2077
}
2178
}

src/Microsoft.DevSkim/Microsoft.DevSkim.Tests/UseCaseTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public void UseCase_OnError_Test()
250250
}
251251

252252
[TestMethod]
253-
public void LangugeSelector_Test()
253+
public void LangugeSelectorTest()
254254
{
255255
RuleProcessor processor = new RuleProcessor(LoadRules(false));
256256
string testString = "<package id=\"Microsoft.IdentityModel.Tokens\" version=\"5.1.0\"";
@@ -270,7 +270,7 @@ public void LangugeSelector_Test()
270270
}
271271

272272
[TestMethod]
273-
public void Commenting_Test()
273+
public void CommentingTest()
274274
{
275275
string str = Language.GetCommentInline("python");
276276
Assert.AreEqual("#", str, "Python comment prefix doesn't match");
@@ -284,7 +284,7 @@ public void Commenting_Test()
284284
}
285285

286286
[TestMethod]
287-
public void Conditions1_Test()
287+
public void Conditions1Test()
288288
{
289289
RuleProcessor processor = new RuleProcessor(LoadRules(false))
290290
{
@@ -307,7 +307,7 @@ public void Conditions1_Test()
307307
}
308308

309309
[TestMethod]
310-
public void Conditions2_Test()
310+
public void Conditions2Test()
311311
{
312312
RuleProcessor processor = new RuleProcessor(LoadRules(false))
313313
{
@@ -330,7 +330,7 @@ public void Conditions2_Test()
330330
}
331331

332332
[TestMethod]
333-
public void Scope_Test()
333+
public void ScopeTest()
334334
{
335335
RuleProcessor processor = new RuleProcessor(LoadRules(false))
336336
{

src/Microsoft.DevSkim/Microsoft.DevSkim/Suppression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Suppression(string text)
3939
/// Test if given rule Id is being suppressed
4040
/// </summary>
4141
/// <param name="issueId">Rule ID</param>
42-
/// <returns>True is rule is suppressed</returns>
42+
/// <returns>True if rule is suppressed</returns>
4343
public SuppressedIssue GetSuppressedIssue(string issueId)
4444
{
4545
bool result = false;

src/flycheck/flycheck-devskim.el

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
;;; flycheck-devskim.el --- Flycheck: DevSkim support -*- lexical-binding: t; -*-
2+
3+
;; Copyright (c) Microsoft Corporation
4+
5+
;; Author: Michael Scovetta <[email protected]
6+
;; Keywords: security, tools
7+
;; Version: 0.1.0
8+
;; URL: https://github.com/Microsoft/DevSkim
9+
;; Package-Requires: ((emacs "25.3") (flycheck "31"))
10+
11+
;; All rights reserved.
12+
;;
13+
;; MIT License
14+
;;
15+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
16+
;; of this software and associated documentation files (the "Software"), to deal
17+
;; in the Software without restriction, including without limitation the rights
18+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19+
;; copies of the Software, and to permit persons to whom the Software is
20+
;; furnished to do so, subject to the following conditions:
21+
;;
22+
;; The above copyright notice and this permission notice shall be included in;
23+
;; all copies or substantial portions of the Software.
24+
;;
25+
;; THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31+
;; SOFTWARE.
32+
33+
;;; Commentary:
34+
35+
;; DevSkim is a code analysis tool for multiple programming languages, based on
36+
;; grep-style rules.
37+
;;
38+
; Usage:
39+
;;
40+
;; Load flycheck-devskim from wherever you placed it.
41+
;; (load "~/.emacs.d/flycheck-devskim")
42+
43+
;;; Code:
44+
45+
(require 'flycheck)
46+
47+
(defun flycheck-parse-devskim (output checker buffer)
48+
"Parse DevSkim warnings.
49+
CHECKER and BUFFER denote the CHECKER that returned OUTPUT and
50+
the BUFFER that was checked."
51+
(let ((errors nil))
52+
(dolist (message (car (flycheck-parse-json output)))
53+
(let-alist message
54+
(push
55+
(flycheck-error-new-at
56+
.start_line
57+
.start_column
58+
(pcase .severity
59+
(`"1" 'error)
60+
(`"2" 'error)
61+
(`"3" 'warning)
62+
(`"4" 'warning)
63+
(_ 'info))
64+
(concat .rule_name " " .recommendation)
65+
:id .rule_id
66+
:checker checker
67+
:buffer buffer
68+
:filename .filename)
69+
errors)))
70+
(nreverse errors)))
71+
72+
(flycheck-define-checker devskim
73+
"A DevSkim checker for Flycheck.
74+
See URL `https://github.com/Microsoft/DevSkim'."
75+
:command ("devskim.exe"
76+
"analyze"
77+
"-f"
78+
"json"
79+
source)
80+
:error-parser flycheck-parse-devskim
81+
:modes (c-mode c++-mode python-mode)
82+
)
83+
84+
(add-to-list 'flycheck-checkers 'devskim)
85+
86+
(provide 'flycheck-devskim)
87+
88+
;;; flycheck-devskim.el ends here

0 commit comments

Comments
 (0)