예제 #1
0
        private IEnumerable <string> ExtractProjectFolders(ProjectAnalyzerResult projectAnalyzerResult)
        {
            var projectFilePath  = projectAnalyzerResult.ProjectFilePath;
            var projectFile      = _fileSystem.File.OpenText(projectFilePath);
            var xDocument        = XDocument.Load(projectFile);
            var folders          = new List <string>();
            var projectDirectory = _fileSystem.Path.GetDirectoryName(projectFilePath);

            folders.Add(projectDirectory);

            foreach (var sharedProject in new ProjectFileReader().FindSharedProjects(xDocument))
            {
                var sharedProjectName = ReplaceMsbuildProperties(sharedProject, projectAnalyzerResult);

                if (!_fileSystem.File.Exists(_fileSystem.Path.Combine(projectDirectory, sharedProjectName)))
                {
                    throw new FileNotFoundException($"Missing shared project {sharedProjectName}");
                }

                var directoryName = _fileSystem.Path.GetDirectoryName(sharedProjectName);
                folders.Add(_fileSystem.Path.Combine(projectDirectory, directoryName));
            }

            return(folders);
        }
        private CSharpParseOptions BuildCsharpParseOptions(ProjectAnalyzerResult analyzerResult, StrykerOptions options)
        {
            var preprocessorSymbols = analyzerResult.DefineConstants;

            var cSharpParseOptions = new CSharpParseOptions(options.LanguageVersion, DocumentationMode.None, preprocessorSymbols: preprocessorSymbols);

            return(cSharpParseOptions);
        }
예제 #3
0
        private FolderComposite FindProjectFilesUsingBuildAlyzer(ProjectAnalyzerResult analyzerResult)
        {
            var inputFiles            = new FolderComposite();
            var projectUnderTestDir   = Path.GetDirectoryName(analyzerResult.ProjectFilePath);
            var projectRoot           = Path.GetDirectoryName(projectUnderTestDir);
            var generatedAssemblyInfo =
                (_fileSystem.Path.GetFileNameWithoutExtension(analyzerResult.ProjectFilePath) + ".AssemblyInfo.cs").ToLowerInvariant();
            var rootFolderComposite = new FolderComposite()
            {
                Name         = string.Empty,
                FullPath     = projectRoot,
                RelativePath = string.Empty,
                RelativePathToProjectFile =
                    Path.GetRelativePath(projectUnderTestDir, projectUnderTestDir)
            };
            var cache = new Dictionary <string, FolderComposite> {
                [string.Empty] = rootFolderComposite
            };

            inputFiles.Add(rootFolderComposite);
            foreach (var sourceFile in analyzerResult.SourceFiles)
            {
                if (sourceFile.EndsWith(".xaml.cs"))
                {
                    continue;
                }

                if (_fileSystem.Path.GetFileName(sourceFile).ToLowerInvariant() == generatedAssemblyInfo)
                {
                    continue;
                }

                var relativePath    = Path.GetRelativePath(projectUnderTestDir, sourceFile);
                var folderComposite = GetOrBuildFolderComposite(cache, Path.GetDirectoryName(relativePath), projectUnderTestDir,
                                                                projectRoot, inputFiles);
                var fileName = Path.GetFileName(sourceFile);
                folderComposite.Add(new FileLeaf()
                {
                    SourceCode   = _fileSystem.File.ReadAllText(sourceFile),
                    Name         = _fileSystem.Path.GetFileName(sourceFile),
                    RelativePath = _fileSystem.Path.Combine(folderComposite.RelativePath, fileName),
                    FullPath     = sourceFile,
                    RelativePathToProjectFile = Path.GetRelativePath(projectUnderTestDir, sourceFile)
                });
            }

            return(inputFiles);
        }
예제 #4
0
        private static string ReplaceMsbuildProperties(string projectReference, ProjectAnalyzerResult projectAnalyzerResult)
        {
            var propertyRegex = new Regex(@"\$\(([a-zA-Z_][a-zA-Z0-9_\-.]*)\)");
            var properties    = projectAnalyzerResult.Properties;

            return(propertyRegex.Replace(projectReference,
                                         m =>
            {
                var property = m.Groups[1].Value;
                if (properties.TryGetValue(property, out var propertyValue))
                {
                    return propertyValue;
                }

                var message = $"Missing MSBuild property ({property}) in project reference ({projectReference}). Please check your project file ({projectAnalyzerResult.ProjectFilePath}) and try again.";
                throw new StrykerInputException(message);
            }));
        }
예제 #5
0
        private FolderComposite FindProjectFilesScanningProjectFolders(ProjectAnalyzerResult analyzerResult)
        {
            var inputFiles          = new FolderComposite();
            var projectUnderTestDir = Path.GetDirectoryName(analyzerResult.ProjectFilePath);

            foreach (var dir in ExtractProjectFolders(analyzerResult))
            {
                var folder = _fileSystem.Path.Combine(Path.GetDirectoryName(projectUnderTestDir), dir);

                _logger.LogDebug($"Scanning {folder}");
                if (!_fileSystem.Directory.Exists(folder))
                {
                    throw new DirectoryNotFoundException($"Can't find {folder}");
                }

                inputFiles.Add(FindInputFiles(folder, projectUnderTestDir));
            }

            return(inputFiles);
        }
예제 #6
0
 public string GetTestBinariesPath(ProjectAnalyzerResult projectAnalyzerResult)
 {
     return(projectAnalyzerResult.AssemblyPath);
 }
예제 #7
0
 public string GetInjectionPath(ProjectAnalyzerResult projectAnalyzerResult)
 {
     return(Path.Combine(Path.GetDirectoryName(FilePathUtils.NormalizePathSeparators(projectAnalyzerResult.AssemblyPath)), Path.GetFileName(ProjectUnderTestAnalyzerResult.AssemblyPath)));
 }
        /// <summary>
        /// Recursively scans the given directory for files to mutate
        /// </summary>
        private FolderComposite FindInputFiles(string path, string projectUnderTestDir, ProjectAnalyzerResult analyzerResult, StrykerOptions options)
        {
            var rootFolderComposite = new FolderComposite
            {
                Name         = Path.GetFileName(path),
                FullPath     = Path.GetFullPath(path),
                RelativePath = Path.GetFileName(path),
                RelativePathToProjectFile = Path.GetRelativePath(projectUnderTestDir, Path.GetFullPath(path))
            };

            CSharpParseOptions cSharpParseOptions = BuildCsharpParseOptions(analyzerResult, options);

            InjectMutantHelpers(rootFolderComposite, cSharpParseOptions);

            rootFolderComposite.Add(
                FindInputFiles(path, Path.GetDirectoryName(analyzerResult.ProjectFilePath), rootFolderComposite.RelativePath, cSharpParseOptions)
                );
            return(rootFolderComposite);
        }
        private FolderComposite FindProjectFilesUsingBuildalyzer(ProjectAnalyzerResult analyzerResult, StrykerOptions options)
        {
            var inputFiles            = new FolderComposite();
            var projectUnderTestDir   = Path.GetDirectoryName(analyzerResult.ProjectFilePath);
            var projectRoot           = Path.GetDirectoryName(projectUnderTestDir);
            var generatedAssemblyInfo = (_fileSystem.Path.GetFileNameWithoutExtension(analyzerResult.ProjectFilePath) + ".AssemblyInfo.cs").ToLowerInvariant();
            var rootFolderComposite   = new FolderComposite()
            {
                Name         = string.Empty,
                FullPath     = projectRoot,
                RelativePath = string.Empty,
                RelativePathToProjectFile = Path.GetRelativePath(projectUnderTestDir, projectUnderTestDir)
            };
            var cache = new Dictionary <string, FolderComposite> {
                [string.Empty] = rootFolderComposite
            };

            inputFiles.Add(rootFolderComposite);

            CSharpParseOptions cSharpParseOptions = BuildCsharpParseOptions(analyzerResult, options);

            InjectMutantHelpers(rootFolderComposite, cSharpParseOptions);

            foreach (var sourceFile in analyzerResult.SourceFiles)
            {
                // Skip xamarin UI generated files
                if (sourceFile.EndsWith(".xaml.cs"))
                {
                    continue;
                }

                // Skip assembly info
                if (_fileSystem.Path.GetFileName(sourceFile).ToLowerInvariant() == generatedAssemblyInfo)
                {
                    continue;
                }

                var relativePath    = Path.GetRelativePath(projectUnderTestDir, sourceFile);
                var folderComposite = GetOrBuildFolderComposite(cache, Path.GetDirectoryName(relativePath), projectUnderTestDir, projectRoot, inputFiles);
                var fileName        = Path.GetFileName(sourceFile);

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

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

                // don't mutate auto generated code
                if (syntaxTree.IsGenerated())
                {
                    _logger.LogDebug("Skipping auto-generated code file: {fileName}", file.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
                }


                file.SyntaxTree = syntaxTree;
                folderComposite.Add(file);
            }

            return(inputFiles);
        }
예제 #10
0
 private static CSharpParseOptions BuildCsharpParseOptions(ProjectAnalyzerResult analyzerResult, StrykerOptions options)
 {
     return(new CSharpParseOptions(options.LanguageVersion, DocumentationMode.None, preprocessorSymbols: analyzerResult.DefineConstants));
 }