Skip to content

Commit

Permalink
(#3) disable installing on non-windows
Browse files Browse the repository at this point in the history
and instead issue a warning.
  • Loading branch information
nils-a committed Jul 30, 2024
1 parent d1bea01 commit 2581a37
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<PackageReference Include="Cake.Testing" Version="$(CakeVersion)">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Cake.Testing.Xunit" Version="$(CakeVersion)">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ internal ChocolateyPackageInstallerFixture()
Package = new PackageReference("choco:?package=windirstat");
PackageType = PackageType.Addin;
InstallPath = new DirectoryPath("./chocolatey");
EnsureDirExists(InstallPath);
}

private void EnsureDirExists(DirectoryPath path)
{
var d = FileSystem.GetDirectory(path.MakeAbsolute(Environment));

if (!d.Exists)
{
d.Create();
}
}

/// <summary>
Expand All @@ -48,7 +59,7 @@ internal ChocolateyPackageInstallerFixture()
/// <returns>The chocolatey package installer.</returns>
internal ChocolateyPackageInstaller CreateInstaller()
{
return new ChocolateyPackageInstaller(Environment, ProcessRunner, Log, ContentResolver, Config);
return new ChocolateyPackageInstaller(Environment, ProcessRunner, Log, ContentResolver, Config, FileSystem);
}

/// <summary>ChocolateyPackageInstallerFixture
Expand All @@ -71,4 +82,4 @@ internal bool CanInstall()
return installer.CanInstall(Package, PackageType);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Packaging;
using Cake.Core.Packaging;
using NSubstitute;
using System;
using System.Collections.Generic;

using Cake.Testing.Xunit;

using Xunit;

namespace Cake.Chocolatey.Module.Tests
Expand Down Expand Up @@ -123,7 +123,7 @@ public void Should_Not_Be_Able_To_Install_If_Scheme_Is_Incorrect()
Assert.False(result);
}

[Fact]
[WindowsFact]
public void Should_Ignore_Custom_Source_If_AbsoluteUri_Is_Used()
{
var fixture = new ChocolateyPackageInstallerFixture();
Expand All @@ -137,7 +137,7 @@ public void Should_Ignore_Custom_Source_If_AbsoluteUri_Is_Used()
fixture.Config.DidNotReceive().GetValue(CHOCOLATEY_CONFIGKEY);
}

[Fact]
[WindowsFact]
public void Should_Use_Custom_Source_If_RelativeUri_Is_Used()
{
var fixture = new ChocolateyPackageInstallerFixture();
Expand Down Expand Up @@ -183,6 +183,19 @@ public void Should_Throw_If_Install_Path_Is_Null()
Assert.IsType<ArgumentNullException>(result);
Assert.Equal("path", ((ArgumentNullException)result).ParamName);
}

[NonWindowsFact]
public void On_Non_Windows_Install_Does_Not_Fail()
{
var fixture = new ChocolateyPackageInstallerFixture();
fixture.Package = new PackageReference("choco:?package=windirstat");

// When
var result = fixture.Install();

// Then
Assert.Single(result);
}
}
}
}
}
23 changes: 22 additions & 1 deletion Source/Cake.Chocolatey.Module/ChocolateyPackageInstaller.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cake.Core;
using Cake.Core.Configuration;
Expand All @@ -19,6 +20,7 @@ public sealed class ChocolateyPackageInstaller : IPackageInstaller
private readonly ICakeLog _log;
private readonly IChocolateyContentResolver _contentResolver;
private readonly ICakeConfiguration _config;
private readonly IFileSystem _fileSystem;

/// <summary>
/// Initializes a new instance of the <see cref="ChocolateyPackageInstaller"/> class.
Expand All @@ -28,13 +30,15 @@ public sealed class ChocolateyPackageInstaller : IPackageInstaller
/// <param name="log">The log.</param>
/// <param name="contentResolver">The Chocolatey Package Content Resolver.</param>
/// <param name="config">the configuration.</param>
public ChocolateyPackageInstaller(ICakeEnvironment environment, IProcessRunner processRunner, ICakeLog log, IChocolateyContentResolver contentResolver, ICakeConfiguration config)
/// <param name="fileSystem">the fileSystem.</param>
public ChocolateyPackageInstaller(ICakeEnvironment environment, IProcessRunner processRunner, ICakeLog log, IChocolateyContentResolver contentResolver, ICakeConfiguration config, IFileSystem fileSystem)
{
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_processRunner = processRunner ?? throw new ArgumentNullException(nameof(processRunner));
_log = log ?? throw new ArgumentNullException(nameof(log));
_contentResolver = contentResolver ?? throw new ArgumentNullException(nameof(contentResolver));
_config = config;
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
}

/// <summary>
Expand Down Expand Up @@ -74,6 +78,23 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType

path = path.MakeAbsolute(_environment);

if (!_environment.Platform.IsWindows())
{
// choco runs only on windows!
_log.Warning("Unable to install {0}, since the choco-scheme can only install on Windows. Ensure {0} exists on your system!", package.Package);

// we need to return something, or else the installation counts as "failed".
var noInstallFilePath = path.CombineWithFilePath("no-choco-install.txt");
var noInstallFile = _fileSystem.GetFile(noInstallFilePath);
using (var stream = noInstallFile.Open(FileMode.Append))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("{0} not installed, since we're not running on windows.", package.Package);
}

return new[] { noInstallFile };
}

// Install the package.
_log.Debug("Installing Chocolatey package {0}...", package.Package);
var process = _processRunner.Start(
Expand Down

0 comments on commit 2581a37

Please sign in to comment.