From 776a75a4dd0bb66b8fbb81181a47c2b33a0ad699 Mon Sep 17 00:00:00 2001 From: James Suplizio Date: Fri, 26 Aug 2022 10:06:30 -0700 Subject: [PATCH 1/2] Add integration reset tests --- .../GitStoreIntegrationResetTests.cs | 397 ++++++++++++++++++ .../GitStoreIntegrationRestoreTests.cs | 7 +- .../TestHelpers.cs | 23 +- .../ConsoleWrapper/ConsoleWrapperImpl.cs | 24 ++ .../ConsoleWrapper/ConsoleWrapperTester.cs | 42 ++ .../ConsoleWrapper/IConsoleWrapper.cs | 13 + .../Azure.Sdk.Tools.TestProxy/Playback.cs | 4 +- .../Azure.Sdk.Tools.TestProxy/Startup.cs | 4 +- .../Store/GitProcessHandler.cs | 4 +- .../Store/GitStore.cs | 34 +- .../Store/IAssetsStore.cs | 4 +- .../Store/NullStore.cs | 3 +- 12 files changed, 542 insertions(+), 17 deletions(-) create mode 100644 tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs create mode 100644 tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs create mode 100644 tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs create mode 100644 tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs new file mode 100644 index 00000000000..29ea2a2a9b9 --- /dev/null +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs @@ -0,0 +1,397 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Azure.Sdk.Tools.TestProxy.Common; +using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; +using Azure.Sdk.Tools.TestProxy.Store; +using Microsoft.Extensions.Logging; +using Xunit; + +namespace Azure.Sdk.Tools.TestProxy.Tests.IntegrationTests +{ + // Reset Test Scenarios involving https://github.com/Azure/azure-sdk-assets-integration + + // Setup: + // The files live under https://github.com/Azure/azure-sdk-assets-integration/tree/main/pull/scenarios. + // Each file contains nothing but a single version digit, which is used for verification purposes. + // Most of the scenarios involve restoring from a SHA, updating, adding, and/or deleting files locally + // and then performing a Reset and verifying that the only the files in the original SHA are there and + // they've been restored to what they were in the original SHA. + public class GitStoreIntegrationResetTests + { + // Right now, this is necessary for testing purposes but the real server won't have + // this issue. + public GitStoreIntegrationResetTests() + { + var loggerFactory = new LoggerFactory(); + DebugLogger.ConfigureLogger(loggerFactory); + } + + private GitStore _defaultStore = new GitStore(); + + // Scenario 1 - Changes to existing files only are detected and overridden with Reset response Y + // 1. Restore from SHA fc54d000d0427c4a68bc8962d40f957f59e14577 + // 2. Expect: 3 files with versions they were checked in with + // 3. Update one or more files, incrementing their version + // 4. Expect: files updated should be at version 2 + // 5. Reset with Y + // 6. Expect: each file should be at it's initial version, the version that was in the original SHA + [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] + [InlineData( + @"{ + ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", + ""AssetsRepoPrefixPath"": ""pull/scenarios"", + ""AssetsRepoId"": """", + ""AssetsRepoBranch"": ""main"", + ""SHA"": ""fc54d000d0427c4a68bc8962d40f957f59e14577"" + }")] + public async Task Scenario1(string inputJson) + { + var folderStructure = new string[] + { + GitStoretests.AssetsJson + }; + + var testFolder = TestHelpers.DescribeTestFolder(inputJson, folderStructure); + try + { + var jsonFileLocation = Path.Join(testFolder, GitStoretests.AssetsJson); + + var parsedConfiguration = await _defaultStore.ParseConfigurationFile(jsonFileLocation); + await _defaultStore.Restore(jsonFileLocation); + + // Calling Path.GetFullPath of the Path.Combine will ensure any directory separators are normalized for + // the OS the test is running on. The reason being is that AssetsRepoPrefixPath, if there's a separator, + // will be a forward one as expected by git but on Windows this won't result in a usable path. + string localFilePath = Path.GetFullPath(Path.Combine(parsedConfiguration.AssetsRepoLocation, parsedConfiguration.AssetsRepoPrefixPath)); + + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1)); + + // Increment file versions to cause a change on disk + TestHelpers.IncrementFileVersion(localFilePath, "file1.txt"); + TestHelpers.IncrementFileVersion(localFilePath, "file3.txt"); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + + // Reset the cloned assets, reponse for overwrite = Y + ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("Y"); + await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + + // Verify all files have been set back to their original versions + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1)); + } + finally + { + DirectoryHelper.DeleteGitDirectory(testFolder); + } + } + + // Scenario 2 - Changes to existing files only are detected and retained with Reset response N + // 1. Restore from SHA fc54d000d0427c4a68bc8962d40f957f59e14577 + // 2. Expect: 3 files with versions they were checked in with + // 3. Update one or more files, incrementing their version + // 4. Expect: files updated should be at version 2 + // 5. Reset with N + // 6. Expect: file versions should be what they were in step 4 + [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] + [InlineData( + @"{ + ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", + ""AssetsRepoPrefixPath"": ""pull/scenarios"", + ""AssetsRepoId"": """", + ""AssetsRepoBranch"": ""main"", + ""SHA"": ""fc54d000d0427c4a68bc8962d40f957f59e14577"" + }")] + public async Task Scenario2(string inputJson) + { + var folderStructure = new string[] + { + GitStoretests.AssetsJson + }; + + var testFolder = TestHelpers.DescribeTestFolder(inputJson, folderStructure); + try + { + var jsonFileLocation = Path.Join(testFolder, GitStoretests.AssetsJson); + + var parsedConfiguration = await _defaultStore.ParseConfigurationFile(jsonFileLocation); + await _defaultStore.Restore(jsonFileLocation); + + // Calling Path.GetFullPath of the Path.Combine will ensure any directory separators are normalized for + // the OS the test is running on. The reason being is that AssetsRepoPrefixPath, if there's a separator, + // will be a forward one as expected by git but on Windows this won't result in a usable path. + string localFilePath = Path.GetFullPath(Path.Combine(parsedConfiguration.AssetsRepoLocation, parsedConfiguration.AssetsRepoPrefixPath)); + + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1)); + + // Increment file versions to cause a change on disk + TestHelpers.IncrementFileVersion(localFilePath, "file1.txt"); + TestHelpers.IncrementFileVersion(localFilePath, "file3.txt"); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + + // Reset the cloned assets, reponse for overwrite = N + ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("N"); + await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + + // Verify all files have been set back to their original versions + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + + } + finally + { + DirectoryHelper.DeleteGitDirectory(testFolder); + } + } + + // Scenario 3 - Restore from SHA, add and remove files, Reset response Y + // 1. Restore from SHA 9e81fbb7d08c2df4cbdbfaffe79cde5d72f560d1 + // 2. Expect: 4 files with versions they were checked in with + // 3. Update add/remove files + // 4. Expect: Untouched files are the same versions as step 2, added files are version 1, removed files are gone + // 5. Reset with Y + // 6. Expect: each file should be at it's initial version, the version that was in the original SHA + [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] + [InlineData( + @"{ + ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", + ""AssetsRepoPrefixPath"": ""pull/scenarios"", + ""AssetsRepoId"": """", + ""AssetsRepoBranch"": ""main"", + ""SHA"": ""9e81fbb7d08c2df4cbdbfaffe79cde5d72f560d1"" + }")] + public async Task Scenario3(string inputJson) + { + var folderStructure = new string[] + { + GitStoretests.AssetsJson + }; + + var testFolder = TestHelpers.DescribeTestFolder(inputJson, folderStructure); + try + { + var jsonFileLocation = Path.Join(testFolder, GitStoretests.AssetsJson); + + var parsedConfiguration = await _defaultStore.ParseConfigurationFile(jsonFileLocation); + await _defaultStore.Restore(jsonFileLocation); + + // Calling Path.GetFullPath of the Path.Combine will ensure any directory separators are normalized for + // the OS the test is running on. The reason being is that AssetsRepoPrefixPath, if there's a separator, + // will be a forward one as expected by git but on Windows this won't result in a usable path. + string localFilePath = Path.GetFullPath(Path.Combine(parsedConfiguration.AssetsRepoLocation, parsedConfiguration.AssetsRepoPrefixPath)); + + // Verify files from SHA + Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file4.txt", 1)); + + // Delete a couple of files + File.Delete(Path.Combine(localFilePath, "file2.txt")); + File.Delete(Path.Combine(localFilePath, "file4.txt")); + // Add a file + TestHelpers.CreateFileWithInitialVersion(localFilePath, "file5.txt"); + + // Verify the set of files after the additions/deletions + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + // Reset the cloned assets, reponse for overwrite = Y + ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("Y"); + await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + + // Verify the only files there are ones from the SHA + Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file4.txt", 1)); + + } + finally + { + DirectoryHelper.DeleteGitDirectory(testFolder); + } + } + + // Scenario 4 - Restore from SHA, add and remove files, Reset response N + // 1. Restore from SHA 9e81fbb7d08c2df4cbdbfaffe79cde5d72f560d1 + // 2. Expect: 4 files with versions they were checked in with + // 3. Update add/remove files + // 4. Expect: Untouched files are the same versions as step 2, added files are version 1, removed files are gone + // 5. Reset with N + // 6. Expect: same files and same versions as step 4 + [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] + [InlineData( + @"{ + ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", + ""AssetsRepoPrefixPath"": ""pull/scenarios"", + ""AssetsRepoId"": """", + ""AssetsRepoBranch"": ""main"", + ""SHA"": ""9e81fbb7d08c2df4cbdbfaffe79cde5d72f560d1"" + }")] + public async Task Scenario4(string inputJson) + { + var folderStructure = new string[] + { + GitStoretests.AssetsJson + }; + + var testFolder = TestHelpers.DescribeTestFolder(inputJson, folderStructure); + try + { + var jsonFileLocation = Path.Join(testFolder, GitStoretests.AssetsJson); + + var parsedConfiguration = await _defaultStore.ParseConfigurationFile(jsonFileLocation); + await _defaultStore.Restore(jsonFileLocation); + + // Calling Path.GetFullPath of the Path.Combine will ensure any directory separators are normalized for + // the OS the test is running on. The reason being is that AssetsRepoPrefixPath, if there's a separator, + // will be a forward one as expected by git but on Windows this won't result in a usable path. + string localFilePath = Path.GetFullPath(Path.Combine(parsedConfiguration.AssetsRepoLocation, parsedConfiguration.AssetsRepoPrefixPath)); + + // Verify files from SHA + Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file4.txt", 1)); + + // Delete a couple of files + File.Delete(Path.Combine(localFilePath, "file2.txt")); + File.Delete(Path.Combine(localFilePath, "file4.txt")); + // Add a file + TestHelpers.CreateFileWithInitialVersion(localFilePath, "file5.txt"); + + // Verify the set of files after the additions/deletions + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + // Reset the cloned assets, reponse for overwrite = N + ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("N"); + await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + + // Verify the only files were not restored from the SHA + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + } + finally + { + DirectoryHelper.DeleteGitDirectory(testFolder); + } + } + + // Scenario 5 - Restore from SHA, add and remove files, Reset response N, then Reset response Y + // 1. Restore from SHA 9e81fbb7d08c2df4cbdbfaffe79cde5d72f560d1 + // 2. Expect: 3 files with versions they were checked in with + // 3. Update add/remove files + // 4. Expect: Untouched files are the same versions as step 2, added files are version 1, removed files are gone + // 5. Reset with N + // 6. Expect: same files and same versions as step 4 + // 7. Reset with Y + // 8. Expect: same files and same versions as step 2 + [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] + [InlineData( + @"{ + ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", + ""AssetsRepoPrefixPath"": ""pull/scenarios"", + ""AssetsRepoId"": """", + ""AssetsRepoBranch"": ""main"", + ""SHA"": ""bb2223a3aa0472ff481f8e1850e7647dc39fbfdd"" + }")] + public async Task Scenario5(string inputJson) + { + var folderStructure = new string[] + { + GitStoretests.AssetsJson + }; + + var testFolder = TestHelpers.DescribeTestFolder(inputJson, folderStructure); + try + { + var jsonFileLocation = Path.Join(testFolder, GitStoretests.AssetsJson); + + var parsedConfiguration = await _defaultStore.ParseConfigurationFile(jsonFileLocation); + await _defaultStore.Restore(jsonFileLocation); + + // Calling Path.GetFullPath of the Path.Combine will ensure any directory separators are normalized for + // the OS the test is running on. The reason being is that AssetsRepoPrefixPath, if there's a separator, + // will be a forward one as expected by git but on Windows this won't result in a usable path. + string localFilePath = Path.GetFullPath(Path.Combine(parsedConfiguration.AssetsRepoLocation, parsedConfiguration.AssetsRepoPrefixPath)); + + // Verify files from SHA + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file4.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + + // Delete a file + File.Delete(Path.Combine(localFilePath, "file4.txt")); + // Add a couple of files + TestHelpers.CreateFileWithInitialVersion(localFilePath, "file1.txt"); + TestHelpers.CreateFileWithInitialVersion(localFilePath, "file3.txt"); + + // Verify the set of files after the additions/deletions + Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + // Reset the cloned assets, reponse for overwrite = N + ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("N"); + await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + + // Verify the files were not restored from the SHA + Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + // Reset the cloned assets, reponse for overwrite = Y + consoleWrapperTester.SetReadLineResponse("Y"); + await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + + // Verify files are from the SHA + Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 2)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file4.txt", 1)); + Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); + + } + finally + { + DirectoryHelper.DeleteGitDirectory(testFolder); + } + } + } +} diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs index b4abe7caecd..a837de59883 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs @@ -11,12 +11,12 @@ namespace Azure.Sdk.Tools.TestProxy.Tests.IntegrationTests { - // Pull Test Scenarios involving https://github.com/Azure/azure-sdk-assets-integration + // Restore Test Scenarios involving https://github.com/Azure/azure-sdk-assets-integration // Setup: // The files live under https://github.com/Azure/azure-sdk-assets-integration/tree/main/pull/scenarios. // Each file contains nothing but a single version digit, which is used for verification purposes. - // There are 3 pull test scenarios and each uses a different SHA. The scenarios are detailed down + // There are restore test scenarios and each uses a different SHA. The scenarios are detailed down // below with their test functions. public class GitStoreIntegrationRestoreTests { @@ -38,6 +38,7 @@ public GitStoreIntegrationRestoreTests() // Added file3.txt // Expect: each file should be version 1 [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] [InlineData( @"{ ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", @@ -90,6 +91,7 @@ public async Task Scenario1(string inputJson) // file3 version 2 // file4 version 1 [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] [InlineData( @"{ ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", @@ -144,6 +146,7 @@ public async Task Scenario2(string inputJson) // file4 version 1 // file5 version 1 [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + //[Theory] [InlineData( @"{ ""AssetsRepo"": ""Azure/azure-sdk-assets-integration"", diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs index d91b12ea2b5..c3c2f90d6ee 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs @@ -231,9 +231,8 @@ public static bool VerifyFileVersion(string testFolder, string fileName, int exp /// Verify the version, inside the file, for a given file inside of a test folder. /// /// The temporary test folder created by TestHelpers.DescribeTestFolder - /// The fileName whose version needs verification - /// The expected version in the file - public static bool IncrementFileVersion(string testFolder, string fileName) + /// The file whose version needs to be incremented + public static void IncrementFileVersion(string testFolder, string fileName) { string fullFileName = Path.Combine(testFolder, fileName); string stringVersion = ""; @@ -254,8 +253,24 @@ public static bool IncrementFileVersion(string testFolder, string fileName) { File.WriteAllText(fullFileName, (++intVersion).ToString()); } + } - return false; + /// + /// Create a new file with an initial version of 1 + /// + /// The temporary test folder created by TestHelpers.DescribeTestFolder + /// The file to be created + public static void CreateFileWithInitialVersion(string testFolder, string fileName) + { + string fullFileName = Path.Combine(testFolder, fileName); + + if (File.Exists(fullFileName)) + { + string errorString = String.Format("FileName {0} already exists", fullFileName); + throw new ArgumentException(errorString); + } + + File.WriteAllText(fullFileName, 1.ToString()); } } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs new file mode 100644 index 00000000000..0f7a08e2570 --- /dev/null +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs @@ -0,0 +1,24 @@ + +using System; + +namespace Azure.Sdk.Tools.TestProxy.ConsoleWrapper +{ + /// + /// Implementation of IConsoleWrapper that's simply a passthrough to the Console functions. + /// + public class ConsoleWrapperImpl : IConsoleWrapper + { + public void Write(string message) + { + Console.Write(message); + } + public void WriteLine(string message) + { + Console.WriteLine(message); + } + public string ReadLine() + { + return Console.ReadLine(); + } + } +} diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs new file mode 100644 index 00000000000..47ad8c1b358 --- /dev/null +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs @@ -0,0 +1,42 @@ +using System; + +namespace Azure.Sdk.Tools.TestProxy.ConsoleWrapper +{ + /// + /// Implementation of IConsoleWrapper that will be used to test commands, like Reset, that require user input. + /// + public class ConsoleWrapperTester : IConsoleWrapper + { + private string _readLineResponse; + + /// + /// Overloaded constructor takes in a string that'll be returned as the ReadLine response. + /// + /// string that'll be returned as the ReadLine response + public ConsoleWrapperTester(string readLineResponse) + { + _readLineResponse = readLineResponse; + } + + /// + /// Set the ReadLine response. + /// + /// string that'll be returned as the ReadLine response + public void SetReadLineResponse(string readLineResponse) + { + _readLineResponse = readLineResponse; + } + public void Write(string message) + { + Console.Write(message); + } + public void WriteLine(string message) + { + Console.WriteLine(message); + } + public string ReadLine() + { + return _readLineResponse; + } + } +} diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs new file mode 100644 index 00000000000..9d3f7e7e32a --- /dev/null +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs @@ -0,0 +1,13 @@ +namespace Azure.Sdk.Tools.TestProxy.ConsoleWrapper +{ + /// + /// IConsoleWrapper is just an interace around Console functions. This is necessary for testing + /// functions, like Reset, which require user input that we need to be able to control. + /// + public interface IConsoleWrapper + { + void Write(string message); + void WriteLine(string message); + string ReadLine(); + } +} diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs index d0ad04f016e..c07168dbaea 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs @@ -3,6 +3,7 @@ using Azure.Sdk.Tools.TestProxy.Common; using Azure.Sdk.Tools.TestProxy.Common.Exceptions; +using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; using Azure.Sdk.Tools.TestProxy.Store; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -62,7 +63,8 @@ public async Task Reset([FromBody()] IDictionary options = null) var pathToAssets = StoreResolver.ParseAssetsJsonBody(options); - await _recordingHandler.Store.Reset(pathToAssets); + ConsoleWrapperImpl consoleWrapper = new ConsoleWrapperImpl(); + await _recordingHandler.Store.Reset(pathToAssets, consoleWrapper); } [HttpPost] diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs index 5605690eebe..47b6fb19a82 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs @@ -17,6 +17,7 @@ using System.Reflection; using Microsoft.AspNetCore.Server.Kestrel.Core; using Azure.Sdk.Tools.TestProxy.Store; +using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; namespace Azure.Sdk.Tools.TestProxy { @@ -79,7 +80,8 @@ public static void Main(bool insecure = false, string storageLocation = null, st DefaultStore.Restore(assetsJsonPath); break; case "reset": - DefaultStore.Reset(assetsJsonPath); + ConsoleWrapperImpl consoleWrapper = new ConsoleWrapperImpl(); + DefaultStore.Reset(assetsJsonPath, consoleWrapper); break; default: throw new Exception($"One must provide a valid value for argument \"command\". \"{command}\" is not a valid option."); diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitProcessHandler.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitProcessHandler.cs index 9ca8fafbe96..38b099e23b4 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitProcessHandler.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitProcessHandler.cs @@ -157,8 +157,8 @@ public virtual bool TryRun(string arguments, GitAssetsConfiguration config, out result = new CommandResult() { ExitCode = process.ExitCode, - StdErr = stdOut, - StdOut = stdErr, + StdErr = stdErr, + StdOut = stdOut, Arguments = arguments }; } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs index cd8784a6f9a..9b2c98de1da 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs @@ -10,6 +10,7 @@ using System.Linq; using Azure.Sdk.Tools.TestProxy.Common.Exceptions; using Azure.Sdk.Tools.TestProxy.Common; +using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; namespace Azure.Sdk.Tools.TestProxy.Store { @@ -122,12 +123,14 @@ public async Task Restore(string pathToAssetsJson) { /// Resets a cloned assets repository to the default contained within the assets.json targeted commit. /// /// + /// /// // This should only ever be called by the user? - public async Task Reset(string pathToAssetsJson) { + public async Task Reset(string pathToAssetsJson, IConsoleWrapper consoleWrapper) + { var config = await ParseConfigurationFile(pathToAssetsJson); var initialized = config.IsAssetsRepoInitialized(); - var allowReset = true; + var allowReset = false; if (!initialized) { @@ -138,7 +141,26 @@ public async Task Reset(string pathToAssetsJson) { if (pendingChanges.Length > 0) { - // TODO: Azure/azure-sdk-tools/3698 + consoleWrapper.WriteLine("There are pending git chances, are you sure you want to reset? [Y|N]"); + while (true) + { + string response = consoleWrapper.ReadLine(); + response = response.ToLower(); + if (response.Equals("y")) + { + allowReset = true; + break; + } + else if (response.Equals("n")) + { + allowReset = false; + break; + } + else + { + consoleWrapper.WriteLine("Please answer [Y|N]"); + } + } } if (allowReset) @@ -146,7 +168,7 @@ public async Task Reset(string pathToAssetsJson) { try { GitHandler.Run("checkout *", config); - GitHandler.Run("git clean -xdf", config); + GitHandler.Run("clean -xdf", config); } catch(GitProcessException e) { @@ -186,7 +208,9 @@ public string[] DetectPendingChanges(GitAssetsConfiguration config) if (!string.IsNullOrWhiteSpace(diffResult.StdOut)) { - var individualResults = diffResult.StdOut.Split(Environment.NewLine).Select(x => x.Trim()).ToArray(); + // Normally, we'd use Environment.NewLine here but this doesn't work on Windows since its NewLine is \r\n and + // Git's NewLine is just \n + var individualResults = diffResult.StdOut.Split("\n").Select(x => x.Trim()).ToArray(); return individualResults; } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs index 695e199cf8a..c4f2de0b316 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs @@ -1,5 +1,6 @@ using System.IO; using System.Threading.Tasks; +using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; namespace Azure.Sdk.Tools.TestProxy.Store { @@ -21,6 +22,7 @@ public interface IAssetsStore /// Given a configuration, determine the state of the resources present under contextPath, reset those resources to their "fresh" state. /// /// - public abstract Task Reset(string pathToAssetsJson); + /// + public abstract Task Reset(string pathToAssetsJson, IConsoleWrapper consoleWrapper); } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs index 4245be7c7c8..c221d6b6677 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs @@ -2,6 +2,7 @@ using System.Net; using System.Threading.Tasks; using Azure.Sdk.Tools.TestProxy.Common.Exceptions; +using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; namespace Azure.Sdk.Tools.TestProxy.Store { @@ -11,7 +12,7 @@ public class NullStore : IAssetsStore public Task Restore(string assetsJsonPath) { return null; } - public Task Reset(string assetsJsonPath) { return null; } + public Task Reset(string assetsJsonPath, IConsoleWrapper consoleWrapper) { return null; } public AssetsConfiguration ParseConfigurationFile(string assetsJsonPath) { From 79538662dba94a9faa6bffa51f3b96c41a671bd6 Mon Sep 17 00:00:00 2001 From: James Suplizio Date: Fri, 26 Aug 2022 12:29:22 -0700 Subject: [PATCH 2/2] Updates based upon feedback --- .../GitStoreIntegrationResetTests.cs | 43 ++++++++++--------- .../GitStoreIntegrationRestoreTests.cs | 2 +- .../TestHelpers.cs | 2 +- .../ConsoleWrapper.cs} | 10 ++--- .../ConsoleWrapperTester.cs | 8 ++-- .../IConsoleWrapper.cs | 4 +- .../Azure.Sdk.Tools.TestProxy/Playback.cs | 4 +- .../Azure.Sdk.Tools.TestProxy/Startup.cs | 19 ++++---- .../Store/GitStore.cs | 28 +++++++----- .../Store/IAssetsStore.cs | 5 +-- .../Store/NullStore.cs | 4 +- 11 files changed, 69 insertions(+), 60 deletions(-) rename tools/test-proxy/Azure.Sdk.Tools.TestProxy/{ConsoleWrapper/ConsoleWrapperImpl.cs => Console/ConsoleWrapper.cs} (60%) rename tools/test-proxy/Azure.Sdk.Tools.TestProxy/{ConsoleWrapper => Console}/ConsoleWrapperTester.cs (87%) rename tools/test-proxy/Azure.Sdk.Tools.TestProxy/{ConsoleWrapper => Console}/IConsoleWrapper.cs (64%) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs index 29ea2a2a9b9..05a7c5b2100 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using Azure.Sdk.Tools.TestProxy.Common; -using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; +using Azure.Sdk.Tools.TestProxy.Console; using Azure.Sdk.Tools.TestProxy.Store; using Microsoft.Extensions.Logging; using Xunit; @@ -22,16 +22,19 @@ namespace Azure.Sdk.Tools.TestProxy.Tests.IntegrationTests // they've been restored to what they were in the original SHA. public class GitStoreIntegrationResetTests { + private GitStore _defaultStore; + private ConsoleWrapperTester _consoleWrapperTester; + // Right now, this is necessary for testing purposes but the real server won't have // this issue. public GitStoreIntegrationResetTests() { var loggerFactory = new LoggerFactory(); DebugLogger.ConfigureLogger(loggerFactory); + _consoleWrapperTester = new ConsoleWrapperTester(); + _defaultStore = new GitStore(_consoleWrapperTester); } - private GitStore _defaultStore = new GitStore(); - // Scenario 1 - Changes to existing files only are detected and overridden with Reset response Y // 1. Restore from SHA fc54d000d0427c4a68bc8962d40f957f59e14577 // 2. Expect: 3 files with versions they were checked in with @@ -39,7 +42,7 @@ public GitStoreIntegrationResetTests() // 4. Expect: files updated should be at version 2 // 5. Reset with Y // 6. Expect: each file should be at it's initial version, the version that was in the original SHA - [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + [Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")] //[Theory] [InlineData( @"{ @@ -81,8 +84,8 @@ public async Task Scenario1(string inputJson) Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); // Reset the cloned assets, reponse for overwrite = Y - ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("Y"); - await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + _consoleWrapperTester.SetReadLineResponse("Y"); + await _defaultStore.Reset(jsonFileLocation); // Verify all files have been set back to their original versions Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1)); @@ -102,7 +105,7 @@ public async Task Scenario1(string inputJson) // 4. Expect: files updated should be at version 2 // 5. Reset with N // 6. Expect: file versions should be what they were in step 4 - [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + [Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")] //[Theory] [InlineData( @"{ @@ -144,8 +147,8 @@ public async Task Scenario2(string inputJson) Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 2)); // Reset the cloned assets, reponse for overwrite = N - ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("N"); - await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + _consoleWrapperTester.SetReadLineResponse("N"); + await _defaultStore.Reset(jsonFileLocation); // Verify all files have been set back to their original versions Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); @@ -167,7 +170,7 @@ public async Task Scenario2(string inputJson) // 4. Expect: Untouched files are the same versions as step 2, added files are version 1, removed files are gone // 5. Reset with Y // 6. Expect: each file should be at it's initial version, the version that was in the original SHA - [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + [Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")] //[Theory] [InlineData( @"{ @@ -217,8 +220,8 @@ public async Task Scenario3(string inputJson) Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); // Reset the cloned assets, reponse for overwrite = Y - ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("Y"); - await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + _consoleWrapperTester.SetReadLineResponse("Y"); + await _defaultStore.Reset(jsonFileLocation); // Verify the only files there are ones from the SHA Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); @@ -241,7 +244,7 @@ public async Task Scenario3(string inputJson) // 4. Expect: Untouched files are the same versions as step 2, added files are version 1, removed files are gone // 5. Reset with N // 6. Expect: same files and same versions as step 4 - [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + [Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")] //[Theory] [InlineData( @"{ @@ -291,8 +294,8 @@ public async Task Scenario4(string inputJson) Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); // Reset the cloned assets, reponse for overwrite = N - ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("N"); - await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + _consoleWrapperTester.SetReadLineResponse("N"); + await _defaultStore.Reset(jsonFileLocation); // Verify the only files were not restored from the SHA Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); @@ -316,7 +319,7 @@ public async Task Scenario4(string inputJson) // 6. Expect: same files and same versions as step 4 // 7. Reset with Y // 8. Expect: same files and same versions as step 2 - [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + [Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")] //[Theory] [InlineData( @"{ @@ -367,8 +370,8 @@ public async Task Scenario5(string inputJson) Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); // Reset the cloned assets, reponse for overwrite = N - ConsoleWrapperTester consoleWrapperTester = new ConsoleWrapperTester("N"); - await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + _consoleWrapperTester.SetReadLineResponse("N"); + await _defaultStore.Reset(jsonFileLocation); // Verify the files were not restored from the SHA Assert.Equal(4, System.IO.Directory.EnumerateFiles(localFilePath).Count()); @@ -378,8 +381,8 @@ public async Task Scenario5(string inputJson) Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file5.txt", 1)); // Reset the cloned assets, reponse for overwrite = Y - consoleWrapperTester.SetReadLineResponse("Y"); - await _defaultStore.Reset(jsonFileLocation, consoleWrapperTester); + _consoleWrapperTester.SetReadLineResponse("Y"); + await _defaultStore.Reset(jsonFileLocation); // Verify files are from the SHA Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count()); diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs index a837de59883..db719ed4e24 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs @@ -37,7 +37,7 @@ public GitStoreIntegrationRestoreTests() // Added file2.txt // Added file3.txt // Expect: each file should be version 1 - [Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")] + [Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")] //[Theory] [InlineData( @"{ diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs index c3c2f90d6ee..2350dcb6e03 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs @@ -270,7 +270,7 @@ public static void CreateFileWithInitialVersion(string testFolder, string fileNa throw new ArgumentException(errorString); } - File.WriteAllText(fullFileName, 1.ToString()); + File.WriteAllText(fullFileName, "1"); } } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapper.cs similarity index 60% rename from tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs rename to tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapper.cs index 0f7a08e2570..ec1a9283348 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperImpl.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapper.cs @@ -1,24 +1,24 @@  using System; -namespace Azure.Sdk.Tools.TestProxy.ConsoleWrapper +namespace Azure.Sdk.Tools.TestProxy.Console { /// /// Implementation of IConsoleWrapper that's simply a passthrough to the Console functions. /// - public class ConsoleWrapperImpl : IConsoleWrapper + public class ConsoleWrapper : IConsoleWrapper { public void Write(string message) { - Console.Write(message); + System.Console.Write(message); } public void WriteLine(string message) { - Console.WriteLine(message); + System.Console.WriteLine(message); } public string ReadLine() { - return Console.ReadLine(); + return System.Console.ReadLine(); } } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapperTester.cs similarity index 87% rename from tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs rename to tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapperTester.cs index 47ad8c1b358..c032419ff5a 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/ConsoleWrapperTester.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapperTester.cs @@ -1,6 +1,6 @@ using System; -namespace Azure.Sdk.Tools.TestProxy.ConsoleWrapper +namespace Azure.Sdk.Tools.TestProxy.Console { /// /// Implementation of IConsoleWrapper that will be used to test commands, like Reset, that require user input. @@ -9,6 +9,8 @@ public class ConsoleWrapperTester : IConsoleWrapper { private string _readLineResponse; + public ConsoleWrapperTester() { } + /// /// Overloaded constructor takes in a string that'll be returned as the ReadLine response. /// @@ -28,11 +30,11 @@ public void SetReadLineResponse(string readLineResponse) } public void Write(string message) { - Console.Write(message); + System.Console.Write(message); } public void WriteLine(string message) { - Console.WriteLine(message); + System.Console.WriteLine(message); } public string ReadLine() { diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/IConsoleWrapper.cs similarity index 64% rename from tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs rename to tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/IConsoleWrapper.cs index 9d3f7e7e32a..04bcd2cbced 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/ConsoleWrapper/IConsoleWrapper.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/IConsoleWrapper.cs @@ -1,7 +1,7 @@ -namespace Azure.Sdk.Tools.TestProxy.ConsoleWrapper +namespace Azure.Sdk.Tools.TestProxy.Console { /// - /// IConsoleWrapper is just an interace around Console functions. This is necessary for testing + /// IConsoleWrapper is just an interface around Console functions. This is necessary for testing /// functions, like Reset, which require user input that we need to be able to control. /// public interface IConsoleWrapper diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs index c07168dbaea..d0ad04f016e 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Playback.cs @@ -3,7 +3,6 @@ using Azure.Sdk.Tools.TestProxy.Common; using Azure.Sdk.Tools.TestProxy.Common.Exceptions; -using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; using Azure.Sdk.Tools.TestProxy.Store; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -63,8 +62,7 @@ public async Task Reset([FromBody()] IDictionary options = null) var pathToAssets = StoreResolver.ParseAssetsJsonBody(options); - ConsoleWrapperImpl consoleWrapper = new ConsoleWrapperImpl(); - await _recordingHandler.Store.Reset(pathToAssets, consoleWrapper); + await _recordingHandler.Store.Reset(pathToAssets); } [HttpPost] diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs index 47b6fb19a82..54bf983e336 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs @@ -17,7 +17,7 @@ using System.Reflection; using Microsoft.AspNetCore.Server.Kestrel.Core; using Azure.Sdk.Tools.TestProxy.Store; -using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; +using Azure.Sdk.Tools.TestProxy.Console; namespace Azure.Sdk.Tools.TestProxy { @@ -60,7 +60,7 @@ public static void Main(bool insecure = false, string storageLocation = null, st var semanticVersion = assembly.GetCustomAttribute().InformationalVersion; var assemblyVersion = assembly.GetName().Version; - Console.WriteLine($"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}-dev.{semanticVersion}"); + System.Console.WriteLine($"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}-dev.{semanticVersion}"); Environment.Exit(0); } @@ -80,8 +80,7 @@ public static void Main(bool insecure = false, string storageLocation = null, st DefaultStore.Restore(assetsJsonPath); break; case "reset": - ConsoleWrapperImpl consoleWrapper = new ConsoleWrapperImpl(); - DefaultStore.Reset(assetsJsonPath, consoleWrapper); + DefaultStore.Reset(assetsJsonPath); break; default: throw new Exception($"One must provide a valid value for argument \"command\". \"{command}\" is not a valid option."); @@ -125,12 +124,12 @@ public static void Main(bool insecure = false, string storageLocation = null, st if (dump) { var config = app.Services?.GetService(); - Console.WriteLine("Dumping Resolved Configuration Values:"); + System.Console.WriteLine("Dumping Resolved Configuration Values:"); if (config != null) { foreach (var c in config.AsEnumerable()) { - Console.WriteLine(c.Key + " = " + c.Value); + System.Console.WriteLine(c.Key + " = " + c.Value); } } } @@ -240,21 +239,21 @@ private static Thread PrintStatus(Func status, bool newLine, Cancellatio if (newLine) { - Console.WriteLine(obj); + System.Console.WriteLine(obj); } else { - Console.Write(obj); + System.Console.Write(obj); needsExtraNewline = true; } } if (needsExtraNewline) { - Console.WriteLine(); + System.Console.WriteLine(); } - Console.WriteLine(); + System.Console.WriteLine(); }); thread.Start(); diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs index 9b2c98de1da..984459be280 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs @@ -10,7 +10,7 @@ using System.Linq; using Azure.Sdk.Tools.TestProxy.Common.Exceptions; using Azure.Sdk.Tools.TestProxy.Common; -using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; +using Azure.Sdk.Tools.TestProxy.Console; namespace Azure.Sdk.Tools.TestProxy.Store { @@ -27,11 +27,20 @@ public class DirectoryEvaluation public class GitStore : IAssetsStore { private HttpClient httpClient = new HttpClient(); + private IConsoleWrapper _consoleWrapper; public GitProcessHandler GitHandler = new GitProcessHandler(); public string DefaultBranch = "main"; public string FileName = "assets.json"; - public GitStore() { } + public GitStore() + { + _consoleWrapper = new ConsoleWrapper(); + } + + public GitStore(IConsoleWrapper consoleWrapper) + { + _consoleWrapper = consoleWrapper; + } public GitStore(GitProcessHandler processHandler) { GitHandler = processHandler; @@ -120,13 +129,12 @@ public async Task Restore(string pathToAssetsJson) { } /// - /// Resets a cloned assets repository to the default contained within the assets.json targeted commit. + /// Resets a cloned assets repository to the default contained within the assets.json targeted commit. This + /// function should only be called by the user as the server will only use Restore. /// /// - /// /// - // This should only ever be called by the user? - public async Task Reset(string pathToAssetsJson, IConsoleWrapper consoleWrapper) + public async Task Reset(string pathToAssetsJson) { var config = await ParseConfigurationFile(pathToAssetsJson); var initialized = config.IsAssetsRepoInitialized(); @@ -141,11 +149,11 @@ public async Task Reset(string pathToAssetsJson, IConsoleWrapper consoleWrapper) if (pendingChanges.Length > 0) { - consoleWrapper.WriteLine("There are pending git chances, are you sure you want to reset? [Y|N]"); + _consoleWrapper.WriteLine("There are pending git chances, are you sure you want to reset? [Y|N]"); while (true) { - string response = consoleWrapper.ReadLine(); - response = response.ToLower(); + string response = _consoleWrapper.ReadLine(); + response = response.ToLowerInvariant(); if (response.Equals("y")) { allowReset = true; @@ -158,7 +166,7 @@ public async Task Reset(string pathToAssetsJson, IConsoleWrapper consoleWrapper) } else { - consoleWrapper.WriteLine("Please answer [Y|N]"); + _consoleWrapper.WriteLine("Please answer [Y|N]"); } } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs index c4f2de0b316..161facea94e 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs @@ -1,6 +1,6 @@ using System.IO; using System.Threading.Tasks; -using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; +using Azure.Sdk.Tools.TestProxy.Console; namespace Azure.Sdk.Tools.TestProxy.Store { @@ -22,7 +22,6 @@ public interface IAssetsStore /// Given a configuration, determine the state of the resources present under contextPath, reset those resources to their "fresh" state. /// /// - /// - public abstract Task Reset(string pathToAssetsJson, IConsoleWrapper consoleWrapper); + public abstract Task Reset(string pathToAssetsJson); } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs index c221d6b6677..12f7a4c1318 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/NullStore.cs @@ -2,7 +2,7 @@ using System.Net; using System.Threading.Tasks; using Azure.Sdk.Tools.TestProxy.Common.Exceptions; -using Azure.Sdk.Tools.TestProxy.ConsoleWrapper; +using Azure.Sdk.Tools.TestProxy.Console; namespace Azure.Sdk.Tools.TestProxy.Store { @@ -12,7 +12,7 @@ public class NullStore : IAssetsStore public Task Restore(string assetsJsonPath) { return null; } - public Task Reset(string assetsJsonPath, IConsoleWrapper consoleWrapper) { return null; } + public Task Reset(string assetsJsonPath) { return null; } public AssetsConfiguration ParseConfigurationFile(string assetsJsonPath) {