Skip to content

Commit

Permalink
Merge branch 'master' into release/Gendarme
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGilham committed Nov 17, 2018
2 parents 175cf3a + d25f5ec commit f579137
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 72 deletions.
42 changes: 23 additions & 19 deletions AltCode.Fake.Tests/AltCode.Fake.DotNet.Gendarme.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let testCases =
let args = Gendarme.getArguments p
Expect.isTrue (p.Console) "A field should have non-default value for a bool"
Expect.equal args
[ "--limit"; "0"; "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
[ "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
"The defaults should be simple"
testCase "Test that string arguments are processed as expected" <| fun _ ->
let config = Guid.NewGuid().ToString()
Expand Down Expand Up @@ -49,8 +49,6 @@ let testCases =
ignore |> Seq.head
"--ignore"
ignore |> Seq.last
"--limit"
"0"
"--console"
"--severity"
"medium+"
Expand All @@ -70,9 +68,8 @@ let testCases =

let args = Gendarme.getArguments p
Expect.equal args
[ name; log; "--limit"; "0"; "--console"; "--severity"; "medium+";
"--confidence"; "normal+" ] ("The log kind should be " + name))

[ name; log; "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
("The log kind should be " + name))
testCase "Test that limit arguments are processed as expected" <| fun _ ->
let p = { Gendarme.Params.Create() with Limit = uint8 (23 + DateTime.Now.Second) }
let args = Gendarme.getArguments p
Expand All @@ -87,22 +84,21 @@ let testCases =
testCase "Test that console may be switched off" <| fun _ ->
let p = { Gendarme.Params.Create() with Console = false }
let args = Gendarme.getArguments p
Expect.equal args
[ "--limit"; "0"; "--severity"; "medium+"; "--confidence"; "normal+" ]
Expect.equal args [ "--severity"; "medium+"; "--confidence"; "normal+" ]
"The console should be gone"

testCase "Test that output may be hushed" <| fun _ ->
let p = { Gendarme.Params.Create() with Quiet = true }
let args = Gendarme.getArguments p
Expect.equal args
[ "--limit"; "0"; "--console"; "--quiet"; "--severity"; "medium+"; "--confidence";
"normal+" ] "The output should be quieted"
[ "--console"; "--quiet"; "--severity"; "medium+"; "--confidence"; "normal+" ]
"The output should be quieted"

testProperty "Test that verbosity may be set" <| fun (x : uint8) ->
let p = { Gendarme.Params.Create() with Verbosity = x }
let args = Gendarme.getArguments p
Expect.equal args
([ "--limit"; "0"; "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
([ "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
@ (Seq.initInfinite (fun _ -> "--v")
|> Seq.take (int x)
|> Seq.toList)) "The verbosity should be set"
Expand All @@ -128,8 +124,7 @@ let testCases =
(fun (s, m) ->
let p = { Gendarme.Params.Create() with Severity = s }
let args = Gendarme.getArguments p
Expect.equal args
[ "--limit"; "0"; "--console"; "--severity"; m; "--confidence"; "normal+" ]
Expect.equal args [ "--console"; "--severity"; m; "--confidence"; "normal+" ]
("The severity should be " + m))

testCase "Test that confidence may be set" <| fun _ ->
Expand All @@ -150,8 +145,7 @@ let testCases =
(fun (c, m) ->
let p = { Gendarme.Params.Create() with Confidence = c }
let args = Gendarme.getArguments p
Expect.equal args
[ "--limit"; "0"; "--console"; "--severity"; "medium+"; "--confidence"; m ]
Expect.equal args [ "--console"; "--severity"; "medium+"; "--confidence"; m ]
("The severity should be " + m))

testCase "Test that command arguments are processed as expected" <| fun _ ->
Expand All @@ -166,7 +160,7 @@ let testCases =
BindingFlags.Instance ||| BindingFlags.NonPublic)
let w = info.GetValue(proc) :?> string option
Expect.equal proc.CommandLine
(fake + " --limit 0 --console --severity medium+ --confidence normal+")
(fake + " --console --severity medium+ --confidence normal+")
"The defaults should be simple"
Expect.equal w None "Default working directory should be empty"

Expand All @@ -176,7 +170,7 @@ let testCases =
let args = Gendarme.getArguments p
let proc = Gendarme.createProcess args p
Expect.equal proc.CommandLine
(fake + " --limit 0 --console --severity medium+ --confidence normal+")
(fake + " --console --severity medium+ --confidence normal+")
"The toolpath should match"

testCase "Test that working directory is processed as expected" <| fun _ ->
Expand All @@ -200,6 +194,16 @@ let testCases =
Targets = [ fake ] }
Expect.equal (Gendarme.run args) () "Should be silent"

testCase "A non-failing run should proceed as expected" <| fun _ ->
let here = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let fake = Path.Combine(here, "AltCode.Nuget.Placeholder.exe")

let args =
{ Gendarme.Params.Create() with ToolPath = fake
Targets = [ fake + ".nonesuch" ]
FailBuildOnDefect = false }
Expect.equal (Gendarme.run args) () "Should be silent"

testCase "A bad run should proceed as expected" <| fun _ ->
let here = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let fake = Path.Combine(here, "AltCode.Nuget.Placeholder.exe")
Expand All @@ -210,7 +214,7 @@ let testCases =
Expect.throwsC (fun () -> Gendarme.run args)
(fun ex ->
Expect.equal ex.Message
("Gendarme --limit 0 --console --severity medium+ --confidence normal+ " + fake
("Gendarme --console --severity medium+ --confidence normal+ " + fake
+ ".nonesuch failed.") "Message should reflect inputs")

testCase "Test that null arguments are processed as expected" <| fun _ ->
Expand All @@ -223,7 +227,7 @@ let testCases =

let args = Gendarme.getArguments p
Expect.equal args
[ "--limit"; "0"; "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
[ "--console"; "--severity"; "medium+"; "--confidence"; "normal+" ]
"The defaults should be simple" ]

[<Tests>]
Expand Down
24 changes: 16 additions & 8 deletions Build/Gendarme.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ type Params =
Severity : Severity
/// Filter defects for the specified confidence levels.
/// Default is 'normal+'
Confidence : Confidence }
Confidence : Confidence
/// Fail the build if a defect is reported
/// Default is true
FailBuildOnDefect : bool }
/// ILMerge default parameters. Tries to automatically locate ilmerge.exe in a subfolder.
static member Create() =
{ ToolPath = Tools.findToolInSubPath "gendarme.exe" <| Shell.pwd()
Expand All @@ -98,7 +101,8 @@ type Params =
Severity = Severity.Medium Grade.Plus
/// Filter defects for the specified confidence levels.
/// Default is 'normal+'
Confidence = Confidence.Normal Grade.Plus }
Confidence = Confidence.Normal Grade.Plus
FailBuildOnDefect = true }

/// Builds the arguments for the Gendarme task
/// [omit]
Expand Down Expand Up @@ -127,7 +131,9 @@ let internal getArguments parameters =
| Xml -> Item "--xml"
| _ -> Item "--html") parameters.Log
ItemList "--ignore" parameters.Ignore
Item "--limit" <| parameters.Limit.ToString(CultureInfo.InvariantCulture)
(if parameters.Limit > 0uy then
Item "--limit" <| parameters.Limit.ToString(CultureInfo.InvariantCulture)
else [])
Flag "--console" parameters.Console
Flag "--quiet" parameters.Quiet

Expand All @@ -143,26 +149,28 @@ let internal getArguments parameters =
|> Seq.map (fun _ -> "--v")
|> Seq.toList
else [])

((ItemList String.Empty parameters.Targets)
|> List.filter (String.isNullOrWhiteSpace >> not)) ]
|> List.concat

let internal createProcess args parameters =
CreateProcess.fromRawCommand parameters.ToolPath args
|> if String.IsNullOrWhiteSpace parameters.WorkingDirectory then id
else CreateProcess.withWorkingDirectory parameters.WorkingDirectory
CreateProcess.fromRawCommand parameters.ToolPath args
|> if String.IsNullOrWhiteSpace parameters.WorkingDirectory then id
else CreateProcess.withWorkingDirectory parameters.WorkingDirectory

/// Uses ILMerge to merge .NET assemblies.
/// ## Parameters
///
/// - `parameters` - A Gendarme.Params value with your required settings.
let run parameters =
use __ = Trace.traceTask "Gendarme" ""
use __ = Trace.traceTask "Gendarme" String.Empty
let args = getArguments parameters

let run =
createProcess args parameters
|> CreateProcess.withFramework
|> Proc.run
if 0 <> run.ExitCode then failwithf "Gendarme %s failed." (String.separated " " args)
if 0 <> run.ExitCode && parameters.FailBuildOnDefect then
failwithf "Gendarme %s failed." (String.separated " " args)
__.MarkSuccess()
88 changes: 46 additions & 42 deletions Build/rules-fake.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
<gendarme>
<ruleset name="default">
<rules include="*"
exclude="AvoidAssemblyVersionMismatchRule"
from="Gendarme.Rules.BadPractice.dll" />
<rules include="*" from="Gendarme.Rules.Concurrency.dll" />
<rules include="*"
from="Gendarme.Rules.Correctness.dll" />
<rules include="*"
exclude="AvoidSmallNamespaceRule "
from="Gendarme.Rules.Design.dll" />
<rules include="*"
exclude="AvoidMethodWithUnusedGenericTypeRule"
from="Gendarme.Rules.Design.Generic.dll" />
<rules include="*"
from="Gendarme.Rules.Design.Linq.dll" />
<rules include="*"
exclude="InstantiateArgumentExceptionCorrectlyRule"
from="Gendarme.Rules.Exceptions.dll" />
<rules include="*" from="Gendarme.Rules.Interoperability.dll" />
<rules include="*" from="Gendarme.Rules.Interoperability.Com.dll" />
<rules include="*"
exclude="UseCorrectCasingRule | AvoidNonAlphanumericIdentifierRule"
from="Gendarme.Rules.Naming.dll" />
<rules include="*"
from="Gendarme.Rules.Performance.dll" />
<rules include="*"
from="Gendarme.Rules.Portability.dll" />
<rules include="*" from="Gendarme.Rules.Security.dll" />
<rules include="*" from="Gendarme.Rules.Security.Cas.dll" />
<rules include="*"
exclude="MarkAllNonSerializableFieldsRule"
from="Gendarme.Rules.Serialization.dll" />
<rules include="*"
from="Gendarme.Rules.Smells.dll" />
<rules include="*" from="Gendarme.Rules.Ui.dll" />
<rules include="*"
from="Gendarme.Rules.Maintainability.dll" />
<rules include="*"
from="Gendarme.Rules.Gendarme.dll" />
<!-- Assembly not in nugget -->
<!-- rules include="*" from="Gendarme.Rules.Globalization.dll"/ -->
</ruleset>
<ruleset name="default">
<rules include="*"
exclude="AvoidAssemblyVersionMismatchRule | AvoidVisibleConstantFieldRule"
from="Gendarme.Rules.BadPractice.dll" />
<rules include="*" from="Gendarme.Rules.Concurrency.dll" />
<rules include="*"
exclude = "CheckParametersNullityInVisibleMethodsRule | EnsureLocalDisposalRule"
from="Gendarme.Rules.Correctness.dll" />
<rules include="*"
exclude="AvoidSmallNamespaceRule | ConsiderConvertingMethodToPropertyRule | AvoidVisibleNestedTypesRule | AvoidMultidimensionalIndexerRule"
from="Gendarme.Rules.Design.dll" />
<rules include="*"
exclude="AvoidMethodWithUnusedGenericTypeRule"
from="Gendarme.Rules.Design.Generic.dll" />
<rules include="*"
from="Gendarme.Rules.Design.Linq.dll" />
<rules include="*"
exclude="InstantiateArgumentExceptionCorrectlyRule"
from="Gendarme.Rules.Exceptions.dll" />
<rules include="*" from="Gendarme.Rules.Interoperability.dll" />
<rules include="*" from="Gendarme.Rules.Interoperability.Com.dll" />
<rules include="*"
exclude="UseCorrectCasingRule | AvoidNonAlphanumericIdentifierRule | UseCorrectPrefixRule | ParameterNamesShouldMatchOverriddenMethodRule | AvoidDeepNamespaceHierarchyRule"
from="Gendarme.Rules.Naming.dll" />
<rules include="*"
exclude="AvoidUnsealedUninheritedInternalTypeRule"
from="Gendarme.Rules.Performance.dll" />
<rules include="*"
from="Gendarme.Rules.Portability.dll" />
<rules include="*" from="Gendarme.Rules.Security.dll" />
<rules include="*" from="Gendarme.Rules.Security.Cas.dll" />
<rules include="*"
exclude="MarkAllNonSerializableFieldsRule"
from="Gendarme.Rules.Serialization.dll" />
<rules include="*"
exclude="AvoidSwitchStatementsRule | AvoidLongParameterListsRule"
from="Gendarme.Rules.Smells.dll" />
<rules include="*" from="Gendarme.Rules.Ui.dll" />
<rules include="*"
exclude="VariableNamesShouldNotMatchFieldNamesRule"
from="Gendarme.Rules.Maintainability.dll" />
<rules include="*"
from="Gendarme.Rules.Gendarme.dll" />
<!-- Assembly not in nugget -->
<!-- rules include="*" from="Gendarme.Rules.Globalization.dll"/ -->
</ruleset>
</gendarme>
25 changes: 24 additions & 1 deletion Build/targets.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,20 @@ _Target "Lint" (fun _ ->

_Target "Gendarme" (fun _ -> // Needs debug because release is compiled --standalone which contaminates everything
Directory.ensure "./_Reports"
[ ("./Build/rules-fake.xml",

let baseRules = Path.getFullName "./Build/rules-fake.xml"
let rules =
if Environment.isWindows then baseRules
else
// Gendarme mono doesn't into .pdb files
let lines = baseRules
|> File.ReadAllLines
|> Seq.map (fun l -> l.Replace ("AvoidSwitchStatementsRule", "AvoidSwitchStatementsRule | AvoidLongMethodsRule"))
let fixup = Path.getFullName "./_Generated/rules-fake.xml"
File.WriteAllLines(fixup, lines)
fixup

[ (rules,
[ "_Binaries/AltCode.Fake.DotNet.Gendarme/Debug+AnyCPU/AltCode.Fake.DotNet.Gendarme.dll" ]) ]
|> Seq.iter (fun (ruleset, files) ->
Gendarme.run { Gendarme.Params.Create() with WorkingDirectory = "."
Expand All @@ -195,6 +208,15 @@ _Target "Gendarme" (fun _ -> // Needs debug because release is compiled --standa

_Target "FxCop" (fun _ -> // Needs debug because release is compiled --standalone which contaminates everything
Directory.ensure "./_Reports"
let installPath () =
use hklmKey =
Microsoft.Win32.RegistryKey.OpenBaseKey
(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry32)
use key = hklmKey.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\SxS\VS7")
key.GetValue("15.0", String.Empty) :?> string

let toolPath = installPath() @@ "Team Tools/Static Analysis Tools/FxCop/FxCopCmd.exe"

[ ([ "_Binaries/AltCode.Fake.DotNet.Gendarme/Debug+AnyCPU/AltCode.Fake.DotNet.Gendarme.dll" ],
[],
[
Expand All @@ -205,6 +227,7 @@ _Target "FxCop" (fun _ -> // Needs debug because release is compiled --standalon
|> Seq.iter (fun (files, types, ruleset) ->
files
|> FxCop.run { FxCop.Params.Create() with WorkingDirectory = "."
ToolPath = toolPath
UseGAC = true
Verbose = false
ReportFileName =
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ For FAKE >= 5.9.3 or later for .net framework and .net core
| **Build** | <sup>AppVeyor</sup> [![Build status](https://img.shields.io/appveyor/ci/SteveGilham/altcode-fake/master.svg)](https://ci.appveyor.com/project/SteveGilham/altcode-fake) ![Build history](https://buildstats.info/appveyor/chart/SteveGilham/altcode-fake?branch=master) | <sup>Travis</sup> [![Build status](https://travis-ci.com/SteveGilham/altcode.fake.svg?branch=master)](https://travis-ci.com/SteveGilham/altcode.fake#) [![Build history](https://buildstats.info/travisci/chart/SteveGilham/altcode.fake?branch=master)](https://travis-ci.com/SteveGilham/altcode.fake/builds)|
| **Unit Test coverage** | <sup>Coveralls</sup> [![Coverage Status](https://coveralls.io/repos/github/SteveGilham/altcode.fake/badge.svg?branch=master)](https://coveralls.io/github/SteveGilham/altcode.fake?branch=master) |
| **Nuget** | |
| Gendarme | [![Nuget](https://buildstats.info/nuget/altcode.fake.dotnet.gendarme)](http://nuget.org/packages/altcode.fake.dotnet.gendarme) [![Nuget](https://img.shields.io/nuget/vpre/altcode.fake.svg)](http://nuget.org/packages/altcode.fake.dotnet.gendarme) |
| Gendarme | [![Nuget](https://buildstats.info/nuget/AltCode.Fake.DotNet.Gendarme)](http://nuget.org/packages/altcode.fake.dotnet.gendarme) [![Nuget](https://img.shields.io/nuget/vpre/AltCode.Fake.DotNet.Gendarme.svg)](http://nuget.org/packages/altcode.fake.dotnet.gendarme) |

## Usage

See the [Wiki page](https://github.com/SteveGilham/altcode.fake/wiki/Usage) for details
See the [Wiki page](https://github.com/SteveGilham/altcode.fake/wiki) for details


## Building
Expand Down
5 changes: 5 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# 5.9.3.10
* [BUGFIX] As "--limit 0" means "report nothing" not "report all", make zero limit emit nothing to the command line
* [Enhancement] `FailBuildOnDefect` parameter, default `true`, to determine if defect detection will fail the build.
* **NOTE** whether --limit is set > 0 or omitted, if there are > 0 defects reported Gendarme exits with return code 1. It limit is set zero, or there are no defects against the rules engaged, then it returns 0. Failing the build on >= N defects for N > 0 does not come for free, but would require parsing one form of output or another, and no form of output is guaranteed -- they can all be switched off.

# 5.9.3.7
* [NEW PACKAGE] `AltCode.Fake.DotNet.Gendarme` containing Gendarme task helper types for FAKE scripts (v5.9.3 or later) : see Wiki entry [The `AltCode.Fake.DotNet.Gendarme` package](https://github.com/SteveGilham/altcode.Fake/wiki/The-AltCode.Fake.DotNet.Gendarme-package)

0 comments on commit f579137

Please sign in to comment.