aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.UnitTesting/Helper.cs
blob: ae7636c7ba5ca6d9bb49cd4e7ff7e562976263f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Tango.Logging;

namespace Tango.UnitTesting
{
    /// <summary>
    /// Contains several unit testing helper methods.
    /// </summary>
    public static class Helper
    {
        /// <summary>
        /// Gets the absolute path to the specified file name in the solution 'Resources' folder.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public static String GetResourcePath(String fileName)
        {
            return Path.GetFullPath(@"..\..\..\Resources\" + fileName);
        }

        /// <summary>
        /// Gets the PMR (Protobuf Messages Repository) path.
        /// </summary>
        /// <returns></returns>
        public static String GetPMRPath()
        {
            return Path.GetFullPath(@"..\..\..\PMR\Messages\");
        }

        /// <summary>
        /// Gets the machine studio exe path.
        /// </summary>
        /// <returns></returns>
        public static String GetMachineStudioPath()
        {
            return Path.GetFullPath(@"..\..\Build\Debug\Tango.MachineStudio.UI.exe");
        }

        /// <summary>
        /// Gets the tango build path.
        /// </summary>
        /// <returns></returns>
        public static String GetBuildPath()
        {
            return Path.GetFullPath(@"..\..\Build\Debug");
        }

        /// <summary>
        /// Gets the SQLite database file path in DB folder.
        /// </summary>
        /// <returns></returns>
        public static String GetSQLiteFilePath()
        {
            return Path.GetFullPath(@"..\..\..\DB\Tango.db");
        }

        /// <summary>
        /// Initializes the logging manager.
        /// </summary>
        public static ConsoleLogger InitializeLogging(bool useConsole = false, [CallerMemberName] string testName = null)
        {
            LogManager.Default.RegisterLogger(new FileLogger(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "logs", "Unit Testing"),"Unit_Testing"));
            if (useConsole)
            {
                var consoleLogger = new ConsoleLogger(testName);
                LogManager.Default.RegisterLogger(consoleLogger);
                return consoleLogger;
            }

            return null;
        }

        /// <summary>
        /// Creates a temporary folder and returns it's path.
        /// </summary>
        /// <returns></returns>
        public static String GetTempFolderPathAppend(String customFolder, [CallerMemberName] string testName = null)
        {
            String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", "Unit Testing", testName, customFolder);
            Directory.CreateDirectory(tempDirectory);
            return tempDirectory;
        }

        /// <summary>
        /// Creates a temporary folder and returns it's path.
        /// </summary>
        /// <returns></returns>
        public static String GetTempFolderPath([CallerMemberName] string testName = null)
        {
            String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", "Unit Testing", testName, Path.GetRandomFileName());
            Directory.CreateDirectory(tempDirectory);
            return tempDirectory;
        }

        /// <summary>
        /// Tries to delete folder.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static bool TryDeleteFolder(String path)
        {
            try
            {
                Directory.Delete(path, true);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Shows the file in explorer.
        /// </summary>
        /// <param name="path">Name of the file/folder.</param>
        public static void ShowInExplorer(String path)
        {
            Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path));
        }
    }
}