Skip to content

Commit 153625b

Browse files
committed
Introduce IntegrationFactAttribute
1 parent c97f914 commit 153625b

24 files changed

+221
-156
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
namespace ElectronNET.IntegrationTests.Common
2+
{
3+
using System.Runtime.InteropServices;
4+
using Xunit.Sdk;
5+
6+
/// <summary>
7+
/// Custom fact attribute with a default timeout of 20 seconds, allowing tests to be skipped on specific environments.
8+
/// </summary>
9+
/// <seealso cref="Xunit.FactAttribute" />
10+
[AttributeUsage(AttributeTargets.Method)]
11+
[XunitTestCaseDiscoverer("Xunit.Sdk.SkippableFactDiscoverer", "Xunit.SkippableFact")]
12+
internal sealed class IntegrationFactAttribute : FactAttribute
13+
{
14+
private static readonly bool IsOnWsl;
15+
16+
private static readonly bool IsOnCI;
17+
18+
static IntegrationFactAttribute()
19+
{
20+
IsOnWsl = DetectWsl();
21+
IsOnCI = DetectCI();
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="IntegrationFactAttribute" /> class.
26+
/// </summary>
27+
public IntegrationFactAttribute()
28+
{
29+
this.Timeout = 20_000;
30+
}
31+
32+
public bool SkipOnWsl { get; set; }
33+
34+
public bool SkipOnCI { get; set; }
35+
36+
/// <summary>
37+
/// Marks the test so that it will not be run, and gets or sets the skip reason
38+
/// </summary>
39+
public override string Skip {
40+
get
41+
{
42+
if (IsOnWsl && this.SkipOnWsl)
43+
{
44+
return "Skipping test on WSL environment.";
45+
}
46+
47+
if (IsOnCI && this.SkipOnCI)
48+
{
49+
return "Skipping test on CI environment.";
50+
}
51+
52+
return base.Skip;
53+
}
54+
set
55+
{
56+
base.Skip = value;
57+
}
58+
}
59+
60+
private static bool DetectWsl()
61+
{
62+
try
63+
{
64+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
65+
{
66+
return false;
67+
}
68+
69+
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WSL_DISTRO_NAME")) ||
70+
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WSL_INTEROP")))
71+
{
72+
return true;
73+
}
74+
75+
return false;
76+
}
77+
catch
78+
{
79+
return false;
80+
}
81+
}
82+
83+
private static bool DetectCI()
84+
{
85+
try
86+
{
87+
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_BUILD")) ||
88+
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS")))
89+
{
90+
return true;
91+
}
92+
93+
return false;
94+
}
95+
catch
96+
{
97+
return false;
98+
}
99+
}
100+
}
101+
}

src/ElectronNET.IntegrationTests/Common/SkipOnWslFactAttribute.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/ElectronNET.IntegrationTests/Tests/AppTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace ElectronNET.IntegrationTests.Tests
66
using System.IO;
77
using System.Runtime.Versioning;
88
using System.Threading.Tasks;
9+
using ElectronNET.IntegrationTests.Common;
910

1011
[Collection("ElectronCollection")]
1112
public class AppTests
@@ -18,15 +19,15 @@ public AppTests(ElectronFixture fx)
1819
this.fx = fx;
1920
}
2021

21-
[Fact(Timeout = 20000)]
22+
[IntegrationFact]
2223
public async Task Can_get_app_path()
2324
{
2425
var path = await Electron.App.GetAppPathAsync();
2526
path.Should().NotBeNullOrWhiteSpace();
2627
Directory.Exists(path).Should().BeTrue();
2728
}
2829

29-
[Fact(Timeout = 20000)]
30+
[IntegrationFact]
3031
public async Task Can_get_version_and_locale()
3132
{
3233
var version = await Electron.App.GetVersionAsync();
@@ -35,7 +36,7 @@ public async Task Can_get_version_and_locale()
3536
locale.Should().NotBeNullOrWhiteSpace();
3637
}
3738

38-
[Fact(Timeout = 20000)]
39+
[IntegrationFact]
3940
public async Task Can_get_special_paths()
4041
{
4142
var userData = await Electron.App.GetPathAsync(PathName.UserData);
@@ -47,22 +48,22 @@ public async Task Can_get_special_paths()
4748
Directory.Exists(temp).Should().BeTrue();
4849
}
4950

50-
[Fact(Timeout = 20000)]
51+
[IntegrationFact]
5152
public async Task Can_get_app_metrics()
5253
{
5354
var metrics = await Electron.App.GetAppMetricsAsync();
5455
metrics.Should().NotBeNull();
5556
metrics.Length.Should().BeGreaterThan(0);
5657
}
5758

58-
[Fact(Timeout = 20000)]
59+
[IntegrationFact]
5960
public async Task Can_get_gpu_feature_status()
6061
{
6162
var status = await Electron.App.GetGpuFeatureStatusAsync();
6263
status.Should().NotBeNull();
6364
}
6465

65-
[SkippableFact(Timeout = 20000)]
66+
[IntegrationFact]
6667
[SupportedOSPlatform("macOS")]
6768
[SupportedOSPlatform("Windows")]
6869
public async Task Can_get_login_item_settings()
@@ -71,7 +72,7 @@ public async Task Can_get_login_item_settings()
7172
settings.Should().NotBeNull();
7273
}
7374

74-
[Fact(Timeout = 20000)]
75+
[IntegrationFact]
7576
public async Task CommandLine_append_and_query_switch()
7677
{
7778
var switchName = "integration-switch";
@@ -80,7 +81,7 @@ public async Task CommandLine_append_and_query_switch()
8081
(await Electron.App.CommandLine.GetSwitchValueAsync(switchName)).Should().Be("value123");
8182
}
8283

83-
[SkippableFact(Timeout = 20000)]
84+
[IntegrationFact]
8485
[SupportedOSPlatform("macOS")]
8586
[SupportedOSPlatform("Windows")]
8687
public async Task Accessibility_support_toggle()
@@ -91,7 +92,7 @@ public async Task Accessibility_support_toggle()
9192
Electron.App.SetAccessibilitySupportEnabled(false);
9293
}
9394

94-
[Fact(Timeout = 20000)]
95+
[IntegrationFact]
9596
public async Task UserAgentFallback_roundtrip()
9697
{
9798
var original = await Electron.App.UserAgentFallbackAsync;
@@ -101,7 +102,7 @@ public async Task UserAgentFallback_roundtrip()
101102
Electron.App.UserAgentFallback = original; // restore
102103
}
103104

104-
[SkippableFact(Timeout = 20000)]
105+
[IntegrationFact]
105106
[SupportedOSPlatform("Linux")]
106107
[SupportedOSPlatform("macOS")]
107108
public async Task BadgeCount_set_and_reset_where_supported()
@@ -113,14 +114,14 @@ public async Task BadgeCount_set_and_reset_where_supported()
113114
await Electron.App.SetBadgeCountAsync(0);
114115
}
115116

116-
[Fact(Timeout = 20000)]
117+
[IntegrationFact]
117118
public async Task App_metrics_have_cpu_info()
118119
{
119120
var metrics = await Electron.App.GetAppMetricsAsync();
120121
metrics[0].Cpu.Should().NotBeNull();
121122
}
122123

123-
[Fact(Timeout = 20000)]
124+
[IntegrationFact]
124125
public async Task App_gpu_feature_status_has_some_fields()
125126
{
126127
var status = await Electron.App.GetGpuFeatureStatusAsync();

src/ElectronNET.IntegrationTests/Tests/AutoUpdaterTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using API;
44
using System.Threading.Tasks;
55
using ElectronNET.Common;
6+
using ElectronNET.IntegrationTests.Common;
67

78
[Collection("ElectronCollection")]
89
public class AutoUpdaterTests
@@ -14,7 +15,7 @@ public AutoUpdaterTests(ElectronFixture fx)
1415
this.fx = fx;
1516
}
1617

17-
[Fact(Timeout = 20000)]
18+
[IntegrationFact]
1819
public async Task AutoDownload_check()
1920
{
2021
Electron.AutoUpdater.AutoDownload = false;
@@ -25,7 +26,7 @@ public async Task AutoDownload_check()
2526
test2.Should().BeTrue();
2627
}
2728

28-
[Fact(Timeout = 20000)]
29+
[IntegrationFact]
2930
public async Task AutoInstallOnAppQuit_check()
3031
{
3132
Electron.AutoUpdater.AutoInstallOnAppQuit = false;
@@ -36,7 +37,7 @@ public async Task AutoInstallOnAppQuit_check()
3637
test2.Should().BeTrue();
3738
}
3839

39-
[Fact(Timeout = 20000)]
40+
[IntegrationFact]
4041
public async Task AllowPrerelease_check()
4142
{
4243
Electron.AutoUpdater.AllowPrerelease = false;
@@ -47,7 +48,7 @@ public async Task AllowPrerelease_check()
4748
test2.Should().BeTrue();
4849
}
4950

50-
[Fact(Timeout = 20000)]
51+
[IntegrationFact]
5152
public async Task FullChangelog_check()
5253
{
5354
Electron.AutoUpdater.FullChangelog = false;
@@ -58,7 +59,7 @@ public async Task FullChangelog_check()
5859
test2.Should().BeTrue();
5960
}
6061

61-
[Fact(Timeout = 20000)]
62+
[IntegrationFact]
6263
public async Task AllowDowngrade_check()
6364
{
6465
Electron.AutoUpdater.AllowDowngrade = false;
@@ -69,22 +70,22 @@ public async Task AllowDowngrade_check()
6970
test2.Should().BeTrue();
7071
}
7172

72-
[Fact(Timeout = 20000)]
73+
[IntegrationFact]
7374
public async Task UpdateConfigPath_check()
7475
{
7576
var test1 = Electron.AutoUpdater.UpdateConfigPath;
7677
test1.Should().Be(string.Empty);
7778
}
7879

79-
[Fact(Timeout = 20000)]
80+
[IntegrationFact]
8081
public async Task CurrentVersionAsync_check()
8182
{
8283
var semver = await Electron.AutoUpdater.CurrentVersionAsync;
8384
semver.Should().NotBeNull();
8485
semver.Major.Should().BeGreaterThan(0);
8586
}
8687

87-
[Fact(Timeout = 20000)]
88+
[IntegrationFact]
8889
public async Task ChannelAsync_check()
8990
{
9091
var test = await Electron.AutoUpdater.ChannelAsync;
@@ -95,7 +96,7 @@ public async Task ChannelAsync_check()
9596
test.Should().Be("beta");
9697
}
9798

98-
[Fact(Timeout = 20000)]
99+
[IntegrationFact]
99100
public async Task RequestHeadersAsync_check()
100101
{
101102
var headers = new Dictionary<string, string>
@@ -112,21 +113,21 @@ public async Task RequestHeadersAsync_check()
112113
test["key1"].Should().Be("value1");
113114
}
114115

115-
[Fact(Timeout = 20000)]
116+
[IntegrationFact]
116117
public async Task CheckForUpdatesAsync_check()
117118
{
118119
var test = await Electron.AutoUpdater.CheckForUpdatesAsync();
119120
test.Should().BeNull();
120121
}
121122

122-
[Fact(Timeout = 20000)]
123+
[IntegrationFact]
123124
public async Task CheckForUpdatesAndNotifyAsync_check()
124125
{
125126
var test = await Electron.AutoUpdater.CheckForUpdatesAsync();
126127
test.Should().BeNull();
127128
}
128129

129-
[Fact(Timeout = 20000)]
130+
[IntegrationFact]
130131
public async Task GetFeedURLAsync_check()
131132
{
132133
var test = await Electron.AutoUpdater.GetFeedURLAsync();

src/ElectronNET.IntegrationTests/Tests/BrowserViewTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace ElectronNET.IntegrationTests.Tests
22
{
33
using ElectronNET.API;
44
using ElectronNET.API.Entities;
5+
using ElectronNET.IntegrationTests.Common;
56

67
[Collection("ElectronCollection")]
78
public class BrowserViewTests
@@ -13,7 +14,7 @@ public BrowserViewTests(ElectronFixture fx)
1314
this.fx = fx;
1415
}
1516

16-
[Fact(Timeout = 20000)]
17+
[IntegrationFact]
1718
public async Task Create_browser_view_and_adjust_bounds()
1819
{
1920
var view = await Electron.WindowManager.CreateBrowserViewAsync(new BrowserViewConstructorOptions());

0 commit comments

Comments
 (0)