Skip to content

Commit 11b01cd

Browse files
test - fix
HelpURL tests now check to see if the document exists before failing if it can't be loaded from the latest documentation update. Adjusted the HelpUrls base portions of the URL so they are internal.
1 parent b4f85f2 commit 11b01cd

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

com.unity.netcode.gameobjects/Runtime/HelpUrls.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ namespace Unity.Netcode.Runtime
33
internal static class HelpUrls
44
{
55
private const string k_BaseUrl = "https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest/?subfolder=/";
6-
private const string k_BaseManualUrl = k_BaseUrl + "manual/";
7-
private const string k_BaseApiUrl = k_BaseUrl + "api/Unity.Netcode";
6+
internal const string BaseManualUrl = k_BaseUrl + "manual/";
7+
internal const string BaseApiUrl = k_BaseUrl + "api/Unity.Netcode";
88

99
// The HelpUrls have to be defined as public for the test to work
10-
public const string NetworkManager = k_BaseManualUrl + "components/core/networkmanager.html";
11-
public const string NetworkObject = k_BaseManualUrl + "components/core/networkobject.html";
12-
public const string NetworkAnimator = k_BaseManualUrl + "components/helper/networkanimator.html";
13-
public const string NetworkRigidbody = k_BaseManualUrl + "components/helper/networkrigidbody.html";
14-
public const string NetworkRigidbody2D = k_BaseManualUrl + "components/helper/networkrigidbody.html";
15-
public const string RigidbodyContactEventManager = k_BaseApiUrl + ".Components.RigidbodyContactEventManager.html";
16-
public const string NetworkTransform = k_BaseManualUrl + "components/helper/networktransform.html";
17-
public const string AnticipatedNetworkTransform = k_BaseManualUrl + "advanced-topics/client-anticipation.html";
18-
public const string UnityTransport = k_BaseApiUrl + ".Transports.UTP.UnityTransport.html";
19-
public const string SecretsLoaderHelper = k_BaseApiUrl + ".Transports.UTP.SecretsLoaderHelper.html";
20-
public const string SinglePlayerTransport = k_BaseApiUrl + ".Transports.SinglePlayer.SinglePlayerTransport.html";
10+
public const string NetworkManager = BaseManualUrl + "components/core/networkmanager.html";
11+
public const string NetworkObject = BaseManualUrl + "components/core/networkobject.html";
12+
public const string NetworkAnimator = BaseManualUrl + "components/helper/networkanimator.html";
13+
public const string NetworkRigidbody = BaseManualUrl + "components/helper/networkrigidbody.html";
14+
public const string NetworkRigidbody2D = BaseManualUrl + "components/helper/networkrigidbody.html";
15+
public const string RigidbodyContactEventManager = BaseApiUrl + ".Components.RigidbodyContactEventManager.html";
16+
public const string NetworkTransform = BaseManualUrl + "components/helper/networktransform.html";
17+
public const string AnticipatedNetworkTransform = BaseManualUrl + "advanced-topics/client-anticipation.html";
18+
public const string UnityTransport = BaseApiUrl + ".Transports.UTP.UnityTransport.html";
19+
public const string SecretsLoaderHelper = BaseApiUrl + ".Transports.UTP.SecretsLoaderHelper.html";
20+
public const string SinglePlayerTransport = BaseApiUrl + ".Transports.SinglePlayer.SinglePlayerTransport.html";
2121
}
2222
}

com.unity.netcode.gameobjects/Tests/Runtime/HelpUrlTests.cs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Net.Http;
8+
using System.Text;
89
using System.Text.RegularExpressions;
910
using System.Threading.Tasks;
1011
using NUnit.Framework;
1112
using Unity.Netcode.Runtime;
1213
using UnityEngine;
1314
using UnityEngine.TestTools;
15+
using UnityEngine.Windows;
1416

1517
namespace Unity.Netcode.RuntimeTests
1618
{
@@ -21,6 +23,7 @@ internal class HelpUrlTests
2123

2224
private bool m_VerboseLogging = false;
2325

26+
// TODO: Since help URIs are only used in the editor, we should migrate this into the editor tests.
2427
// IOS platform can't run this test for some reason.
2528
[UnityTest]
2629
[UnityPlatform(exclude = new[] { RuntimePlatform.IPhonePlayer })]
@@ -62,18 +65,30 @@ private async Task<bool> AreUnityDocsAvailableAt(string url)
6265
{
6366
try
6467
{
65-
var split = url.Split('#');
66-
url = split[0];
68+
var split = (string[])null;
69+
var hasAnchor = url.Contains("#");
70+
if (hasAnchor)
71+
{
72+
split = url.Split('#');
73+
url = split[0];
74+
}
75+
76+
var documentText = ContentExistsInDocumentation(url);
77+
var docExists = !string.IsNullOrEmpty(documentText);
6778

68-
var stream = await GetContentFromRemoteFile(url);
79+
var stream = await GetContentFromRemoteFile(url, docExists);
6980

7081
var redirectUrl = CalculateRedirectURl(url, stream);
7182
VerboseLog($"Calculated Redirect URL: {redirectUrl}");
7283

73-
var content = await GetContentFromRemoteFile(redirectUrl);
84+
var content = await GetContentFromRemoteFile(redirectUrl, docExists);
7485

86+
if (string.IsNullOrEmpty(content))
87+
{
88+
content = documentText;
89+
}
7590
// If original url had an anchor part (e.g. some/url.html#anchor)
76-
if (split.Length > 1)
91+
if (hasAnchor)
7792
{
7893
var anchorString = split[1];
7994

@@ -83,7 +98,6 @@ private async Task<bool> AreUnityDocsAvailableAt(string url)
8398
return false;
8499
}
85100
}
86-
87101
return true;
88102
}
89103
catch (Exception e)
@@ -93,12 +107,36 @@ private async Task<bool> AreUnityDocsAvailableAt(string url)
93107
}
94108
}
95109

110+
/// <summary>
111+
/// Checks if the help URI is yet to be published but exists as a document.
112+
/// </summary>
113+
/// <param name="url">the help uri</param>
114+
/// <returns>the contents of the file if it exist otherwise it returns an emtpy string</returns>
115+
private string ContentExistsInDocumentation(string url)
116+
{
117+
var splitFilter = url.Contains(HelpUrls.BaseManualUrl) ? HelpUrls.BaseManualUrl : HelpUrls.BaseApiUrl;
118+
var split = url.Split(splitFilter);
119+
var current = System.IO.Directory.GetCurrentDirectory().Replace("testproject", string.Empty);
120+
var filePath = $"{current}com.unity.netcode.gameobjects/Documentation~/{split[1].Replace(".html", ".md")}";
121+
try
122+
{
123+
if (File.Exists(filePath))
124+
{
125+
var bytes = File.ReadAllBytes(filePath);
126+
return Encoding.UTF8.GetString(bytes);
127+
}
128+
}
129+
catch
130+
{ }
131+
return string.Empty;
132+
}
133+
96134
/// <summary>
97135
/// Checks if a remote file at the <paramref name="url"/> exists, and if access is not restricted.
98136
/// </summary>
99137
/// <param name="url">URL to a remote file.</param>
100138
/// <returns>True if the file at the <paramref name="url"/> is able to be downloaded, false if the file does not exist, or if the file is restricted.</returns>
101-
private async Task<string> GetContentFromRemoteFile(string url)
139+
private async Task<string> GetContentFromRemoteFile(string url, bool documentExists)
102140
{
103141
//Checking if URI is well formed is optional
104142
var uri = new Uri(url);
@@ -113,7 +151,11 @@ private async Task<string> GetContentFromRemoteFile(string url)
113151
using var response = await k_HttpClient.SendAsync(request);
114152
if (!response.IsSuccessStatusCode || response.Content.Headers.ContentLength <= 0)
115153
{
116-
throw new Exception($"Failed to get remote file from URL {url}");
154+
if (!documentExists)
155+
{
156+
throw new Exception($"Failed to get remote file from URL {url}");
157+
}
158+
return string.Empty;
117159
}
118160

119161
return await response.Content.ReadAsStringAsync();

0 commit comments

Comments
 (0)