Skip to content

Commit 8c6e51a

Browse files
authored
Merge pull request #2857 from corbob/2851-fix-license-validation-output
(#2851) fix license validation output
2 parents fcc049a + 6d7200d commit 8c6e51a

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

src/chocolatey/infrastructure/licensing/LicenseValidation.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.IO;
19+
using System.Linq;
1920
using chocolatey.infrastructure.app;
2021
using chocolatey.infrastructure.logging;
2122
using Rhino.Licensing;
@@ -36,6 +37,8 @@ public static ChocolateyLicense Validate()
3637
};
3738

3839
var regularLogOutput = ShouldLogErrorsToConsole();
40+
var normalLogger = regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly;
41+
var importantLogger = regularLogOutput ? ChocolateyLoggers.Important : ChocolateyLoggers.LogFileOnly;
3942

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

6568

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

@@ -94,15 +97,15 @@ public static ChocolateyLicense Validate()
9497
{
9598
chocolateyLicense.IsValid = false;
9699
chocolateyLicense.InvalidReason = e.Message;
97-
"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,
100+
"chocolatey".Log().Error(normalLogger, "A license was not found for a licensed version of Chocolatey:{0} {1}{0} {2}".FormatWith(Environment.NewLine, e.Message,
98101
"A license was also not found in the user profile: '{0}'.".FormatWith(ApplicationParameters.UserLicenseFileLocation)));
99102
}
100103
catch (Exception e)
101104
{
102105
//license may be invalid
103106
chocolateyLicense.IsValid = false;
104107
chocolateyLicense.InvalidReason = e.Message;
105-
"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));
108+
"chocolatey".Log().Error(normalLogger, "A license was found for a licensed version of Chocolatey, but is invalid:{0} {1}".FormatWith(Environment.NewLine, e.Message));
106109
}
107110

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

141144
private static bool ShouldLogErrorsToConsole()
142145
{
146+
var limitOutputArguments = new string[]
147+
{
148+
"--limit-output",
149+
"--limitoutput",
150+
"-r"
151+
};
143152
var args = Environment.GetCommandLineArgs();
144153
// I think this check is incorrect??? if --version is supposed to return false, it can return true at this point?
145154
if (args == null || args.Length < 2)
@@ -153,6 +162,11 @@ private static bool ShouldLogErrorsToConsole()
153162
return false;
154163
}
155164

165+
if (args.Count(argument => limitOutputArguments.Contains(argument)) > 0)
166+
{
167+
return false;
168+
}
169+
156170
return true;
157171
}
158172

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Import-Module helpers/common-helpers
2+
3+
Describe "choco limit-output tests" -Tag Chocolatey, LimitOutputFeature {
4+
BeforeDiscovery {
5+
$Flags = @(
6+
'-r'
7+
'--limitoutput'
8+
'--limit-output'
9+
)
10+
}
11+
12+
BeforeAll {
13+
Initialize-ChocolateyTestInstall
14+
New-ChocolateyInstallSnapshot
15+
}
16+
17+
AfterAll {
18+
Remove-ChocolateyTestInstall
19+
}
20+
21+
Context 'Running with flag <_> with wrong license files existing' -ForEach $Flags {
22+
BeforeAll {
23+
$paths = Restore-ChocolateyInstallSnapshot
24+
New-Item -Path "$($paths.InstallPath)\license" -ItemType Directory
25+
Set-Content -Path "$($paths.InstallPath)\license\test.xml" -Value ""
26+
27+
$Output = Invoke-Choco help $_
28+
}
29+
30+
It "Should not output Files not found in license directory." {
31+
[array]$foundLine = $Output.Lines | Where-Object { $_ -match "Files found in directory|valid license file. License should be named"}
32+
$foundLine.Count | Should -Be 0 -Because $Output.String
33+
}
34+
}
35+
36+
Context 'Running with <_> with license file in wrong location' -ForEach $Flags {
37+
BeforeAll {
38+
$paths = Restore-ChocolateyInstallSnapshot
39+
Set-Content -Path "$($paths.InstallPath)\chocolatey.license.xml" -Value ""
40+
41+
$Output = Invoke-Choco help $_
42+
}
43+
44+
It "Should not output license file being in wrong location" {
45+
$Output.Lines | Should -Not -Contain "Chocolatey license found in the wrong location. File must be located at" -Because $Output.String
46+
}
47+
}
48+
49+
Context 'Running with <_> with invalid license file in correct location' -ForEach $Flags {
50+
BeforeAll {
51+
$paths = Restore-ChocolateyInstallSnapshot
52+
New-Item -Path "$($paths.InstallPath)\license" -ItemType Directory
53+
Set-Content -Path "$($paths.InstallPath)\license\chocolatey.license.xml" -Value ""
54+
55+
$Output = Invoke-Choco help $_
56+
}
57+
58+
It "Should not output license being invalid" {
59+
$Output.Lines | Should -Not -Contain "A license was found for a licensed version of Chocolatey, but is invalid:"
60+
$Output.Lines | Should -Not -Contain "Could not validate existing license"
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)