public void GetTestSet_OnePathAndOneMappingWithMatchingPath_ReturnsTestSetMatchingPath() { //declarations HashSet <String> paths = new HashSet <String>() { "src/path1/fullpath" }; Dictionary <String, String[]> map = new Dictionary <String, String[]>() { { "src/path1", new String[] { "test1.dll" } } }; HashSet <String> expected = new HashSet <String>() { "test1.dll" }; HashSet <String> actual = new HashSet <String>(); //act actual = TestSetGenerator.GetTestSet(paths, map); //assert Assert.True(expected.SetEquals(actual)); }
public void GetTestSet_OnePathAndMultipleMappingsWithNonMatchingPaths_ReturnsAllTests() { //arrange HashSet <String> paths = new HashSet <String>() { "src/random" }; Dictionary <String, String[]> map = new Dictionary <String, String[]>() { { "src/path1", new String[] { "test1.dll" } }, { "src/path2", new String[] { "test2.dll" } }, { "src/path3", new String[] { "test2.dll", "test3.dll" } } }; HashSet <String> expected = new HashSet <String>() { "test1.dll", "test2.dll", "test3.dll" }; HashSet <String> actual = new HashSet <String>(); //act actual = TestSetGenerator.GetTestSet(paths, map); //assert Assert.True(expected.SetEquals(actual)); }
public void GetTests_WithActualMappings_FilesFound_ReturnsMatchingTests() { //arrange HashSet <string> paths = new HashSet <string>() { ".github", "documentation", "src/ServiceManagement/file", "src/ResourceManager/LogicApp/file", "src/ResourceManager/UsageAggregates/file" }; string mapFilePath = MapFilePath; HashSet <string> expected = new HashSet <string>() { @".\src\ServiceManagement\Common\Commands.Common.Test\bin\Debug\Microsoft.WindowsAzure.Commands.Common.Test.dll", @".\src\ServiceManagement\Services\Commands.Test\bin\Debug\Microsoft.WindowsAzure.Commands.Test.dll", @".\src\ServiceManagement\StorSimple\Commands.StorSimple.Test\bin\Debug\Microsoft.WindowsAzure.Commands.StorSimple.Test.dll", @".\src\ServiceManagement\Common\Commands.ScenarioTest\bin\Debug\Microsoft.WindowsAzure.Commands.ScenarioTest.dll", @".\src\ServiceManagement\RecoveryServices\Commands.RecoveryServices.Test\bin\Debug\Microsoft.Azure.Commands.RecoveryServices.Test.dll", @".\src\ServiceManagement\Network\Commands.Network.Test\bin\Debug\Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.dll", @".\src\ResourceManager\UsageAggregates\Commands.UsageAggregates.Test\bin\Debug\Microsoft.Azure.Commands.UsageAggregates.Test.dll", @".\src\ResourceManager\LogicApp\Commands.LogicApp.Test\bin\Debug\Microsoft.Azure.Commands.LogicApp.Test.dll" }; IEnumerable <string> actual; //act actual = TestSetGenerator.GetTests(paths, mapFilePath); //assert Assert.True(expected.SetEquals(actual)); }
public void GetTestSet_SomePathIsNull_ShouldThrowArgumentNullException() { //arrange HashSet <String> paths = new HashSet <String>() { null, "random2", "random3" }; Dictionary <String, String[]> map = new Dictionary <String, String[]>() { { "src/path1", new String[] { "test1.dll" } }, { "src/path2", new String[] { } }, { "path3", new String[] { "test2.dll", "test3.dll" } } }; //act try { TestSetGenerator.GetTestSet(paths, map); } catch (ArgumentNullException e) { // assert Assert.Contains("One or more of the paths provided are null.", e.Message); return; } throw new Exception("No exception was thrown."); }
public void GetTests_InvalidPath_ThrowNotNullException() { //arrange HashSet <string> paths = new HashSet <string>() { "src /path1", "src/path2", "random3" }; string mapFilePath = @"random"; try { TestSetGenerator.GetTests(paths, mapFilePath); } catch (System.IO.FileNotFoundException e) { // assert Assert.Contains("The filepath provided for the map could not be found.", e.Message); return; } throw new Exception("No exception was thrown."); }
public void GetTests_MultiplePathsAndMultipleMappingsWithSomeMatchingPaths_ReturnsAllTests() { //arrange HashSet <string> paths = new HashSet <string>() { "src/ResourceManager/StreamAnalytics/", "src/path2", "random3" }; string mapFilePath = MapFilePath; HashSet <string> expected = new HashSet <string>() { "test1.dll", "test2.dll", "test3.dll" }; int expectedNumberFiles = 53; HashSet <string> actual; //act actual = (HashSet <string>)(TestSetGenerator.GetTests(paths, mapFilePath)); //assert Assert.True(expectedNumberFiles <= actual.Count); }
public void GetTestSet_NoMappingsProvided_ShouldThrowArgumentException() { //arrange HashSet <String> paths = new HashSet <String>() { "random1", "random2", "random3" }; Dictionary <String, String[]> map = new Dictionary <String, String[]>(); //act try { TestSetGenerator.GetTestSet(paths, map); } catch (ArgumentException e) { // assert Assert.Contains(e.Message, "Map does not contain any element."); return; } throw new Exception("No exception was thrown."); }
/// <summary> /// Static method used to generate a set of tests to be run based on /// a Json file which maps files to test Dlls. /// </summary> /// <param name="files">This is a set of paths.</param> /// <param name="mapFilePath">This is the filepath of the map that contains /// the mapping between files and test DLLs.</param> /// <returns>Set of tests to be run</returns> public static IEnumerable <string> GetTests(IEnumerable <string> files, string mapFilePath) { if (mapFilePath == null) { throw new ArgumentNullException("The filepath of the map should never be null."); } if (files == null) { throw new ArgumentNullException("The files should never be null."); } HashSet <string> paths = new HashSet <string>(files); Dictionary <string, string[]> pathToTestsMappings; try { pathToTestsMappings = JsonConvert.DeserializeObject <Dictionary <string, string[]> >(File.ReadAllText(mapFilePath)); } catch (FileNotFoundException) { throw new FileNotFoundException("The filepath provided for the map could not be found. Please provide a valid filepath."); } return(TestSetGenerator.GetTestSet(paths, pathToTestsMappings)); }
public void GetTests_EmptyListOfFiles_ShouldReturnAllTests() { //arrange HashSet <string> paths = new HashSet <string>() { }; string mapFilePath = MapFilePath; int expectedNumberFiles = 53; HashSet <string> actual; //act actual = (HashSet <string>)(TestSetGenerator.GetTests(paths, mapFilePath)); //assert Assert.True(expectedNumberFiles <= actual.Count); }
public void GetTests_FilesNull_ThrowNullException() { //arrange string mapFilePath = MapFilePath; try { TestSetGenerator.GetTests(null, mapFilePath); } catch (ArgumentNullException e) { // assert Assert.Contains("The files should never be null.", e.Message); return; } throw new Exception("No exception was thrown."); }
public void GetTests_MultiplePathsAndMultipleMappingsWithMatchingPaths_ReturnsMatchingTests() { //arrange HashSet <string> paths = new HashSet <string>() { "src/ResourceManager/StreamAnalytics/file", "src/ResourceManager/Websites/" }; string mapFilePath = MapFilePath; int expectedNumberFiles = 2; HashSet <string> actual; //act actual = (HashSet <string>)(TestSetGenerator.GetTests(paths, mapFilePath)); //assert Assert.True(expectedNumberFiles == actual.Count); }
public void GetTests_WithActualMappings_FilesNotFound_ReturnsAllTests() { //arrange HashSet <string> paths = new HashSet <string>() { ".github", "documentation", "random1", "random2", "random3" }; string mapFilePath = MapFilePath; int expectedNumberFiles = 53; HashSet <string> actual; //act actual = (HashSet <string>)(TestSetGenerator.GetTests(paths, mapFilePath)); //assert Assert.True(expectedNumberFiles <= actual.Count); }
public void GetTestSet_NoPathsProvided_ReturnsAllTests() { //arrange HashSet <String> paths = new HashSet <String>() { }; Dictionary <String, String[]> map = new Dictionary <String, String[]>() { { "src/path1", new String[] { "test1.dll" } }, { "src/path2", new String[] { } }, { "path3", new String[] { "test2.dll", "test3.dll" } } }; int expectedNumberFiles = 3; HashSet <string> actual; //act actual = TestSetGenerator.GetTestSet(paths, map); //assert Assert.True(expectedNumberFiles == actual.Count); }
public void GetTestSet_MapNullArgument_ShouldThrowArgumentNullException() { //arrange HashSet <String> paths = new HashSet <String>() { "random1", "random2", "random3" }; //act try { TestSetGenerator.GetTestSet(paths, null); } catch (ArgumentNullException e) { // assert Assert.Contains("Mapping should never be null.", e.Message); return; } throw new Exception("No exception was thrown."); }
public void GetTestSet_PathsNullArgument_ShouldThrowArgumentNullException() { //arrange Dictionary <String, String[]> map = new Dictionary <String, String[]>() { { "src/path1", new String[] { "test1.dll" } }, { "src/path2", new String[] { } }, { "path3", new String[] { "test2.dll", "test3.dll" } } }; //act try { TestSetGenerator.GetTestSet(null, map); } catch (ArgumentNullException e) { // assert Assert.Contains("Paths set should never be null.", e.Message); return; } throw new Exception("No exception was thrown."); }
/// <summary> /// Executes the task to generate a list of test assemblies /// based on file changes from a specified Pull Request. /// The output it produces is said list. /// </summary> /// <returns> Returns a value indicating wheter the success status of the task. </returns> public override bool Execute() { // validate parameters if (RepositoryOwner == null) { throw new ArgumentNullException("The RepositoryOwner cannot be null."); } if (RepositoryName == null) { throw new ArgumentNullException("The RepositoryName cannot be null."); } if (MapFilePath == null) { throw new ArgumentNullException("The MapFilePath cannot be null."); } var debugEnvironmentVariable = Environment.GetEnvironmentVariable("DebugLocalBuildTasks"); bool debug; if (!Boolean.TryParse(debugEnvironmentVariable, out debug)) { debug = false; } int ParsedPullRequestNumber; // The next statement will convert the string representation of a number to its integer equivalent. // If it succeeds it will return 'true'. if (int.TryParse(PullRequestNumber, out ParsedPullRequestNumber)) { List <string> filesChanged = new List <string>(); Collection <PSObject> psOutput = new Collection <PSObject>(); var GetFilesScript = File.ReadAllText(ScriptFilePath); PowerShell powerShell = PowerShell.Create(); powerShell.AddScript(GetFilesScript); if (debug) { powerShell.AddScript("$DebugPreference=\"Continue\""); } powerShell.AddScript($"Get-PullRequestFileChanges " + $"-RepositoryOwner {RepositoryOwner} " + $"-RepositoryName {RepositoryName} " + $"-PullRequestNumber {ParsedPullRequestNumber}"); powerShell.Streams.Debug.Clear(); try { if (debug) { Console.WriteLine("DEBUG: ---Starting PS script to detect file changes..."); } psOutput = powerShell.Invoke(); if (debug) { foreach (var debugRecord in powerShell.Streams.Debug) { Console.WriteLine("[PS]DEBUG: " + debugRecord.ToString()); } } if (psOutput == null) { return(false); } if (debug) { Console.WriteLine("DEBUG: ---Using these files: "); } foreach (var element in psOutput) { var filename = element.ToString(); if (debug) { Console.WriteLine("DEBUG: " + filename); } filesChanged.Add(filename); } if (debug) { Console.WriteLine("Total: " + filesChanged.Count); } } catch (Exception e) { Console.WriteLine("---Exception Caught when trying to detect file changes with PS script: " + e.ToString()); } TestAssemblies = new List <string>(TestSetGenerator.GetTests(filesChanged, MapFilePath)).ToArray(); } else { TestAssemblies = new List <string>(TestSetGenerator.GetTests(MapFilePath)).ToArray(); } return(true); }