Skip to content

Commit

Permalink
Fixing bug with no longer finding config files for piped in files
Browse files Browse the repository at this point in the history
closes #1028
  • Loading branch information
belav committed Nov 22, 2023
1 parent d2e6431 commit 89adac4
Show file tree
Hide file tree
Showing 2 changed files with 43 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
9 changes: 7 additions & 2 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ 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,
console.InputEncoding
);

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

0 comments on commit 89adac4

Please sign in to comment.