Browse Source

Remove duplicate GetManifestResourceStream helpers

Robert Hague 2 năm trước cách đây
mục cha
commit
1fc6360cf8

+ 7 - 18
test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs

@@ -10,32 +10,32 @@
 
         public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethod()
         {
-            var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
+            var privateKeyFile = GetPrivateKey("resources.client.id_rsa");
             return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
         }
 
         public PrivateKeyAuthenticationMethod CreateRegularUserMultiplePrivateKeyAuthenticationMethod()
         {
-            var privateKeyFile1 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
-            var privateKeyFile2 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
+            var privateKeyFile1 = GetPrivateKey("resources.client.id_rsa");
+            var privateKeyFile2 = GetPrivateKey("resources.client.id_rsa");
             return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile1, privateKeyFile2);
         }
 
         public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseAuthenticationMethod()
         {
-            var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", "tester");
+            var privateKeyFile = GetPrivateKey("resources.client.id_rsa_with_pass", "tester");
             return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
         }
 
         public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithEmptyPassPhraseAuthenticationMethod()
         {
-            var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", null);
+            var privateKeyFile = GetPrivateKey("resources.client.id_rsa_with_pass", null);
             return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
         }
 
         public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethodWithBadKey()
         {
-            var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_noaccess.rsa");
+            var privateKeyFile = GetPrivateKey("resources.client.id_noaccess.rsa");
             return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
         }
 
@@ -64,21 +64,10 @@
 
         private PrivateKeyFile GetPrivateKey(string resourceName, string passPhrase = null)
         {
-            using (var stream = GetResourceStream(resourceName))
+            using (var stream = TestBase.GetData(resourceName))
             {
                 return new PrivateKeyFile(stream, passPhrase);
             }
         }
-
-        private Stream GetResourceStream(string resourceName)
-        {
-            var type = GetType();
-            var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
-            if (resourceStream == null)
-            {
-                throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
-            }
-            return resourceStream;
-        }
     }
 }

+ 2 - 8
test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs

@@ -87,15 +87,9 @@ namespace Renci.SshNet.IntegrationTests
 
         private PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource)
         {
-            var privateKey = CreatePrivateKeyFromManifestResource("Renci.SshNet.IntegrationTests.resources.client." + keyResource);
-            return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKey);
-        }
-
-        private PrivateKeyFile CreatePrivateKeyFromManifestResource(string resourceName)
-        {
-            using (var stream = GetManifestResourceStream(resourceName))
+            using (var stream = GetData($"resources.client.{keyResource}"))
             {
-                return new PrivateKeyFile(stream);
+                return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, new PrivateKeyFile(stream));
             }
         }
     }

+ 1 - 12
test/Renci.SshNet.IntegrationTests/SftpTests.cs

@@ -300,7 +300,7 @@ namespace Renci.SshNet.IntegrationTests
 
                 try
                 {
-                    using (var imageStream = GetResourceStream("Renci.SshNet.IntegrationTests.resources.issue #70.png"))
+                    using (var imageStream = GetData("resources.issue #70.png"))
                     {
                         using (var fs = client.Create(remoteFile))
                         {
@@ -6301,17 +6301,6 @@ namespace Renci.SshNet.IntegrationTests
             return textBytes;
         }
 
-        private static Stream GetResourceStream(string resourceName)
-        {
-            var type = typeof(SftpTests);
-            var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
-            if (resourceStream == null)
-            {
-                throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
-            }
-            return resourceStream;
-        }
-
         private static decimal CalculateTransferSpeed(long length, long elapsedMilliseconds)
         {
             return (length / 1024m) / (elapsedMilliseconds / 1000m);

+ 5 - 8
test/Renci.SshNet.IntegrationTests/TestBase.cs

@@ -66,15 +66,12 @@ namespace Renci.SshNet.IntegrationTests
             }
         }
 
-        protected Stream GetManifestResourceStream(string resourceName)
+        internal static Stream GetData(string name)
         {
-            var type = GetType();
-            var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
-            if (resourceStream == null)
-            {
-                throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
-            }
-            return resourceStream;
+            string resourceName = $"Renci.SshNet.IntegrationTests.{name}";
+
+            return typeof(TestBase).Assembly.GetManifestResourceStream(resourceName)
+                ?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'.", nameof(resourceName));
         }
     }
 }

+ 2 - 4
test/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs

@@ -1,11 +1,11 @@
 using System;
 using System.Linq;
-using System.Reflection;
 using System.Threading;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
 using Renci.SshNet.Common;
 using Renci.SshNet.Security;
+using Renci.SshNet.Tests.Common;
 
 namespace Renci.SshNet.Tests.Classes
 {
@@ -136,9 +136,7 @@ namespace Renci.SshNet.Tests.Classes
 
         private static KeyHostAlgorithm GetKeyHostAlgorithm()
         {
-            var executingAssembly = Assembly.GetExecutingAssembly();
-
-            using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
+            using (var s = TestBase.GetData("Key.RSA.txt"))
             {
                 var privateKey = new PrivateKeyFile(s);
                 return (KeyHostAlgorithm) privateKey.HostKeyAlgorithms.First();

+ 1 - 4
test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs

@@ -3,7 +3,6 @@ using Renci.SshNet.Common;
 using Renci.SshNet.Security;
 using Renci.SshNet.Tests.Common;
 using System.Linq;
-using System.Reflection;
 
 namespace Renci.SshNet.Tests.Classes.Common
 {
@@ -86,9 +85,7 @@ namespace Renci.SshNet.Tests.Classes.Common
 
         private static KeyHostAlgorithm GetKeyHostAlgorithm()
         {
-            var executingAssembly = Assembly.GetExecutingAssembly();
-
-            using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
+            using (var s = GetData("Key.RSA.txt"))
             {
                 var privateKey = new PrivateKeyFile(s);
                 return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();

+ 2 - 4
test/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Linq;
-using System.Reflection;
 using System.Threading;
 
 using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -9,6 +8,7 @@ using Moq;
 
 using Renci.SshNet.Common;
 using Renci.SshNet.Security;
+using Renci.SshNet.Tests.Common;
 
 namespace Renci.SshNet.Tests.Classes
 {
@@ -109,9 +109,7 @@ namespace Renci.SshNet.Tests.Classes
 
         private static KeyHostAlgorithm GetKeyHostAlgorithm()
         {
-            var executingAssembly = Assembly.GetExecutingAssembly();
-
-            using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
+            using (var s = TestBase.GetData("Key.RSA.txt"))
             {
                 var privateKey = new PrivateKeyFile(s);
                 return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();

+ 2 - 10
test/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Linq;
-using System.Reflection;
 using System.Threading;
 
 using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -9,6 +8,7 @@ using Moq;
 
 using Renci.SshNet.Common;
 using Renci.SshNet.Security;
+using Renci.SshNet.Tests.Common;
 
 namespace Renci.SshNet.Tests.Classes
 {
@@ -118,16 +118,8 @@ namespace Renci.SshNet.Tests.Classes
 
         private static KeyHostAlgorithm GetKeyHostAlgorithm()
         {
-            var executingAssembly = Assembly.GetExecutingAssembly();
-            var resourceName = string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt");
-
-            using (var s = executingAssembly.GetManifestResourceStream(resourceName))
+            using (var s = TestBase.GetData("Key.RSA.txt"))
             {
-                if (s is null)
-                {
-                    throw new ArgumentException($"Resource '{resourceName}' does not exist in assembly '{executingAssembly.GetName().Name}'.");
-                }
-
                 var privateKey = new PrivateKeyFile(s);
                 return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
             }

+ 5 - 2
test/Renci.SshNet.Tests/Common/TestBase.cs

@@ -49,9 +49,12 @@ namespace Renci.SshNet.Tests.Common
             }
         }
 
-        protected static Stream GetData(string name)
+        internal static Stream GetData(string name)
         {
-            return ExecutingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", name));
+            string resourceName = $"Renci.SshNet.Tests.Data.{name}";
+
+            return ExecutingAssembly.GetManifestResourceStream(resourceName)
+                ?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'.");
         }
     }
 }