コード例 #1
0
ファイル: Program.cs プロジェクト: mristin/doctest-csharp
        private static int Handle(string[] inputOutput, string suffix, string[]?excludes, bool check, bool verbose)
        {
            int exitCode = 0;

            var inputOutputOrError = Input.ParseInputOutput(inputOutput, suffix);

            if (inputOutputOrError.Error != null)
            {
                Console.Error.WriteLine($"Failed to parse --input-output: {inputOutputOrError.Error}");
                return(1);
            }

            if (inputOutputOrError.InputOutput == null)
            {
                throw new InvalidOperationException(
                          "Invalid inputOutputOrError: both InputOutput and Error are null.");
            }

            string cwd = Directory.GetCurrentDirectory();

            foreach (var(input, output) in inputOutputOrError.InputOutput)
            {
                string rootedInput =
                    Path.IsPathRooted(input)
                        ? input
                        : Path.Join(cwd, input);

                string rootedOutput =
                    Path.IsPathRooted(output)
                        ? output
                        : Path.Join(cwd, output);

                IEnumerable <string> relativePaths = Input.MatchFiles(
                    rootedInput,
                    new List <string> {
                    "**/*.cs"
                },
                    new List <string>(excludes ?? new string[0]));

                foreach (string relativePath in relativePaths)
                {
                    if (Path.IsPathRooted(relativePath))
                    {
                        throw new InvalidOperationException(
                                  $"Expected path to be relative, but got rooted path: {relativePath}");
                    }

                    string inputPath  = Process.InputPath(relativePath, rootedInput);
                    string outputPath = Process.OutputPath(relativePath, rootedOutput);

                    var doctestsAndErrors = Extraction.Extract(
                        CSharpSyntaxTree.ParseText(
                            File.ReadAllText(inputPath)));

                    if (doctestsAndErrors.Errors.Count > 0)
                    {
                        Console.WriteLine($"Failed to extract doctest(s) from: {inputPath}");
                        foreach (var error in doctestsAndErrors.Errors)
                        {
                            Console.WriteLine($"* Line {error.Line + 1}, column {error.Column + 1}: {error.Message}");
                        }

                        exitCode = 1;

                        continue;
                    }

                    var doctests = doctestsAndErrors.Doctests;

                    if (!check)
                    {
                        bool generated = Process.Generate(doctests, relativePath, outputPath);

                        if (verbose)
                        {
                            Console.WriteLine(
                                generated
                                    ? $"Generated doctest(s) for: {inputPath} -> {outputPath}"
                                    : $"No doctests found in: {inputPath}");
                        }
                    }
                    else
                    {
                        var report = Process.Check(doctests, relativePath, outputPath);
                        switch (report)
                        {
                        case Report.Ok _:
                            if (verbose)
                            {
                                Console.WriteLine(
                                    (doctests.Count > 0)
                                            ? $"OK: {inputPath} -> {outputPath}"
                                            : $"OK, no doctests: {inputPath}");
                            }
                            break;

                        case Report.Different reportDifferent:
                            Console.WriteLine($"Expected different content: {inputPath} -> {outputPath}");
                            Console.WriteLine(
                                "Here is the diff between the expected content and the actual content:");

                            foreach (var line in reportDifferent.Diff.Lines)
                            {
                                switch (line.Type)
                                {
                                case DiffPlex.DiffBuilder.Model.ChangeType.Inserted:
                                    Console.Write("+ ");
                                    break;

                                case DiffPlex.DiffBuilder.Model.ChangeType.Deleted:
                                    Console.Write("- ");
                                    break;

                                default:
                                    Console.Write("  ");
                                    break;
                                }

                                Console.WriteLine(line.Text);
                            }

                            exitCode = 1;
                            break;

                        case Report.DoesntExist _:
                            Console.WriteLine($"Output file does not exist: {inputPath} -> {outputPath}");
                            exitCode = 1;
                            break;

                        case Report.ShouldNotExist _:
                            Console.WriteLine(
                                $"No doctests found in: {inputPath}; the output should not exist: {outputPath}");
                            exitCode = 1;
                            break;

                        default:
                            throw new NotImplementedException($"Uncovered report: {report}");
                        }
                    }
                }
            }

            return(exitCode);
        }
コード例 #2
0
        private static int Handle(DirectoryInfo input, DirectoryInfo output, string[]?excludes, bool check)
        {
            int exitCode = 0;

            IEnumerable <string> relativePaths = Input.MatchFiles(
                input.FullName,
                new List <string> {
                "**/*.cs"
            },
                new List <string>(excludes ?? new string[0]));

            foreach (string relativePath in relativePaths)
            {
                if (Path.IsPathRooted(relativePath))
                {
                    throw new InvalidOperationException(
                              $"Expected path to be relative, but got rooted path: {relativePath}");
                }

                string inputPath  = Process.InputPath(relativePath, input);
                string outputPath = Process.OutputPath(relativePath, output);

                var doctestsAndErrors = Extraction.Extract(
                    CSharpSyntaxTree.ParseText(
                        File.ReadAllText(inputPath)));

                if (doctestsAndErrors.Errors.Count > 0)
                {
                    Console.WriteLine($"Failed to extract doctest(s) from: {inputPath}");
                    foreach (var error in doctestsAndErrors.Errors)
                    {
                        Console.WriteLine($"* Line {error.Line + 1}, column {error.Column + 1}: {error.Message}");
                    }

                    exitCode = 1;

                    continue;
                }

                var doctests = doctestsAndErrors.Doctests;

                if (!check)
                {
                    bool generated = Process.Generate(doctests, outputPath);
                    Console.WriteLine(
                        generated
                        ? $"Generated doctest(s) for: {inputPath} -> {outputPath}"
                        : $"No doctests found in: {inputPath}; not generating {outputPath}");
                }
                else
                {
                    var report = Process.Check(doctests, outputPath);
                    switch (report)
                    {
                    case Process.Report.Ok:
                        Console.WriteLine($"OK: {inputPath} -> {outputPath}");
                        break;

                    case Process.Report.Different:
                        Console.WriteLine($"Expected different content: {inputPath} -> {outputPath}");
                        exitCode = 1;
                        break;

                    case Process.Report.DoesntExist:
                        Console.WriteLine($"Output file does not exist: {inputPath} -> {outputPath}");
                        exitCode = 1;
                        break;

                    case Process.Report.ShouldNotExist:
                        Console.WriteLine(
                            $"No doctests found in: {inputPath}; the output should not exist: {outputPath}");
                        exitCode = 1;
                        break;

                    default:
                        throw new NotImplementedException($"Uncovered report: {report}");
                    }
                }
            }

            return(exitCode);
        }