Пример #1
0
        public void CompareProject(CompareProject inProject, string inOutput)
        {
            if (inProject == null)
            {
                return;
            }

            StringBuilder projectLog = new StringBuilder();

            Console.WriteLine($"Starting comparision for project {inProject.Name}");
            projectLog.AppendLine($"Starting comparision for project {inProject.Name}");

            List <string>             ProjectFileNames      = new List <string>();
            Dictionary <string, bool> FileComparisionResult = new Dictionary <string, bool>();
            // Use first path as reference
            string ReferencePath = inProject.Paths[0];

            FileInfo[] files = (new DirectoryInfo(ReferencePath)).GetFiles("*.json");
            foreach (FileInfo info in files)
            {
                ProjectFileNames.Add(info.Name);
            }

            // Compare files
            foreach (string fileName in ProjectFileNames)
            {
                projectLog.AppendLine($"Comparing file: {fileName}");
                // Load files
                List <JToken> filesToCompare = new List <JToken>();
                foreach (string baseFolder in inProject.Paths)
                {
                    try
                    {
                        filesToCompare.Add(Utils.LoadJsonFromFile($"{baseFolder}\\{fileName}"));
                    } catch (FileNotFoundException)
                    {
                        projectLog.AppendLine($"File: {baseFolder}\\{fileName} does not exists.");
                    }
                }

                // use first file as base for comparision
                JToken baseFile = filesToCompare[0];
                bool   isMatch  = true;

                foreach (JToken jsonFile in filesToCompare)
                {
                    if (baseFile == jsonFile)
                    {
                        continue;
                    }

                    isMatch = JToken.DeepEquals(baseFile, jsonFile);

                    projectLog.AppendLine($"Are files equal? {isMatch}");
                }

                FileComparisionResult.Add(fileName, isMatch);
            }

            // Check for potential failures
            var failedFiles = FileComparisionResult.Where(f => f.Value == false);

            foreach (var failedFile in failedFiles)
            {
                projectLog.AppendLine($"File: {failedFile.Key} does not match reference file [comparision result: {failedFile.Value}]");
            }

            // Write log file
            using (StreamWriter sw = new StreamWriter($"{inOutput}\\{inProject.Name}.log"))
            {
                sw.Write(projectLog.ToString());
                sw.Close();
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            if (ContainsArg("--generateProject", args))
            {
                bool skipHandcrafted = ContainsArg("--skipHandcrafted", args);

                if (skipHandcrafted)
                {
                    Console.WriteLine("Skipping handcrafted queries");
                }

                Dictionary <string, List <string> > queryAndPaths = new Dictionary <string, List <string> >();

                DirectoryInfo projectDir = Directory.CreateDirectory(ProjectFileDir);;

                foreach (DirectoryInfo queryDir in projectDir.EnumerateDirectories())
                {
                    if (queryDir.Name.Contains("handcrafted") && skipHandcrafted)
                    {
                        continue;
                    }
                    string dirName   = queryDir.Name.Replace("_handcrafted", "");
                    string queryName = dirName.Substring(0, dirName.LastIndexOf("_"));

                    if (queryAndPaths.ContainsKey(queryName))
                    {
                        List <string> queryPaths = queryAndPaths[queryName];
                        queryPaths.Add(queryDir.FullName);
                    }
                    else
                    {
                        queryAndPaths.Add(queryName, new List <string>()
                        {
                            queryDir.FullName
                        });
                    }
                }

                ProjectFile proj = new ProjectFile();
                proj.Output = DefaultLogDir;

                foreach (KeyValuePair <string, List <string> > queryAndPath in queryAndPaths)
                {
                    CompareProject compareProj = new CompareProject();
                    compareProj.Name  = queryAndPath.Key;
                    compareProj.Paths = queryAndPath.Value;

                    proj.CompareProjects.Add(compareProj);
                }

                using (StreamWriter sw = new StreamWriter(ProjectFilePath, false))
                {
                    sw.Write(JsonConvert.SerializeObject(proj));
                    sw.Close();
                }

                Console.WriteLine("Project file created.");
            }

            // Load project file
            ProjectFile  project  = ProjectFile.FromFile(ProjectFilePath);
            JsonComparer comparer = new JsonComparer();

            foreach (CompareProject cp in project.CompareProjects)
            {
                comparer.CompareProject(cp, project.Output);
            }
        }