Skip to content

Commit

Permalink
Cleaning up analysis issues
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanp committed Apr 30, 2015
1 parent 60601f9 commit 55f6b6b
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 158 deletions.
6 changes: 5 additions & 1 deletion SonarQube.Bootstrapper/BuildAgentUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public bool TryUpdate(string hostUrl, string targetDir, ILogger logger)
{
throw new ArgumentNullException("targetDir");
}

if (logger == null)
{
throw new ArgumentNullException("logger");
}

string integrationUrl = GetDownloadZipUrl(hostUrl);
string downloadedZipFilePath = Path.Combine(targetDir, SonarQubeIntegrationFilename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static bool TryGetSetting(this AnalysisConfig config, string settingId, o

if (config.AdditionalSettings != null)
{
result = config.AdditionalSettings.FirstOrDefault(ar => settingId.Equals(ar.Id, StringComparison.InvariantCulture));
result = config.AdditionalSettings.FirstOrDefault(ar => settingId.Equals(ar.Id, StringComparison.Ordinal));
}
return result != null;
}
Expand Down
6 changes: 2 additions & 4 deletions SonarQube.Common/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace SonarQube.Common
/// </summary>
public class ConsoleLogger : ILogger
{
public const string WarningPrefix = "WARNING: ";

#region Public methods

public ConsoleLogger() : this(includeTimestamp: false)
Expand Down Expand Up @@ -44,7 +42,7 @@ public void LogMessage(string message, params object[] args)

public void LogWarning(string message, params object[] args)
{
string finalMessage = this.GetFormattedMessage(WarningPrefix + message, args);
string finalMessage = this.GetFormattedMessage(Resources.Logger_WarningPrefix + message, args);
Console.Error.WriteLine(finalMessage);
}

Expand All @@ -68,7 +66,7 @@ private string GetFormattedMessage(string message, params object[] args)

if (this.IncludeTimestamp)
{
finalMessage = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}: {1}", System.DateTime.Now.ToString("hh:mm:ss"), finalMessage);
finalMessage = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} {1}", System.DateTime.Now.ToString("T"), finalMessage);
}
return finalMessage;
}
Expand Down
6 changes: 3 additions & 3 deletions SonarQube.Common/ProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class ProcessRunner
/// </summary>
/// <param name="exeName">Name of the file to execute. This can be a full name or just the file name (if the file is on the %PATH%).</param>
/// <remarks>The standard and error output will be streamed to the logger</remarks>
public bool Execute(string exeName, string args, string workingDirectory, int timeoutInMs, ILogger logger)
public bool Execute(string exeName, string args, string workingDirectory, int timeoutInMilliseconds, ILogger logger)
{
if (string.IsNullOrWhiteSpace(exeName))
{
Expand Down Expand Up @@ -69,9 +69,9 @@ public bool Execute(string exeName, string args, string workingDirectory, int ti
process.BeginErrorReadLine();
process.BeginOutputReadLine();

logger.LogMessage(Resources.DIAG_ExecutingFile, exeName, args, workingDirectory, timeoutInMs, process.Id);
logger.LogMessage(Resources.DIAG_ExecutingFile, exeName, args, workingDirectory, timeoutInMilliseconds, process.Id);

succeeded = process.WaitForExit(timeoutInMs);
succeeded = process.WaitForExit(timeoutInMilliseconds);
if (succeeded)
{
process.WaitForExit(); // Give any asynchronous events the chance to complete
Expand Down
5 changes: 4 additions & 1 deletion SonarQube.Common/ProjectInfo/ProjectInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ public static string TryGetAnalysisFileLocation(this ProjectInfo projectInfo, An
AnalysisResult result = null;
if (projectInfo.TryGetAnalyzerResult(analysisType, out result))
{
location = result.Location;
if (File.Exists(result.Location))
{
location = result.Location;
}
}
return location;
}
Expand Down
11 changes: 10 additions & 1 deletion SonarQube.Common/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions SonarQube.Common/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@
<data name="PropertyNotFoundInFileError" xml:space="preserve">
<value>The settings file '{0}' does not contain a value for the property '{1}'</value>
</data>
<data name="Logger_WarningPrefix" xml:space="preserve">
<value>WARNING: </value>
</data>
</root>
6 changes: 3 additions & 3 deletions SonarQube.MSBuild.Tasks/IsTestFileByName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace SonarQube.MSBuild.Tasks
/// </summary>
/// <remarks>The task applies a regular expression to the file name being tested to determine whether
/// the file is test file or not. The regular expression used is read from the analysis config file.</remarks>
public class IsTestFileByName : Task, SonarQube.Common.ILogger
public sealed class IsTestFileByName : Task, SonarQube.Common.ILogger
{
/// <summary>
/// Id of the SonarQube test setting that specifies the RegEx to use when determining
Expand Down Expand Up @@ -61,7 +61,7 @@ public override bool Execute()
{
bool taskSuccess = true;

AnalysisConfig config = this.TryGetConfig(this.AnalysisConfigDir);
AnalysisConfig config = this.TryGetConfig();

if (config != null)
{
Expand Down Expand Up @@ -104,7 +104,7 @@ private string TryGetRegularExpression(AnalysisConfig config)
return regEx;
}

private AnalysisConfig TryGetConfig(string filePath)
private AnalysisConfig TryGetConfig()
{
AnalysisConfig config = null;
if (string.IsNullOrEmpty(this.AnalysisConfigDir)) // not specified
Expand Down
20 changes: 9 additions & 11 deletions SonarQube.TeamBuild.Integration/CoverageReportConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ private static IDictionary<string, string> GetVsShellFolders(RegistryKey vsKey,
if (Regex.IsMatch(key, @"\d+.\d+"))
{
// Check for the shell dir subkey
string shellDir = vsKey.GetValue(key + "\\ShellFolder", null) as string;

string shellFolder = Registry.GetValue(vsKey.Name + "\\" + key, "ShellFolder", null) as string;
if (shellFolder != null)
{
Expand Down Expand Up @@ -178,33 +176,33 @@ private static double TryGetVersionAsDouble(string versionKey)


// was internal
public static bool ConvertBinaryToXml(string converterExeFilePath, string inputfullBinaryFilePath, string outputFullXmlFilePath, ILogger logger)
public static bool ConvertBinaryToXml(string converterExeFilePath, string inputBinaryFilePath, string outputXmlFilePath, ILogger logger)
{
Debug.Assert(!string.IsNullOrEmpty(converterExeFilePath), "Expecting the conversion tool path to have been set");
Debug.Assert(File.Exists(converterExeFilePath), "Expecting the converter exe to exist: " + converterExeFilePath);
Debug.Assert(Path.IsPathRooted(inputfullBinaryFilePath), "Expecting the input file name to be a full absolute path");
Debug.Assert(File.Exists(inputfullBinaryFilePath), "Expecting the input file to exist: " + inputfullBinaryFilePath);
Debug.Assert(Path.IsPathRooted(outputFullXmlFilePath), "Expecting the output file name to be a full absolute path");
Debug.Assert(Path.IsPathRooted(inputBinaryFilePath), "Expecting the input file name to be a full absolute path");
Debug.Assert(File.Exists(inputBinaryFilePath), "Expecting the input file to exist: " + inputBinaryFilePath);
Debug.Assert(Path.IsPathRooted(outputXmlFilePath), "Expecting the output file name to be a full absolute path");

string args = string.Format(System.Globalization.CultureInfo.InvariantCulture,
@"analyze /output:""{0}"" ""{1}""",
outputFullXmlFilePath, inputfullBinaryFilePath);
outputXmlFilePath, inputBinaryFilePath);

ProcessRunner runner = new ProcessRunner();
bool success = runner.Execute(converterExeFilePath, args, Path.GetDirectoryName(outputFullXmlFilePath), ConversionTimeoutInMs, logger);
bool success = runner.Execute(converterExeFilePath, args, Path.GetDirectoryName(outputXmlFilePath), ConversionTimeoutInMs, logger);

if (success)
{
// Check the output file actually exists
if (!File.Exists(outputFullXmlFilePath))
if (!File.Exists(outputXmlFilePath))
{
logger.LogError(Resources.CONV_ERROR_OutputFileNotFound, outputFullXmlFilePath);
logger.LogError(Resources.CONV_ERROR_OutputFileNotFound, outputXmlFilePath);
success = false;
}
}
else
{
logger.LogError(Resources.CONV_ERROR_ConversionToolFailed, inputfullBinaryFilePath);
logger.LogError(Resources.CONV_ERROR_ConversionToolFailed, inputBinaryFilePath);
}

return success;
Expand Down
3 changes: 1 addition & 2 deletions SonarQube.TeamBuild.PostProcessor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ internal class Program
private const int SuccessCode = 0;
private const int ErrorCode = 1;

static int Main(string[] args)
static int Main()
{
ILogger logger = new ConsoleLogger(includeTimestamp: true);

// TODO: consider using command line arguments if supplied
AnalysisConfig config = GetAnalysisConfig(logger);
if (config == null)
{
Expand Down
16 changes: 5 additions & 11 deletions SonarQube.TeamBuild.PreProcessor/TeamBuildPreProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,21 @@ public class TeamBuildPreProcessor
public const string FxCopRulesetFileName = "SonarQubeAnalysis.ruleset";
private const string DefaultSonarServerUrl = "http://localhost:9000";

private readonly ILogger logger;
private readonly IPropertiesFetcher propertiesFetcher;
private readonly IRulesetGenerator rulesetGenerator;

#region Constructor(s)

public TeamBuildPreProcessor()
: this(new ConsoleLogger(), new PropertiesFetcher(), new RulesetGenerator())
: this(new PropertiesFetcher(), new RulesetGenerator())
{
}

/// <summary>
/// Internal constructor for testing
/// </summary>
public TeamBuildPreProcessor(ILogger logger, IPropertiesFetcher propertiesFetcher, IRulesetGenerator rulesetGenerator) // was internal
public TeamBuildPreProcessor(IPropertiesFetcher propertiesFetcher, IRulesetGenerator rulesetGenerator) // was internal
{
if (logger == null)
{
throw new ArgumentNullException("logger");
}
if (propertiesFetcher == null)
{
throw new ArgumentNullException("propertiesFetcher");
Expand All @@ -48,7 +43,6 @@ public TeamBuildPreProcessor(ILogger logger, IPropertiesFetcher propertiesFetche
throw new ArgumentNullException("rulesetGenerator");
}

this.logger = logger;
this.propertiesFetcher = propertiesFetcher;
this.rulesetGenerator = rulesetGenerator;
}
Expand Down Expand Up @@ -108,7 +102,7 @@ public bool Execute(ILogger logger, string projectKey, string projectName, strin
FetchSonarQubeProperties(config, ws);

// Generate the FxCop ruleset
GenerateFxCopRuleset(config, ws);
GenerateFxCopRuleset(config, ws, logger);
}

// Save the config file
Expand Down Expand Up @@ -153,9 +147,9 @@ private void FetchSonarQubeProperties(AnalysisConfig config, SonarWebService ws)
}
}

private void GenerateFxCopRuleset(AnalysisConfig config, SonarWebService ws)
private void GenerateFxCopRuleset(AnalysisConfig config, SonarWebService ws, ILogger logger)
{
this.logger.LogMessage(Resources.DIAG_GeneratingRuleset);
logger.LogMessage(Resources.DIAG_GeneratingRuleset);
this.rulesetGenerator.Generate(ws, config.SonarProjectKey, Path.Combine(config.SonarConfigDir, FxCopRulesetFileName));
}

Expand Down
2 changes: 1 addition & 1 deletion SonarRunner.Shim/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static int Main(string[] args)
{
ConsoleLogger logger = new ConsoleLogger();

if (args.Length != 1)
if (args == null || args.Length != 1)
{
logger.LogError(Resources.ERR_InvalidCommandLineArgs);
return 1;
Expand Down
8 changes: 7 additions & 1 deletion SonarRunner.Shim/PropertiesWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ public class PropertiesWriter
private IList<ProjectInfo> projects;

#region Public methods

public static string Escape(string value)
{
if (value == null)
{
return null;
}

StringBuilder sb = new StringBuilder();

foreach (char c in value)
Expand Down Expand Up @@ -93,7 +99,7 @@ public void WriteSettingsForProject(ProjectInfo project, IEnumerable<string> fil
if (this.FinishedWriting)
{
throw new InvalidOperationException();
};
}

if (project == null)
{
Expand Down
Loading

0 comments on commit 55f6b6b

Please sign in to comment.