|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.IO; |
| 8 | + |
| 9 | + using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; |
| 10 | + using Microsoft.VisualStudio.TestPlatform.ObjectModel; |
| 11 | + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// A host that loads the test source |
| 15 | + /// </summary> |
| 16 | + public class TestSourceHost : ITestSourceHost |
| 17 | + { |
| 18 | + private string sourceFileName; |
| 19 | + private string currentDirectory = null; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="TestSourceHost"/> class. |
| 23 | + /// </summary> |
| 24 | + /// <param name="sourceFileName"> The source file name. </param> |
| 25 | + /// <param name="runSettings"> The run-settings provided for this session. </param> |
| 26 | + /// <param name="frameworkHandle"> The handle to the test platform. </param> |
| 27 | + public TestSourceHost(string sourceFileName, IRunSettings runSettings, IFrameworkHandle frameworkHandle) |
| 28 | + { |
| 29 | + this.sourceFileName = sourceFileName; |
| 30 | + |
| 31 | + // Set the environment context. |
| 32 | + this.SetContext(sourceFileName); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Setup the isolation host. |
| 37 | + /// </summary> |
| 38 | + public void SetupHost() |
| 39 | + { |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 44 | + /// </summary> |
| 45 | + public void Dispose() |
| 46 | + { |
| 47 | + this.ResetContext(); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Creates an instance of a given type in the test source host. |
| 52 | + /// </summary> |
| 53 | + /// <param name="type"> The type that needs to be created in the host. </param> |
| 54 | + /// <param name="args">The arguments to pass to the constructor. |
| 55 | + /// This array of arguments must match in number, order, and type the parameters of the constructor to invoke. |
| 56 | + /// Pass in null for a constructor with no arguments. |
| 57 | + /// </param> |
| 58 | + /// <returns> An instance of the type created in the host. |
| 59 | + /// <see cref="object"/>. |
| 60 | + /// </returns> |
| 61 | + public object CreateInstanceForType(Type type, object[] args) |
| 62 | + { |
| 63 | + return Activator.CreateInstance(type, args); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Sets context required for running tests. |
| 68 | + /// </summary> |
| 69 | + /// <param name="source"> |
| 70 | + /// source parameter used for setting context |
| 71 | + /// </param> |
| 72 | + private void SetContext(string source) |
| 73 | + { |
| 74 | + if (string.IsNullOrEmpty(source)) |
| 75 | + { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + Exception setWorkingDirectoryException = null; |
| 80 | + this.currentDirectory = Directory.GetCurrentDirectory(); |
| 81 | + try |
| 82 | + { |
| 83 | + var dirName = Path.GetDirectoryName(source); |
| 84 | + if (string.IsNullOrEmpty(dirName)) |
| 85 | + { |
| 86 | + dirName = Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location); |
| 87 | + } |
| 88 | + |
| 89 | + Directory.SetCurrentDirectory(dirName); |
| 90 | + } |
| 91 | + catch (IOException ex) |
| 92 | + { |
| 93 | + setWorkingDirectoryException = ex; |
| 94 | + } |
| 95 | + catch (System.Security.SecurityException ex) |
| 96 | + { |
| 97 | + setWorkingDirectoryException = ex; |
| 98 | + } |
| 99 | + |
| 100 | + if (setWorkingDirectoryException != null) |
| 101 | + { |
| 102 | + EqtTrace.Error("MSTestExecutor.SetWorkingDirectory: Failed to set the working directory to '{0}'. {1}", Path.GetDirectoryName(source), setWorkingDirectoryException); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Resets the context as it was before calling SetContext() |
| 108 | + /// </summary> |
| 109 | + private void ResetContext() |
| 110 | + { |
| 111 | + if (!string.IsNullOrEmpty(this.currentDirectory)) |
| 112 | + { |
| 113 | + Directory.SetCurrentDirectory(this.currentDirectory); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +#pragma warning restore SA1649 // SA1649FileNameMustMatchTypeName |
| 119 | +} |
0 commit comments