コード例 #1
0
        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);
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        /// <summary>
        /// Matches all the files defined by the patterns, includes and excludes.
        /// If any of the patterns is given as a relative directory,
        /// current working directory is prepended.
        /// </summary>
        /// <param name="cwd">current working directory</param>
        /// <param name="patterns">GLOB patterns to match files for inspection</param>
        /// <param name="excludes">GLOB patterns to exclude files matching patterns</param>
        /// <returns>Paths of the matched files</returns>
        public static IEnumerable <string> MatchFiles(
            string cwd,
            List <string> patterns,
            List <string> excludes)
        {
            ////
            // Pre-condition(s)
            ////

            if (cwd.Length == 0)
            {
                throw new ArgumentException("Expected a non-empty cwd");
            }

            if (!Path.IsPathRooted(cwd))
            {
                throw new ArgumentException("Expected cwd to be rooted");
            }

            ////
            // Implementation
            ////

            if (patterns.Count == 0)
            {
                yield break;
            }

            var globExcludes = excludes.Select(
                (pattern) =>
            {
                string rootedPattern = (Path.IsPathRooted(pattern))
                        ? pattern
                        : Path.Join(cwd, pattern);

                return(new GlobExpressions.Glob(rootedPattern));
            }).ToList();

            foreach (var pattern in patterns)
            {
                IEnumerable <string>?files;

                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);

                    files = GlobExpressions.Glob.Files(root, relPattern)
                            .Select((path) => Path.Join(root, path));
                }
                else
                {
                    files = GlobExpressions.Glob.Files(cwd, pattern);
                }

                List <string> accepted =
                    files
                    .Where((path) =>
                {
                    string rootedPath = (Path.IsPathRooted(path))
                                ? path
                                : Path.Join(cwd, path);

                    return(globExcludes.TrueForAll((glob) => !glob.IsMatch(rootedPath)));
                })
                    .ToList();

                accepted.Sort(StringComparer.InvariantCulture);

                foreach (string path in accepted)
                {
                    yield return(path);
                }
            }
        }
コード例 #4
0
        private static bool ValidateInputs()
        {
            // needs to get refactored with EnumerateFiles function and search pattern like *.xml - looks not right
            string cwd   = System.IO.Directory.GetCurrentDirectory();
            var    valid = xmlInputs.Count() > 0;

            foreach (string pattern in xmlInputs)
            {
                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)
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ValidationType = ValidationType.Schema;
                    settings.Schemas        = xmlSchemaSet;

                    var messages = new List <string>();
                    settings.ValidationEventHandler += (object sender, ValidationEventArgs e) =>
                    {
                        messages.Add(e.Message);
                    };

                    XmlReader reader = XmlReader.Create(path, settings);

                    while (reader.Read())
                    {
                        // Invoke callbacks
                    }
                    ;

                    if (messages.Count > 0)
                    {
                        Console.Error.WriteLine($"FAIL: {path}");
                        foreach (string message in messages)
                        {
                            Console.Error.WriteLine(message);
                        }

                        valid = valid & false;
                    }
                    else
                    {
                        Console.WriteLine($"OK: {path}");
                        valid = valid & true;
                    }
                }
            }

            return(valid);
        }