-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix analysis on MacOSX with .NET 8 when begin runtime doesn't match w…
…ith build runtime (#1863) Co-authored-by: Zsolt Kolbay <[email protected]>
- Loading branch information
1 parent
53e50fc
commit b7b0400
Showing
8 changed files
with
310 additions
and
152 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Tests/SonarScanner.MSBuild.Common.Test/EnvironmentBasedPlatformHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* SonarScanner for .NET | ||
* Copyright (C) 2016-2024 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. | ||
*/ | ||
|
||
using System; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using static SonarScanner.MSBuild.Common.EnvironmentBasedPlatformHelper; | ||
|
||
namespace SonarScanner.MSBuild.Common.Test; | ||
|
||
[TestClass] | ||
public class EnvironmentBasedPlatformHelperTests | ||
{ | ||
[TestMethod] | ||
public void GetFolderPath_WithUserProfile() | ||
{ | ||
Instance.GetFolderPath(Environment.SpecialFolder.UserProfile, Environment.SpecialFolderOption.None) | ||
.Should().Be(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile, Environment.SpecialFolderOption.None)); | ||
} | ||
|
||
[TestMethod] | ||
public void DirectoryExists_WithCurrentDirectory() | ||
{ | ||
Instance.DirectoryExists(Environment.CurrentDirectory).Should().BeTrue(); | ||
} | ||
} |
185 changes: 101 additions & 84 deletions
185
Tests/SonarScanner.MSBuild.Common.Test/MsBuildPathSettingsTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/SonarScanner.MSBuild.Common/EnvironmentBasedPlatformHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* SonarScanner for .NET | ||
* Copyright (C) 2016-2024 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. | ||
*/ | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
|
||
namespace SonarScanner.MSBuild.Common; | ||
|
||
public sealed class EnvironmentBasedPlatformHelper : IPlatformHelper | ||
{ | ||
public static IPlatformHelper Instance { get; } = new EnvironmentBasedPlatformHelper(); | ||
private readonly Lazy<PlatformOS> operatingSystem = new(CurrentOperatingSystem); | ||
|
||
public PlatformOS OperatingSystem => operatingSystem.Value; | ||
|
||
private EnvironmentBasedPlatformHelper() | ||
{ | ||
} | ||
|
||
public string GetFolderPath(Environment.SpecialFolder folder, Environment.SpecialFolderOption option) => Environment.GetFolderPath(folder, option); | ||
public bool DirectoryExists(string path) => Directory.Exists(path); | ||
|
||
// Not stable testable, manual testing was done by running the scanner on Windows, Mac OS X and Linux. | ||
[ExcludeFromCodeCoverage] | ||
private static PlatformOS CurrentOperatingSystem() | ||
{ | ||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) | ||
{ | ||
return PlatformOS.Windows; | ||
} | ||
else if (IsMacOSX()) | ||
{ | ||
return PlatformOS.MacOSX; | ||
} | ||
// Note: the Check for Mac OS X must preceed the check for Unix, because Environment.OSVersion.Platform returns PlatformID.Unix on Mac OS X | ||
else if (Environment.OSVersion.Platform == PlatformID.Unix) | ||
{ | ||
return PlatformOS.Unix; | ||
} | ||
else | ||
{ | ||
return PlatformOS.Unknown; | ||
} | ||
} | ||
|
||
// RuntimeInformation.IsOSPlatform is not suported in .NET Framework 4.6.2, it's only available from 4.7.1 | ||
// SystemVersion.plist exists on Mac OS X (and iOS) at least since 2002, so it's safe to check it, even though it's not a robust, future-proof solution. | ||
// See: https://stackoverflow.com/a/38795621 | ||
// TODO: once we drop support for .NET Framework 4.6.2 remove the call to File.Exists and use RuntimeInformation.IsOSPlatform instead of the Environment.OSVersion.Platform property | ||
private static bool IsMacOSX() => | ||
#if NETSTANDARD1_1_OR_GREATER || NETCOREAPP1_0_OR_GREATER | ||
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX); | ||
#else | ||
File.Exists("/System/Library/CoreServices/SystemVersion.plist"); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters