From 89adac481987fcd52984bfa57d477046b35529df Mon Sep 17 00:00:00 2001 From: "bela.vandervoort" Date: Tue, 21 Nov 2023 19:07:18 -0600 Subject: [PATCH 1/2] Fixing bug with no longer finding config files for piped in files closes #1028 --- Src/CSharpier.Cli.Tests/CliTests.cs | 36 +++++++++++++++++++++++ Src/CSharpier.Cli/CommandLineFormatter.cs | 9 ++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index 009a9751a..f71659151 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -152,6 +152,42 @@ public async Task Should_Format_Piped_File(string lineEnding) result.ExitCode.Should().Be(0); } + [Test] + public async Task Should_Format_Piped_File_With_Config() + { + await this.WriteFileAsync(".csharpierrc", "printWidth: 10"); + + var formattedContent1 = "var x =\n _________________longName;\n"; + var unformattedContent1 = "var x = _________________longName;\n"; + + var result = await new CsharpierProcess() + .WithPipedInput(unformattedContent1) + .ExecuteAsync(); + + result.Output.Should().Be(formattedContent1); + result.ExitCode.Should().Be(0); + } + + [Test] + public async Task Should_Format_Piped_File_With_EditorConfig() + { + await this.WriteFileAsync( + ".editorconfig", + @"[*] +max_line_length = 10" + ); + + var formattedContent1 = "var x =\n _________________longName;\n"; + var unformattedContent1 = "var x = _________________longName;\n"; + + var result = await new CsharpierProcess() + .WithPipedInput(unformattedContent1) + .ExecuteAsync(); + + result.Output.Should().Be(formattedContent1); + result.ExitCode.Should().Be(0); + } + [Test] public async Task Should_Format_Unicode() { diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index c45a179aa..469f7a04c 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -24,7 +24,12 @@ CancellationToken cancellationToken if (commandLineOptions.StandardInFileContents != null) { - var filePath = commandLineOptions.DirectoryOrFilePaths[0]; + // currently when piping stdin, this will always be a directory + var directoryPath = commandLineOptions.DirectoryOrFilePaths[0]; + // but we need a filename to correctly find config files + // #288 should be done once we support csproj/xml, and possibly be required + var filePath = Path.Combine(directoryPath, "StdIn.cs"); + var fileToFormatInfo = FileToFormatInfo.Create( filePath, commandLineOptions.StandardInFileContents, @@ -32,7 +37,7 @@ CancellationToken cancellationToken ); var optionsProvider = await OptionsProvider.Create( - fileSystem.Path.GetDirectoryName(filePath), + directoryPath, commandLineOptions.ConfigPath, fileSystem, logger, From 869d6dbef825183454ecdd803708e7f448a1011f Mon Sep 17 00:00:00 2001 From: "bela.vandervoort" Date: Tue, 21 Nov 2023 19:28:47 -0600 Subject: [PATCH 2/2] Fix this for piping multiple files, which does contain proper filePath --- Src/CSharpier.Cli/CommandLineFormatter.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index 469f7a04c..cd74f9e1d 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -24,11 +24,14 @@ CancellationToken cancellationToken if (commandLineOptions.StandardInFileContents != null) { - // currently when piping stdin, this will always be a directory - var directoryPath = commandLineOptions.DirectoryOrFilePaths[0]; - // but we need a filename to correctly find config files - // #288 should be done once we support csproj/xml, and possibly be required - var filePath = Path.Combine(directoryPath, "StdIn.cs"); + var directoryOrFilePath = commandLineOptions.DirectoryOrFilePaths[0]; + var directoryPath = fileSystem.Directory.Exists(directoryOrFilePath) + ? directoryOrFilePath + : fileSystem.Path.GetDirectoryName(directoryOrFilePath); + var filePath = + directoryOrFilePath != directoryPath + ? directoryOrFilePath + : Path.Combine(directoryPath, "StdIn.cs"); var fileToFormatInfo = FileToFormatInfo.Create( filePath,