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

Parse settings from Azure Devops Extension #1246

Merged
merged 13 commits into from Jun 9, 2022
Expand Up @@ -135,6 +135,7 @@ private IBootstrapperSettings MockBootstrapSettings(AnalysisPhase phase, bool de

File.Create(Path.Combine(rootDir, "SonarScanner.MSBuild.Common.dll")).Close();
File.Create(Path.Combine(rootDir, "SonarScanner.MSBuild.Tasks.dll")).Close();
File.Create(Path.Combine(rootDir, "Newtonsoft.Json.dll")).Close();

mockBootstrapSettings = new Mock<IBootstrapperSettings>();
mockBootstrapSettings.SetupGet(x => x.ChildCmdLineArgs).Returns(args.ToArray);
Expand Down
20 changes: 16 additions & 4 deletions Tests/SonarScanner.MSBuild.Test/BootstrapperClassTests.cs
Expand Up @@ -119,13 +119,15 @@ public void CopyDlls_WhenFileDoNotExist_FilesAreCopied()
// Sanity
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll")).Should().BeFalse();
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll")).Should().BeFalse();
File.Exists(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll")).Should().BeFalse();

// Act
CheckExecutionSucceeds(AnalysisPhase.PreProcessing, false, null, "/d:sonar.host.url=http://anotherHost");

// Assert
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll")).Should().BeTrue();
}
}

Expand All @@ -140,13 +142,15 @@ public void CopyDlls_WhenFileExistButAreNotLocked_FilesAreCopied()
Directory.CreateDirectory(Path.Combine(tempDir, "bin"));
File.Create(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll")).Close();
File.Create(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll")).Close();
File.Create(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll")).Close();

// Act
CheckExecutionSucceeds(AnalysisPhase.PreProcessing, false, null, "/d:sonar.host.url=http://anotherHost");

// Assert
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll")).Should().BeTrue();
}
}

Expand All @@ -161,17 +165,20 @@ public void CopyDlls_WhenFileExistAndAreLockedButSameVersion_DoNothing()
Directory.CreateDirectory(Path.Combine(tempDir, "bin"));
var file1 = File.Create(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll"));
var file2 = File.Create(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll"));
var file3 = File.Create(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll"));

// Act
CheckExecutionSucceeds(AnalysisPhase.PreProcessing, false, _ => new Version(), "/d:sonar.host.url=http://anotherHost");

// Assert
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll")).Should().BeTrue();

// Do not close before to ensure the file is locked
file1.Close();
file2.Close();
file3.Close();
}
}

Expand All @@ -186,6 +193,7 @@ public void CopyDlls_WhenFileExistAndAreLockedButDifferentVersion_Fails()
Directory.CreateDirectory(Path.Combine(tempDir, "bin"));
var file1 = File.Create(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll"));
var file2 = File.Create(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll"));
var file3 = File.Create(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll"));

var callCount = 0;
Func<string, Version> getAssemblyVersion = _ =>
Expand All @@ -205,11 +213,13 @@ public void CopyDlls_WhenFileExistAndAreLockedButDifferentVersion_Fails()
// Assert
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Common.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "SonarScanner.MSBuild.Tasks.dll")).Should().BeTrue();
File.Exists(Path.Combine(tempDir, "bin", "Newtonsoft.Json.dll")).Should().BeTrue();

logger.DebugMessages.Should().HaveCount(3);
logger.DebugMessages[0].Should().Match(@"Cannot delete directory: '*\.sonarqube\bin' because The process cannot access the file 'SonarScanner.MSBuild.Common.dll' because it is being used by another process..");
logger.DebugMessages[1].Should().Match(@"Cannot delete file: '*\.sonarqube\bin\SonarScanner.MSBuild.Common.dll' because The process cannot access the file '*SonarScanner.MSBuild.Common.dll' because it is being used by another process..");
logger.DebugMessages[2].Should().Match(@"Cannot delete file: '*\.sonarqube\bin\SonarScanner.MSBuild.Tasks.dll' because The process cannot access the file '*SonarScanner.MSBuild.Tasks.dll' because it is being used by another process..");
logger.DebugMessages.Should().HaveCount(4);
logger.DebugMessages[0].Should().Match(@"Cannot delete directory: '*\.sonarqube\bin' because The process cannot access the file 'Newtonsoft.Json.dll' because it is being used by another process..");
logger.DebugMessages[1].Should().Match(@"Cannot delete file: '*\.sonarqube\bin\Newtonsoft.Json.dll' because The process cannot access the file '*Newtonsoft.Json.dll' because it is being used by another process..");
logger.DebugMessages[2].Should().Match(@"Cannot delete file: '*\.sonarqube\bin\SonarScanner.MSBuild.Common.dll' because The process cannot access the file '*SonarScanner.MSBuild.Common.dll' because it is being used by another process..");
logger.DebugMessages[3].Should().Match(@"Cannot delete file: '*\.sonarqube\bin\SonarScanner.MSBuild.Tasks.dll' because The process cannot access the file '*SonarScanner.MSBuild.Tasks.dll' because it is being used by another process..");

logger.AssertErrorLogged(@"Cannot copy a different version of the SonarScanner for MSBuild assemblies because they are used by a running MSBuild/.Net Core process. To resolve this problem try one of the following:
- Analyze this project using the same version of SonarScanner for MSBuild
Expand All @@ -218,6 +228,7 @@ public void CopyDlls_WhenFileExistAndAreLockedButDifferentVersion_Fails()
// Do not close before to ensure the file is locked
file1.Close();
file2.Close();
file3.Close();
}
}

Expand Down Expand Up @@ -391,6 +402,7 @@ private IBootstrapperSettings MockBootstrapSettings(AnalysisPhase phase, bool de

File.Create(Path.Combine(rootDir, "SonarScanner.MSBuild.Common.dll")).Close();
File.Create(Path.Combine(rootDir, "SonarScanner.MSBuild.Tasks.dll")).Close();
File.Create(Path.Combine(rootDir, "Newtonsoft.Json.dll")).Close();
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

mockBootstrapSettings = new Mock<IBootstrapperSettings>();
mockBootstrapSettings.SetupGet(x => x.ChildCmdLineArgs).Returns(args.ToArray);
Expand Down
@@ -0,0 +1,38 @@
/*
csaba-sagi-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
csaba-sagi-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
* SonarScanner for .NET
* Copyright (C) 2016-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.sonar.it.scanner.msbuild;

public class EnvironmentVariable {
private final String name;
private final String value;

public EnvironmentVariable(String name, String value) {
this.name = name;
this.value = value;
}

public String getName() {
csaba-sagi-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
csaba-sagi-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
return name;
}

public String getValue() {
return value;
}
}