Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
skylarnam committed Feb 15, 2023
1 parent a65429d commit 3b9130f
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 44 deletions.
2 changes: 2 additions & 0 deletions VSConfigFinder.Test/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ dotnet_style_allow_statement_immediately_after_block_experimental = true:silent

# Diagnostics

# CS8602: Dereference of a possibly null reference.
dotnet_diagnostic.CS8602.severity = error
# CS8603: Possible null reference return.
dotnet_diagnostic.CS8603.severity = error
dotnet_style_namespace_match_folder = true:suggestion
Expand Down
95 changes: 95 additions & 0 deletions VSConfigFinder.Test/UtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace VSConfigFinder.Test
{
using Moq;
using System.Text;
using System.Text.Json;
using Xunit;

public class UtilitiesTests
Expand All @@ -25,5 +28,97 @@ public void ValidateIsNotNullOrEmpty_NotNullOrEmpty_String_Succeeds()
var str = "some string";
Utilities.ValidateIsNotNullOrEmpty(str, nameof(str));
}

[Fact]
public void CreateOutput_Creates_File_With_Expected_String()
{
var fileSystem = new Mock<IFileSystem>();
var finalConfig = new VSConfig
{
Version = new Version("1.0"),
Components = new[]
{
"Microsoft.VisualStudio.Component.NuGet",
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
"Microsoft.Component.MSBuild",
"Microsoft.NetCore.Component.Runtime.6.0"
},
};

var jsonString = """
{
"Version": "1.0",
"Components": [
"Microsoft.VisualStudio.Component.NuGet",
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
"Microsoft.Component.MSBuild",
"Microsoft.NetCore.Component.Runtime.6.0"
]
}
""";

var options = new CommandLineOptions
{
FolderPath = "C:\\input",
CreateFile = true,
ConfigOutputPath = "C:\\output",
};

Utilities.CreateOutput(fileSystem.Object, finalConfig, options);

var outputPath = Path.Combine(options.ConfigOutputPath, ".vsconfig");
fileSystem.Verify(x => x.WriteAllText(outputPath, jsonString));
}

[Fact]
public void ReadComponents_Reads_AllNestedDirectories_And_OutputsAllComponents()
{
var fileSystem = new Mock<IFileSystem>();

var options = new CommandLineOptions
{
FolderPath = "C:\\input",
ConfigOutputPath = "C:\\output",
};

// pathA
var pathA = "C:\\pathA";
var pathAConfig = """
{
"Version": "1.0",
"Components": [
"Microsoft.VisualStudio.Component.NuGet",
"Microsoft.Component.MSBuild",
]
}
""";
var pathAReader = new MemoryStream(Encoding.UTF8.GetBytes(pathAConfig));

// pathB
var pathB = "C:\\pathB";
var pathBConfig = """
{
"Version": "1.0",
"Components": [
"Microsoft.VisualStudio.Component.NuGet",
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
]
}
""";
var pathBReader = new MemoryStream(Encoding.UTF8.GetBytes(pathBConfig));

fileSystem.Setup(x => x.GetFileSystemEntries(options.FolderPath, ".vsconfig", true)).Returns(new[] { pathA, pathB });

fileSystem.Setup(x => x.OpenFile(pathA)).Returns(pathAReader);
fileSystem.Setup(x => x.OpenFile(pathB)).Returns(pathBReader);

var components = Utilities.ReadComponents(fileSystem.Object, options);
Assert.Equal(3, components.Length);
Assert.Collection(
components,
x => Assert.Equal("Microsoft.VisualStudio.Component.NuGet", x),
x => Assert.Equal("Microsoft.Component.MSBuild", x),
x => Assert.Equal("Microsoft.VisualStudio.Component.Roslyn.Compiler", x));
}
}
}
1 change: 1 addition & 0 deletions VSConfigFinder.Test/VSConfigFinder.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 3 additions & 1 deletion VSConfigFinder/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file_header_template = <copyright file="{fileName}" company="Microsoft Corporation">\nCopyright (C) Microsoft Corporation. All rights reserved.\nLicensed under the MIT license. See LICENSE.txt in the project root for license information.\n</copyright>
file_header_template = <copyright file="{fileName}" company="Microsoft Corporation">\nCopyright (C) Microsoft Corporation. All rights reserved.\nLicensed under the MIT license. See LICENSE.txt in the project root for license information.\n</copyright>

# All files
[*]
Expand Down Expand Up @@ -118,6 +118,8 @@ dotnet_style_allow_statement_immediately_after_block_experimental = true:silent

# Diagnostics

# CS8602: Dereference of a possibly null reference.
dotnet_diagnostic.CS8602.severity = error
# CS8603: Possible null reference return.
dotnet_diagnostic.CS8603.severity = error
dotnet_style_namespace_match_folder = true:suggestion
Expand Down
20 changes: 15 additions & 5 deletions VSConfigFinder/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace VSConfigFinder
// <copyright file="FileSystem.cs" company="Microsoft Corporation">
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
// </copyright>

namespace VSConfigFinder
{
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;

/// <inheritdoc/>
public class FileSystem : IFileSystem
{
/// <inheritdoc/>
Expand All @@ -24,5 +26,13 @@ public Stream OpenFile(string path)

return File.OpenRead(path);
}

/// <inheritdoc/>
public void WriteAllText(string path, string data)
{
Utilities.ValidateIsNotNullOrEmpty(path, nameof(Path));

File.WriteAllText(path, data);
}
}
}
36 changes: 34 additions & 2 deletions VSConfigFinder/IFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
namespace VSConfigFinder
{
// <copyright file="IFileSystem.cs" company="Microsoft Corporation">
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
// </copyright>

namespace VSConfigFinder
{
/// <summary>
/// The interface for the <see cref="FileSystem"/>.
/// </summary>
public interface IFileSystem
{
/// <summary>
/// Get files and directories within directory.
/// </summary>
/// <param name="path">directory path</param>
/// <param name="pattern">pattern to match for file names. To match anything and everything, specify '*'</param>
/// <param name="recursive">Optional: recursively search sub directories</param>
/// <returns>array of files within the directory</returns>
/// <exception cref="ArgumentException"><paramref name="path"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
public string[] GetFileSystemEntries(string path, string pattern, bool recursive = false);

/// <summary>
/// Opens a file for reading.
/// </summary>
/// <param name="path">The path to a file to open.</param>
/// <returns>A stream of the opened file.</returns>
/// <exception cref="ArgumentException"><paramref name="path"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
public Stream OpenFile(string path);

/// <summary>
/// Writes all text in a path.
/// </summary>
/// <param name="path">The path to a file to write.</param>
/// <param name="data">The string data to write.</param>
/// <exception cref="ArgumentException"><paramref name="path"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
public void WriteAllText(string path, string data);
}
}
24 changes: 13 additions & 11 deletions VSConfigFinder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace VSConfigFinder
{
using CommandLine;
using System.IO;
using System.Text.Json;

/// <summary>
/// <see cref="Program"/> class for the .vsconfig finder tool.
Expand All @@ -25,30 +24,33 @@ public static void Main(string[] args)
with.CaseSensitive = false;
});

// Take in the command line arguments
parser.ParseArguments<CommandLineOptions>(args)
.WithParsed(Run)
.WithNotParsed(HandleParseError);
}

private static void Run(CommandLineOptions options)
{
// Run
if (options.CreateFile)
{
options.ConfigOutputPath ??= Directory.GetCurrentDirectory();
}
var fileSystem = new FileSystem();

string[] allComponents = Utilities.ReadComponents(options);
ResolveCommandLineOptions(options);

var finalConfig = new VSConfig()
{
// This is the new final .vsconfig, so version 1.0 is used.
Version = new Version("1.0"),
Components = allComponents,
Components = Utilities.ReadComponents(fileSystem, options),
};

// Output
Utilities.CreateOutput(finalConfig, options);
Utilities.CreateOutput(fileSystem, finalConfig, options);
}

private static void ResolveCommandLineOptions(CommandLineOptions options)
{
if (options.CreateFile)
{
options.ConfigOutputPath ??= Directory.GetCurrentDirectory();
}
}

private static void HandleParseError(IEnumerable<Error> errors)
Expand Down
Loading

0 comments on commit 3b9130f

Please sign in to comment.