From ef3c6d7035265b89a0ad80ac5715bf449d3b1a23 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 21 Nov 2019 10:12:00 -0600 Subject: [PATCH] (GH-1949) Release mode - use entry assembly to check When in release mode, a chocolatey.dll could be next to something being tested. However, most of the time, for testing, it will be located at the machine level install. When that is the case, the chocolatey assembly is loaded into memory to keep from locking the files and the executing assembly path is null, so it uses the current directory. Instead prefer to look near the entry assembly, then defer to the environment variable for the machine installation. --- .../infrastructure.app/ApplicationParameters.cs | 16 +++++++++++++++- .../infrastructure/adapters/Assembly.cs | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 82c6c82ac0..072d4c6ec1 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -35,9 +35,23 @@ public static class ApplicationParameters // always look at the official location of the machine installation public static readonly string InstallLocation = System.Environment.GetEnvironmentVariable(ChocolateyInstallEnvironmentVariableName) ?? _fileSystem.get_directory_name(_fileSystem.get_current_assembly_path()); public static readonly string LicensedAssemblyLocation = _fileSystem.combine_paths(InstallLocation, "extensions", "chocolatey", "chocolatey.licensed.dll"); -#else +#elif DEBUG // Install location is choco.exe or chocolatey.dll public static readonly string InstallLocation = _fileSystem.get_directory_name(_fileSystem.get_current_assembly_path()); + // when being used as a reference, start by looking next to Chocolatey, then in a subfolder. + public static readonly string LicensedAssemblyLocation = _fileSystem.file_exists(_fileSystem.combine_paths(InstallLocation, "chocolatey.licensed.dll")) ? _fileSystem.combine_paths(InstallLocation, "chocolatey.licensed.dll") : _fileSystem.combine_paths(InstallLocation, "extensions", "chocolatey", "chocolatey.licensed.dll"); +#else + // Install locations is chocolatey.dll or choco.exe - In Release mode + // we might be testing on a server or in the local debugger. Either way, + // start from the assembly location and if unfound, head to the machine + // locations instead. This is a merge of official and Debug modes. + public static readonly string InstallLocation = _fileSystem.file_exists(_fileSystem.combine_paths(_fileSystem.get_directory_name(Assembly.GetEntryAssembly().CodeBase.Replace("file:///", string.Empty)), "chocolatey.dll")) ? + _fileSystem.get_directory_name(Assembly.GetEntryAssembly().CodeBase.Replace("file:///", string.Empty)) : + !string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable(ChocolateyInstallEnvironmentVariableName)) ? + System.Environment.GetEnvironmentVariable(ChocolateyInstallEnvironmentVariableName) : + @"C:\ProgramData\Chocolatey" + ; + // when being used as a reference, start by looking next to Chocolatey, then in a subfolder. public static readonly string LicensedAssemblyLocation = _fileSystem.file_exists(_fileSystem.combine_paths(InstallLocation, "chocolatey.licensed.dll")) ? _fileSystem.combine_paths(InstallLocation, "chocolatey.licensed.dll") : _fileSystem.combine_paths(InstallLocation, "extensions", "chocolatey", "chocolatey.licensed.dll"); #endif diff --git a/src/chocolatey/infrastructure/adapters/Assembly.cs b/src/chocolatey/infrastructure/adapters/Assembly.cs index d8bc94eb63..2759d22692 100644 --- a/src/chocolatey/infrastructure/adapters/Assembly.cs +++ b/src/chocolatey/infrastructure/adapters/Assembly.cs @@ -113,6 +113,11 @@ public static IAssembly GetExecutingAssembly() public static IAssembly GetCallingAssembly() { return new Assembly(System.Reflection.Assembly.GetCallingAssembly()); + } + + public static IAssembly GetEntryAssembly() + { + return new Assembly(System.Reflection.Assembly.GetEntryAssembly()); } public static IAssembly set_assembly(System.Reflection.Assembly value)