From a4cf135c087a088c25e3a3a857ce9c003afbd5d0 Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Wed, 19 Aug 2020 20:10:11 +0200 Subject: [PATCH] Fixed issue #620 (#621) * Fixed issue #620 * Produce a useful error message if the path of a project file is incorrect in tye.yaml. * Added a test. * Fixed test on Linux and OSX. * Check full exception message in test and fixed test name. * More deterministic behavior considering directory check and test execution. Moved directory check one level higher from EnsureMSBuildRegistered() to the caller ReadProjectDetailsAsync(). This means that this code is always executed. In EnsureMSBuildRegistered() it was only executed once per proces because of the static field "registered". The placement of the check after evaluation of the the field "registered" was the reason why the test WrongProjectPathProducesCorrectErrorMessage worked when executed alone but not when any other test that called EnsureMSBuildRegistered() was executed before. With this change the directory check is always executed now, even if the EnsureMSBuildRegistered() was already executed successfully. But the performance impact should not be significant while the type of generated error message is more deterministic and not dependent on execution order. --- src/Microsoft.Tye.Core/ProjectReader.cs | 5 +++++ test/E2ETest/ApplicationFactoryTests.cs | 14 ++++++++++++++ .../frontend-backend/tye-wrong-projectpath.yaml | 8 ++++++++ 3 files changed, 27 insertions(+) create mode 100644 test/E2ETest/testassets/projects/frontend-backend/tye-wrong-projectpath.yaml diff --git a/src/Microsoft.Tye.Core/ProjectReader.cs b/src/Microsoft.Tye.Core/ProjectReader.cs index af5076c5..e2203c4d 100644 --- a/src/Microsoft.Tye.Core/ProjectReader.cs +++ b/src/Microsoft.Tye.Core/ProjectReader.cs @@ -73,6 +73,11 @@ namespace Microsoft.Tye throw new ArgumentNullException(nameof(project)); } + if (!Directory.Exists(project.ProjectFile.DirectoryName)) + { + throw new CommandException($"Failed to locate directory: '{project.ProjectFile.DirectoryName}'."); + } + EnsureMSBuildRegistered(output, project.ProjectFile); EvaluateProject(output, project); diff --git a/test/E2ETest/ApplicationFactoryTests.cs b/test/E2ETest/ApplicationFactoryTests.cs index 88ab1b7d..b32f7261 100644 --- a/test/E2ETest/ApplicationFactoryTests.cs +++ b/test/E2ETest/ApplicationFactoryTests.cs @@ -107,5 +107,19 @@ services: Assert.Equal("redis2", ((ContainerServiceBuilder)redisService).Image); } + + [Fact] + public async Task WrongProjectPathProducesCorrectErrorMessage() + { + using var projectDirectory = TestHelpers.CopyTestProjectDirectory("frontend-backend"); + var projectFile = new FileInfo(Path.Combine(projectDirectory.DirectoryPath, "tye-wrong-projectpath.yaml")); + var outputContext = new OutputContext(_sink, Verbosity.Debug); + + var exception = await Assert.ThrowsAsync(async () => + await ApplicationFactory.CreateAsync(outputContext, projectFile)); + + var wrongProjectPath = Path.Combine(projectDirectory.DirectoryPath, "backend1"); + Assert.Equal($"Failed to locate directory: '{wrongProjectPath}'.", exception.Message); + } } } diff --git a/test/E2ETest/testassets/projects/frontend-backend/tye-wrong-projectpath.yaml b/test/E2ETest/testassets/projects/frontend-backend/tye-wrong-projectpath.yaml new file mode 100644 index 00000000..0ff65ceb --- /dev/null +++ b/test/E2ETest/testassets/projects/frontend-backend/tye-wrong-projectpath.yaml @@ -0,0 +1,8 @@ +# tye application configuration file +# read all about it at https://github.com/dotnet/tye +name: frontend-backend +services: +- name: backend + project: backend1/backend.csproj +- name: frontend + project: frontend/frontend.csproj