Skip to content
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

Code quality (SonarQube) fixes #180

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
107 changes: 51 additions & 56 deletions src/NAnt.Contrib/Tasks/Msi/InstallerCreationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Based on original work by Jayme C. Edwards (jcedwards@users.sourceforge.net)
//

// Simona Avornicesei (simona@avornicesei.com)

using System;
using System.Collections;
Expand Down Expand Up @@ -645,71 +646,63 @@ public abstract class InstallerCreationCommand {
/// <summary>
/// Retrieves a DOS 8.3 filename for a directory.
/// </summary>
/// <param name="LongPath">The path to shorten.</param>
/// <param name="longPath">The path to shorten.</param>
/// <returns>The new shortened path.</returns>
private string GetShortPath(string LongPath) {
if (LongPath.Length <= 8) {
return LongPath;
private string GetShortPath(string longPath) {
if (longPath.Length <= 8) {
return longPath;
}

StringBuilder shortPath = new StringBuilder(255);
int result = GetShortPathName(LongPath, shortPath, shortPath.Capacity);
int result = GetShortPathName(longPath, shortPath, shortPath.Capacity);
if (result == 0) {
throw new BuildException (string.Format (CultureInfo.InvariantCulture,
"The short path for '{0}' could not be determined.",
LongPath), Location);
longPath), Location);
}

Uri shortPathUri = null;
try {
shortPathUri = new Uri("file://" + shortPath.ToString());
if (shortPathUri == null) {
// done to prevent CS0219 warning, variable is assigned but
// its value is never used
}
}
catch (Exception) {
throw new BuildException(String.Format(CultureInfo.InvariantCulture, "Directory {0} not found.", LongPath), Location);
if(!Uri.TryCreate($"file://{shortPath}", UriKind.RelativeOrAbsolute, out Uri shortPathUri))
{
throw new BuildException(
string.Format(CultureInfo.InvariantCulture, "Directory {0} not found.", longPath),
Location);
}

string[] shortPathSegments = shortPathUri.Segments;
if (shortPathSegments.Length == 0) {
return LongPath;
}
if (shortPathSegments.Length == 1) {
return shortPathSegments[0];
switch (shortPathSegments.Length)
{
case 0:
return longPath;
case 1:
return shortPathSegments[0];
default:
return shortPathSegments[shortPathSegments.Length-1];
}
return shortPathSegments[shortPathSegments.Length-1];
}

/// <summary>
/// Retrieves a DOS 8.3 filename for a complete directory.
/// </summary>
/// <param name="LongPath">The path to shorten.</param>
/// <param name="longPath">The path to shorten.</param>
/// <returns>The new shortened path.</returns>
private string GetShortDir(string LongPath) {
if (LongPath.Length <= 8) {
return LongPath;
private string GetShortDir(string longPath) {
if (longPath.Length <= 8) {
return longPath;
}

StringBuilder shortPath = new StringBuilder(255);
int result = GetShortPathName(LongPath, shortPath, shortPath.Capacity);
int result = GetShortPathName(longPath, shortPath, shortPath.Capacity);
if (result == 0) {
throw new BuildException (string.Format (CultureInfo.InvariantCulture,
"The short path for '{0}' could not be determined.",
LongPath), Location);
longPath), Location);
}

Uri shortPathUri = null;
try {
shortPathUri = new Uri("file://" + shortPath.ToString());
if (shortPathUri == null) {
// done to prevent CS0219 warning, variable is assigned but
// its value is never used
}
}
catch (Exception) {
throw new BuildException(String.Format(CultureInfo.InvariantCulture, "Directory {0} not found.", LongPath), Location);
if(!Uri.TryCreate($"file://{shortPath}", UriKind.RelativeOrAbsolute, out Uri _))
{
throw new BuildException(
string.Format(CultureInfo.InvariantCulture, "Directory {0} not found.", longPath),
Location);
}

return shortPath.ToString();
Expand All @@ -719,16 +712,16 @@ public abstract class InstallerCreationCommand {
/// Recursively expands properties of all attributes of
/// a nodelist and their children.
/// </summary>
/// <param name="Nodes">The nodes to recurse.</param>
private void ExpandPropertiesInNodes(XmlNodeList Nodes) {
foreach (XmlNode node in Nodes) {
if (node.ChildNodes != null) {
ExpandPropertiesInNodes(node.ChildNodes);
if (node.Attributes != null) {
foreach (XmlAttribute attr in node.Attributes) {
attr.Value = Properties.ExpandProperties(attr.Value, Location);
}
}
/// <param name="nodes">The nodes to recurse.</param>
private void ExpandPropertiesInNodes(XmlNodeList nodes) {
foreach (XmlNode node in nodes)
{
if (!node.HasChildNodes) continue;
ExpandPropertiesInNodes(node.ChildNodes);

if (node.Attributes == null) continue;
foreach (XmlAttribute attr in node.Attributes) {
attr.Value = Properties.ExpandProperties(attr.Value, Location);
}
}
}
Expand All @@ -737,16 +730,18 @@ public abstract class InstallerCreationCommand {
/// Converts the Byte array in a public key
/// token of an assembly to a string MSI expects.
/// </summary>
/// <param name="ByteArray">The array of bytes.</param>
/// <param name="byteArray">The array of bytes.</param>
/// <returns>The string containing the array.</returns>
private string ByteArrayToString(Byte[] ByteArray) {
if (ByteArray == null || ByteArray.Length == 0)
private string ByteArrayToString(byte[] byteArray) {
if (byteArray == null || byteArray.Length == 0)
return string.Empty;
StringBuilder sb = new StringBuilder ();
sb.Append (ByteArray[0].ToString("x2"));
for (int i = 1; i < ByteArray.Length; i++) {
sb.Append(ByteArray[i].ToString("x2"));

StringBuilder sb = new StringBuilder();
sb.Append (byteArray[0].ToString("x2"));
for (int i = 1; i < byteArray.Length; i++) {
sb.Append(byteArray[i].ToString("x2"));
}

return sb.ToString().ToUpper();
}

Expand Down
37 changes: 19 additions & 18 deletions src/NAnt.Contrib/Tasks/NUnit2Report/NUnit2ReportTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

// Gilles Bayon (gilles.bayon@laposte.net)
// Ian Maclean (imaclean@gmail.com)
// Gert Driesen (drieseng@users.sourceforge.net)
Expand Down Expand Up @@ -253,9 +254,9 @@ public class NUnit2ReportTask : Task {

xmlReader.Close();
writerFinal.Close();
} else {
XmlTextReader reader = null;

}
else
{
try {
// create the index.html
StringReader stream = new StringReader("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0' >" +
Expand Down Expand Up @@ -315,24 +316,25 @@ public class NUnit2ReportTask : Task {
XPathNodeIterator iterator = xpathNavigator.Select(expr);
while (iterator.MoveNext()) {
// output directory
string path = "";
string path = string.Empty;

XPathNavigator xpathNavigator2 = iterator.Current;
string testSuiteName = iterator.Current.GetAttribute("name", "");
string testSuiteName = iterator.Current.GetAttribute("name", string.Empty);

// Get get the path for the current test-suite.
XPathNodeIterator iterator2 = xpathNavigator2.SelectAncestors("", "", true);
string parent = "";
XPathNodeIterator iterator2 = xpathNavigator2.SelectAncestors(string.Empty, string.Empty, true);
string parent = string.Empty;
int parentIndex = -1;

while (iterator2.MoveNext()) {
string directory = iterator2.Current.GetAttribute("name","");
if (directory != "" && directory.IndexOf(".dll") < 0) {
path = directory + "/" + path;
if (!string.IsNullOrEmpty(directory) && directory.IndexOf(".dll", StringComparison.OrdinalIgnoreCase) < 0) {
path = $"{directory}/{path}";
}
if (parentIndex == 1) {
parent = directory;
}

parentIndex++;
}

Expand All @@ -352,9 +354,9 @@ public class NUnit2ReportTask : Task {
"<xsl:output method='html' indent='yes' encoding='ISO-8859-1'/>" +
"<xsl:include href=\"NUnit-Frame.xsl\"/>" +
"<xsl:template match=\"/\">" +
" <xsl:for-each select=\"//test-suite[@name='"+testSuiteName+"' and ancestor::test-suite[@name='"+parent+"'][position()=last()]]\">" +
" <xsl:for-each select=\"//test-suite[@name='"+ testSuiteName+"' and ancestor::test-suite[@name='"+parent+"'][position()=last()]]\">" +
" <xsl:call-template name=\"test-case\">" +
" <xsl:with-param name=\"dir.test\">"+String.Join(".", path.Split('/'))+"</xsl:with-param>" +
" <xsl:with-param name=\"dir.test\">"+string.Join(".", path.Split('/'))+"</xsl:with-param>" +
" </xsl:call-template>" +
" </xsl:for-each>" +
" </xsl:template>" +
Expand All @@ -363,16 +365,15 @@ public class NUnit2ReportTask : Task {

Log(Level.Debug,"dir={0} Generating {1}.html", path, testSuiteName);
}
} finally {
}
finally
{
Log(Level.Debug, "Processing of stream complete.");

// Finished with XmlTextReader
if (reader != null) {
reader.Close();
}
}
}
} catch (Exception ex) {
}
catch (Exception ex)
{
throw new BuildException("Failure generating report.", Location, ex);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/NAnt.Contrib/Tasks/SlingshotTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class SlingshotTask : Task {
Solution, Output, Format);

// get a SLiNgshoT SolutionWriter for the specified format.
SolutionWriter solutionWriter = CreateSolutionWriter(Format);
ISolutionWriter solutionWriter = CreateSolutionWriter(Format);

// make sure the specified format is supported
if (solutionWriter == null) {
Expand Down Expand Up @@ -178,14 +178,14 @@ public class SlingshotTask : Task {
#region Private Instance Methods

/// <summary>
/// Creates the <see cref="SolutionWriter" /> for the specified format.
/// Creates the <see cref="ISolutionWriter" /> for the specified format.
/// </summary>
/// <returns>
/// The <see cref="SolutionWriter" /> for the specified format, or
/// The <see cref="ISolutionWriter" /> for the specified format, or
/// <see langword="null" /> if an unknown format was specified.
/// </returns>
private SolutionWriter CreateSolutionWriter(string format) {
SolutionWriter writer = null;
private ISolutionWriter CreateSolutionWriter(string format) {
ISolutionWriter writer = null;

switch (format) {
case "nant":
Expand Down
Loading
Loading