diff --git a/.gitignore b/.gitignore index 2d5fb407f..16f9ad8e3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ # mstest test results TestResults +Tests/temp ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/DataStorage/Datastore.cs b/DataStorage/Datastore.cs index f91d67443..f6ffb540c 100644 --- a/DataStorage/Datastore.cs +++ b/DataStorage/Datastore.cs @@ -46,6 +46,10 @@ namespace MatterHackers.MatterControl.DataStorage private static readonly string applicationDataFolderName = "MatterControl"; private readonly string datastoreName = "MatterControl.db"; private string applicationPath; + //private string testDataPath = Path.Combine("..", "..", "..", "..", "Tests", "TestData"); + + + public ApplicationDataStorage() //Constructor - validates that local storage folder exists, creates if necessary { @@ -71,6 +75,8 @@ namespace MatterHackers.MatterControl.DataStorage } } + private static string applicationUserDataPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), applicationDataFolderName); + public string ApplicationLibraryDataPath { get @@ -87,6 +93,14 @@ namespace MatterHackers.MatterControl.DataStorage } } + internal void OverrideApplicationPath(string path) + { + Directory.CreateDirectory(path); + applicationUserDataPath = path; + Datastore.Instance = new Datastore(); + Datastore.Instance.Initialize(); + } + public string ApplicationPath { get @@ -119,8 +133,9 @@ namespace MatterHackers.MatterControl.DataStorage { get { - return Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), applicationDataFolderName); + return applicationUserDataPath; } + } /// /// Returns the path to the sqlite database @@ -235,6 +250,10 @@ namespace MatterHackers.MatterControl.DataStorage } return globalInstance; } + internal set + { + globalInstance = value; + } } public void Exit() { diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index b165ee253..ece2d026b 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -1,5 +1,6 @@ using System.Reflection; using System.Resources; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following @@ -19,6 +20,9 @@ using System.Runtime.InteropServices; // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] +[assembly: InternalsVisibleTo("MatterControl.Tests")] +[assembly: InternalsVisibleTo("MatterControl.AutomationTests")] + // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("1558c103-dff3-49bd-854b-97d57339d662")] diff --git a/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs b/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs index dc5a4e022..9706c8684 100644 --- a/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs @@ -63,7 +63,7 @@ namespace MatterControl.Tests [Test, RunInApplicationDomain] public void LibraryProviderSqlite_NavigationWorking() { - MatterControlUtilities.DataFolderState staticDataState = MatterControlUtilities.MakeNewMatterControlAppDataFolderForTesting(); + MatterControlUtilities.MakeNewMatterControlAppDataFolderForTesting(); LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null, null, null, "Local Library"); testProvider.DataReloaded += (sender, e) => { dataReloaded = true; }; @@ -113,7 +113,7 @@ namespace MatterControl.Tests Assert.IsTrue(testProvider.CollectionCount == 0); Assert.IsTrue(!NamedCollectionExists(collectionName)); // assert that the record does not exist in the DB - MatterControlUtilities.RestoreStaticDataAfterTesting(staticDataState, true); + //MatterControlUtilities.RestoreStaticDataAfterTesting(staticDataState, true); } #endif diff --git a/Tests/MatterControl.Tests/MatterControl/MatterControlUtilities.cs b/Tests/MatterControl.Tests/MatterControl/MatterControlUtilities.cs index 95875cf70..9a6a0775c 100644 --- a/Tests/MatterControl.Tests/MatterControl/MatterControlUtilities.cs +++ b/Tests/MatterControl.Tests/MatterControl/MatterControlUtilities.cs @@ -67,9 +67,7 @@ namespace MatterHackers.MatterControl.UI public static string PathToQueueItemsFolder(string queueItemToLoad) { string temnp = Directory.GetCurrentDirectory(); - string pathToQueueItemFolder = Path.Combine("..", "..", "..", "..", "Tests", "TestData", "QueueItems"); - string fullPathToQueueItem = Path.Combine(pathToQueueItemFolder, queueItemToLoad); return Path.GetFullPath(fullPathToQueueItem); @@ -109,58 +107,17 @@ namespace MatterHackers.MatterControl.UI OutputImage(test, "image-test.tga"); } - public class DataFolderState + private static int testID = 0; + private static string runName = DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss"); + + public static void MakeNewMatterControlAppDataFolderForTesting(string testDBFolderName = null) { - internal bool undoDataRename; - internal string userDataPath; - internal string renamedUserDataPath; + + string temp = Path.Combine("..", "..", "..", "..", "Tests","temp"); + ApplicationDataStorage.Instance.OverrideApplicationPath(Path.Combine(temp, runName, $"Test{testID++}")); + } - public static DataFolderState MakeNewMatterControlAppDataFolderForTesting(string testDBFolderName = null) - { - DataFolderState state = new DataFolderState(); - state.userDataPath = MatterHackers.MatterControl.DataStorage.ApplicationDataStorage.ApplicationUserDataPath; - state.renamedUserDataPath = Path.Combine(Path.GetDirectoryName(state.userDataPath), "-MatterControl"); - - int testCount = 0; - while (Directory.Exists(state.renamedUserDataPath + testCount.ToString())) - { - testCount++; - } - - state.renamedUserDataPath = state.renamedUserDataPath + testCount.ToString(); - - state.undoDataRename = false; - if (Directory.Exists(state.userDataPath)) - { - Directory.Move(state.userDataPath, state.renamedUserDataPath); - state.undoDataRename = true; - } - - Stopwatch time = Stopwatch.StartNew(); - // Wait for up to some amount of time for the directory to be moved. - while (!Directory.Exists(state.renamedUserDataPath) - && time.ElapsedMilliseconds < 1000) - { - Thread.Sleep(1); // make sure we are not eating all the cpu time. - } - - if (testDBFolderName != null) - { - string fullPathToDataContents = Path.Combine("..", "..", "..", "TestData", "TestDatabaseStates", testDBFolderName); - CopyTestDataDBFolderToTemporaryMCAppDataDirectory(fullPathToDataContents); - - if (Directory.Exists(state.renamedUserDataPath)) - { - state.undoDataRename = true; - } - return state; - } - - Datastore.Instance.Initialize(); - - return state; - } public static void AddItemsToQueue(string queueItemFolderToLoad) { @@ -168,9 +125,6 @@ namespace MatterHackers.MatterControl.UI //DEFAULT LOCATION OF MCP FILE (LOCATION IS CORRECT) string mcpPath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "default.mcp"); - //RENAME ORIGINAL MCP FILE - //string backupMcp = Path.ChangeExtension(mcpPath, "_original"); - Directory.CreateDirectory(Path.GetDirectoryName(mcpPath)); if (!File.Exists(mcpPath)) @@ -206,17 +160,6 @@ namespace MatterHackers.MatterControl.UI } - /*public static void ResetMCPFile() - { - //DEFAULT LOCATION OF MCP FILE - string mcpPath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "default.mcp"); - - //RENAME ORIGINAL MCP FILE - string backupMcp = Path.ChangeExtension(mcpPath, "_original"); - - File.Copy(backupMcp, mcpPath, true); - }*/ - public static LibraryProvider CurrentProvider() { return ApplicationController.Instance.CurrentLibraryDataView.CurrentLibraryProvider; @@ -235,60 +178,6 @@ namespace MatterHackers.MatterControl.UI return goodNavigate; } - public static void RestoreStaticDataAfterTesting(DataFolderState state, bool closeDataBase) - { - if (state.undoDataRename) - { - Thread.Sleep(500); - if (closeDataBase) - { - Datastore.Instance.Exit(); - } - Stopwatch timeTryingToDelete = Stopwatch.StartNew(); - while (Directory.Exists(state.userDataPath) - && timeTryingToDelete.Elapsed.TotalSeconds < 10) - { - try - { - Directory.Delete(state.userDataPath, true); - } - catch (Exception e) - { - Debug.Print(e.Message); - GuiWidget.BreakInDebugger(); - } - } - Stopwatch time = Stopwatch.StartNew(); - // Wait for up to some amount of time for the directory to be gone. - while (Directory.Exists(state.userDataPath) - && time.ElapsedMilliseconds < 100) - { - Thread.Sleep(1); // make sure we are not eating all the cpu time. - } - if (!Directory.Exists(state.userDataPath)) - { - Directory.Move(state.renamedUserDataPath, state.userDataPath); - } - } - } - - public static void CopyTestDataDBFolderToTemporaryMCAppDataDirectory(string testDataDBDirectory) - { - string matterControlAppDataFolder = MatterHackers.MatterControl.DataStorage.ApplicationDataStorage.ApplicationUserDataPath; - - foreach (string folder in Directory.GetDirectories(testDataDBDirectory, "*", SearchOption.AllDirectories)) - { - string directoryToCopyFilesTo = folder.Replace(testDataDBDirectory, matterControlAppDataFolder); - Directory.CreateDirectory(directoryToCopyFilesTo); - } - - foreach (string fileName in Directory.GetFiles(testDataDBDirectory, "*", SearchOption.AllDirectories)) - { - string newFileFullName = fileName.Replace(testDataDBDirectory, matterControlAppDataFolder); - File.Copy(fileName, newFileFullName, true); - } - } - public static AutomationTesterHarness RunTest(Action testToRun, string testDbFolder = null, string staticDataPathOverride = null, string queueItemFolderToAdd = null, double maxTimeToRun = 60) { StackTrace st = new StackTrace(false); @@ -303,7 +192,7 @@ namespace MatterHackers.MatterControl.UI StaticData.Instance = new MatterHackers.Agg.FileSystemStaticData(staticDataPathOverride); #endif bool showWindow; - MatterControlUtilities.DataFolderState staticDataState = MatterControlUtilities.MakeNewMatterControlAppDataFolderForTesting(testDbFolder); + /*MatterControlUtilities.DataFolderState staticDataState =*/MatterControlUtilities.MakeNewMatterControlAppDataFolderForTesting(testDbFolder); if (queueItemFolderToAdd != null) { @@ -312,7 +201,6 @@ namespace MatterHackers.MatterControl.UI MatterControlApplication matterControlWindow = MatterControlApplication.CreateInstance(out showWindow); AutomationTesterHarness testHarness = AutomationTesterHarness.ShowWindowAndExectueTests(matterControlWindow, testToRun, maxTimeToRun); - MatterControlUtilities.RestoreStaticDataAfterTesting(staticDataState, true); return testHarness; }