-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Geims83/features/spectre
Switch from System.Commandline to Spectre.Console
- Loading branch information
Showing
9 changed files
with
318 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Data.SqlClient; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using qest.Models; | ||
using YamlDotNet.Serialization; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
|
||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
namespace qest.Commands | ||
{ | ||
internal static class Run | ||
{ | ||
|
||
internal static void Execute(FileInfo? file, DirectoryInfo? folder, string tcs) | ||
internal sealed class RunCommand : AsyncCommand<RunCommand.Settings> | ||
{ | ||
public sealed class Settings : CommandSettings | ||
{ | ||
List<Test> TestCollection = new List<Test>(); | ||
[Description("Test file")] | ||
[CommandOption("-f|--file <FILE>")] | ||
public string? File { get; init; } | ||
|
||
if (file is not null) | ||
{ | ||
TestCollection.AddRange(SafeReadYaml(file)); | ||
} | ||
else if (folder is not null) | ||
{ | ||
foreach (var item in folder.EnumerateFiles().Where(f => f.Extension == ".yml" || f.Extension == ".yaml")) | ||
TestCollection.AddRange(SafeReadYaml(item)); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("One parameter between --file or --folder is mandatory."); | ||
Environment.Exit(1); | ||
} | ||
[Description("Folder containing test files")] | ||
[CommandOption("-d|--folder <FOLDER>")] | ||
public string? Folder { get; init; } | ||
|
||
[Description("Target connection string")] | ||
[CommandOption("-c|--tcs <TARGETCONNECTIONSTRING>")] | ||
public string ConnectionString { get; init; } | ||
|
||
if (TestCollection.Count == 0) | ||
public override ValidationResult Validate() | ||
{ | ||
Console.WriteLine("No test loaded"); | ||
Environment.Exit(1); | ||
} | ||
if (Folder is null && File is null) | ||
{ | ||
return ValidationResult.Error("One parameter between FILE or FOLDER must be supplied."); | ||
} | ||
|
||
var sqlConnection = new SqlConnection(tcs); | ||
List<Test> testsToRun = new(); | ||
|
||
foreach (var test in TestCollection) | ||
{ | ||
test.Connection = sqlConnection; | ||
bool pass = test.Run(); | ||
if (!pass) | ||
Environment.Exit(1); | ||
if (File is not null) | ||
{ | ||
FileInfo fileToLoad = new FileInfo(File); | ||
if (!fileToLoad.Exists) | ||
return ValidationResult.Error("File specified does not exist."); | ||
else | ||
testsToRun.AddRange(Utils.SafeReadYaml(fileToLoad)); | ||
} | ||
|
||
if (Folder is not null) | ||
{ | ||
DirectoryInfo folderToLoad = new DirectoryInfo(Folder); | ||
if (!folderToLoad.Exists) | ||
return ValidationResult.Error("Folder specified does not exist."); | ||
else | ||
foreach (var fileToLoad in folderToLoad.EnumerateFiles().Where(f => f.Extension == ".yml" || f.Extension == ".yaml")) | ||
testsToRun.AddRange(Utils.SafeReadYaml(fileToLoad)); | ||
} | ||
|
||
if (!testsToRun.Any()) | ||
return ValidationResult.Error("No tests found in file or folder."); | ||
|
||
var result = Validators.ValidateConnectionString(ConnectionString); | ||
|
||
return result; | ||
} | ||
Environment.Exit(0); | ||
|
||
} | ||
|
||
static List<Test> SafeReadYaml(FileInfo file) | ||
private SqlConnection? TargetSqlConnection; | ||
|
||
public override async Task<int> ExecuteAsync([NotNull] CommandContext context, [NotNull] Settings settings) | ||
{ | ||
List<Test> list = new List<Test>(); | ||
|
||
var deserializer = new DeserializerBuilder() | ||
.WithNamingConvention(CamelCaseNamingConvention.Instance) | ||
.Build(); | ||
List<Test> TestCollection = new(); | ||
|
||
try | ||
if (settings.File is not null) | ||
{ | ||
using var stream = new StreamReader(file.FullName); | ||
string yaml = stream.ReadToEnd(); | ||
list.AddRange(deserializer.Deserialize<List<Test>>(yaml)); | ||
FileInfo fileToLoad = new FileInfo(settings.File); | ||
|
||
TestCollection.AddRange(await Utils.SafeReadYamlAsync(fileToLoad)); | ||
} | ||
else if (settings.Folder is not null) | ||
{ | ||
DirectoryInfo folderToLoad = new DirectoryInfo(settings.Folder); | ||
foreach (var item in folderToLoad.EnumerateFiles().Where(f => f.Extension == ".yml" || f.Extension == ".yaml")) | ||
TestCollection.AddRange(await Utils.SafeReadYamlAsync(item)); | ||
} | ||
catch (Exception ex) | ||
|
||
AnsiConsole.MarkupLine($"[grey]{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}[/] {TestCollection.Count} tests loaded."); | ||
|
||
TargetSqlConnection = new SqlConnection(settings.ConnectionString); | ||
|
||
int exitCode = 0; | ||
|
||
foreach (var test in TestCollection) | ||
{ | ||
Console.WriteLine($"Error deserializing {file.FullName}: {ex.Message}"); | ||
|
||
test.Connection = TargetSqlConnection; | ||
bool pass = await test.RunAsync(); | ||
|
||
if (!pass) | ||
{ | ||
exitCode = 1; | ||
break; | ||
} | ||
} | ||
return list; | ||
|
||
return exitCode; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Data.SqlClient; | ||
using Spectre.Console; | ||
|
||
namespace qest.Commands | ||
{ | ||
internal static class Validators | ||
{ | ||
internal static ValidationResult ValidateConnectionString(string ConnectionString) | ||
{ | ||
if (ConnectionString is null || ConnectionString.Length < Utils.ConnectionStringBarebone.Length) | ||
return ValidationResult.Error("Connection string not supplied or too short (are you missing any keyword?)"); | ||
else | ||
{ | ||
var sqlConnection = new SqlConnection(ConnectionString); | ||
try | ||
{ | ||
sqlConnection.Open(); | ||
sqlConnection.Close(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
AnsiConsole.WriteException(ex); | ||
return ValidationResult.Error("Connection to the targed database failed."); | ||
} | ||
} | ||
|
||
return ValidationResult.Success(); | ||
} | ||
} | ||
} |
Oops, something went wrong.