public void IsPackageTest() { //Arrange var TestString = "A,1"; //Act var packageInstallerService = new PackageInstallerService(); var TestResult = packageInstallerService.IsPackage(TestString); //Assert Assert.IsTrue(TestResult); }
//The goal of this method is to read the data into an object for organized processing. //It's long and ugly...if I had more time, then I'd brainstorm ways of refactoring it. static void ProcessInputFile(string filePath, Installation installation, PackageInstallerService packageInstallerService) { var FileText = ReadFileIntoText(filePath); var PackageCount = 0; var DependencyCount = 0; foreach (var line in FileText) { try //Attempt to read the line, but don't allow the entire list to be thrown out from one bad line, so try/catch each iteration. { //If the line is a number, then it's either the number of packages to install or the number of dependencies to handle. if (int.TryParse(line, out int LineConvertedToNumber)) { if (PackageCount == 0) { PackageCount = LineConvertedToNumber; //This sort of acts as a boolean switch for the first line in the file, applied to the IF condition. } else { DependencyCount = LineConvertedToNumber; } } else { //This code block is reached when the line is either a package or a dependency list. //The quickest way forward, assuming a consistent format, is to look at the Count variables. //Either way, we are going to need to split the line into a string array. var LineArray = line.Split(","); if (PackageCount > 0) { //Validate that we are working with a package. //This Regex function will check to make sure there are two elements in LineArray, so using LineArray.Count() == 2 is redundant and less valid. if (!packageInstallerService.IsPackage(line)) { throw new Exception( "An invalid package was sent to the input file processor. Package count: " + PackageCount.ToString() + " Line: " + line); } //This line is going to be a package. var version = int.Parse(LineArray.ElementAt(1)); //We already know from the Regex above that this will be \d. var NewPackage = new Package(LineArray.ElementAt(0), version); installation.Packages.Add(NewPackage); //One of our primary packages is added. PackageCount--; //We subtract one package as it has been recorded for processing. continue; //Go to the next iteration since we are done with this line. } if (DependencyCount > 0) { //A package line will be in the format A,1...so that means there will only be one comma, whereas a dependency list will have multiple. if (!IsDependency(line)) { throw new Exception( "An invalid dependency list was sent to the input file processor. Package count: " + PackageCount.ToString() + " Line: " + line); } //LineArray will be a list of packages. //We need to look for the first package and append this dependency to that package. var ParentPackage = installation.Packages.FirstOrDefault(x => x.Name == LineArray.ElementAt(0) && x.Version == int.Parse(LineArray.ElementAt(1))); if (ParentPackage != null) { //Add dependencies to this package. Start at 2 because we skip the first package in the line as it is parent. for (var i = 2; i < LineArray.Count(); i += 2) { var NewDependency = new Package(LineArray.ElementAt(i), int.Parse(LineArray.ElementAt(i + 1))); ParentPackage.Dependencies.Add(NewDependency); } } else { //No parent package, just a dependency to track. var NewDependencyPackage = new Package(LineArray.ElementAt(0), int.Parse(LineArray.ElementAt(1))); for (var i = 2; i < LineArray.Count(); i += 2) { var NewDependency = new Package(LineArray.ElementAt(i), int.Parse(LineArray.ElementAt(i + 1))); NewDependencyPackage.Dependencies.Add(NewDependency); } installation.DormantDependencies.Add(NewDependencyPackage); } DependencyCount--; continue; } } } catch (Exception e) { WriteTextIntoFile(LogFileLocation, "Error_" + Guid.NewGuid() + ".txt", e.ToString()); } } }