From 1d496fc778ab8bce6b4f940be4cce14001e385b3 Mon Sep 17 00:00:00 2001 From: Ivan Paulovich Date: Mon, 15 Apr 2019 21:47:11 +0200 Subject: [PATCH] Acceptance tests --- .vscode/launch.json | 2 +- TodoList.sln | 15 +++++++++ .../Commands/CommandParser.cs | 1 + .../Commands/DevelopmentModeCommand.cs | 24 ++++++++++++++ .../Controllers/TodoItemsController.cs | 16 ++++++++++ .../GistGateway/GistItemGateway.cs | 32 +++++++++++-------- .../TodoList.AcceptanceTests/ConsoleTests.cs | 22 +++++++++++++ .../TodoList.AcceptanceTests.csproj | 6 ++-- 8 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 source/TodoList.ConsoleApp/Commands/DevelopmentModeCommand.cs create mode 100644 tests/TodoList.AcceptanceTests/ConsoleTests.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 3c2bb71..ad68c66 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -30,7 +30,7 @@ "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/source/TodoList.ConsoleApp/bin/Debug/netcoreapp2.2/todo.dll", - "args": ["i"], + "args": ["dm"], "cwd": "${workspaceFolder}/source/TodoList.ConsoleApp", // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window "console": "integratedTerminal", diff --git a/TodoList.sln b/TodoList.sln index 40db155..60bb9dc 100644 --- a/TodoList.sln +++ b/TodoList.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.Infrastructure", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.IntegrationTests", "tests\TodoList.IntegrationTests\TodoList.IntegrationTests.csproj", "{0A9077E0-1B39-4F02-A3F0-54A3680A7056}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.AcceptanceTests", "tests\TodoList.AcceptanceTests\TodoList.AcceptanceTests.csproj", "{AC22743B-52E2-49A0-9C79-09E67009F872}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -104,6 +106,18 @@ Global {0A9077E0-1B39-4F02-A3F0-54A3680A7056}.Release|x64.Build.0 = Release|Any CPU {0A9077E0-1B39-4F02-A3F0-54A3680A7056}.Release|x86.ActiveCfg = Release|Any CPU {0A9077E0-1B39-4F02-A3F0-54A3680A7056}.Release|x86.Build.0 = Release|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Debug|x64.ActiveCfg = Debug|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Debug|x64.Build.0 = Debug|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Debug|x86.ActiveCfg = Debug|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Debug|x86.Build.0 = Debug|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Release|Any CPU.Build.0 = Release|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Release|x64.ActiveCfg = Release|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Release|x64.Build.0 = Release|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Release|x86.ActiveCfg = Release|Any CPU + {AC22743B-52E2-49A0-9C79-09E67009F872}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {96C25576-F135-4E24-B791-2C311335760C} = {29821818-FE38-42A7-BFDB-919FDE2CD2F8} @@ -112,5 +126,6 @@ Global {54FC9A81-37E4-4F7B-9E56-75FA0004E0CD} = {29821818-FE38-42A7-BFDB-919FDE2CD2F8} {555C3909-D08A-4E08-9803-23CD51DF9A04} = {29821818-FE38-42A7-BFDB-919FDE2CD2F8} {0A9077E0-1B39-4F02-A3F0-54A3680A7056} = {FC5F711A-336E-4310-84D2-5FEAE78F07CF} + {AC22743B-52E2-49A0-9C79-09E67009F872} = {FC5F711A-336E-4310-84D2-5FEAE78F07CF} EndGlobalSection EndGlobal diff --git a/source/TodoList.ConsoleApp/Commands/CommandParser.cs b/source/TodoList.ConsoleApp/Commands/CommandParser.cs index 2d24f7d..edcbe20 100644 --- a/source/TodoList.ConsoleApp/Commands/CommandParser.cs +++ b/source/TodoList.ConsoleApp/Commands/CommandParser.cs @@ -10,6 +10,7 @@ public CommandParser() { commands = new ICommand[] { + new DevelopmentModeCommand(), new InteractiveCommand(), new DoCommand(), new HelpCommand(), diff --git a/source/TodoList.ConsoleApp/Commands/DevelopmentModeCommand.cs b/source/TodoList.ConsoleApp/Commands/DevelopmentModeCommand.cs new file mode 100644 index 0000000..51b9bc1 --- /dev/null +++ b/source/TodoList.ConsoleApp/Commands/DevelopmentModeCommand.cs @@ -0,0 +1,24 @@ +namespace TodoList.ConsoleApp.Commands +{ + using System; + using TodoList.ConsoleApp.Controllers; + + public sealed class DevelopmentModeCommand : ICommand + { + private readonly string[] Tokens = { "-dm", "/dm", "dm", "dev-mode", "--dev-mode" }; + + public void Execute(TodoItemsController controller) + { + controller.DevelopmentMode(); + } + + public bool Match(string[] args) + { + if (args.Length != 1) + return false; + + bool match = CommandArgsParser.Match(args, Tokens); + return match; + } + } +} \ No newline at end of file diff --git a/source/TodoList.ConsoleApp/Controllers/TodoItemsController.cs b/source/TodoList.ConsoleApp/Controllers/TodoItemsController.cs index 1f46d69..4ad5ff6 100644 --- a/source/TodoList.ConsoleApp/Controllers/TodoItemsController.cs +++ b/source/TodoList.ConsoleApp/Controllers/TodoItemsController.cs @@ -131,5 +131,21 @@ private void SetGistIdSettings(string gistId) contents = JsonConvert.SerializeObject(configuration, Formatting.Indented); File.WriteAllText(appsettingsPath, contents); } + + public void DevelopmentMode() + { + var appsettingsPath = Path.GetDirectoryName( + Assembly.GetExecutingAssembly().Location) + + Path.DirectorySeparatorChar + + "appsettings.json"; + + var contents = File.ReadAllText(appsettingsPath); + var configuration = JsonConvert.DeserializeObject(contents); + + configuration["Environment"] = "Development"; + + contents = JsonConvert.SerializeObject(configuration, Formatting.Indented); + File.WriteAllText(appsettingsPath, contents); + } } } \ No newline at end of file diff --git a/source/TodoList.Infrastructure/GistGateway/GistItemGateway.cs b/source/TodoList.Infrastructure/GistGateway/GistItemGateway.cs index 4638218..6e771bc 100644 --- a/source/TodoList.Infrastructure/GistGateway/GistItemGateway.cs +++ b/source/TodoList.Infrastructure/GistGateway/GistItemGateway.cs @@ -15,16 +15,25 @@ namespace TodoList.Infrastructure.GistGateway public sealed class GistItemGateway : IItemGateway { - private GitHubClient _githubClient; - private string _gistToken; - private string _gistId; + private static GitHubClient _gitHubClient; - private void CreateGitHubConnection() + private GitHubClient GitHubClient { - _githubClient = new GitHubClient(new ProductHeaderValue("todo")); - _githubClient.Credentials = new Credentials(_gistToken); + get + { + if (_gitHubClient != null) + return _gitHubClient; + + _gitHubClient = new GitHubClient(new ProductHeaderValue("todo")); + _gitHubClient.Credentials = new Credentials(_gistToken); + + return _gitHubClient; + } } + private string _gistToken; + private string _gistId; + private List EnsureItemsAreLoaded() { if (string.IsNullOrWhiteSpace(_gistId)) @@ -51,7 +60,7 @@ private List EnsureItemsAreLoaded() private Gist LoadGist() { - var gist = _githubClient + var gist = GitHubClient .Gist .Get(_gistId).GetAwaiter() .GetResult(); @@ -66,7 +75,7 @@ private Gist CreateGist(List items) newGist.Description = "todolist"; newGist.Files.Add("todolist.json", jsonContents); - var gist = _githubClient.Gist + var gist = GitHubClient.Gist .Create(newGist) .GetAwaiter() .GetResult(); @@ -87,7 +96,7 @@ private void UpdateGist(List items) var gistUpdate = new GistUpdate(); gistUpdate.Description = "todolist"; gistUpdate.Files.Add("todolist.json", gistFileUpdate); - _githubClient.Gist + GitHubClient.Gist .Edit(_gistId, gistUpdate) .GetAwaiter() .GetResult(); @@ -135,7 +144,6 @@ private void WriteGistIdToAppSettings() public void Add(IItem item) { ReadGitHubCredentialsFromAppSettings(); - CreateGitHubConnection(); var items = EnsureItemsAreLoaded(); items.Add((Item) item); @@ -145,7 +153,6 @@ public void Add(IItem item) public void Delete(string itemId) { ReadGitHubCredentialsFromAppSettings(); - CreateGitHubConnection(); var items = EnsureItemsAreLoaded(); @@ -162,7 +169,6 @@ public void Delete(string itemId) public IItem Get(string itemId) { ReadGitHubCredentialsFromAppSettings(); - CreateGitHubConnection(); var items = EnsureItemsAreLoaded(); Item item = items @@ -174,7 +180,6 @@ public IItem Get(string itemId) public IList List() { ReadGitHubCredentialsFromAppSettings(); - CreateGitHubConnection(); var items = EnsureItemsAreLoaded(); return items.Cast().ToList(); @@ -183,7 +188,6 @@ public IList List() public void Update(IItem item) { ReadGitHubCredentialsFromAppSettings(); - CreateGitHubConnection(); var items = EnsureItemsAreLoaded(); diff --git a/tests/TodoList.AcceptanceTests/ConsoleTests.cs b/tests/TodoList.AcceptanceTests/ConsoleTests.cs new file mode 100644 index 0000000..74d9480 --- /dev/null +++ b/tests/TodoList.AcceptanceTests/ConsoleTests.cs @@ -0,0 +1,22 @@ +namespace TodoList.AcceptanceTests +{ + using System.Diagnostics; + using Xunit; + + public sealed class ConsoleTests + { + [Fact] + public void RunInMemory() + { + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "dotnet", + Arguments = "source/TodoList.ConsoleApp/bin/Debug/netcoreapp2.2/todo.dll -- dm" + } + }; + process.Start(); + } + } +} \ No newline at end of file diff --git a/tests/TodoList.AcceptanceTests/TodoList.AcceptanceTests.csproj b/tests/TodoList.AcceptanceTests/TodoList.AcceptanceTests.csproj index 6658cb0..dbbaedc 100644 --- a/tests/TodoList.AcceptanceTests/TodoList.AcceptanceTests.csproj +++ b/tests/TodoList.AcceptanceTests/TodoList.AcceptanceTests.csproj @@ -7,9 +7,9 @@ - - - + + +