Ejemplo n.º 1
0
 private static void InjectMutantHelpers(FolderComposite rootFolderComposite, CSharpParseOptions cSharpParseOptions)
 {
     foreach (var(name, code) in CodeInjection.MutantHelpers)
     {
         rootFolderComposite.AddCompilationSyntaxTree(CSharpSyntaxTree.ParseText(code, path: name, encoding: Encoding.UTF32, options: cSharpParseOptions));
     }
 }
        /// <summary>
        /// Recursively scans the given directory for files to mutate
        /// </summary>
        private FolderComposite FindInputFiles(string path, string projectUnderTestDir, string parentFolder, CSharpParseOptions cSharpParseOptions)
        {
            var lastPathComponent = Path.GetFileName(path);

            var folderComposite = new FolderComposite
            {
                Name         = lastPathComponent,
                FullPath     = Path.GetFullPath(path),
                RelativePath = Path.Combine(parentFolder, lastPathComponent),
                RelativePathToProjectFile = Path.GetRelativePath(projectUnderTestDir, Path.GetFullPath(path))
            };

            foreach (var folder in _fileSystem.Directory.EnumerateDirectories(folderComposite.FullPath).Where(x => !_foldersToExclude.Contains(Path.GetFileName(x))))
            {
                folderComposite.Add(FindInputFiles(folder, projectUnderTestDir, folderComposite.RelativePath, cSharpParseOptions));
            }
            foreach (var file in _fileSystem.Directory.GetFiles(folderComposite.FullPath, "*.cs", SearchOption.TopDirectoryOnly))
            {
                // Roslyn cannot compile xaml.cs files generated by xamarin.
                // Since the files are generated they should not be mutated anyway, so skip these files.
                if (!file.EndsWith(".xaml.cs"))
                {
                    var fileName = Path.GetFileName(file);

                    var fileLeaf = new FileLeaf()
                    {
                        SourceCode   = _fileSystem.File.ReadAllText(file),
                        Name         = _fileSystem.Path.GetFileName(file),
                        RelativePath = Path.Combine(folderComposite.RelativePath, fileName),
                        FullPath     = file,
                        RelativePathToProjectFile = Path.GetRelativePath(projectUnderTestDir, file)
                    };

                    // Get the syntax tree for the source file
                    var syntaxTree = CSharpSyntaxTree.ParseText(fileLeaf.SourceCode,
                                                                path: fileLeaf.FullPath,
                                                                options: cSharpParseOptions);

                    // don't mutate auto generated code
                    if (syntaxTree.IsGenerated())
                    {
                        _logger.LogDebug("Skipping auto-generated code file: {fileName}", fileLeaf.Name);
                        folderComposite.AddCompilationSyntaxTree(syntaxTree); // Add the syntaxTree to the list of compilationSyntaxTrees
                        continue;                                             // Don't add the file to the folderComposite as we're not reporting on the file
                    }

                    fileLeaf.SyntaxTree = syntaxTree;

                    folderComposite.Add(fileLeaf);
                }
            }

            return(folderComposite);
        }