Skip to content

(#2851) fix license validation output #2857

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

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/chocolatey/infrastructure/licensing/LicenseValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.IO;
using System.Linq;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.logging;
using Rhino.Licensing;
Expand All @@ -36,6 +37,8 @@ public static ChocolateyLicense Validate()
};

var regularLogOutput = ShouldLogErrorsToConsole();
var normalLogger = regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly;
var importantLogger = regularLogOutput ? ChocolateyLoggers.Important : ChocolateyLoggers.LogFileOnly;

var licenseFile = ApplicationParameters.LicenseFileLocation;
var userLicenseFile = ApplicationParameters.UserLicenseFileLocation;
Expand All @@ -56,19 +59,19 @@ public static ChocolateyLicense Validate()
{
if (Directory.GetFiles(licenseDirectory).Length != 0)
{
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, @"Files found in directory '{0}' but not a
"chocolatey".Log().Error(normalLogger, @"Files found in directory '{0}' but not a
valid license file. License should be named '{1}'.".FormatWith(licenseDirectory, licenseFileName));
"chocolatey".Log().Warn(ChocolateyLoggers.Important, @" Rename license file to '{0}' to allow commercial features.".FormatWith(licenseFileName));
"chocolatey".Log().Warn(importantLogger, @" Rename license file to '{0}' to allow commercial features.".FormatWith(licenseFileName));
}
}


// - user put the license file in the top level location and/or forgot to rename it
if (File.Exists(Path.Combine(ApplicationParameters.InstallLocation, licenseFileName)) || File.Exists(Path.Combine(ApplicationParameters.InstallLocation, licenseFileName + ".txt")))
{
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, @"Chocolatey license found in the wrong location. File must be located at
"chocolatey".Log().Error(normalLogger, @"Chocolatey license found in the wrong location. File must be located at
'{0}'.".FormatWith(ApplicationParameters.LicenseFileLocation));
"chocolatey".Log().Warn(regularLogOutput ? ChocolateyLoggers.Important : ChocolateyLoggers.LogFileOnly, @" Move license file to '{0}' to allow commercial features.".FormatWith(ApplicationParameters.LicenseFileLocation));
"chocolatey".Log().Warn(importantLogger, @" Move license file to '{0}' to allow commercial features.".FormatWith(ApplicationParameters.LicenseFileLocation));
}
}

Expand All @@ -94,15 +97,15 @@ public static ChocolateyLicense Validate()
{
chocolateyLicense.IsValid = false;
chocolateyLicense.InvalidReason = e.Message;
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, "A license was not found for a licensed version of Chocolatey:{0} {1}{0} {2}".FormatWith(Environment.NewLine, e.Message,
"chocolatey".Log().Error(normalLogger, "A license was not found for a licensed version of Chocolatey:{0} {1}{0} {2}".FormatWith(Environment.NewLine, e.Message,
"A license was also not found in the user profile: '{0}'.".FormatWith(ApplicationParameters.UserLicenseFileLocation)));
}
catch (Exception e)
{
//license may be invalid
chocolateyLicense.IsValid = false;
chocolateyLicense.InvalidReason = e.Message;
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, "A license was found for a licensed version of Chocolatey, but is invalid:{0} {1}".FormatWith(Environment.NewLine, e.Message));
"chocolatey".Log().Error(normalLogger, "A license was found for a licensed version of Chocolatey, but is invalid:{0} {1}".FormatWith(Environment.NewLine, e.Message));
}

var chocolateyLicenseType = ChocolateyLicenseType.Unknown;
Expand Down Expand Up @@ -140,6 +143,12 @@ public static ChocolateyLicense Validate()

private static bool ShouldLogErrorsToConsole()
{
var limitOutputArguments = new string[]
{
"--limit-output",
"--limitoutput",
"-r"
};
var args = Environment.GetCommandLineArgs();
// I think this check is incorrect??? if --version is supposed to return false, it can return true at this point?
if (args == null || args.Length < 2)
Expand All @@ -153,6 +162,11 @@ private static bool ShouldLogErrorsToConsole()
return false;
}

if (args.Count(argument => limitOutputArguments.Contains(argument)) > 0)
{
return false;
}

return true;
}

Expand Down
63 changes: 63 additions & 0 deletions tests/pester-tests/features/LimitOutput.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Import-Module helpers/common-helpers

Describe "choco limit-output tests" -Tag Chocolatey, LimitOutputFeature {
BeforeDiscovery {
$Flags = @(
'-r'
'--limitoutput'
'--limit-output'
)
}

BeforeAll {
Initialize-ChocolateyTestInstall
New-ChocolateyInstallSnapshot
}

AfterAll {
Remove-ChocolateyTestInstall
}

Context 'Running with flag <_> with wrong license files existing' -ForEach $Flags {
BeforeAll {
$paths = Restore-ChocolateyInstallSnapshot
New-Item -Path "$($paths.InstallPath)\license" -ItemType Directory
Set-Content -Path "$($paths.InstallPath)\license\test.xml" -Value ""

$Output = Invoke-Choco help $_
}

It "Should not output Files not found in license directory." {
[array]$foundLine = $Output.Lines | Where-Object { $_ -match "Files found in directory|valid license file. License should be named"}
$foundLine.Count | Should -Be 0 -Because $Output.String
}
}

Context 'Running with <_> with license file in wrong location' -ForEach $Flags {
BeforeAll {
$paths = Restore-ChocolateyInstallSnapshot
Set-Content -Path "$($paths.InstallPath)\chocolatey.license.xml" -Value ""

$Output = Invoke-Choco help $_
}

It "Should not output license file being in wrong location" {
$Output.Lines | Should -Not -Contain "Chocolatey license found in the wrong location. File must be located at" -Because $Output.String
}
}

Context 'Running with <_> with invalid license file in correct location' -ForEach $Flags {
BeforeAll {
$paths = Restore-ChocolateyInstallSnapshot
New-Item -Path "$($paths.InstallPath)\license" -ItemType Directory
Set-Content -Path "$($paths.InstallPath)\license\chocolatey.license.xml" -Value ""

$Output = Invoke-Choco help $_
}

It "Should not output license being invalid" {
$Output.Lines | Should -Not -Contain "A license was found for a licensed version of Chocolatey, but is invalid:"
$Output.Lines | Should -Not -Contain "Could not validate existing license"
}
}
}
Loading