[Mono-patches] r58121 - in trunk/cecil/gendarme: . rules
rules/Gendarme.Rules.Portability
rules/Gendarme.Rules.Portability/Test
Sebastien Pouliot (sebastien at ximian.com)
mono-patches-list at lists.ximian.com
Fri Mar 17 14:33:50 EST 2006
Author: spouliot
Date: 2006-03-17 14:33:49 -0500 (Fri, 17 Mar 2006)
New Revision: 58121
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/ChangeLog
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/Makefile.am
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/NewLineLiteralTest.cs
Modified:
trunk/cecil/gendarme/configure.ac
trunk/cecil/gendarme/rules/ChangeLog
trunk/cecil/gendarme/rules/Makefile.am
trunk/cecil/gendarme/rules/rules.xml
Log:
Add a new rule, in a new assembly, to Gendarme (a perfect simple sample ;-)
Modified: trunk/cecil/gendarme/configure.ac
===================================================================
--- trunk/cecil/gendarme/configure.ac 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/configure.ac 2006-03-17 19:33:49 UTC (rev 58121)
@@ -64,4 +64,7 @@
rules/Gendarme.Rules.Correctness/Makefile
rules/Gendarme.Rules.Correctness/Gendarme.Rules.Correctness.xml
rules/Gendarme.Rules.Correctness/Test/Makefile
+rules/Gendarme.Rules.Portability/Makefile
+rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml
+rules/Gendarme.Rules.Portability/Test/Makefile
])
Modified: trunk/cecil/gendarme/rules/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/ChangeLog 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/ChangeLog 2006-03-17 19:33:49 UTC (rev 58121)
@@ -1,3 +1,7 @@
+2006-03-06 Sebastien Pouliot <sebastien at ximian.com>
+
+ * Makefile.am: Added Gendarme.Rules.Portability
+
2006-03-01 Sebastien Pouliot <sebastien at ximian.com>
* Makefile.am: Added test and run-test targets.
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,7 @@
+2006-03-17 Sebastien Pouliot <sebastien at ximian.com>
+
+ * Makefile.am: New. Build Gendarme.Rules.Portability rules and tests.
+ * Gendarme.Rules.Portability.xml.in: New. Configuration for Gendarme.
+ Rules.Portability.
+ * NewLineLiteralRule.cs: New. Rule to check if a method is directly
+ using \r, \n or \r\n instead of Environment.NewLine.
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,8 @@
+<rules>
+ <rule Name="NewLineLiteralRule"
+ Type="Gendarme.Rules.Portability.NewLineLiteralRule, Gendarme.Rules.Portability, Version=@VERSION@, Culture=neutral, PublicKeyToken=null"
+ Uri="http://www.mono-project.com/Gendarme" >
+ <problem>The method '{0}' use some literal values for new lines (e.g. \r\n) which aren't portable across operating systems.</problem>
+ <solution>Replace literals with Environment.NewLine.</solution>
+ </rule>
+</rules>
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,43 @@
+SUBDIRS=Test
+
+portability_rulesdir=$(pkglibdir)
+portability_rules_SCRIPTS = Gendarme.Rules.Portability.dll
+EXTRA_DIST = $(portability_rules_sources) $(portability_rules_sources_in) Gendarme.Rules.Portability.xml
+CLEANFILES = Gendarme.Rules.Portability.dll Test.Rules.Portability.dll
+DISTCLEANFILES = Makefile.in Gendarme.Rules.Portability.xml TestResult.xml
+
+portability_rules_sources_in = ../../AssemblyInfo.cs.in
+portability_rules_generated_sources = $(portability_rules_sources_in:.in=)
+portability_rules_sources = NewLineLiteralRule.cs
+
+portability_rules_build_sources = $(addprefix $(srcdir)/, $(portability_rules_sources))
+portability_rules_build_sources += $(portability_rules_generated_sources)
+
+Gendarme.Rules.Portability.dll: $(portability_rules_build_sources)
+ $(MCS) -target:library -pkg:mono-cecil -r:../../framework/Gendarme.Framework.dll -out:$@ $(portability_rules_build_sources)
+
+# Install Unstable Mono Libraries (see configure.ac)
+
+install-data-hook:
+ $(INSTALL) -c -m 0644 Gendarme.Rules.Portability.xml $(DESTDIR)$(pkglibdir);
+ for ASM in $(INSTALLED_ASSEMBLIES); do \
+ $(INSTALL) -c -m 0755 $$ASM $(DESTDIR)$(pkglibdir); \
+ done;
+
+uninstall-hook:
+ $(INSTALL) -c -m 0644 Gendarme.Rules.Portability.xml $(DESTDIR)$(pkglibdir);
+ for ASM in $(INSTALLED_ASSEMBLIES); do \
+ rm -f $(DESTDIR)$(pkglibdir)/`basename $$ASM`; \
+ done;
+
+portability_test_sources = NewLineLiteralTest.cs
+portability_test_build_sources = $(addprefix $(srcdir)/Test/, $(portability_test_sources))
+
+Test.Rules.Portability.dll: $(portability_rules_build_sources)
+ $(MCS) -target:library -pkg:mono-cecil -r:nunit.framework.dll -r:../../framework/Gendarme.Framework.dll \
+ -r:Gendarme.Rules.Portability.dll -out:$@ $(portability_test_build_sources)
+
+test: Gendarme.Rules.Portability.dll Test.Rules.Portability.dll
+
+run-test: Gendarme.Rules.Portability.dll Test.Rules.Portability.dll
+ MONO_PATH=../../framework/:$(CECIL_PATH):$(MONO_PATH) nunit-console Test.Rules.Portability.dll
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,86 @@
+//
+// Gendarme.Rules.Portability.NewLineLiteralRule
+//
+// Authors:
+// Sebastien Pouliot <sebastien at ximian.com>
+//
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// 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;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Portability {
+
+ public class NewLineLiteralRule: IMethodRule {
+
+ private static char[] InvalidChar = { '\r', '\n' };
+
+ private ArrayList results;
+
+ private ArrayList Results {
+ get { return results; }
+ }
+
+ private void AddMessage (string s)
+ {
+ if (results == null)
+ results = new ArrayList ();
+
+ // make the invalid char visible on output
+ s = s.Replace ("\n", "\\n");
+ s = s.Replace ("\r", "\\r");
+ // add the "problematic" string for easier validation
+ results.Add (String.Format ("Found string: \"{0}\"", s));
+ }
+
+ public IList CheckMethod (IAssemblyDefinition assembly, IModuleDefinition module, ITypeDefinition type, IMethodDefinition method, Runner runner)
+ {
+ // methods can be empty (e.g. p/invoke declarations)
+ if ((method.Body == null) || (method.Body.Instructions == null))
+ return null;
+
+ results = null;
+ foreach (IInstruction ins in method.Body.Instructions) {
+ switch (ins.OpCode.Name) {
+ case "ldstr":
+ // check the string being referenced by the instruction
+ string s = (ins.Operand as string);
+ if (s == null)
+ continue;
+
+ if (s.IndexOfAny (InvalidChar) >= 0)
+ AddMessage (s);
+ break;
+ default:
+ break;
+ }
+ }
+ // no literal for new line has been found
+ return Results;
+ }
+ }
+}
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/ChangeLog 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/ChangeLog 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,3 @@
+2006-03-17 Sebastien Pouliot <sebastien at ximian.com>
+
+ * NewLineLiteralTest.cs: New. Unit tests for the NewLineLiteralRule.
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/Makefile.am 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/Makefile.am 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,4 @@
+EXTRA_DIST = *.cs
+DISTCLEANFILES = Makefile.in
+
+all:
Added: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/NewLineLiteralTest.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/NewLineLiteralTest.cs 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Test/NewLineLiteralTest.cs 2006-03-17 19:33:49 UTC (rev 58121)
@@ -0,0 +1,138 @@
+//
+// Unit tests for NewLineLiteralRule
+//
+// Authors:
+// Sebastien Pouliot <sebastien at ximian.com>
+//
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// 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.Reflection;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Portability;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Interop {
+
+ [TestFixture]
+ public class NewLineTest {
+
+ private IMethodRule rule;
+ private IAssemblyDefinition assembly;
+ private IModuleDefinition module;
+ private ITypeDefinition type;
+
+ private string GetNewLineLiteral_13 ()
+ {
+ return "Hello\nMono";
+ }
+
+ private string GetNewLineLiteral_10 ()
+ {
+ return "\rHello Mono";
+ }
+
+ private string GetNewLineLiteral ()
+ {
+ return "Hello Mono\r\n";
+ }
+
+ private string GetNewLine ()
+ {
+ return String.Concat ("Hello Mono", Environment.NewLine);
+ }
+
+ private string GetNull ()
+ {
+ return null;
+ }
+
+ private string GetEmpty ()
+ {
+ return "";
+ }
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ module = assembly.MainModule;
+ type = assembly.MainModule.Types ["Test.Rules.Interop.NewLineTest"];
+ rule = new NewLineLiteralRule ();
+ }
+
+ private IMethodDefinition GetTest (string name)
+ {
+ foreach (IMethodDefinition method in type.Methods) {
+ if (method.Name == name)
+ return method;
+ }
+ return null;
+ }
+
+ [Test]
+ public void HasNewLineLiteral_13 ()
+ {
+ IMethodDefinition method = GetTest ("GetNewLineLiteral_13");
+ Assert.IsNotNull (rule.CheckMethod (assembly, module, type, method, new MinimalRunner ()));
+ }
+
+ [Test]
+ public void HasNewLineLiteral_10 ()
+ {
+ IMethodDefinition method = GetTest ("GetNewLineLiteral_10");
+ Assert.IsNotNull (rule.CheckMethod (assembly, module, type, method, new MinimalRunner ()));
+ }
+
+ [Test]
+ public void HasNewLineLiteral ()
+ {
+ IMethodDefinition method = GetTest ("GetNewLineLiteral");
+ Assert.IsNotNull (rule.CheckMethod (assembly, module, type, method, new MinimalRunner ()));
+ }
+
+ [Test]
+ public void HasNewLine ()
+ {
+ IMethodDefinition method = GetTest ("GetNewLine");
+ Assert.IsNull (rule.CheckMethod (assembly, module, type, method, new MinimalRunner ()));
+ }
+
+ [Test]
+ public void HasNull ()
+ {
+ IMethodDefinition method = GetTest ("GetNull");
+ Assert.IsNull (rule.CheckMethod (assembly, module, type, method, new MinimalRunner ()));
+ }
+
+ [Test]
+ public void HasEmpty ()
+ {
+ IMethodDefinition method = GetTest ("GetEmpty");
+ Assert.IsNull (rule.CheckMethod (assembly, module, type, method, new MinimalRunner ()));
+ }
+ }
+}
Modified: trunk/cecil/gendarme/rules/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Makefile.am 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/Makefile.am 2006-03-17 19:33:49 UTC (rev 58121)
@@ -1,5 +1,7 @@
SUBDIRS= Gendarme.Rules.Performance Gendarme.Rules.Security \
- Gendarme.Rules.Correctness Gendarme.Rules.Concurrency
+ Gendarme.Rules.Correctness Gendarme.Rules.Concurrency \
+ Gendarme.Rules.Portability
+
EXTRA_DIST = rules.xml
DISTCLEANFILES = Makefile.in
Modified: trunk/cecil/gendarme/rules/rules.xml
===================================================================
--- trunk/cecil/gendarme/rules/rules.xml 2006-03-17 19:25:29 UTC (rev 58120)
+++ trunk/cecil/gendarme/rules/rules.xml 2006-03-17 19:33:49 UTC (rev 58121)
@@ -4,6 +4,7 @@
<rules include="*" from="Gendarme.Rules.Correctness.dll" />
<rules include="*" from="Gendarme.Rules.Performance.dll" />
<rules include="*" from="Gendarme.Rules.Security.dll" />
+ <rules include="*" from="Gendarme.Rules.Portability.dll" />
</ruleset>
<ruleset name="concurrency">
<rules include="*" from="Gendarme.Rules.Concurrency.dll" />
@@ -17,4 +18,7 @@
<ruleset name="performance">
<rules include="*" from="Gendarme.Rules.Performance.dll" />
</ruleset>
+ <ruleset name="portability">
+ <rules include="*" from="Gendarme.Rules.Portability.dll" />
+ </ruleset>
</gendarme>
More information about the Mono-patches
mailing list