Skip to content

Commit

Permalink
[URP][2023.2] Adding URPHelpURLAttribute tests
Browse files Browse the repository at this point in the history
Adds editor tests for the `URPHelpURL `Attribute that is used to give documentation links in various places inside the editor.

These attributes were recently broken and undetected until package validation so this test was made to ensure that doesn't happen again.
  • Loading branch information
ellioman authored and Evergreen committed May 20, 2023
1 parent 6462cd7 commit 8401879
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.Universal;

namespace Documentation
{
class DocumentationTests
{
private bool TestURL(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

// Timeout set to 10 seconds
request.Timeout = 10000;

// Only get header information
request.Method = "HEAD";

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;

// Successful requests
if (statusCode >= 100 && statusCode < 400)
return true;

// Server Errors
if (statusCode >= 500 && statusCode <= 510)
return false;
}
}
catch
{
// ignored
}

return false;
}

[Test]
public void TestURPHelpURLAttributes()
{
// Start with checking a bad URL to make sure TestURL() works.
string badURL = "https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@16.0/manual/incorrect-url";
bool badURLResults = TestURL(badURL);
Assert.IsFalse(badURLResults, "TestURL() failed. A broken URL should have been detected.");

// Find everything that uses URPHelpURLAttribute
TypeCache.TypeCollection typesWithHelpUrls = TypeCache.GetTypesWithAttribute<URPHelpURLAttribute>();

// Check if all the URLs are valid.
bool allURLsAreValid = true;
StringBuilder failures = new ();
foreach (Type type in typesWithHelpUrls)
{
Debug.Log("Checking \"" + type.FullName + "\"");
object[] attributes = type.GetCustomAttributes(typeof(URPHelpURLAttribute), false);
for (int i = 0; i < attributes.Length; i++)
{
URPHelpURLAttribute attribute = attributes[i] as URPHelpURLAttribute;
Assert.IsNotNull(attribute);

string url = attribute.URL;
bool urlIsValid = TestURL(url);

Debug.Log("\t" + url);
Debug.Log(urlIsValid ? "\tSuccess!\n" : "\tFailure!\n");

if (urlIsValid)
continue;

allURLsAreValid = false;
failures.AppendLine("\t" + type.FullName);
failures.AppendLine("\t" + url);
failures.AppendLine();
}
}

if (!allURLsAreValid)
Assert.Fail("The following URLs failed:\n" + failures);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8401879

Please sign in to comment.