Skip to content

Commit

Permalink
(maint) Extract all used environment variables
Browse files Browse the repository at this point in the history
These changes extracts all of the environment variables that we
make use of while running Chocolatey CLI. Both variables that are
used as part of running package scripts, and variables we use
only when they are set on the system.

A change to make these environment variables and all other internal
types available to other products and the test projects has been made.
  • Loading branch information
AdmiringWorm committed Jun 30, 2023
1 parent d836138 commit bf0d8b9
Show file tree
Hide file tree
Showing 21 changed files with 830 additions and 196 deletions.
11 changes: 11 additions & 0 deletions src/chocolatey.console/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("2f406051-3fe2-45f4-97bf-40968b86c5a9")]

// TODO: Make internals visible to Chocolatey Licensed Extension. This section is commented out as it will cause problems upstream when ilmerged.
// We allow the officially built chocolatey.extension to always see the internals.
//[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
//[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]

//#if !FORCE_CHOCOLATEY_OFFICIAL_KEY
//[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
//[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
//#endif
Binary file added src/chocolatey.tests.integration/TestKey.snk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>TestKey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Chocolatey.NuGet.Common">
<HintPath>..\packages\Chocolatey.NuGet.Common.3.3.0\lib\net472\Chocolatey.NuGet.Common.dll</HintPath>
Expand Down Expand Up @@ -176,6 +182,7 @@
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="TestKey.snk" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\chocolatey.tests\chocolatey.tests.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace chocolatey.tests.integration.infrastructure.app.builders
using Microsoft.Win32;
using scenarios;
using FluentAssertions;
using static chocolatey.StringResources;

public class ConfigurationBuilderSpecs
{
Expand Down Expand Up @@ -116,11 +117,11 @@ public override void Context()

if (EnvironmentVariableSet)
{
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn("http_proxy", "https_proxy"))).Returns(EnvironmentVariableProxyValue);
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn(EnvironmentVariables.System.HttpProxy, EnvironmentVariables.System.HttpsProxy))).Returns(EnvironmentVariableProxyValue);
}
else
{
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn("http_proxy", "https_proxy"))).Returns(string.Empty);
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn(EnvironmentVariables.System.HttpProxy, EnvironmentVariables.System.HttpsProxy))).Returns(string.Empty);
}

if (ConfigSet)
Expand Down Expand Up @@ -192,11 +193,11 @@ public override void Context()

if (EnvironmentVariableSet)
{
Environment.Setup(e => e.GetEnvironmentVariable("no_proxy")).Returns(EnvironmentVariableProxyValue);
Environment.Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.System.NoProxy)).Returns(EnvironmentVariableProxyValue);
}
else
{
Environment.Setup(e => e.GetEnvironmentVariable("no_proxy")).Returns(string.Empty);
Environment.Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.System.NoProxy)).Returns(string.Empty);
}

if (ArgumentSet)
Expand Down
Binary file added src/chocolatey.tests/TestKey.snk
Binary file not shown.
7 changes: 4 additions & 3 deletions src/chocolatey.tests/TinySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace chocolatey.tests
using chocolatey.infrastructure.logging;
using System.IO;
using chocolatey.infrastructure.app.nuget;
using static chocolatey.StringResources;

// ReSharper disable InconsistentNaming

Expand All @@ -32,12 +33,12 @@ public class NUnitSetup
{
public static MockLogger MockLogger { get; set; }

private static readonly string _installLocationVariable = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName);
private static readonly string _installLocationVariable = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall);

[OneTimeSetUp]
public virtual void BeforeEverything()
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, string.Empty);
Environment.SetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, string.Empty);
MockLogger = new MockLogger();
Log.InitializeWith(MockLogger);
// do not log trace messages
Expand All @@ -48,7 +49,7 @@ public virtual void BeforeEverything()
[OneTimeTearDown]
public void AfterEverything()
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, _installLocationVariable);
Environment.SetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, _installLocationVariable);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>TestKey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Chocolatey.NuGet.Commands">
<HintPath>..\packages\Chocolatey.NuGet.Commands.3.3.0\lib\net472\Chocolatey.NuGet.Commands.dll</HintPath>
Expand Down Expand Up @@ -223,6 +229,7 @@
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="TestKey.snk" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\chocolatey\chocolatey.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace chocolatey.tests.infrastructure.filesystem
using Moq;
using NUnit.Framework;
using FluentAssertions;
using static chocolatey.StringResources;

public class DotNetFileSystemSpecs
{
Expand Down Expand Up @@ -135,8 +136,8 @@ public class When_finding_paths_to_executables_with_dotNetFileSystem : DotNetFil
public override void Context()
{
base.Context();
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.PathExtensions)).Returns(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL");
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.Path)).Returns(
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.PathExtensions)).Returns(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL");
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.Path)).Returns(
@"C:\ProgramData\Chocolatey\bin{0}C:\Program Files\Microsoft\Web Platform Installer\{0}C:\Users\yes\AppData\Roaming\Boxstarter{0}C:\tools\ChocolateyPackageUpdater{0}C:\Windows\system32{0}C:\Windows{0}C:\Windows\System32\Wbem{0}C:\Windows\System32\WindowsPowerShell\v1.0\{0}"
.FormatWith(Path.PathSeparator)
);
Expand Down Expand Up @@ -192,8 +193,8 @@ public class When_finding_paths_to_executables_with_dotNetFileSystem_with_empty_
public override void Context()
{
base.Context();
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.PathExtensions)).Returns(string.Empty);
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.Path)).Returns(
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.PathExtensions)).Returns(string.Empty);
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.Path)).Returns(
"/usr/local/bin{0}/usr/bin/{0}/bin{0}/usr/sbin{0}/sbin"
.FormatWith(Path.PathSeparator)
);
Expand Down
9 changes: 5 additions & 4 deletions src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace chocolatey
using IFileSystem = infrastructure.filesystem.IFileSystem;
using ILog = infrastructure.logging.ILog;
using System.Linq;
using static chocolatey.StringResources;

/// <summary>
/// Entry point for API
Expand Down Expand Up @@ -490,19 +491,19 @@ private void EnsureEnvironment()
string chocolateyInstall = string.Empty;

#if !DEBUG
chocolateyInstall = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, EnvironmentVariableTarget.Machine);
chocolateyInstall = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, EnvironmentVariableTarget.Machine);
if (string.IsNullOrWhiteSpace(chocolateyInstall))
{
chocolateyInstall = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, EnvironmentVariableTarget.User);
chocolateyInstall = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, EnvironmentVariableTarget.User);
}
#endif

if (string.IsNullOrWhiteSpace(chocolateyInstall))
{
chocolateyInstall = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName);
chocolateyInstall = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall);
}

Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, chocolateyInstall);
Environment.SetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, chocolateyInstall);
}

private void ExtractResources()
Expand Down
15 changes: 15 additions & 0 deletions src/chocolatey/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("bd59231e-97d1-4fc0-a975-80c3fed498b7")]

[assembly: InternalsVisibleTo("chocolatey.tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059b0331d79eca3ed9087221c6252d3383087f35fa7c54f2ce6223c40905ed2c44e2de8576ae81bcd2f0471961bdbc083a8457d564912d3d3263477756337565ac8ce8dfe912b15cd762ac9e5c4641d82768d9da09265cde5b414ed08e654a3ebf48b9c70b34ebeab0adcb145c439289b5fa8790ef218a6d8123c8d02251c33cf")]

[assembly: InternalsVisibleTo("chocolatey.tests.integration, PublicKey=002400000480000094000000060200000024000052534131000400000100010059b0331d79eca3ed9087221c6252d3383087f35fa7c54f2ce6223c40905ed2c44e2de8576ae81bcd2f0471961bdbc083a8457d564912d3d3263477756337565ac8ce8dfe912b15cd762ac9e5c4641d82768d9da09265cde5b414ed08e654a3ebf48b9c70b34ebeab0adcb145c439289b5fa8790ef218a6d8123c8d02251c33cf")]

// TODO: Make internals visible to Chocolatey Licensed Extension. This section is commented out as it will cause problems upstream when ilmerged.
// We allow the officially built chocolatey.extension to always see the internals.
//[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
//[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]

//#if !FORCE_CHOCOLATEY_OFFICIAL_KEY
//[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
//[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
//#endif
Loading

0 comments on commit bf0d8b9

Please sign in to comment.