Skip to content

Commit

Permalink
fix after rebase + use .json for settings + more controll in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
JoC0de committed Mar 11, 2024
1 parent e3d3976 commit 3e4442c
Show file tree
Hide file tree
Showing 23 changed files with 574 additions and 417 deletions.
13 changes: 13 additions & 0 deletions src/NuGetForUnity.Cli/Fakes/BuildTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace UnityEditor
{
internal enum BuildTarget
{
StandaloneWindows64,

StandaloneWindows,

StandaloneLinux64,

StandaloneOSX,
}
}
4 changes: 2 additions & 2 deletions src/NuGetForUnity.Cli/Fakes/JsonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static void FromJsonOverwrite<T>(string jsonString, T target)
// not needed for the CLI
}

internal static string ToJson<T>(T value)
internal static string ToJson<T>(T value, bool pretty = false)
{
return JsonSerializer.Serialize(value, new JsonSerializerOptions { IncludeFields = true });
return JsonSerializer.Serialize(value, new JsonSerializerOptions { IncludeFields = true, WriteIndented = pretty });
}
}
}
7 changes: 7 additions & 0 deletions src/NuGetForUnity.Cli/Fakes/NuGetForUnityUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NugetForUnity.Ui
{
internal static class NuGetForUnityUpdater
{
public const string UpmPackageName = "com.github-glitchenzo.nugetforunity";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "PlayTest",
"name": "NuGetForUnity.PlayTests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
Expand All @@ -19,4 +19,4 @@
],
"versionDefines": [],
"noEngineReferences": false
}
}
81 changes: 49 additions & 32 deletions src/NuGetForUnity.Tests/Assets/PlayTests/NuGetPlayTests.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
using System;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using NugetForUnity;
using NugetForUnity.Configuration;
using NugetForUnity.Models;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;

[assembly: PrebuildSetup(typeof(NuGetPlayTests))]
[assembly: PostBuildCleanup(typeof(NuGetPlayTests))]

/// <summary>
/// Play mode tests allow us to install NuGet packages with Native code before play mode starts, then when play mode
/// runs the native libraries are available for use.
///
/// There seems to be some Unity internals that prevents an edit mode test from adding a Native library and finding it
/// in the same run.
/// Play mode tests allow us to install NuGet packages with Native code before play mode starts, then when play mode
/// runs the native libraries are available for use.
/// There seems to be some Unity internals that prevents an edit mode test from adding a Native library and finding it
/// in the same run.
/// </summary>
public class NuGetPlayTests : IPrebuildSetup, IPostBuildCleanup
{
readonly NugetPackageIdentifier sqlite = new NugetPackageIdentifier("SQLitePCLRaw.lib.e_sqlite3", "2.0.7");

/// <summary>
/// This is the version number of sqlite with periods replaced with zeros.
///
/// Note: Version of the SQLite library does not match the NuGet package version
/// This is the version number of sqlite with periods replaced with zeros.
/// Note: Version of the SQLite library does not match the NuGet package version.
/// </summary>
private readonly int _expectedVersion = 3035005;
private readonly int expectedVersion = 3035005;

private readonly NugetPackageIdentifier sqlite = new NugetPackageIdentifier("SQLitePCLRaw.lib.e_sqlite3", "2.0.7");

[UnityTest]
public IEnumerator InstallAndRunSqlite()
{
yield return new WaitForFixedUpdate();

Assert.That(Path.Combine(ConfigurationManager.NugetConfigFile.RepositoryPath, $"{sqlite.Id}.{sqlite.Version}"), Does.Exist.IgnoreFiles);

// Test the actual library by calling a "extern" method
var version = sqlite3_libversion_number();
Assert.That(version, Is.EqualTo(_expectedVersion));
Assert.That(version, Is.EqualTo(expectedVersion));
}

/// <summary>
/// Call to the SQLite native file, this actually tests loading and access the library when called.
///
/// On windows the call to sqlite3_libversion returns a garbled string so use the integer
/// </summary>
/// <returns></returns>
[DllImport("e_sqlite3")]
private static extern int sqlite3_libversion_number();

/// <inheritdoc />
public void Setup()
{
try
Expand All @@ -57,20 +53,41 @@ public void Setup()
{
}

var targetPackageDirectory = Path.Combine(ConfigurationManager.NugetConfigFile.RepositoryPath, $"{sqlite.Id}.{sqlite.Version}");
if (Directory.Exists(targetPackageDirectory))
{
Directory.Delete(targetPackageDirectory, true);
}

// For windows we end up importing two identical DLLs temporarily, this causes an error log that NUnit detects
// and would fail the test if we don't tell it to ignore the Failing messages
NugetHelper.LoadNugetConfigFile();
NugetHelper.LoadSettingFile();
LogAssert.ignoreFailingMessages = true;
NugetHelper.InstallIdentifier(sqlite);
Assert.IsTrue(NugetHelper.IsInstalled(sqlite), "The package was NOT installed: {0} {1}", sqlite.Id,
sqlite.Version);

var config = PackagesConfigFile.Load();
config.Packages.Add(new PackageConfig { Id = sqlite.Id, Version = sqlite.Version, IsManuallyInstalled = true });
config.GetType().GetField("contentIsSameAsInFilePath", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(config, null);
config.Save();
AssetDatabase.Refresh();

Assert.That(targetPackageDirectory, Does.Exist.IgnoreFiles);
}

/// <inheritdoc />
public void Cleanup()
{
NugetHelper.Uninstall(sqlite);
Assert.IsFalse(NugetHelper.IsInstalled(sqlite), "The packages are STILL installed: {0} {1}", sqlite.Id,
sqlite.Version);
var config = PackagesConfigFile.Load();
config.Packages.Clear();
config.GetType().GetField("contentIsSameAsInFilePath", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(config, null);
config.Save();

AssetDatabase.Refresh();
}
}

/// <summary>
/// Call to the SQLite native file, this actually tests loading and access the library when called.
/// On windows the call to sqlite3_libversion returns a garbled string so use the integer
/// </summary>
/// <returns>The version number.</returns>
[DllImport("e_sqlite3")]
private static extern int sqlite3_libversion_number();
}
18 changes: 10 additions & 8 deletions src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,16 +1003,18 @@ public void TestSourceCodePackageInstall(string packageId, string packageVersion
[TestCase("win-x86", BuildTarget.StandaloneWindows)]
[TestCase("linux-x64", BuildTarget.StandaloneLinux64)]
[TestCase("osx-x64", BuildTarget.StandaloneOSX)]
public void NativeSettingsTest(string key, BuildTarget buildTarget)
public void NativeSettingsTest(string runtime, BuildTarget buildTarget)
{
Settings.CreateDefault(NugetHelper.SettingsFilePath);
// Call load settings directly to ensure we're testing the deserialised file
var settings = Settings.Load(NugetHelper.SettingsFilePath);
var nativeRuntimes = settings.NativeRuntimesMappings;

Assert.IsTrue(nativeRuntimes.ContainsKey(key), $"Native mappings is missing {key}");
Assert.IsTrue(nativeRuntimes[key].Contains(buildTarget),
$"Native mapping for {key} is missing build target {buildTarget}");
var settings = ConfigurationManager.NativeRuntimeSettings;
var nativeRuntimes = settings.Configurations;

var runtimeConfig = nativeRuntimes.Find(config => config.Runtime == runtime);
Assert.That(runtimeConfig, Is.Not.Null, $"Native mappings is missing {runtime}");
Assert.That(
runtimeConfig.SupportetPlatformTargets,
Is.EqualTo(new[] { buildTarget }),
$"Native mapping for {runtime} is missing build target {buildTarget}");
}

private static void ConfigureNugetConfig(InstallMode installMode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"configurations": [
{
"supportetPlatformTargets": ["StandaloneWindows64"],
"cpuArchitecture": "x86_64",
"runtime": "win10-x64",
"editorCpuArchitecture": "x86_64",
"editorOperatingSystem": "Windows"
},
{
"supportetPlatformTargets": ["StandaloneWindows"],
"cpuArchitecture": "AnyCPU",
"runtime": "win10-x86",
"editorCpuArchitecture": "AnyCPU",
"editorOperatingSystem": "Windows"
},
{
"supportetPlatformTargets": ["StandaloneWindows64"],
"cpuArchitecture": "x86_64",
"runtime": "win7-x64",
"editorCpuArchitecture": "x86_64",
"editorOperatingSystem": "Windows"
},
{
"supportetPlatformTargets": ["StandaloneWindows"],
"cpuArchitecture": "AnyCPU",
"runtime": "win7-x86",
"editorCpuArchitecture": "AnyCPU",
"editorOperatingSystem": "Windows"
},
{
"supportetPlatformTargets": ["StandaloneWindows64"],
"cpuArchitecture": "x86_64",
"runtime": "win-x64",
"editorCpuArchitecture": "x86_64",
"editorOperatingSystem": "Windows"
},
{
"supportetPlatformTargets": ["StandaloneWindows"],
"cpuArchitecture": "AnyCPU",
"runtime": "win-x86",
"editorCpuArchitecture": "AnyCPU",
"editorOperatingSystem": "Windows"
},
{
"supportetPlatformTargets": ["StandaloneLinux64"],
"cpuArchitecture": "x86_64",
"runtime": "linux-x64",
"editorCpuArchitecture": "x86_64",
"editorOperatingSystem": "Linux"
},
{
"supportetPlatformTargets": ["StandaloneOSX"],
"cpuArchitecture": "x86_64",
"runtime": "osx-x64",
"editorCpuArchitecture": "x86_64",
"editorOperatingSystem": "OSX"
}
]
}
28 changes: 28 additions & 0 deletions src/NuGetForUnity/Editor/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
using NugetForUnity.Models;
using NugetForUnity.PackageSource;
using NugetForUnity.PluginSupport;
using NugetForUnity.Ui;
using UnityEngine;

[assembly: InternalsVisibleTo("NuGetForUnity.Editor.Tests")]
[assembly: InternalsVisibleTo("NuGetForUnity.PlayTests")]

namespace NugetForUnity.Configuration
{
Expand All @@ -32,10 +34,22 @@ public static class ConfigurationManager
[CanBeNull]
private static NugetConfigFile nugetConfigFile;

[CanBeNull]
private static NativeRuntimeSettings nativeRuntimeSettings;

[NotNull]
private static readonly string nativeRuntimeSettingsFilePath;

Check warning on line 41 in src/NuGetForUnity/Editor/Configuration/ConfigurationManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

Readonly fields should appear before non-readonly fields (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1214.md)

Check warning on line 41 in src/NuGetForUnity/Editor/Configuration/ConfigurationManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

Static readonly fields should begin with upper-case letter (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1311.md)

static ConfigurationManager()
{
NugetConfigFileDirectoryPath = UnityPathHelper.AbsoluteAssetsPath;
NugetConfigFilePath = Path.Combine(NugetConfigFileDirectoryPath, NugetConfigFile.FileName);
nativeRuntimeSettingsFilePath = Path.Combine(
UnityPathHelper.AbsoluteProjectPath,
"ProjectSettings",
"Packages",
NuGetForUnityUpdater.UpmPackageName,
"NativeRuntimeSettings.json");
}

/// <summary>
Expand Down Expand Up @@ -65,6 +79,20 @@ public static NugetConfigFile NugetConfigFile
}
}

[NotNull]
internal static NativeRuntimeSettings NativeRuntimeSettings
{
get
{
if (nativeRuntimeSettings is null)
{
nativeRuntimeSettings = NativeRuntimeSettings.LoadOrCreateDefault(nativeRuntimeSettingsFilePath);
}

return nativeRuntimeSettings;
}
}

/// <summary>
/// Gets the path to the directory containing the NuGet.config file.
/// </summary>
Expand Down
Loading

0 comments on commit 3e4442c

Please sign in to comment.