private string PrintDifference(string expected, string actual) { var testConsole = new TestConsole(); StringDiffer.PrintFirstDifference(expected, actual, testConsole); return(testConsole.GetOutput()); }
private void PerformCheck(string filePath, CSharpierResult result, string fileContents) { if ( this.CommandLineOptions.Check && !this.CommandLineOptions.ShouldWriteStandardOut && result.Code != fileContents ) { WriteLine(GetPath(filePath) + " - was not formatted"); StringDiffer.PrintFirstDifference(result.Code, fileContents, this.Console); Interlocked.Increment(ref this.Result.UnformattedFiles); } }
private async Task FormatFile(string file, CancellationToken cancellationToken) { if (ShouldIgnoreFile(file)) { return; } cancellationToken.ThrowIfCancellationRequested(); var fileReaderResult = await FileReader.ReadFile(file, this.FileSystem, cancellationToken); if (fileReaderResult.FileContents.Length == 0) { return; } if (fileReaderResult.DefaultedEncoding) { WriteLine( $"{GetPath(file)} - unable to detect file encoding. Defaulting to {fileReaderResult.Encoding}" ); } cancellationToken.ThrowIfCancellationRequested(); CSharpierResult result; try { result = await new CodeFormatter().FormatAsync( fileReaderResult.FileContents, this.PrinterOptions, cancellationToken ); } catch (OperationCanceledException) { throw; } catch (Exception ex) { Interlocked.Increment(ref this.Result.Files); WriteLine(GetPath(file) + " - threw exception while formatting"); WriteLine(ex.Message); WriteLine(ex.StackTrace); WriteLine(); Interlocked.Increment(ref this.Result.ExceptionsFormatting); return; } if (result.Errors.Any()) { Interlocked.Increment(ref this.Result.Files); WriteLine(GetPath(file) + " - failed to compile"); return; } if (!result.FailureMessage.IsBlank()) { Interlocked.Increment(ref this.Result.Files); WriteLine(GetPath(file) + " - " + result.FailureMessage); return; } if (!this.CommandLineOptions.Fast) { var syntaxNodeComparer = new SyntaxNodeComparer( fileReaderResult.FileContents, result.Code, cancellationToken ); try { var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken); if (!string.IsNullOrEmpty(failure)) { Interlocked.Increment(ref this.Result.FailedSyntaxTreeValidation); WriteLine(GetPath(file) + " - failed syntax tree validation"); WriteLine(failure); } } catch (Exception ex) { Interlocked.Increment(ref this.Result.ExceptionsValidatingSource); WriteLine( GetPath(file) + " - failed with exception during syntax tree validation" + Environment.NewLine + ex.Message + ex.StackTrace ); } } if (this.CommandLineOptions.Check) { if (result.Code != fileReaderResult.FileContents) { WriteLine(GetPath(file) + " - was not formatted"); StringDiffer.PrintFirstDifference( result.Code, fileReaderResult.FileContents, this.Console ); Interlocked.Increment(ref this.Result.UnformattedFiles); } } cancellationToken.ThrowIfCancellationRequested(); Interlocked.Increment(ref this.Result.Files); if ( !this.CommandLineOptions.Check && !this.CommandLineOptions.SkipWrite && result.Code != fileReaderResult.FileContents ) { // purposely avoid async here, that way the file completely writes if the process gets cancelled while running. this.FileSystem.File.WriteAllText(file, result.Code, fileReaderResult.Encoding); } }