|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using CommandLine; |
| 7 | + |
| 8 | +namespace OrderedTestFileGenerator |
| 9 | +{ |
| 10 | + class InputOptions |
| 11 | + { |
| 12 | + [OptionArray('a', "assemblypaths", Required = true, HelpText = "The file paths to the assembly DLLs containing the mstests.")] |
| 13 | + public string[] AssemblyFilePathsRaw { get; set; } |
| 14 | + |
| 15 | + public IList<FileInfo> AssemblyFilePaths |
| 16 | + { |
| 17 | + get |
| 18 | + { |
| 19 | + if (AssemblyFilePathsRaw == null) return null; |
| 20 | + var paths = AssemblyFilePathsRaw.Select(x => TryParseFileInfo(x)).ToList(); |
| 21 | + return paths.Any(x => x == null) ? null : paths; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + [Option('o', "outputpath", Required = true, HelpText = "The file path be to used for the generated Ordered Test file.")] |
| 26 | + public string OutputFilePathRaw { get; set; } |
| 27 | + |
| 28 | + public FileInfo OutputFilePath |
| 29 | + { |
| 30 | + get { return TryParseFileInfo(OutputFilePathRaw); } |
| 31 | + } |
| 32 | + |
| 33 | + [OptionList('c', "categories", Separator = ';', HelpText = "The sequence of categories for ordered tests (semi-colon separated). Either categories or categoriesfile required.")] |
| 34 | + public IList<string> Categories { get; set; } |
| 35 | + |
| 36 | + [Option('x', "categoriesfile", HelpText = "The file path of a linebreak separated list of categories.")] |
| 37 | + public string CategoriesFilePathRaw { get; set; } |
| 38 | + |
| 39 | + public FileInfo CategoriesFilePath |
| 40 | + { |
| 41 | + get { return TryParseFileInfo(CategoriesFilePathRaw); } |
| 42 | + } |
| 43 | + |
| 44 | + [Option('p', "appendorphans", DefaultValue = false, HelpText = "Specify to append tests which don't match any of the specified categories to the end of the ordered test file.")] |
| 45 | + public bool AppendOrphans { get; set; } |
| 46 | + |
| 47 | + public string GetHelp() |
| 48 | + { |
| 49 | + return new StringBuilder() |
| 50 | + .AppendLine("Usage:") |
| 51 | + .AppendLine("-a The file paths to the assembly DLLs containing the mstests. Required.") |
| 52 | + .AppendLine("-o The file path be to used for the generated Ordered Test file. Required.") |
| 53 | + .AppendLine("Categories (at least one must be specified):") |
| 54 | + .AppendLine("-c The sequence of categories for ordered tests (semi-colon separated).") |
| 55 | + .AppendLine("-x The file path of a linebreak separated list of categories.") |
| 56 | + .AppendLine() |
| 57 | + .AppendLine("-p Specify to append tests which don't match any of the specified categories to the end of the ordered test file.") |
| 58 | + .AppendLine() |
| 59 | + |
| 60 | + .ToString(); |
| 61 | + } |
| 62 | + |
| 63 | + public string GetErrors() |
| 64 | + { |
| 65 | + StringBuilder sb = new StringBuilder(); |
| 66 | + if (!IsValidAssemblyFilePaths) |
| 67 | + { |
| 68 | + sb.AppendLine("- Invalid assembly path(s)"); |
| 69 | + } |
| 70 | + if (!IsValidOutputFilePath) |
| 71 | + { |
| 72 | + sb.AppendLine("- Invalid output path"); |
| 73 | + } |
| 74 | + if (!IsValidCategoryFilePath) |
| 75 | + { |
| 76 | + sb.AppendLine("- Invalid categories path"); |
| 77 | + } |
| 78 | + else if (!IsValidCategoryOption) |
| 79 | + { |
| 80 | + sb.AppendLine("- No categories specified"); |
| 81 | + } |
| 82 | + |
| 83 | + if (sb.Length != 0) sb.Insert(0, "Errors:" + Environment.NewLine); |
| 84 | + return sb.ToString(); |
| 85 | + } |
| 86 | + |
| 87 | + internal bool IsValidAssemblyFilePaths |
| 88 | + { |
| 89 | + get { return AssemblyFilePaths != null && AssemblyFilePaths.Count != 0 && AssemblyFilePaths.All(x => x.Exists); } |
| 90 | + } |
| 91 | + |
| 92 | + internal bool IsValidOutputFilePath |
| 93 | + { |
| 94 | + get { return OutputFilePath != null && OutputFilePath.Exists; } |
| 95 | + } |
| 96 | + |
| 97 | + internal bool IsValidCategoryFilePath |
| 98 | + { |
| 99 | + get { return CategoriesFilePath == null || CategoriesFilePath.Exists; } |
| 100 | + } |
| 101 | + |
| 102 | + internal bool IsValidCategoryOption |
| 103 | + { |
| 104 | + get { return CategoriesFilePath != null || (Categories != null && Categories.Count != 0); } |
| 105 | + } |
| 106 | + |
| 107 | + internal FileInfo TryParseFileInfo(string fullName) |
| 108 | + { |
| 109 | + if (string.IsNullOrWhiteSpace(fullName)) return null; |
| 110 | + try |
| 111 | + { |
| 112 | + return new FileInfo(fullName); |
| 113 | + } |
| 114 | + catch (NotSupportedException) |
| 115 | + { |
| 116 | + return null; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments