Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/winapp-CLI/WinApp.Cli.Tests/UpdateNotificationGatingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Globalization;
using WinApp.Cli.Services;

namespace WinApp.Cli.Tests;

Expand All @@ -22,13 +23,22 @@ public class UpdateNotificationGatingTests
private string? _savedCaller;
private string? _savedUpdateCheck;

// All environment variable names checked by CIEnvironmentDetectorForTelemetry
private static readonly string[] CiVarNames =
[
"CI", "GITHUB_ACTIONS", "TF_BUILD", "APPVEYOR", "TRAVIS", "CIRCLECI",
"TEAMCITY_VERSION", "JB_SPACE_API_URL",
"CODEBUILD_BUILD_ID", "AWS_REGION", "BUILD_ID", "BUILD_URL", "PROJECT_ID"
];
private Dictionary<string, string?> _savedCiVars = [];

[TestInitialize]
public void Setup()
{
// Create temp cache directory and seed with a "newer" version
_tempCacheDir = Path.Combine(Path.GetTempPath(), $"winapp_gating_test_{Guid.NewGuid():N}");
Directory.CreateDirectory(_tempCacheDir);
SeedUpdateCheckCache("99.0.0");
SeedUpdateCheckCache(GetGuaranteedNewerVersion());

// Create first-run marker so FirstRunService doesn't trigger logging
File.Create(Path.Combine(_tempCacheDir, ".first-run-complete")).Dispose();
Expand All @@ -37,15 +47,17 @@ public void Setup()
_savedCacheDir = Environment.GetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY");
_savedCaller = Environment.GetEnvironmentVariable("WINAPP_CLI_CALLER");
_savedUpdateCheck = Environment.GetEnvironmentVariable("WINAPP_CLI_UPDATE_CHECK");
_savedCiVars = CiVarNames.ToDictionary(name => name, name => Environment.GetEnvironmentVariable(name));

Environment.SetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY", _tempCacheDir);
Environment.SetEnvironmentVariable("WINAPP_CLI_CALLER", null);
Environment.SetEnvironmentVariable("WINAPP_CLI_UPDATE_CHECK", null);

// Clear CI vars to avoid suppression
Environment.SetEnvironmentVariable("CI", null);
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", null);
Environment.SetEnvironmentVariable("TF_BUILD", null);
foreach (var name in CiVarNames)
{
Environment.SetEnvironmentVariable(name, null);
}
}

[TestCleanup]
Expand All @@ -54,6 +66,10 @@ public void Cleanup()
Environment.SetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY", _savedCacheDir);
Environment.SetEnvironmentVariable("WINAPP_CLI_CALLER", _savedCaller);
Environment.SetEnvironmentVariable("WINAPP_CLI_UPDATE_CHECK", _savedUpdateCheck);
foreach (var (name, value) in _savedCiVars)
{
Environment.SetEnvironmentVariable(name, value);
}

try { Directory.Delete(_tempCacheDir, recursive: true); } catch { /* best effort */ }
}
Expand Down Expand Up @@ -125,6 +141,14 @@ private void SeedUpdateCheckCache(string version)
File.WriteAllText(Path.Combine(_tempCacheDir, ".update-check"), content);
}

private static string GetGuaranteedNewerVersion()
{
var currentCore = UpdateNotificationService.GetCoreVersion(WinApp.Cli.Helpers.VersionHelper.GetVersionString());
return Version.TryParse(currentCore, out var parsed)
? $"{parsed.Major + 1}.0.0"
: "5.0.0";
}

/// <summary>
/// Invokes Program.Main with captured stdout/stderr.
/// Writers are intentionally not disposed to avoid ObjectDisposedException from
Expand Down
Loading
Loading