Skip to content

Commit

Permalink
Review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource committed Jul 10, 2024
1 parent 16bdeef commit 7138c77
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ private sealed class OperatingSystemProvider(
public PlatformOS OperatingSystem() => os;
public bool DirectoryExists(string path) => directoryExistsFunc(path);
public string GetFolderPath(Environment.SpecialFolder folder, Environment.SpecialFolderOption option) => pathFunc(folder, option);
public bool IsUnix() => throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public async Task Checksum_DownloadFile_FileOpenFails()
fileWrapper.Open(file).Throws<IOException>();

var sut = CreateSutWithSubstitutes();
var result = await sut.DownloadJreAsync(home, new( "filename.tar.gz", "sha256", "javaPath"), () => Task.FromResult<Stream>(new MemoryStream()));
var result = await sut.DownloadJreAsync(home, new("filename.tar.gz", "sha256", "javaPath"), () => Task.FromResult<Stream>(new MemoryStream()));
result.Should().BeOfType<JreCacheFailure>().Which.Message.Should().Be("The checksum of the downloaded Java runtime environment does not match the expected checksum.");
testLogger.AssertDebugLogged(@"The calculation of the checksum of the file 'C:\Users\user\.sonar\cache\sha256\filename.tar.gz' failed with message " +
"'I/O error occurred.'.");
Expand All @@ -559,7 +559,7 @@ public async Task UnpackerFactory_Success()
var home = @"C:\Users\user\.sonar";
var cache = Path.Combine(home, "cache");
var sha = Path.Combine(cache, "sha256");
var file = Path.Combine(sha, "filename.tar.gz");
var file = Path.Combine(sha, "filename.tar.gz");
directoryWrapper.Exists(cache).Returns(true);
directoryWrapper.Exists(sha).Returns(true);
fileWrapper.Exists(file).Returns(false);
Expand Down Expand Up @@ -612,7 +612,7 @@ public async Task UnpackerFactory_UnsupportedFormat()
var sha = Path.Combine(cache, "sha256");
directoryWrapper.Exists(cache).Returns(true);
directoryWrapper.Exists(sha).Returns(true);
unpackerFactory.Create(directoryWrapper, fileWrapper,operatingSystemProvider, "filename.tar.gz").ReturnsNull();
unpackerFactory.Create(directoryWrapper, fileWrapper, operatingSystemProvider, "filename.tar.gz").ReturnsNull();

var sut = CreateSutWithSubstitutes();
var result = await sut.DownloadJreAsync(home, new("filename.tar.gz", "sha256", "javaPath"), () => Task.FromResult<Stream>(new MemoryStream()));
Expand All @@ -630,7 +630,7 @@ public async Task Unpack_Success()
var home = @"C:\Users\user\.sonar";
var cache = Path.Combine(home, "cache");
var sha = Path.Combine(cache, "sha256");
var file = Path.Combine(sha, "filename.tar.gz");
var file = Path.Combine(sha, "filename.tar.gz");
directoryWrapper.Exists(cache).Returns(true);
directoryWrapper.Exists(sha).Returns(true);
fileWrapper.Create(Arg.Any<string>()).Returns(new MemoryStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class UnpackerFactoryTests
[DataRow(@"/usr/File.zip", typeof(ZipUnpacker))]
[DataRow("File.tar.gz", typeof(TarGzUnpacker))]
[DataRow("File.TAR.GZ", typeof(TarGzUnpacker))]
[DataRow(@"c:\test\File.GZ", typeof(TarGzUnpacker))]
[DataRow(@"/usr/File.TAR.gz", typeof(TarGzUnpacker))]
[DataRow(@"/usr/File.tar.GZ", typeof(TarGzUnpacker))]
public void SupportedFileExtensions(string fileName, Type expectedUnpacker)
{
var sut = new UnpackerFactory();
Expand All @@ -51,6 +51,8 @@ public void SupportedFileExtensions(string fileName, Type expectedUnpacker)
[DataTestMethod]
[DataRow("File.rar")]
[DataRow("File.7z")]
[DataRow("File.gz")]
[DataRow("File.tar")]
public void UnsupportedFileExtensions(string fileName)
{
var sut = new UnpackerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private sealed class UnixTestOperatingSystemProvider : IOperatingSystemProvider
public PlatformOS OperatingSystem() => PlatformOS.Linux;

public string GetFolderPath(Environment.SpecialFolder folder, Environment.SpecialFolderOption option) => throw new NotSupportedException();

public bool DirectoryExists(string path) => throw new NotSupportedException();
public bool IsUnix() => throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface IOperatingSystemProvider
{
public PlatformOS OperatingSystem();

public bool IsUnix();

string GetFolderPath(Environment.SpecialFolder folder, Environment.SpecialFolderOption option);

bool DirectoryExists(string path);
Expand Down
5 changes: 5 additions & 0 deletions src/SonarScanner.MSBuild.Common/OperatingSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public bool IsAlpine() =>
IsAlpineRelease("/etc/os-release")
|| IsAlpineRelease("/usr/lib/os-release");

// Not stable testable
[ExcludeFromCodeCoverage]
public bool IsUnix() =>
OperatingSystem() is PlatformOS.Linux or PlatformOS.Alpine;

// Not stable testable, manual testing was done by running the scanner on Windows, Mac OS X and Linux.
[ExcludeFromCodeCoverage]
private PlatformOS OperatingSystemCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ private void ExtractEntry(TarInputStream tar, string destinationFullPath, TarEnt
directoryWrapper.CreateDirectory(destinationFileDirectory);

using var outputStream = fileWrapper.Create(destinationFile);
// If translation is disabled, just copy the entry across directly.
tar.CopyEntryContents(outputStream);

#if NETSTANDARD
if (operatingSystemProvider.OperatingSystem() is PlatformOS.Linux or PlatformOS.Alpine)
if (operatingSystemProvider.IsUnix())
{
_ = new Mono.Unix.UnixFileInfo(destinationFile)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.IO;
using SonarScanner.MSBuild.Common;

Expand All @@ -31,7 +32,10 @@ public IUnpacker Create(IDirectoryWrapper directoryWrapper, IFileWrapper fileWra
Path.GetExtension(archive).ToUpperInvariant() switch
{
".ZIP" => new ZipUnpacker(),
".GZ" => new TarGzUnpacker(directoryWrapper, fileWrapper, operatingSystemProvider),
".GZ" when IsTarball(archive) => new TarGzUnpacker(directoryWrapper, fileWrapper, operatingSystemProvider),
_ => null
};

private static bool IsTarball(string filename) =>
string.Equals(Path.GetExtension(Path.GetFileNameWithoutExtension(filename)), ".tar", StringComparison.OrdinalIgnoreCase);
}

0 comments on commit 7138c77

Please sign in to comment.