public void TestReportToStdout() { using var tmpdir = new TemporaryDirectory(); using var consoleCapture = new ConsoleCapture(); string nl = Environment.NewLine; string path = Path.Join(tmpdir.Path, "SomeProgram.cs"); File.WriteAllText(path, $"// TODO (mristin, 2020-07-20): Do something!{nl}"); int exitCode = Program.MainWithCode(new[] { "--inputs", path, "--report-path", "-" }); Assert.AreEqual("", consoleCapture.Error()); Assert.AreEqual( $@"[ {{ ""path"": ""{path.Replace(@"\", @"\\")}"", ""records"": [ {{ ""prefix"": ""TODO"", ""suffix"": "" (mristin, 2020-07-20): Do something!"", ""line"": 0, ""column"": 0, ""status"": ""ok"" }} ] }} ] ", consoleCapture.Output()); Assert.AreEqual(0, exitCode); }
public void TestRuleArgumentsConsidered() { using var tmpdir = new TemporaryDirectory(); using var consoleCapture = new ConsoleCapture(); string nl = Environment.NewLine; string pathNotOk = Path.Join(tmpdir.Path, "NotOk.cs"); File.WriteAllText(pathNotOk, $"// AAA: Do something!{nl}"); int exitCode = Program.MainWithCode( new[] { "--inputs", pathNotOk, "--prefixes", "^AAA", "--disallowed-prefixes", "^BBB", "--suffixes", "^CCC" }); Assert.AreEqual( $"FAILED: {pathNotOk}{nl}" + $" * Line 1, column 1: invalid suffix (see --suffixes): : Do something!{nl}" + $"One or more TODOs were invalid. Please see above.{nl}" + $"--prefixes was set to: ^AAA{nl}" + $"--disallowed-prefixes was set to: ^BBB{nl}" + @"--suffixes was set to: ^CCC" + nl, consoleCapture.Error()); Assert.AreEqual(1, exitCode); }
public void TestCheckDoesntExist() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); string outputPath = Path.Join(output.FullName, "DocTestSomeProgram.cs"); File.WriteAllText( inputPath, @"/// <code doctest=""true""> /// var x = 1; /// </code> "); // Test pre-condition Assert.IsFalse(File.Exists(outputPath)); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--input", input.FullName, "--output", output.FullName, "--check" }); string nl = Environment.NewLine; Assert.AreEqual(1, exitCode); Assert.AreEqual($"Output file does not exist: {inputPath} -> {outputPath}{nl}", consoleCapture.Output()); }
public void TestNoDoctest() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); string outputPath = Path.Join(output.FullName, "DocTestSomeProgram.cs"); File.WriteAllText(inputPath, "no doctests"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--input-output", $"{input.FullName}{Path.PathSeparator}{output.FullName}", "--verbose" }); string nl = Environment.NewLine; Assert.AreEqual(0, exitCode); Assert.AreEqual( $"No doctests found in: {inputPath}{nl}", consoleCapture.Output()); Assert.IsFalse(File.Exists(outputPath)); }
public void TestDoctest() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); string outputPath = Path.Join(output.FullName, "DocTestSomeProgram.cs"); File.WriteAllText( inputPath, @"/// <code doctest=""true""> /// var x = 1; /// </code> "); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode(new[] { "--input", input.FullName, "--output", output.FullName }); string nl = Environment.NewLine; Assert.AreEqual(0, exitCode); Assert.AreEqual( $"Generated doctest(s) for: {inputPath} -> {outputPath}{nl}", consoleCapture.Output()); }
public void TestExtractionError() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); File.WriteAllText( inputPath, @"/// <code doctest=""true""> /// var a = 0; /// // --- /// var x = 1; /// </code> "); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode(new[] { "--input", input.FullName, "--output", output.FullName }); string nl = Environment.NewLine; Assert.AreEqual(1, exitCode); Assert.AreEqual( $"Failed to extract doctest(s) from: {inputPath}{nl}" + $"* Line 1, column 1: Expected only using directives in the header, but got: FieldDeclaration{nl}", consoleCapture.Output()); }
public void TestWithInvalidAndValidTodoAndVerbose() { using var tmpdir = new TemporaryDirectory(); using var consoleCapture = new ConsoleCapture(); string nl = Environment.NewLine; string pathNotOk = Path.Join(tmpdir.Path, "NotOk.cs"); File.WriteAllText(pathNotOk, $"// TODO (mristin): Do something!{nl}"); string pathOk = Path.Join(tmpdir.Path, "Ok.cs"); File.WriteAllText(pathOk, "// TODO (mristin, 2020-07-20): Do something else!"); int exitCode = Program.MainWithCode(new[] { "--inputs", pathNotOk, pathOk, "--verbose" }); Assert.AreEqual( $"FAILED: {pathNotOk}{nl}" + $" * Line 1, column 1: invalid suffix (see --suffixes): (mristin): Do something!{nl}" + $"One or more TODOs were invalid. Please see above.{nl}" + $"--prefixes was set to: ^TODO ^BUG ^HACK{nl}" + $"--disallowed-prefixes was set to: ^DONT-CHECK-IN ^Todo ^todo ^ToDo ^Bug ^bug ^Hack ^hack{nl}" + @"--suffixes was set to: ^ \([^)]+, [0-9]{4}-[0-9]{2}-[0-9]{2}\): ." + nl, consoleCapture.Error()); Assert.AreEqual($"OK, 1 todo(s): {pathOk}{nl}", consoleCapture.Output()); Assert.AreEqual(1, exitCode); }
public void TestInputFail() { using var tmpdir = new TemporaryDirectory(); string path = Path.Join(tmpdir.Path, "SomeProgram.cs"); System.IO.File.WriteAllText(path, "12345\n123456\n"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--inputs", path, "--max-line-length", "3", "--max-lines-in-file", "1" }); string nl = Environment.NewLine; Assert.AreEqual(1, exitCode); Assert.AreEqual( $"FAIL {path}{nl}" + $" * The following line(s) have more than allowed 3 characters:{nl}" + $" * Line 1: 5 characters{nl}" + $" * Line 2: 6 characters{nl}" + $" * The file is too long. It contains 2 line(s), but only 1 line(s) are allowed.{nl}", consoleCapture.Output()); Assert.AreEqual($"One or more input files failed the checks.{nl}", consoleCapture.Error()); }
public void TestExcludes() { using var tmpdir = new TemporaryDirectory(); string pathExcluded = Path.Join(tmpdir.Path, "ExcludedProgram.cs"); System.IO.File.WriteAllText(pathExcluded, "12345\n123456\n"); string pathIncluded = Path.Join(tmpdir.Path, "IncludedProgram.cs"); System.IO.File.WriteAllText(pathIncluded, "123\n"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--inputs", Path.Join(tmpdir.Path, "Included*.cs"), "--excludes", Path.Join(tmpdir.Path, "Excluded*.cs"), "--max-line-length", "3", "--max-lines-in-file", "1", "--verbose" }); string nl = Environment.NewLine; Assert.AreEqual(0, exitCode); Assert.AreEqual("", consoleCapture.Error()); Assert.AreEqual( $"OK {pathIncluded}{nl}", consoleCapture.Output()); }
public void TestIgnoreLinesMatching() { using var tmpdir = new TemporaryDirectory(); string path = Path.Join(tmpdir.Path, "SomeProgram.cs"); System.IO.File.WriteAllText(path, "12345\nAAA 123456\n123456\n"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--inputs", path, "--max-line-length", "3", "--ignore-lines-matching", "^AAA" }); string nl = Environment.NewLine; Assert.AreEqual(1, exitCode); Assert.AreEqual( $"FAIL {path}{nl}" + $" * The following line(s) have more than allowed 3 characters:{nl}" + $" * Line 1: 5 characters{nl}" + $" * Line 3: 6 characters{nl}" + $"The --ignore-lines-matching was set to: ^AAA{nl}", consoleCapture.Output()); Assert.AreEqual($"One or more input files failed the checks.{nl}", consoleCapture.Error()); }
public void TestCheckShouldntExist() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); string outputPath = Path.Join(output.FullName, "DocTestSomeProgram.cs"); File.WriteAllText(inputPath, "no code"); File.WriteAllText(outputPath, "unexpected content"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--input", input.FullName, "--output", output.FullName, "--check" }); string nl = Environment.NewLine; Assert.AreEqual(1, exitCode); Assert.AreEqual( $"No doctests found in: {inputPath}; the output should not exist: {outputPath}{nl}", consoleCapture.Output()); }
public void TestDoesntExist() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "DocTestSomeProgram.cs"); string programText = @" /// <code doctest=""true""> /// var x = 1; /// </code> "; var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( programText)); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; // Test pre-condition Assert.IsFalse(File.Exists(outputPath)); var got = Process.Check(doctests, "SomeProgram.cs", outputPath); Assert.IsInstanceOf <Report.DoesntExist>(got); }
public void Test() { using var tmpdir = new TemporaryDirectory(); string got = Process.InputPath("SomeProgram.cs", tmpdir.Path); Assert.AreEqual(Path.Join(tmpdir.Path, "SomeProgram.cs"), got); }
public void TestCheckDifferent() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); string outputPath = Path.Join(output.FullName, "DocTestSomeProgram.cs"); File.WriteAllText( inputPath, @"/// <code doctest=""true""> /// var x = 1; /// </code> "); File.WriteAllText(outputPath, "different content"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--input-output", $"{input.FullName}{Path.PathSeparator}{output.FullName}", "--check", "--verbose" }); string nl = Environment.NewLine; Assert.AreEqual(1, exitCode); Assert.AreEqual( $"Expected different content: {inputPath} -> {outputPath}{nl}" + $"Here is the diff between the expected content and the actual content:{nl}" + $"- // This file was automatically generated by doctest-csharp.{nl}" + $"+ different content{nl}" + $"- // !!! DO NOT EDIT OR APPEND !!!{nl}" + $"- {nl}" + $"- using NUnit.Framework;{nl}" + $"- {nl}" + $"- namespace Tests{nl}" + $"- {{{nl}" + $"- public class DocTest_SomeProgram_cs{nl}" + $"- {{{nl}" + $"- [Test]{nl}" + $"- public void AtLine0AndColumn4(){nl}" + $"- {{{nl}" + $"- var x = 1;{nl}" + $"- }}{nl}" + $"- }}{nl}" + $"- }}{nl}" + $"- {nl}" + $"- // This file was automatically generated by doctest-csharp.{nl}" + $"- // !!! DO NOT EDIT OR APPEND !!!{nl}" + $"- {nl}", consoleCapture.Output()); }
public static string InputPath(string relativePath, DirectoryInfo input) { if (Path.IsPathRooted(relativePath)) { throw new ArgumentException($"Expected a relative path, but got a rooted one: {relativePath}"); } return(Path.Join(input.FullName, relativePath)); }
private static int Handle(string[] inputs, FileInfo schema) { string schemaText = System.IO.File.ReadAllText(schema.FullName); var jSchema = Newtonsoft.Json.Schema.JSchema.Parse(schemaText); string cwd = System.IO.Directory.GetCurrentDirectory(); bool failed = false; foreach (string pattern in inputs) { IEnumerable <string> paths; if (Path.IsPathRooted(pattern)) { var root = Path.GetPathRoot(pattern); if (root == null) { throw new ArgumentException( $"Root could not be retrieved from rooted pattern: {pattern}"); } var relPattern = Path.GetRelativePath(root, pattern); paths = GlobExpressions.Glob.Files(root, relPattern) .Select((path) => Path.Join(root, relPattern)); } else { paths = GlobExpressions.Glob.Files(cwd, pattern) .Select((path) => Path.Join(cwd, path)); } foreach (string path in paths) { string text = System.IO.File.ReadAllText(path); var jObject = Newtonsoft.Json.Linq.JObject.Parse(text); bool valid = jObject.IsValid(jSchema, out IList <string> messages); if (!valid) { Console.Error.WriteLine($"FAIL: {path}"); foreach (string message in messages) { Console.Error.WriteLine(message); } failed = true; } else { Console.WriteLine($"OK: {path}"); } } } return((failed) ? 1 : 0); }
private static void WriteDummyFile(string prefix, string dirName, string subdirName, string name) { var parent = Path.Join(prefix, dirName, subdirName); System.IO.Directory.CreateDirectory(parent); var path = Path.Join(parent, name); System.IO.File.WriteAllText(path, "some content"); }
public void TestCheckOk() { using var tmpdir = new TemporaryDirectory(); DirectoryInfo input = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject")); DirectoryInfo output = Directory.CreateDirectory(Path.Join(tmpdir.Path, "SomeProject.Test/doctests")); string inputPath = Path.Join(input.FullName, "SomeProgram.cs"); string outputPath = Path.Join(output.FullName, "DocTestSomeProgram.cs"); File.WriteAllText( inputPath, @"/// <code doctest=""true""> /// var x = 1; /// </code> "); File.WriteAllText( outputPath, @"// This file was automatically generated by doctest-csharp. // !!! DO NOT EDIT OR APPEND !!! using NUnit.Framework; namespace Tests { public class DocTest_SomeProgram_cs { [Test] public void AtLine0AndColumn4() { var x = 1; } } } // This file was automatically generated by doctest-csharp. // !!! DO NOT EDIT OR APPEND !!! "); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode( new[] { "--input-output", $"{input.FullName}{Path.PathSeparator}{output.FullName}", "--check", "--verbose" }); string nl = Environment.NewLine; Assert.AreEqual(0, exitCode); Assert.AreEqual($"OK: {inputPath} -> {outputPath}{nl}", consoleCapture.Output()); }
public static string OutputPath(string relativePath, DirectoryInfo output) { if (Path.IsPathRooted(relativePath)) { throw new ArgumentException($"Expected a relative path, but got a rooted one: {relativePath}"); } string doctestRelativePath = Path.Join( Path.GetDirectoryName(relativePath), "DocTest" + Path.GetFileName(relativePath)); return(Path.Join(output.FullName, doctestRelativePath)); }
public void TestWithNonCodeInput() { using var tmpdir = new TemporaryDirectory(); using var consoleCapture = new ConsoleCapture(); string path = Path.Join(tmpdir.Path, "SomeProgram.cs"); File.WriteAllText(path, "this is not parsable C# code."); int exitCode = Program.MainWithCode(new[] { "--inputs", path }); Assert.AreEqual("", consoleCapture.Error()); Assert.AreEqual("", consoleCapture.Output()); Assert.AreEqual(0, exitCode); }
public void TestOkNoDoctest() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "Output.cs"); var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( "No doctest at all")); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; Process.Report got = Process.Check(doctests, outputPath); Assert.AreEqual(Process.Report.Ok, got); }
public void TestOkNoDoctest() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "DocTestSomeProgram.cs"); var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( "No doctest at all")); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; var got = Process.Check(doctests, "SomeProgram.cs", outputPath); Assert.IsInstanceOf <Report.Ok>(got); }
public void TestNoDoctest() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "DocTestSomeProgram.cs"); var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( "No doctest at all")); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; bool got = Process.Generate(doctests, "SomeProgram.cs", outputPath); Assert.IsFalse(got); Assert.IsFalse(File.Exists(outputPath)); }
public void TestShouldntExist() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "DocTestSomeProgram.cs"); var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( "no doctest")); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; File.WriteAllText(outputPath, "should not exist"); var got = Process.Check(doctests, "SomeProgram.cs", outputPath); Assert.IsInstanceOf <Report.ShouldNotExist>(got); }
public void TestWithValidTodoAndCaseInsensitive() { using var tmpdir = new TemporaryDirectory(); using var consoleCapture = new ConsoleCapture(); string nl = Environment.NewLine; string path = Path.Join(tmpdir.Path, "SomeProgram.cs"); File.WriteAllText(path, $"// todo (mristin, 2020-07-20): Do something!{nl}"); int exitCode = Program.MainWithCode(new[] { "--inputs", path, "--case-insensitive" }); Assert.AreEqual("", consoleCapture.Error()); Assert.AreEqual($"{path}:1:1:todo (mristin, 2020-07-20): Do something!{nl}", consoleCapture.Output()); Assert.AreEqual(0, exitCode); }
public void TestInputOk() { using var tmpdir = new TemporaryDirectory(); string path = Path.Join(tmpdir.Path, "SomeProgram.cs"); System.IO.File.WriteAllText(path, "123\n"); using var consoleCapture = new ConsoleCapture(); int exitCode = Program.MainWithCode(new[] { "--inputs", path, "--verbose" }); string nl = Environment.NewLine; Assert.AreEqual(0, exitCode); Assert.AreEqual( $"OK {path}{nl}", consoleCapture.Output()); }
public void TestOkDoctest() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "DocTestSomeProgram.cs"); string programText = @" /// <code doctest=""true""> /// var x = 1; /// </code> "; var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( programText)); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; File.WriteAllText(outputPath, @"// This file was automatically generated by doctest-csharp. // !!! DO NOT EDIT OR APPEND !!! using NUnit.Framework; namespace Tests { public class DocTest_SomeProgram_cs { [Test] public void AtLine1AndColumn4() { var x = 1; } } } // This file was automatically generated by doctest-csharp. // !!! DO NOT EDIT OR APPEND !!! "); var got = Process.Check(doctests, "SomeProgram.cs", outputPath); Assert.IsInstanceOf <Report.Ok>(got); }
public void TestThatItWorks() { var cases = new List <(string, string)> { ("SomeProgram.cs", "DocTestSomeProgram.cs"), (Path.Join("SomeSubdir", "SomeProgram.cs"), Path.Join("SomeSubdir", "DocTestSomeProgram.cs")) }; foreach ((string relativePath, string expectedRelativePath) in cases) { using var tmpdir = new TemporaryDirectory(); string absoluteOutputPath = Process.OutputPath( relativePath, tmpdir.Path); string relativeOutputPath = Path.GetRelativePath(tmpdir.Path, absoluteOutputPath); Assert.AreEqual(expectedRelativePath, relativeOutputPath); } }
public void TestThatFilesAreMatchedAsRootedPaths() { using var tmpdir = new TemporaryDirectory(); WriteDummyFile(tmpdir.Path, "a", "b", "Program.cs"); List <string> matchedFiles = Input.MatchFiles( tmpdir.Path, new List <string> { Path.Join(tmpdir.Path, "**", "*.cs") }, new List <string>()) .ToList(); var expectedFiles = new List <string>() { Path.Join(tmpdir.Path, "a", "b", "Program.cs") }; Assert.That(matchedFiles, Is.EquivalentTo(expectedFiles)); }
public void TestDoctest() { using var tmpdir = new TemporaryDirectory(); string outputPath = Path.Join(tmpdir.Path, "DocTestSomeProgram.cs"); var doctestsAndErrors = Extraction.Extract( CSharpSyntaxTree.ParseText( @"/// <code doctest=""true""> /// var x = 1; /// </code>")); Assert.AreEqual(0, doctestsAndErrors.Errors.Count); var doctests = doctestsAndErrors.Doctests; bool got = Process.Generate(doctests, "SomeProgram.cs", outputPath); Assert.IsTrue(got); Assert.IsTrue(File.Exists(outputPath)); Assert.AreEqual(@"// This file was automatically generated by doctest-csharp. // !!! DO NOT EDIT OR APPEND !!! using NUnit.Framework; namespace Tests { public class DocTest_SomeProgram_cs { [Test] public void AtLine0AndColumn4() { var x = 1; } } } // This file was automatically generated by doctest-csharp. // !!! DO NOT EDIT OR APPEND !!! ", File.ReadAllText(outputPath)); }