Skip to content

Commit

Permalink
fix: prevent System.IO.DirectoryNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Pattison committed Aug 24, 2022
1 parent f8778d4 commit 5d56dff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 18 additions & 0 deletions DotNet.Project.LaunchSettings.Tests/LaunchSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace DotNet.Project.LaunchSettings.Tests;

using System;
using System.IO;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -68,6 +69,23 @@ public void Missing_visual_studio_launch_settings_file_should_return_empty_profi
actual.Should().BeEquivalentTo(expected);
}

[Fact]
public void Path_to_directories_that_dont_exist_should_return_empty_profile()
{
var filePath = "/path/to/somewhere/that/does/not/exist/file.json";
if (OperatingSystem.IsWindows())
{
filePath = "C:" + filePath;
}

// ReSharper disable once ExplicitCallerInfoArgument
var actual = GetFirstOrEmptyProfile(VisualStudioLaunchSettings.FromCaller(filePath));

var expected = StubbedProfiles.Empty;

actual.Should().BeEquivalentTo(expected);
}

[Fact]
public void VisualStudio_launch_settings_should_deserialize_correctly()
{
Expand Down
12 changes: 8 additions & 4 deletions DotNet.Project.LaunchSettings/VisualStudioLaunchSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ public static VisualStudioLaunchSettings FromCaller([CallerFilePath] string file
do
{
directory = directory is null ? Path.GetDirectoryName(filePath) : Directory.GetParent(directory)?.FullName;
} while (directory is not null && HasCsproj(directory));
} while (LookingForCsproj(directory));

return new VisualStudioLaunchSettings(directory is {} ? Path.Combine(directory, "Properties", "launchSettings.json") : "");
}

private static bool HasCsproj(string? directory)
private static bool LookingForCsproj(string? directory)
{
return string.IsNullOrWhiteSpace(directory) is false &&
Directory.GetFiles(directory, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault() is null;
return directory is not null && Directory.Exists(directory) && HasCsproj(directory);
}

private static bool HasCsproj(string directory)
{
return Directory.GetFiles(directory, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault() is null;
}

protected override Stream GetReader()
Expand Down

0 comments on commit 5d56dff

Please sign in to comment.