示例#1
0
        public async Task <string> TestFormatWorkspaceAsync(string workspaceFilePath, IEnumerable <string> include, IEnumerable <string> exclude, int expectedExitCode, int expectedFilesFormatted, int expectedFileCount)
        {
            var workspacePath = Path.GetFullPath(workspaceFilePath);

            WorkspaceType workspaceType;

            if (Directory.Exists(workspacePath))
            {
                workspaceType = WorkspaceType.Folder;
            }
            else
            {
                workspaceType = workspacePath.EndsWith(".sln")
                    ? WorkspaceType.Solution
                    : WorkspaceType.Project;
            }

            var fileMatcher   = SourceFileMatcher.CreateMatcher(include, exclude);
            var logger        = new TestLogger();
            var formatOptions = new FormatOptions(
                workspacePath,
                workspaceType,
                LogLevel.Trace,
                saveFormattedFiles: false,
                changesAreErrors: false,
                fileMatcher,
                reportPath: string.Empty);
            var formatResult = await CodeFormatter.FormatWorkspaceAsync(formatOptions, logger, CancellationToken.None);

            Assert.Equal(expectedExitCode, formatResult.ExitCode);
            Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted);
            Assert.Equal(expectedFileCount, formatResult.FileCount);

            return(logger.GetLog());
        }
示例#2
0
        public async Task <string> TestFormatWorkspaceAsync(string workspaceFilePath, IEnumerable <string> files, int expectedExitCode, int expectedFilesFormatted, int expectedFileCount)
        {
            var workspacePath = Path.GetFullPath(workspaceFilePath);

            WorkspaceType workspaceType;

            if (Directory.Exists(workspacePath))
            {
                workspaceType = WorkspaceType.Folder;
            }
            else
            {
                workspaceType = workspacePath.EndsWith(".sln")
                    ? WorkspaceType.Solution
                    : WorkspaceType.Project;
            }

            var filesToFormat = files.Select(Path.GetFullPath).ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);

            var logger        = new TestLogger();
            var formatOptions = new FormatOptions(
                workspacePath,
                workspaceType,
                LogLevel.Trace,
                saveFormattedFiles: false,
                changesAreErrors: false,
                filesToFormat);
            var formatResult = await CodeFormatter.FormatWorkspaceAsync(formatOptions, logger, CancellationToken.None);

            Assert.Equal(expectedExitCode, formatResult.ExitCode);
            Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted);
            Assert.Equal(expectedFileCount, formatResult.FileCount);

            return(logger.GetLog());
        }
示例#3
0
        internal async Task <string> TestFormatWorkspaceAsync(
            string workspaceFilePath,
            string[] include,
            string[] exclude,
            bool includeGenerated,
            int expectedExitCode,
            int expectedFilesFormatted,
            int expectedFileCount,
            FixCategory fixCategory = FixCategory.Whitespace,
            DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
            DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error)
        {
            var workspacePath = Path.GetFullPath(workspaceFilePath);

            WorkspaceType workspaceType;

            if (Directory.Exists(workspacePath))
            {
                workspaceType = WorkspaceType.Folder;
            }
            else
            {
                workspaceType = workspacePath.EndsWith(".sln")
                    ? WorkspaceType.Solution
                    : WorkspaceType.Project;
            }

            var logger      = new TestLogger();
            var msBuildPath = MSBuildRegistrar.RegisterInstance(logger);

            logger.LogDebug(Resources.The_dotnet_runtime_version_is_0, Program.GetRuntimeVersion());
            logger.LogTrace(Resources.Using_msbuildexe_located_in_0, msBuildPath);

            var fileMatcher   = SourceFileMatcher.CreateMatcher(include, exclude);
            var formatOptions = new FormatOptions(
                workspacePath,
                workspaceType,
                LogLevel.Trace,
                fixCategory,
                codeStyleSeverity,
                analyzerSeverity,
                saveFormattedFiles: false,
                changesAreErrors: false,
                fileMatcher,
                reportPath: string.Empty,
                includeGenerated);
            var formatResult = await CodeFormatter.FormatWorkspaceAsync(formatOptions, logger, CancellationToken.None);

            var log = logger.GetLog();

            _output.WriteLine(log);

            Assert.Equal(expectedExitCode, formatResult.ExitCode);
            Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted);
            Assert.Equal(expectedFileCount, formatResult.FileCount);

            return(log);
        }
示例#4
0
        public async Task <string> TestFormatWorkspaceAsync(string solutionOrProjectPath, IEnumerable <string> files, int expectedExitCode, int expectedFilesFormatted, int expectedFileCount)
        {
            var workspacePath = Path.GetFullPath(solutionOrProjectPath);
            var isSolution    = workspacePath.EndsWith(".sln");
            var filesToFormat = files.Select(Path.GetFullPath).ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);

            var logger       = new TestLogger();
            var formatResult = await CodeFormatter.FormatWorkspaceAsync(workspacePath, isSolution, logWorkspaceWarnings : false, saveFormattedFiles : false, filesToFormat, logger, CancellationToken.None);

            Assert.Equal(expectedExitCode, formatResult.ExitCode);
            Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted);
            Assert.Equal(expectedFileCount, formatResult.FileCount);

            return(logger.GetLog());
        }
示例#5
0
        public async Task FSharpProjectsDoNotCreateException()
        {
            var logger = new TestLogger();
            var path   = Path.GetFullPath("tests/projects/for_code_formatter/fsharp_project/fsharp_project.fsproj", SolutionPath);

            var exitCode = await CodeFormatter.FormatWorkspaceAsync(logger, path, isSolution : false, logAllWorkspaceWarnings : false, saveFormattedFiles : false, cancellationToken : CancellationToken.None);

            var logLines = logger.GetLog().Split(Environment.NewLine);

            Assert.Equal(4, logLines.Length);
            var actualErrorMessage   = logLines[2];
            var expectedErrorMessage = string.Format(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, path);

            Assert.Equal(expectedErrorMessage, actualErrorMessage);
            Assert.Equal(1, exitCode);
        }
示例#6
0
        public async Task FilesFormattedInUnformattedSolution()
        {
            var logger = new TestLogger();
            var path   = Path.GetFullPath("tests/projects/for_code_formatter/unformatted_solution/unformatted_solution.sln", SolutionPath);

            var exitCode = await CodeFormatter.FormatWorkspaceAsync(logger, path, isSolution : true, logAllWorkspaceWarnings : false, saveFormattedFiles : false, cancellationToken : CancellationToken.None);

            var log            = logger.GetLog();
            var pattern        = string.Format(Resources.Formatted_0_of_1_files_in_2_ms, "(\\d+)", "\\d+", "\\d+");
            var filesFormatted = new Regex(pattern, RegexOptions.Multiline);
            var match          = filesFormatted.Match(log);

            Assert.Equal(0, exitCode);
            Assert.True(match.Success, log);
            Assert.Equal("1", match.Groups[1].Value);
        }
示例#7
0
        public async Task NoFilesFormattedInFormattedProject()
        {
            var logger = new TestLogger();
            var path   = Path.GetFullPath("tests/projects/for_code_formatter/formatted_project/formatted_project.csproj", SolutionPath);

            var formatResult = await CodeFormatter.FormatWorkspaceAsync(logger, path, isSolution : false, logAllWorkspaceWarnings : false, saveFormattedFiles : false, filesToFormat : null, cancellationToken : CancellationToken.None);

            var log            = logger.GetLog();
            var pattern        = string.Format(Resources.Formatted_0_of_1_files_in_2_ms, "(\\d+)", "\\d+", "\\d+");
            var filesFormatted = new Regex(pattern, RegexOptions.Multiline);
            var match          = filesFormatted.Match(log);

            Assert.True(match.Success, log);
            Assert.Equal("0", match.Groups[1].Value);

            Assert.Equal(0, formatResult.ExitCode);
            Assert.Equal(0, formatResult.FilesFormatted);
            Assert.Equal(3, formatResult.FileCount);
        }
        internal async Task <string> TestFormatWorkspaceAsync(
            string workspaceFilePath,
            string[] include,
            string[] exclude,
            bool includeGenerated,
            int expectedExitCode,
            int expectedFilesFormatted,
            int expectedFileCount,
            FixCategory fixCategory = FixCategory.Whitespace,
            DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
            DiagnosticSeverity analyzerSeverity  = DiagnosticSeverity.Error,
            string[] diagnostics    = null,
            bool noRestore          = false,
            bool saveFormattedFiles = false)
        {
            var currentDirectory = Environment.CurrentDirectory;

            Environment.CurrentDirectory = TestProjectsPathHelper.GetProjectsDirectory();

            var workspacePath = Path.GetFullPath(workspaceFilePath);

            WorkspaceType workspaceType;

            if (Directory.Exists(workspacePath))
            {
                workspaceType = WorkspaceType.Folder;
            }
            else
            {
                workspaceType = workspacePath.EndsWith("proj")
                    ? WorkspaceType.Project
                    : WorkspaceType.Solution;
            }

            var logger      = new TestLogger();
            var msBuildPath = MSBuildRegistrar.RegisterInstance();

            logger.LogTrace(Resources.Using_msbuildexe_located_in_0, msBuildPath);

            var fileMatcher   = SourceFileMatcher.CreateMatcher(include, exclude);
            var formatOptions = new FormatOptions(
                workspacePath,
                workspaceType,
                noRestore,
                LogLevel.Trace,
                fixCategory,
                codeStyleSeverity,
                analyzerSeverity,
                diagnostics?.ToImmutableHashSet() ?? ImmutableHashSet <string> .Empty,
                ExcludeDiagnostics: ImmutableHashSet <string> .Empty,
                saveFormattedFiles,
                ChangesAreErrors: false,
                fileMatcher,
                ReportPath: string.Empty,
                IncludeGeneratedFiles: includeGenerated,
                BinaryLogPath: null);
            var formatResult = await CodeFormatter.FormatWorkspaceAsync(formatOptions, logger, CancellationToken.None);

            Environment.CurrentDirectory = currentDirectory;

            var log = logger.GetLog();

            try
            {
                Assert.Equal(expectedExitCode, formatResult.ExitCode);
                Assert.Equal(expectedFilesFormatted, formatResult.FilesFormatted);
                Assert.Equal(expectedFileCount, formatResult.FileCount);
            }
            catch
            {
                _output.WriteLine(log);
                throw;
            }

            return(log);
        }