Skip to content

Commit

Permalink
Fixing bug with no longer finding config files for piped in files (#1038
Browse files Browse the repository at this point in the history
)

* Fixing bug with no longer finding config files for piped in files

closes #1028

* Fix this for piping multiple files, which does contain proper filePath
  • Loading branch information
belav authored Nov 22, 2023
1 parent b12ec2b commit 4df56a7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
36 changes: 36 additions & 0 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
12 changes: 10 additions & 2 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ CancellationToken cancellationToken

if (commandLineOptions.StandardInFileContents != null)
{
var filePath = commandLineOptions.DirectoryOrFilePaths[0];
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,
commandLineOptions.StandardInFileContents,
console.InputEncoding
);

var optionsProvider = await OptionsProvider.Create(
fileSystem.Path.GetDirectoryName(filePath),
directoryPath,
commandLineOptions.ConfigPath,
fileSystem,
logger,
Expand Down

0 comments on commit 4df56a7

Please sign in to comment.