Skip to content

Commit 7cfa192

Browse files
committed
(#3675) Extract all used environment variables
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.
1 parent 659f5d3 commit 7cfa192

File tree

24 files changed

+891
-253
lines changed

24 files changed

+891
-253
lines changed

src/chocolatey.console/Properties/AssemblyInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
using System.Runtime.CompilerServices;
1718
using System.Runtime.InteropServices;
1819

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

2122
[assembly: Guid("2f406051-3fe2-45f4-97bf-40968b86c5a9")]
23+
24+
// We allow the officially built chocolatey.extension to always see the internals.
25+
[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
26+
[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
27+
28+
#if !FORCE_CHOCOLATEY_OFFICIAL_KEY
29+
[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
30+
[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
31+
#endif
596 Bytes
Binary file not shown.

src/chocolatey.tests.integration/chocolatey.tests.integration.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
6262
<Prefer32Bit>false</Prefer32Bit>
6363
</PropertyGroup>
64+
<PropertyGroup>
65+
<SignAssembly>true</SignAssembly>
66+
</PropertyGroup>
67+
<PropertyGroup>
68+
<AssemblyOriginatorKeyFile>TestKey.snk</AssemblyOriginatorKeyFile>
69+
</PropertyGroup>
6470
<ItemGroup>
6571
<Reference Include="AnyOf, Version=0.3.0.0, Culture=neutral, PublicKeyToken=b35e6abbb527c6b1, processorArchitecture=MSIL">
6672
<HintPath>..\packages\AnyOf.0.3.0\lib\net45\AnyOf.dll</HintPath>
@@ -493,6 +499,7 @@
493499
<None Include="packages.config">
494500
<SubType>Designer</SubType>
495501
</None>
502+
<None Include="TestKey.snk" />
496503
</ItemGroup>
497504
<ItemGroup>
498505
<ProjectReference Include="..\chocolatey.tests\chocolatey.tests.csproj">

src/chocolatey.tests.integration/infrastructure.app/builders/ConfigurationBuilderSpecs.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
using Microsoft.Win32;
3333
using chocolatey.tests.integration.scenarios;
3434
using FluentAssertions;
35+
using static chocolatey.StringResources;
3536

3637
namespace chocolatey.tests.integration.infrastructure.app.builders
3738
{
@@ -116,11 +117,11 @@ public override void Context()
116117

117118
if (EnvironmentVariableSet)
118119
{
119-
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn("http_proxy", "https_proxy"))).Returns(EnvironmentVariableProxyValue);
120+
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn(EnvironmentVariables.System.HttpProxy, EnvironmentVariables.System.HttpsProxy))).Returns(EnvironmentVariableProxyValue);
120121
}
121122
else
122123
{
123-
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn("http_proxy", "https_proxy"))).Returns(string.Empty);
124+
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn(EnvironmentVariables.System.HttpProxy, EnvironmentVariables.System.HttpsProxy))).Returns(string.Empty);
124125
}
125126

126127
if (ConfigSet)
@@ -192,11 +193,11 @@ public override void Context()
192193

193194
if (EnvironmentVariableSet)
194195
{
195-
Environment.Setup(e => e.GetEnvironmentVariable("no_proxy")).Returns(EnvironmentVariableProxyValue);
196+
Environment.Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.System.NoProxy)).Returns(EnvironmentVariableProxyValue);
196197
}
197198
else
198199
{
199-
Environment.Setup(e => e.GetEnvironmentVariable("no_proxy")).Returns(string.Empty);
200+
Environment.Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.System.NoProxy)).Returns(string.Empty);
200201
}
201202

202203
if (ArgumentSet)

src/chocolatey.tests/TestKey.snk

596 Bytes
Binary file not shown.

src/chocolatey.tests/TinySpec.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using chocolatey.infrastructure.logging;
2121
using System.IO;
2222
using chocolatey.infrastructure.app.nuget;
23+
using static chocolatey.StringResources;
2324

2425
namespace chocolatey.tests
2526
{
@@ -30,12 +31,12 @@ public class NUnitSetup
3031
{
3132
public static MockLogger MockLogger { get; set; }
3233

33-
private static readonly string _installLocationVariable = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName);
34+
private static readonly string _installLocationVariable = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall);
3435

3536
[OneTimeSetUp]
3637
public virtual void BeforeEverything()
3738
{
38-
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, string.Empty);
39+
Environment.SetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, string.Empty);
3940
MockLogger = new MockLogger();
4041
Log.InitializeWith(MockLogger);
4142
// do not log trace messages
@@ -46,7 +47,7 @@ public virtual void BeforeEverything()
4647
[OneTimeTearDown]
4748
public void AfterEverything()
4849
{
49-
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, _installLocationVariable);
50+
Environment.SetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, _installLocationVariable);
5051
}
5152
}
5253

src/chocolatey.tests/chocolatey.tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
6060
<Prefer32Bit>false</Prefer32Bit>
6161
</PropertyGroup>
62+
<PropertyGroup>
63+
<SignAssembly>true</SignAssembly>
64+
</PropertyGroup>
65+
<PropertyGroup>
66+
<AssemblyOriginatorKeyFile>TestKey.snk</AssemblyOriginatorKeyFile>
67+
</PropertyGroup>
6268
<ItemGroup>
6369
<Reference Include="Chocolatey.NuGet.Commands">
6470
<HintPath>..\packages\Chocolatey.NuGet.Commands.3.4.3\lib\net472\Chocolatey.NuGet.Commands.dll</HintPath>
@@ -207,6 +213,7 @@
207213
<None Include="packages.config">
208214
<SubType>Designer</SubType>
209215
</None>
216+
<None Include="TestKey.snk" />
210217
</ItemGroup>
211218
<ItemGroup>
212219
<ProjectReference Include="..\chocolatey\chocolatey.csproj">

src/chocolatey.tests/infrastructure/filesystem/DotNetFileSystemSpecs.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Moq;
2424
using NUnit.Framework;
2525
using FluentAssertions;
26+
using static chocolatey.StringResources;
2627

2728
namespace chocolatey.tests.infrastructure.filesystem
2829
{
@@ -135,8 +136,8 @@ public class When_finding_paths_to_executables_with_dotNetFileSystem : DotNetFil
135136
public override void Context()
136137
{
137138
base.Context();
138-
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.PathExtensions)).Returns(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL");
139-
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.Path)).Returns(
139+
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.PathExtensions)).Returns(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL");
140+
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.Path)).Returns(
140141
@"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}"
141142
.FormatWith(Path.PathSeparator)
142143
);
@@ -192,8 +193,8 @@ public class When_finding_paths_to_executables_with_dotNetFileSystem_with_empty_
192193
public override void Context()
193194
{
194195
base.Context();
195-
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.PathExtensions)).Returns(string.Empty);
196-
Environment.Setup(x => x.GetEnvironmentVariable(ApplicationParameters.Environment.Path)).Returns(
196+
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.PathExtensions)).Returns(string.Empty);
197+
Environment.Setup(x => x.GetEnvironmentVariable(EnvironmentVariables.System.Path)).Returns(
197198
"/usr/local/bin{0}/usr/bin/{0}/bin{0}/usr/sbin{0}/sbin"
198199
.FormatWith(Path.PathSeparator)
199200
);

src/chocolatey/GetChocolatey.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
using IFileSystem = chocolatey.infrastructure.filesystem.IFileSystem;
4242
using ILog = chocolatey.infrastructure.logging.ILog;
4343
using System.Linq;
44+
using static chocolatey.StringResources;
4445

4546
namespace chocolatey
4647
{
@@ -505,19 +506,19 @@ private void EnsureEnvironment()
505506
var chocolateyInstall = string.Empty;
506507

507508
#if !DEBUG
508-
chocolateyInstall = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, EnvironmentVariableTarget.Machine);
509+
chocolateyInstall = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, EnvironmentVariableTarget.Machine);
509510
if (string.IsNullOrWhiteSpace(chocolateyInstall))
510511
{
511-
chocolateyInstall = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, EnvironmentVariableTarget.User);
512+
chocolateyInstall = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, EnvironmentVariableTarget.User);
512513
}
513514
#endif
514515

515516
if (string.IsNullOrWhiteSpace(chocolateyInstall))
516517
{
517-
chocolateyInstall = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName);
518+
chocolateyInstall = Environment.GetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall);
518519
}
519520

520-
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, chocolateyInstall);
521+
Environment.SetEnvironmentVariable(EnvironmentVariables.System.ChocolateyInstall, chocolateyInstall);
521522
}
522523

523524
private void ExtractResources()
@@ -538,8 +539,8 @@ private void ExtractResources()
538539
}
539540
catch (Exception ex)
540541
{
541-
this.Log().Warn(ChocolateyLoggers.Important, "Please ensure that ChocolateyInstall environment variable is set properly and you've run once as an administrator to ensure all resources are extracted.");
542-
this.Log().Error("Unable to extract resources. Please ensure the ChocolateyInstall environment variable is set properly. You may need to run once as an admin to ensure all resources are extracted. Details:{0} {1}".FormatWith(Environment.NewLine, ex.ToString()));
542+
this.Log().Warn(ChocolateyLoggers.Important, "Please ensure that {0} environment variable is set properly and you've run once as an administrator to ensure all resources are extracted.", EnvironmentVariables.System.ChocolateyInstall);
543+
this.Log().Error("Unable to extract resources. Please ensure the {2} environment variable is set properly. You may need to run once as an admin to ensure all resources are extracted. Details:{0} {1}".FormatWith(Environment.NewLine, ex.ToString(), EnvironmentVariables.System.ChocolateyInstall));
543544
}
544545
#endif
545546
}

src/chocolatey/Properties/AssemblyInfo.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
using System.Runtime.CompilerServices;
1718
using System.Runtime.InteropServices;
1819

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

2122
[assembly: Guid("bd59231e-97d1-4fc0-a975-80c3fed498b7")]
23+
24+
[assembly: InternalsVisibleTo("chocolatey.tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059b0331d79eca3ed9087221c6252d3383087f35fa7c54f2ce6223c40905ed2c44e2de8576ae81bcd2f0471961bdbc083a8457d564912d3d3263477756337565ac8ce8dfe912b15cd762ac9e5c4641d82768d9da09265cde5b414ed08e654a3ebf48b9c70b34ebeab0adcb145c439289b5fa8790ef218a6d8123c8d02251c33cf")]
25+
26+
[assembly: InternalsVisibleTo("chocolatey.tests.integration, PublicKey=002400000480000094000000060200000024000052534131000400000100010059b0331d79eca3ed9087221c6252d3383087f35fa7c54f2ce6223c40905ed2c44e2de8576ae81bcd2f0471961bdbc083a8457d564912d3d3263477756337565ac8ce8dfe912b15cd762ac9e5c4641d82768d9da09265cde5b414ed08e654a3ebf48b9c70b34ebeab0adcb145c439289b5fa8790ef218a6d8123c8d02251c33cf")]
27+
28+
// We allow the officially built chocolatey.extension to always see the internals.
29+
[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
30+
[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
31+
32+
#if !FORCE_CHOCOLATEY_OFFICIAL_KEY
33+
[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
34+
[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
35+
#endif

0 commit comments

Comments
 (0)