Exemplo n.º 1
0
        public void logErrorTest()
        {
            var codeAnaliser = new CodeAnalyser(@"D:/Main/Service/Services/ServicesR1/Cdiscount.Business.Stock/Cdiscount.Business.Stock.csproj",
                                                new List <string> {
                "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\Stock.cs", "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\StockMiddle.cs"
            });

            PrivateObject accessor = new PrivateObject(codeAnaliser);

            CheckReadOnlyDALResultMessage message = new CheckReadOnlyDALResultMessage();

            accessor.Invoke("logError", new object[] { message, "msg", "file1.cs", 5, "DALType", "spTest" });

            Assert.AreEqual(1, message.ErrorMessages.Count);
            Assert.AreEqual("msg", message.ErrorMessages[0]);

            Assert.AreEqual(1, message.SourceFileNames.Count);
            Assert.AreEqual("file1.cs", message.SourceFileNames[0]);

            Assert.AreEqual(1, message.SourceLineNumbers.Count);
            Assert.AreEqual(5, message.SourceLineNumbers[0]);

            Assert.AreEqual(1, message.TypeOfDALobjects.Count);
            Assert.AreEqual("DALType", message.TypeOfDALobjects[0]);

            Assert.AreEqual(1, message.StoredProcedureNames.Count);
            Assert.AreEqual("spTest", message.StoredProcedureNames[0]);
        }
Exemplo n.º 2
0
        public void CodeAnaliserTest()
        {
            Dictionary <string, List <string> > projToFilesDict = targetFilesFetcher.getProjToSrcFilesDict("D:/Main/Service/Services/ServicesR1/Cdiscount.Business.Stock");

            var myEnum = projToFilesDict.Keys.GetEnumerator();

            Assert.IsTrue(myEnum.MoveNext());

            string key = myEnum.Current;

            var codeAnaliser = new CodeAnalyser(key, projToFilesDict[key]);

            Assert.IsNotNull(codeAnaliser);
        }
Exemplo n.º 3
0
        public void getStoredProcNameTest()
        {
            var codeAnaliser = new CodeAnalyser(@"D:/Main/Service/Services/ServicesR1/Cdiscount.Business.Stock/Cdiscount.Business.Stock.csproj",
                                                new List <string> {
                "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\Stock.cs", "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\StockMiddle.cs"
            });

            PrivateObject accessor = new PrivateObject(codeAnaliser);

            ISymbol iSymbol = (ISymbol)accessor.Invoke("getNextReadOnlyDALCalledMethod");

            string spName = (string)accessor.Invoke("getStoredProcName", iSymbol);

            Assert.AreEqual("dbo.ps_cds_stock_getStockOrderLines", spName);
        }
Exemplo n.º 4
0
        public void getInitCommandCollectionMethodFromCalledMethodTest()
        {
            var codeAnaliser = new CodeAnalyser(@"D:/Main/Service/Services/ServicesR1/Cdiscount.Business.Stock/Cdiscount.Business.Stock.csproj",
                                                new List <string> {
                "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\Stock.cs", "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\StockMiddle.cs"
            });

            PrivateObject accessor = new PrivateObject(codeAnaliser);
            ISymbol       iSymbol  = (ISymbol)accessor.Invoke("getNextReadOnlyDALCalledMethod");

            SyntaxNode syntaxNode = iSymbol.DeclaringSyntaxReferences[0].GetSyntaxAsync().Result;

            MethodDeclarationSyntax initCommandCollectionMethodDeclarationSyntax = (MethodDeclarationSyntax)accessor.Invoke("getInitCommandCollectionMethodFromCalledMethod", syntaxNode);

            Assert.IsNotNull(initCommandCollectionMethodDeclarationSyntax);
            Assert.AreEqual("InitCommandCollection", initCommandCollectionMethodDeclarationSyntax.Identifier.ValueText);
        }
Exemplo n.º 5
0
        public void getNextReadOnlyDALtypeAndCalledMethodTest()
        {
            var codeAnaliser = new CodeAnalyser(@"D:/Main/Service/Services/ServicesR1/Cdiscount.Business.Stock/Cdiscount.Business.Stock.csproj",
                                                new List <string> {
                "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\Stock.cs", "D:\\Main\\Service\\Services\\ServicesR1\\Cdiscount.Business.Stock\\StockMiddle.cs"
            });

            Assert.IsNotNull(codeAnaliser);

            string        resultType, methodName;
            ISymbol       iSymbol;
            PrivateObject accessor = new PrivateObject(codeAnaliser);

            iSymbol    = (ISymbol)accessor.Invoke("getNextReadOnlyDALCalledMethod");
            resultType = iSymbol.ContainingType.ToString();
            methodName = iSymbol.MetadataName;
            Assert.AreEqual("Cdiscount.Dal.Stock.StockMiddleDataSetTableAdapters.StockOrderDal", resultType);
            Assert.AreEqual("GetData", methodName);

            iSymbol    = (ISymbol)accessor.Invoke("getNextReadOnlyDALCalledMethod");
            resultType = iSymbol.ContainingType.ToString();
            methodName = iSymbol.MetadataName;
            Assert.AreEqual("Cdiscount.Dal.Stock.StockMiddleDataSetTableAdapters.WarehouseDal", resultType);
            Assert.AreEqual("GetData", methodName);

            iSymbol    = (ISymbol)accessor.Invoke("getNextReadOnlyDALCalledMethod");
            resultType = iSymbol.ContainingType.ToString();
            methodName = iSymbol.MetadataName;
            Assert.AreEqual("Cdiscount.Dal.Stock.StockMiddleDataSetTableAdapters.WarehouseDal", resultType);
            Assert.AreEqual("GetDataById", methodName);

            iSymbol    = (ISymbol)accessor.Invoke("getNextReadOnlyDALCalledMethod");
            resultType = iSymbol.ContainingType.ToString();
            methodName = iSymbol.MetadataName;
            Assert.AreEqual("Cdiscount.Bll.Stock.Dal.WarehouseDataSetTableAdapters.StockWarehouseDal", resultType);
            Assert.AreEqual("GetData", methodName);
        }
        public SolutionTree()
        {
            InitializeComponent();

            _codeAnalyser = new CodeAnalyser();
        }
        public void PerformAnalysis(string rootFolder)
        {
            Console.WriteLine("Analyzing commits...");
            using (var repo = new Repository(rootFolder))
            {
                var analysis     = new Analysis();
                var renamedFiles = new Dictionary <string, string>();
                var cyclomaticComplexityCounter = new CyclomaticComplexityCounter();
                var linesOfCodeCalculator       = new LinesOfCodeCalculator();
                var typeScriptAst = new TypeScriptAST();
                foreach (var tag in repo.Tags)
                {
                    var commit     = repo.Lookup <Commit>(tag.Target.Sha);
                    var commitDate = commit.Author.When.UtcDateTime.Date;
                    analysis.Tags.Add(commitDate, tag.FriendlyName);
                }
                foreach (var branch in repo.Branches.Where(br => br.IsRemote))
                {
                    analysis.Branches.Add(branch.FriendlyName);
                }
                foreach (var commit in repo.Commits)
                {
                    var username   = commit.Author.Name;
                    var commitDate = commit.Author.When.UtcDateTime.Date;
                    UpdateAnalysisCommitDates(analysis, commitDate);
                    IncDictionaryValue(analysis.CommitsEachDay, commitDate);
                    foreach (var parent in commit.Parents)
                    {
                        var patch = repo.Diff.Compare <Patch>(parent.Tree, commit.Tree);
                        IncDictionaryValue(analysis.LinesOfCodeAddedEachDay, commitDate, patch.LinesAdded);
                        IncDictionaryValue(analysis.LinesOfCodeDeletedEachDay, commitDate, patch.LinesDeleted);
                        foreach (TreeEntryChanges change in repo.Diff.Compare <TreeChanges>(parent.Tree, commit.Tree))
                        {
                            int cyclomaticComplexity = 0;
                            int methodCount          = 0;
                            var fullPath             = Path.Combine(rootFolder, change.Path);
                            if (change.Path != change.OldPath)
                            {
                                if (analysis.FileCommits.ContainsKey(change.OldPath))
                                {
                                    analysis.FileCommits[change.Path] = analysis.FileCommits[change.OldPath];
                                    analysis.FileCommits.Remove(change.OldPath);
                                }
                                if (!renamedFiles.ContainsKey(change.OldPath))
                                {
                                    renamedFiles.Add(change.OldPath, change.Path);
                                }
                            }
                            string filename = renamedFiles.ContainsKey(change.OldPath) ? renamedFiles[change.OldPath] : change.Path;
                            var    fileType = Path.GetExtension(filename);
                            if (IgnoreFiletype(fileType))
                            {
                                break;
                            }

                            if (analysis.FileCommits.ContainsKey(filename))
                            {
                                analysis.FileCommits[filename].CommitCount++;
                            }
                            else
                            {
                                int linesOfCode = 0;
                                var fileExists  = fileHandling.FileExists(fullPath);
                                if (fileExists)
                                {
                                    var fileContents = fileHandling.ReadFileContent(fullPath);
                                    linesOfCode = linesOfCodeCalculator.Calculate(fileContents);
                                    if (change.Path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
                                    {
                                        var syntaxTree            = CodeAnalyser.GetSyntaxTree(fileContents);
                                        var methodDeclarationNode = CodeAnalyser.GetMethodDeclarationSyntaxe(syntaxTree);
                                        cyclomaticComplexity = cyclomaticComplexityCounter.Calculate(methodDeclarationNode, syntaxTree);
                                        methodCount          = MethodCounter.Calculate(methodDeclarationNode);
                                    }
                                    else if (change.Path.EndsWith(".ts", StringComparison.OrdinalIgnoreCase))
                                    {
                                        methodCount = MethodCounter.Calculate(typeScriptAst, fileContents);
                                    }
                                    analysis.LinesOfCodeanalyzed += linesOfCode;
                                }
                                analysis.FileCommits[filename] = new FileStat {
                                    Filename = filename, CyclomaticComplexity = cyclomaticComplexity, LinesOfCode = linesOfCode, MethodCount = methodCount, FileExists = fileExists
                                };
                                IncDictionaryValue(analysis.FileTypes, fileType);
                            }
                            analysis.FileCommits[filename].CommitDates.Add(commitDate);
                            if (analysis.FileCommits[filename].LatestCommit < commitDate)
                            {
                                analysis.FileCommits[filename].LatestCommit = commitDate;
                            }
                            IncDictionaryValue(analysis.CodeAge, analysis.FileCommits[filename].CodeAge);

                            var usernameFilename = UsernameFilename.GetDictKey(filename, username);
                            if (analysis.UserfileCommits.ContainsKey(usernameFilename))
                            {
                                analysis.UserfileCommits[usernameFilename].CommitCount++;
                            }
                            else
                            {
                                analysis.UserfileCommits[usernameFilename] = new FileStat {
                                    Filename = filename, Username = username
                                };
                            }
                            analysis.UserfileCommits[usernameFilename].CommitDates.Add(commitDate);
                        }
                    }
                }
                var folderStats = new Dictionary <string, int>();
                foreach (var fileChange in analysis.FileCommits)
                {
                    int    commitCount = fileChange.Value.CommitCount;
                    var    folders     = fileChange.Key.Split("/");
                    string root        = folders[0];
                    if (fileChange.Key.IndexOf("/") == -1)
                    {
                        root = ".";
                    }
                    IncFolderCommitValue(analysis.FolderCommits, root, commitCount);
                    analysis.FolderCommits[root].IsRoot = true;
                    string currentFolder = root;
                    var    children      = analysis.FolderCommits[currentFolder].Children;
                    for (int i = 1; i < folders.Length; i++)
                    {
                        currentFolder = folders[i];
                        IncFolderCommitValue(children, currentFolder, commitCount);
                        children = children[currentFolder].Children;
                    }
                    var codeAge = fileChange.Value.CommitDates.OrderByDescending(cd => cd).First();
                }
                var o = analysis.FolderCommits.OrderBy(p => p.Key);
                analysis.AnalysisTime = (DateTime.UtcNow.Ticks - analysis.CreatedDate.Ticks) / 10000; // Analysis time in milliseconds
                foreach (var report in reports)
                {
                    report.Generate(analysis);
                }
            }
        }