Skip to content

Commit d31ed02

Browse files
authored
Merge pull request #3676 from gep13/issue3668
(#3668) Add choco support command
2 parents 8c6e51a + 416d352 commit d31ed02

File tree

5 files changed

+339
-0
lines changed

5 files changed

+339
-0
lines changed

src/chocolatey.tests/chocolatey.tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<Compile Include="infrastructure.app\commands\ChocolateyApiKeyCommandSpecs.cs" />
152152
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommandSpecs.cs" />
153153
<Compile Include="infrastructure.app\commands\ChocolateyRuleCommandSpecs.cs" />
154+
<Compile Include="infrastructure.app\commands\ChocolateySupportCommandSpecs.cs" />
154155
<Compile Include="infrastructure.app\commands\ChocolateyTemplateCommandSpecs.cs" />
155156
<Compile Include="infrastructure.app\commands\ChocolateyExportCommandSpecs.cs" />
156157
<Compile Include="infrastructure.app\commands\ChocolateyFeatureCommandSpecs.cs" />
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright © 2017 - 2025 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
using chocolatey.infrastructure.app.attributes;
18+
using chocolatey.infrastructure.app.commands;
19+
using chocolatey.infrastructure.app.configuration;
20+
using FluentAssertions;
21+
using Moq;
22+
using System.Collections.Generic;
23+
using System.Linq;
24+
using System.Reflection;
25+
26+
namespace chocolatey.tests.infrastructure.app.commands
27+
{
28+
public class ChocolateySupportCommandSpecs
29+
{
30+
[ConcernFor("support")]
31+
public abstract class ChocolateySupportCommandSpecsBase : TinySpec
32+
{
33+
protected ChocolateySupportCommand Command;
34+
protected ChocolateyConfiguration Configuration = new ChocolateyConfiguration();
35+
36+
public override void Context()
37+
{
38+
Command = new ChocolateySupportCommand();
39+
}
40+
}
41+
42+
public class When_Implementing_Command_For : ChocolateySupportCommandSpecsBase
43+
{
44+
private List<CommandForAttribute> _results;
45+
46+
public override void Because()
47+
{
48+
_results = Command.GetType().GetCustomAttributes<CommandForAttribute>().ToList();
49+
}
50+
51+
[Fact]
52+
public void Should_Have_Expected_Number_Of_Commands()
53+
{
54+
_results.Should().HaveCount(1);
55+
}
56+
57+
[InlineData("support")]
58+
public void Should_Implement_Expected_Command(string name)
59+
{
60+
_results.Should().ContainSingle(r => r.CommandName == name);
61+
}
62+
63+
[Fact]
64+
public void Should_Specify_Expected_Version_For_All_Commands()
65+
{
66+
_results.Should().AllSatisfy(r => r.Version.Should().Be("2.5.0"));
67+
}
68+
}
69+
70+
public class When_Noop_Is_Called : ChocolateySupportCommandSpecsBase
71+
{
72+
public override void Because()
73+
{
74+
Command.DryRun(Configuration);
75+
}
76+
77+
[Fact]
78+
public void Should_log_a_message()
79+
{
80+
MockLogger.Verify(l => l.Info(It.IsAny<string>()), Times.AtLeastOnce);
81+
}
82+
83+
[Fact]
84+
public void Should_log_the_message_we_expect()
85+
{
86+
var messages = MockLogger.MessagesFor(LogLevel.Info);
87+
messages.Should().ContainSingle();
88+
messages[0].Should().Contain("Unfortunately, we are unable to provide private support");
89+
}
90+
}
91+
92+
public class When_Run_Is_Called : ChocolateySupportCommandSpecsBase
93+
{
94+
public override void Because()
95+
{
96+
Command.Run(Configuration);
97+
}
98+
99+
[Fact]
100+
public void Should_log_a_message()
101+
{
102+
MockLogger.Verify(l => l.Info(It.IsAny<string>()), Times.AtLeastOnce);
103+
}
104+
105+
[Fact]
106+
public void Should_log_the_message_we_expect()
107+
{
108+
var messages = MockLogger.MessagesFor(LogLevel.Info);
109+
messages.Should().ContainSingle();
110+
messages[0].Should().Contain("Unfortunately, we are unable to provide private support");
111+
}
112+
}
113+
114+
public class When_Help_Is_Called : ChocolateySupportCommandSpecsBase
115+
{
116+
public override void Because()
117+
{
118+
Command.HelpMessage(Configuration);
119+
}
120+
121+
[Fact]
122+
public void Should_log_a_message()
123+
{
124+
MockLogger.Verify(l => l.Info(It.IsAny<string>()), Times.AtLeastOnce);
125+
}
126+
127+
[Fact]
128+
public void Should_log_the_message_we_expect()
129+
{
130+
var messages = MockLogger.MessagesFor(LogLevel.Info);
131+
messages.Should().HaveCount(2);
132+
messages[0].Should().Contain("Support Command");
133+
messages[1].Should().Contain("As an open-source user of Chocolatey CLI, we are not able to");
134+
}
135+
}
136+
}
137+
}

src/chocolatey/chocolatey.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommand.cs" />
182182
<Compile Include="infrastructure.app\commands\ChocolateyListCommand.cs" />
183183
<Compile Include="infrastructure.app\commands\ChocolateyRuleCommand.cs" />
184+
<Compile Include="infrastructure.app\commands\ChocolateySupportCommand.cs" />
184185
<Compile Include="infrastructure.app\commands\ChocolateyTemplateCommand.cs" />
185186
<Compile Include="infrastructure.app\commands\ChocolateyCommandBase.cs" />
186187
<Compile Include="infrastructure.app\domain\ApiKeyCommandType.cs" />
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright © 2017 - 2025 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using chocolatey.infrastructure.app.attributes;
20+
using chocolatey.infrastructure.app.configuration;
21+
using chocolatey.infrastructure.commandline;
22+
using chocolatey.infrastructure.commands;
23+
using chocolatey.infrastructure.logging;
24+
25+
namespace chocolatey.infrastructure.app.commands
26+
{
27+
[CommandFor("support", "provides support information", Version = "2.5.0")]
28+
public class ChocolateySupportCommand : ICommand
29+
{
30+
public void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration configuration)
31+
{
32+
// Intentionally left blank
33+
}
34+
35+
public void ParseAdditionalArguments(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
36+
{
37+
// Intentionally left blank
38+
}
39+
40+
public void Validate(ChocolateyConfiguration configuration)
41+
{
42+
// Intentionally left blank
43+
}
44+
45+
public void HelpMessage(ChocolateyConfiguration configuration)
46+
{
47+
this.Log().Info(ChocolateyLoggers.Important, "Support Command");
48+
this.Log().Info(@"
49+
As an open-source user of Chocolatey CLI, we are not able to
50+
provide private support. See https://chocolatey.org/support
51+
for details.
52+
");
53+
54+
}
55+
56+
public void DryRun(ChocolateyConfiguration configuration)
57+
{
58+
Run(configuration);
59+
}
60+
61+
public void Run(ChocolateyConfiguration config)
62+
{
63+
var isLicensed = config.Information.IsLicensedVersion;
64+
65+
if (config.Information.IsLicensedVersion)
66+
{
67+
this.Log().Warn(@"
68+
As a licensed customer, you can access our Support Team, however,
69+
it looks like the Chocolatey Licensed Extension package is not
70+
currently installed. Ensure that you run:
71+
`choco install chocolatey.extension`
72+
and run `choco support` again.
73+
");
74+
}
75+
else
76+
{
77+
this.Log().Info(@"
78+
Unfortunately, we are unable to provide private support for
79+
open-source users. However, there are a lot of avenues for open
80+
source users within the community to get the help they need.
81+
82+
If you are an open-source user, please visit
83+
https://chocolatey.org/support for the full list of options, or
84+
https://docs.chocolatey.org for our open source documentation.
85+
");
86+
}
87+
}
88+
89+
public bool MayRequireAdminAccess()
90+
{
91+
return false;
92+
}
93+
94+
#pragma warning disable IDE1006
95+
[Obsolete("This overload is deprecated and will be removed in v3.")]
96+
public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
97+
{
98+
ConfigureArgumentParser(optionSet, configuration);
99+
}
100+
101+
[Obsolete("This overload is deprecated and will be removed in v3.")]
102+
public virtual void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
103+
{
104+
ParseAdditionalArguments(unparsedArguments, configuration);
105+
}
106+
107+
[Obsolete("This overload is deprecated and will be removed in v3.")]
108+
public virtual void handle_validation(ChocolateyConfiguration configuration)
109+
{
110+
Validate(configuration);
111+
}
112+
113+
[Obsolete("This overload is deprecated and will be removed in v3.")]
114+
public virtual void help_message(ChocolateyConfiguration configuration)
115+
{
116+
HelpMessage(configuration);
117+
}
118+
119+
[Obsolete("This overload is deprecated and will be removed in v3.")]
120+
public virtual void noop(ChocolateyConfiguration configuration)
121+
{
122+
DryRun(configuration);
123+
}
124+
125+
[Obsolete("This overload is deprecated and will be removed in v3.")]
126+
public virtual void run(ChocolateyConfiguration configuration)
127+
{
128+
Run(configuration);
129+
}
130+
131+
[Obsolete("This overload is deprecated and will be removed in v3.")]
132+
public virtual bool may_require_admin_access()
133+
{
134+
return MayRequireAdminAccess();
135+
}
136+
#pragma warning restore IDE1006
137+
}
138+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Import-Module helpers/common-helpers
2+
3+
Describe "choco support" -Tag Chocolatey, SupportCommand {
4+
BeforeDiscovery {
5+
$HasLicensedExtension = Test-PackageIsEqualOrHigher -PackageName 'chocolatey.extension' -Version '6.0.0'
6+
}
7+
8+
BeforeAll {
9+
Remove-NuGetPaths
10+
Initialize-ChocolateyTestInstall
11+
New-ChocolateyInstallSnapshot
12+
}
13+
14+
AfterAll {
15+
Remove-ChocolateyTestInstall
16+
}
17+
18+
Context "Support" {
19+
BeforeAll {
20+
$Output = Invoke-Choco support
21+
}
22+
23+
It "Exits successfully (0)" {
24+
$Output.ExitCode | Should -Be 0 -Because $Output.String
25+
}
26+
27+
It "Reports support options" {
28+
$ExpectedOutput = if($HasLicensedExtension) {
29+
"Howdy, you have access to private support channels."
30+
} else {
31+
"Unfortunately, we are unable to provide private support for"
32+
}
33+
$Output.Lines | Should -Contain $ExpectedOutput -Because $Output.String
34+
}
35+
}
36+
37+
Context "Help Documentation (<_>)" -ForEach @("--help", "-?", "-help") {
38+
BeforeAll {
39+
$Output = Invoke-Choco support $_
40+
}
41+
42+
It "Exits successfully (0)" {
43+
$Output.ExitCode | Should -Be 0 -Because $Output.String
44+
}
45+
46+
It "Outputs Help for Support" {
47+
$Output.String | Should -Match "Support Command" -Because $Output.String
48+
}
49+
50+
It "Outputs Help for Support" {
51+
$ExpectedOutput = if($HasLicensedExtension) {
52+
"As a licensed customer, you can reach out to"
53+
} else {
54+
"As an open-source user of Chocolatey CLI, we are not able to"
55+
}
56+
$Output.Lines | Should -Contain $ExpectedOutput -Because $Output.String
57+
}
58+
}
59+
60+
# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
61+
Test-NuGetPaths
62+
}

0 commit comments

Comments
 (0)