Skip to content

Commit ad3ee54

Browse files
fix: Fix unit tests.
1 parent 6a3f1e7 commit ad3ee54

6 files changed

Lines changed: 72 additions & 77 deletions

File tree

Test-ShareFileModule/CopySFItemsTests.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.ObjectModel;
4-
using System.IO;
1+
using System.Collections.ObjectModel;
52
using System.Management.Automation;
63
using System.Management.Automation.Runspaces;
74
using Microsoft.VisualStudio.TestTools.UnitTesting;
8-
using ShareFile.Api.Powershell;
95

106
namespace Test_ShareFileSnapIn
117
{
@@ -18,14 +14,7 @@ public class CopySFItemsTests
1814
[TestInitialize]
1915
public void InitializeTests()
2016
{
21-
RunspaceConfiguration config = RunspaceConfiguration.Create();
22-
23-
PSSnapInException warning;
24-
25-
config.AddPSSnapIn("ShareFile", out warning);
26-
27-
runspace = RunspaceFactory.CreateRunspace(config);
28-
runspace.Open();
17+
runspace = Utils.OpenRunspace();
2918

3019
// do login first to start tests
3120
using (Pipeline pipeline = runspace.CreatePipeline())
@@ -58,6 +47,13 @@ public void InitializeTests()
5847
}
5948
}
6049

50+
[TestCleanup]
51+
public void CleanupTests()
52+
{
53+
runspace?.Dispose();
54+
runspace = null;
55+
}
56+
6157
[TestMethod]
6258
public void TM1_CopyItemToLocalTest()
6359
{
@@ -128,7 +124,7 @@ public void TM3_CopyItemToShareFileServerTest()
128124
Collection<PSObject> psObjects = pipeline.Invoke();
129125

130126
Assert.AreEqual<int>(1, psObjects.Count);
131-
Assert.AreEqual("ShareFile.Api.Models.File", psObjects[0].BaseObject.ToString());
127+
Assert.AreEqual("ShareFile.Api.Client.Models.File", psObjects[0].BaseObject.ToString());
132128
}
133129
}
134130

@@ -160,7 +156,7 @@ public void TM4_CopyItemToShareFileServerForceTest()
160156
Collection<PSObject> psObjects = pipeline.Invoke();
161157

162158
Assert.AreEqual<int>(1, psObjects.Count);
163-
Assert.AreEqual("ShareFile.Api.Models.File", psObjects[0].BaseObject.ToString());
159+
Assert.AreEqual("ShareFile.Api.Client.Models.File", psObjects[0].BaseObject.ToString());
164160
}
165161
}
166162

Test-ShareFileModule/LoginTests.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using System;
2-
using System.Collections.ObjectModel;
3-
using System.IO;
1+
using System.Collections.ObjectModel;
42
using System.Management.Automation;
53
using System.Management.Automation.Runspaces;
64
using Microsoft.VisualStudio.TestTools.UnitTesting;
7-
using ShareFile.Api.Powershell;
85

96
namespace Test_ShareFileSnapIn
107
{
@@ -16,31 +13,27 @@ public class LoginTests
1613
[TestInitialize]
1714
public void InitializeTests()
1815
{
19-
RunspaceConfiguration config = RunspaceConfiguration.Create();
20-
21-
PSSnapInException warning;
22-
23-
config.AddPSSnapIn("ShareFile", out warning);
16+
runspace = Utils.OpenRunspace();
17+
}
2418

25-
runspace = RunspaceFactory.CreateRunspace(config);
26-
runspace.Open();
19+
[TestCleanup]
20+
public void CleanupTests()
21+
{
22+
runspace?.Dispose();
23+
runspace = null;
2724
}
2825

2926
[TestMethod]
3027
public void TM1_0_NewLoginTest()
3128
{
32-
PowerShell ps = PowerShell.Create();
33-
34-
ps.AddCommand("Add-PSSnapIn");
35-
ps.AddArgument("ShareFile");
36-
37-
Collection<PSObject> psObjects = ps.Invoke();
29+
using var ps = PowerShell.Create();
30+
ps.Runspace = runspace;
3831

3932
ps.Commands.Clear();
4033
ps.AddCommand("New-SFClient");
4134
ps.AddParameter("Name", Utils.LoginFilePath);
4235

43-
psObjects = ps.Invoke();
36+
var psObjects = ps.Invoke();
4437

4538
Assert.AreEqual<int>(1, psObjects.Count);
4639
}

Test-ShareFileModule/MapDriveTests.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
42
using System.Management.Automation;
53
using System.Management.Automation.Runspaces;
64
using Microsoft.VisualStudio.TestTools.UnitTesting;
7-
using ShareFile.Api.Powershell;
85

96
namespace Test_ShareFileSnapIn
107
{
@@ -17,15 +14,7 @@ public class MapDriveTests
1714
[TestInitialize]
1815
public void InitializeTests()
1916
{
20-
21-
RunspaceConfiguration config = RunspaceConfiguration.Create();
22-
23-
PSSnapInException warning;
24-
25-
config.AddPSSnapIn("ShareFile", out warning);
26-
27-
runspace = RunspaceFactory.CreateRunspace(config);
28-
runspace.Open();
17+
runspace = Utils.OpenRunspace();
2918

3019
// do login first to start tests
3120
using (Pipeline pipeline = runspace.CreatePipeline())
@@ -41,7 +30,14 @@ public void InitializeTests()
4130
sfLogin = objs[0];
4231
}
4332
}
44-
33+
34+
[TestCleanup]
35+
public void CleanupTests()
36+
{
37+
runspace?.Dispose();
38+
runspace = null;
39+
}
40+
4541
[TestMethod]
4642
public void TM1_MapDriveTest()
4743
{
@@ -72,7 +68,7 @@ public void TM2_CheckHomeContentsTest()
7268
Command command = new Command("New-PSDrive");
7369
command.Parameters.Add("Name", Utils.ShareFileDriveLetter);
7470
command.Parameters.Add("PSProvider", "ShareFile");
75-
command.Parameters.Add("Root", "/My Files & Folders");
71+
command.Parameters.Add("Root", "/");
7672
command.Parameters.Add("Client", sfLogin);
7773

7874
pipeline.Commands.Add(command);

Test-ShareFileModule/SyncSFItemsTests.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
42
using System.Management.Automation;
53
using System.Management.Automation.Runspaces;
64
using Microsoft.VisualStudio.TestTools.UnitTesting;
7-
using ShareFile.Api.Powershell;
85

96
namespace Test_ShareFileSnapIn
107
{
@@ -17,14 +14,7 @@ public class SyncSFItemsTests
1714
[TestInitialize]
1815
public void InitializeTests()
1916
{
20-
RunspaceConfiguration config = RunspaceConfiguration.Create();
21-
22-
PSSnapInException warning;
23-
24-
config.AddPSSnapIn("ShareFile", out warning);
25-
26-
runspace = RunspaceFactory.CreateRunspace(config);
27-
runspace.Open();
17+
runspace = Utils.OpenRunspace();
2818

2919
// do login first to start tests
3020
using (Pipeline pipeline = runspace.CreatePipeline())
@@ -57,6 +47,13 @@ public void InitializeTests()
5747
}
5848
}
5949

50+
[TestCleanup]
51+
public void CleanupTests()
52+
{
53+
runspace?.Dispose();
54+
runspace = null;
55+
}
56+
6057

6158
[TestMethod]
6259
public void TM1_SyncDownloadFileTest()
@@ -108,7 +105,7 @@ public void TM3_SyncDownloadRecursiveTest()
108105
Utils.DeleteLocalFolder(Utils.LocalFolderDownloaded);
109106

110107
Command command = new Command("Sync-SfItem");
111-
command.Parameters.Add("ShareFilePath", Utils.ShareFileFolder);
108+
command.Parameters.Add("ShareFilePath", Utils.ShareFileFolderFullPath);
112109
command.Parameters.Add("LocalPath", Utils.LocalBaseFolder);
113110
command.Parameters.Add("Download", true);
114111
command.Parameters.Add("Recursive", true);
@@ -129,7 +126,7 @@ public void TM4_SyncDownloadOverwriteTest()
129126
using (Pipeline pipeline = runspace.CreatePipeline())
130127
{
131128
Command command = new Command("Sync-SfItem");
132-
command.Parameters.Add("ShareFilePath", Utils.ShareFileFolder);
129+
command.Parameters.Add("ShareFilePath", Utils.ShareFileFolderFullPath);
133130
command.Parameters.Add("LocalPath", Utils.LocalBaseFolder);
134131
command.Parameters.Add("Download", true);
135132
command.Parameters.Add("OverWrite", true);
@@ -152,7 +149,7 @@ public void TM5_SyncDownloadMoveTest()
152149
Utils.DeleteLocalFolder(Utils.LocalFolderDownloaded);
153150

154151
Command command = new Command("Sync-SfItem");
155-
command.Parameters.Add("ShareFilePath", Utils.ShareFileFolder);
152+
command.Parameters.Add("ShareFilePath", Utils.ShareFileFolderFullPath);
156153
command.Parameters.Add("LocalPath", Utils.LocalBaseFolder);
157154
command.Parameters.Add("Download", true);
158155
command.Parameters.Add("Move", true);
@@ -195,7 +192,7 @@ public void TM11_SyncUploadFileTest()
195192
Collection<PSObject> psObjects = pipeline.Invoke();
196193

197194
Assert.AreEqual<int>(1, psObjects.Count);
198-
Assert.AreEqual("ShareFile.Api.Models.File", psObjects[0].BaseObject.ToString());
195+
Assert.AreEqual("ShareFile.Api.Client.Models.File", psObjects[0].BaseObject.ToString());
199196
}
200197
}
201198

Test-ShareFileModule/Test-ShareFileModule.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<RootNamespace>Test_ShareFileSnapIn</RootNamespace>
44
<TargetFramework>net48</TargetFramework>
5+
<LangVersion>10</LangVersion>
56
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
67
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
78
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>

Test-ShareFileModule/Utils.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.IO;
3+
using System.Management.Automation.Runspaces;
4+
using System.Management.Automation;
35

46
namespace Test_ShareFileSnapIn
57
{
@@ -12,9 +14,9 @@ public class Utils
1214
private static string _sfFolder = "Folder1Q";
1315

1416
/// <summary>
15-
/// C:\sflogin.sfps
17+
/// %LOCALAPPDATA%\ShareFile\sflogin.sfps
1618
/// </summary>
17-
public static string LoginFilePath = @"C:\sflogin.sfps";
19+
public static string LoginFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ShareFile", "sflogin.sfps");
1820

1921
/// <summary>
2022
/// sf
@@ -25,20 +27,20 @@ public class Utils
2527
/// </summary>
2628
public static string ShareFileDrivePath = string.Format("{0}{1}",ShareFileDriveLetter, ":");
2729
/// <summary>
28-
/// sf:/My Files & Folders
30+
/// sf:/Personal Folders
2931
/// </summary>
30-
public static string ShareFileHomeFolder = string.Format("{0}{1}", ShareFileDrivePath, "/My Files & Folders");
32+
public static string ShareFileHomeFolder = string.Format("{0}{1}", ShareFileDrivePath, "/Personal Folders");
3133

3234
/// <summary>
3335
/// sf:/Folder1
3436
/// </summary>
3537
public static string ShareFileFolder = string.Format("{0}{1}{2}", ShareFileDrivePath, "/", _localFolder);
3638
/// <summary>
37-
/// sf:/My Files & Folders/Folder1Q
39+
/// sf:/Personal Folders/Folder1Q
3840
/// </summary>
3941
public static string ShareFileFolderUploaded = string.Format("{0}{1}{2}", ShareFileHomeFolder, "/", _sfFolder);
4042
/// <summary>
41-
/// sf:/My Files & Folders/Folder1
43+
/// sf:/Personal Folders/Folder1
4244
/// </summary>
4345
public static string ShareFileFolderFullPath = string.Format("{0}{1}{2}", ShareFileHomeFolder, "/", _localFolder);
4446
/// <summary>
@@ -50,28 +52,28 @@ public class Utils
5052
/// </summary>
5153
public static string ShareFileFileUploaded = string.Format("{0}{1}{2}", ShareFileHomeFolder, "/", _localFile);
5254
/// <summary>
53-
/// sf:/My Files & Folders/DeepText.txt
55+
/// sf:/Personal Folders/DeepText.txt
5456
/// </summary>
5557
public static string ShareFileFileFullPath = string.Format("{0}{1}{2}", ShareFileHomeFolder, "/", _sfFile);
5658

5759
/// <summary>
58-
/// D:\SFTemp
60+
/// %LOCALAPPDATA%\ShareFile\SFTemp
5961
/// </summary>
60-
public static string LocalBaseFolder = @"D:\SFTemp";
62+
public static string LocalBaseFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ShareFile", "SFTemp");
6163
/// <summary>
62-
/// D:\SFTemp\Folder1Q
64+
/// %LOCALAPPDATA%\ShareFile\SFTemp\Folder1Q
6365
/// </summary>
6466
public static string LocalFolder = string.Format("{0}{1}{2}", LocalBaseFolder, @"\", _sfFolder);
6567
/// <summary>
66-
/// D:\SFTemp\Folder1
68+
/// %LOCALAPPDATA%\ShareFile\SFTemp\Folder1
6769
/// </summary>
6870
public static string LocalFolderDownloaded = string.Format("{0}{1}{2}", LocalBaseFolder, @"\", _localFolder);
6971
/// <summary>
70-
/// D:\SFTemp\ToUpload.txt
72+
/// %LOCALAPPDATA%\ShareFile\SFTemp\ToUpload.txt
7173
/// </summary>
7274
public static string LocalFile = string.Format("{0}{1}{2}", LocalBaseFolder, @"\", _localFile);
7375
/// <summary>
74-
/// D:\SFTemp\DeepText.txt
76+
/// %LOCALAPPDATA%\ShareFile\SFTemp\DeepText.txt
7577
/// </summary>
7678
public static string LocalFileDownloaded = string.Format("{0}{1}{2}", LocalBaseFolder, @"\", _sfFile);
7779

@@ -115,6 +117,16 @@ public static void DeleteLocalFolder(string path)
115117
}
116118
}
117119

120+
public static Runspace OpenRunspace()
121+
{
122+
const string moduleName = "ShareFile";
123+
InitialSessionState initial = InitialSessionState.CreateDefault();
124+
initial.ImportPSModule(new string[] { moduleName });
125+
var runspace = RunspaceFactory.CreateRunspace(initial);
126+
runspace.Open();
127+
return runspace;
128+
}
129+
118130
private static void DeleteLocalItemRecursive(FileSystemInfo source)
119131
{
120132
if (source is DirectoryInfo)

0 commit comments

Comments
 (0)