#if(DEBUG) using System.Runtime.InteropServices; using FileFlows.Plugin; using Microsoft.VisualStudio.TestTools.UnitTesting; using FileFlows.Plugin.Helpers; using FileFlows.Plugin.Services; using Moq; namespace PluginTestLibrary; /// /// Base class for the tests /// [TestClass] public abstract class TestBase { /// /// The test context instance /// private TestContext testContextInstance = null!; protected TestLogger Logger = new(); protected Mock MockFileService = new(); /// /// Gets or sets the test context /// public TestContext TestContext { get => testContextInstance; set => testContextInstance = value; } /// /// A File created for the test /// public string TempFile { get; private set; } = null!; /// /// A path in the temp directory created for the test /// public string TempPath { get; private set; } = null!; public readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); public readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); [TestInitialize] public void TestInitialize() { FileHelper.DontChangeOwner = true; FileHelper.DontSetPermissions = true; Logger.Writer = (msg) => TestContext.WriteLine(msg); var tempPath = Environment.GetEnvironmentVariable("FF_TEMP_PATH"); if (string.IsNullOrWhiteSpace(tempPath)) tempPath = Path.GetTempPath(); TempPath = System.IO.Path.Combine(tempPath, "tests", Guid.NewGuid().ToString()); System.IO.Directory.CreateDirectory(TempPath); TempFile = System.IO.Path.Combine(TempPath, Guid.NewGuid() + ".txt"); System.IO.File.WriteAllText(TempFile, Guid.NewGuid().ToString()); if (Directory.Exists(this.TempPath) == false) Directory.CreateDirectory(this.TempPath); TestStarting(); } [TestCleanup] public void CleanUp() { TestCleanUp(); if (Directory.Exists(TempPath)) Directory.Delete(TempPath, true); } protected virtual void TestStarting() { } protected virtual void TestCleanUp() { } public string GetPluginSettingsJson(string plugin) { if (Environment.GetEnvironmentVariable("Docker") == "1") return File.ReadAllText("/plugin-settings/" + plugin + ".json"); return File.ReadAllText("../../../settings.json"); } /// /// Gets the node parameters for a file /// /// the name of the file /// if the file is a directory /// the node parameters protected NodeParameters GetNodeParameters(string filename, bool isDirectory =false) { string tempPath = TempPath; string libPath = Path.Combine(tempPath, "media"); if (Directory.Exists(libPath) == false) Directory.CreateDirectory(libPath); var args = new NodeParameters(filename, Logger, isDirectory, libPath, new LocalFileService()) { LibraryFileName = filename, TempPath = tempPath }; return args; } } #endif