LaunchSettingsProfileTest.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using Microsoft.DotNet.Watcher.Internal;
  4. using Microsoft.Extensions.Tools.Internal;
  5. namespace Microsoft.DotNet.Watcher.Tools;
  6. public class LaunchSettingsProfileTest
  7. {
  8. private readonly IReporter _reporter;
  9. private readonly TestAssetsManager _testAssets;
  10. public LaunchSettingsProfileTest(ITestOutputHelper output)
  11. {
  12. _reporter = new TestReporter(output);
  13. _testAssets = new TestAssetsManager(output);
  14. }
  15. [Fact]
  16. public void LoadsLaunchProfiles()
  17. {
  18. var project = _testAssets.CreateTestProject(new TestProject("Project1")
  19. {
  20. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  21. });
  22. WriteFile(project, Path.Combine("Properties", "launchSettings.json"),
  23. """
  24. {
  25. "profiles": {
  26. "http": {
  27. "applicationUrl": "http://localhost:5000",
  28. "commandName": "Project",
  29. "environmentVariables": {
  30. "ASPNETCORE_ENVIRONMENT": "Development"
  31. }
  32. },
  33. "https": {
  34. "applicationUrl": "https://localhost:5001",
  35. "commandName": "Project",
  36. "environmentVariables": {
  37. "ASPNETCORE_ENVIRONMENT": "Development"
  38. }
  39. }, // This comment and trailing comma shouldn't cause any issues
  40. }
  41. }
  42. """);
  43. var projectDirectory = Path.Combine(project.TestRoot, "Project1");
  44. var expected = LaunchSettingsProfile.ReadLaunchProfile(projectDirectory, "http", _reporter);
  45. Assert.NotNull(expected);
  46. Assert.Equal("http://localhost:5000", expected.ApplicationUrl);
  47. expected = LaunchSettingsProfile.ReadLaunchProfile(projectDirectory, "https", _reporter);
  48. Assert.NotNull(expected);
  49. Assert.Equal("https://localhost:5001", expected.ApplicationUrl);
  50. expected = LaunchSettingsProfile.ReadLaunchProfile(projectDirectory, "notfound", _reporter);
  51. Assert.NotNull(expected);
  52. }
  53. private static string WriteFile(TestAsset testAsset, string name, string contents = "")
  54. {
  55. var path = Path.Combine(GetTestProjectDirectory(testAsset), name);
  56. Directory.CreateDirectory(Path.GetDirectoryName(path));
  57. File.WriteAllText(path, contents);
  58. return path;
  59. }
  60. private static string WriteFile(TestDirectory testAsset, string name, string contents = "")
  61. {
  62. var path = Path.Combine(testAsset.Path, name);
  63. Directory.CreateDirectory(Path.GetDirectoryName(path));
  64. File.WriteAllText(path, contents);
  65. return path;
  66. }
  67. private static string GetTestProjectDirectory(TestAsset testAsset)
  68. => Path.Combine(testAsset.Path, testAsset.TestProject.Name);
  69. }