Exemple #1
0
 public static string GetAssemblyName(this IAnalyzerResult analyzerResult)
 {
     return(FilePathUtils.NormalizePathSeparators(analyzerResult.Properties["AssemblyName"]));
 }
        public void ScanDiffReturnsListOfFiles_ExcludingFilesInDiffIgnoreFiles_Multi_Asterisk()
        {
            // Arrange
            var diffIgnoreFiles = new string[1];

            diffIgnoreFiles[0] = "**/file.cs";

            var basePath = FilePathUtils.NormalizePathSeparators("/c/Users/JohnDoe/Project/Tests");
            var options  = new StrykerOptions(gitDiffTarget: "d670460b4b4aece5915caf5c68d12f560a9fe3e4", basePath: basePath, fileSystem: new MockFileSystem(), diffIgnoreFiles: diffIgnoreFiles);

            var gitInfoMock                    = new Mock <IGitInfoProvider>();
            var repositoryMock                 = new Mock <IRepository>(MockBehavior.Loose);
            var commitMock                     = new Mock <Commit>(MockBehavior.Loose);
            var branchMock                     = new Mock <Branch>(MockBehavior.Strict);
            var patchMock                      = new Mock <Patch>(MockBehavior.Strict);
            var patchEntryChangesMock          = new Mock <PatchEntryChanges>(MockBehavior.Strict);
            var patchEntryChangesGitIgnoreMock = new Mock <PatchEntryChanges>(MockBehavior.Strict);

            // Setup of mocks
            commitMock
            .SetupGet(x => x.Tree)
            .Returns(new Mock <Tree>(MockBehavior.Loose).Object);

            branchMock
            .SetupGet(x => x.Tip)
            .Returns(commitMock.Object);

            branchMock.SetupGet(x => x.CanonicalName).Returns("refs/heads/branch");

            branchMock.SetupGet(x => x.FriendlyName).Returns("branch");

            repositoryMock
            .Setup(x => x.Branches.GetEnumerator())
            .Returns(new List <Branch> {
                branchMock.Object
            }.GetEnumerator())
            .Verifiable();

            patchEntryChangesMock
            .SetupGet(x => x.Path)
            .Returns("/c/Users/JohnDoe/Project/file.cs");

            patchMock
            .Setup(x => x.GetEnumerator())
            .Returns(((IEnumerable <PatchEntryChanges>) new List <PatchEntryChanges>
            {
                patchEntryChangesMock.Object
            }).GetEnumerator());

            repositoryMock
            .Setup(x => x.Diff.Compare <Patch>(It.IsAny <Tree>(), DiffTargets.WorkingDirectory))
            .Returns(patchMock.Object);

            repositoryMock
            .Setup(x => x.Lookup(It.IsAny <ObjectId>())).Returns(commitMock.Object);

            gitInfoMock.Setup(x => x.DetermineCommit()).Returns(commitMock.Object);

            gitInfoMock.SetupGet(x => x.Repository).Returns(repositoryMock.Object);
            gitInfoMock.SetupGet(x => x.RepositoryPath).Returns("/c/Path/To/Repo");
            var target = new GitDiffProvider(options, gitInfoMock.Object);

            // Act
            var res = target.ScanDiff();

            // Assert
            res.ChangedSourceFiles.Count().ShouldBe(0);
        }
Exemple #3
0
 public static string GetAssemblyPath(this IAnalyzerResult analyzerResult)
 {
     return(FilePathUtils.NormalizePathSeparators(Path.Combine(
                                                      FilePathUtils.NormalizePathSeparators(analyzerResult.Properties["TargetDir"]),
                                                      FilePathUtils.NormalizePathSeparators(analyzerResult.Properties["TargetFileName"]))));
 }
 public string GetInjectionPath(ProjectAnalyzerResult testProject)
 {
     return(Path.Combine(Path.GetDirectoryName(FilePathUtils.NormalizePathSeparators(testProject.AssemblyPath)),
                         Path.GetFileName(ProjectUnderTestAnalyzerResult.AssemblyPath)));
 }
Exemple #5
0
        public IEnumerable <string> FindSharedProjects(XDocument document)
        {
            var projectReferenceElements = document.Elements().Descendants().Where(x => string.Equals(x.Name.LocalName, "Import", StringComparison.OrdinalIgnoreCase));

            return(projectReferenceElements.SelectMany(x => x.Attributes(XName.Get("Project"))).Select(y => FilePathUtils.ConvertPathSeparators(y.Value)));
        }
Exemple #6
0
        public string GetDefaultVsTestExtensionsPath(string vstestToolPath)
        {
            string vstestMainPath = vstestToolPath.Substring(0, vstestToolPath.LastIndexOf(FilePathUtils.ConvertPathSeparators("\\")));
            string extensionPath  = Path.Combine(vstestMainPath, "Extensions");

            if (_fileSystem.Directory.Exists(extensionPath))
            {
                return(extensionPath);
            }
            else
            {
                throw new FileNotFoundException("VsTest test framework adapters not found at " + extensionPath);
            }
        }
Exemple #7
0
        public void InputFileResolver_ShouldChooseGivenTestProjectFileIfPossible_AtRelativeLocation()
        {
            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { Path.Combine(_filesystemRoot, "ExampleProject", "ExampleProject.csproj"), new MockFileData(defaultTestProjectFileContents) },
                { Path.Combine(_filesystemRoot, "ExampleProject", "SubFolder", "TestProject.csproj"), new MockFileData(defaultTestProjectFileContents) },
                { Path.Combine(_filesystemRoot, "ExampleProject", "Recursive.cs"), new MockFileData("content") }
            });
            var target = new InputFileResolver(fileSystem, null);

            var actual = target.FindProjectFile(Path.Combine(_filesystemRoot, "ExampleProject"), FilePathUtils.ConvertPathSeparators("SubFolder\\TestProject.csproj"));

            actual.ShouldBe(Path.Combine(_filesystemRoot, "ExampleProject", "SubFolder", "TestProject.csproj"));
        }
        public void InitializeShouldThrowIfImportPropertyCannotBeResolved()
        {
            string sourceFile = File.ReadAllText(_currentDirectory + "/TestResources/ExampleSourceFile.cs");

            string sharedFile = "<Project />";

            string projectFile = @"
<Project Sdk=""Microsoft.NET.Sdk"">
    <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <ItemGroup>
    </ItemGroup>

    <Import Project=""../$(SharedDir)/Example.projitems"" Label=""Shared"" />

    <ItemGroup>
        <ProjectReference Include=""../ExampleProject/ExampleProject.csproj"" />
    </ItemGroup>
</Project>";

            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { Path.Combine(_filesystemRoot, "SharedProject", "Example.projitems"), new MockFileData(sharedFile) },
                { Path.Combine(_filesystemRoot, "SharedProject", "Shared.cs"), new MockFileData(sourceFile) },
                { projectUnderTestPath, new MockFileData(projectFile) },
                { Path.Combine(_filesystemRoot, "ExampleProject", "Recursive.cs"), new MockFileData(sourceFile) },
                { Path.Combine(_filesystemRoot, "ExampleProject", "OneFolderDeeper", "Recursive.cs"), new MockFileData(sourceFile) },
                { testProjectPath, new MockFileData(defaultTestProjectFileContents) }
            });

            var projectFileReaderMock = new Mock <IProjectFileReader>(MockBehavior.Strict);

            projectFileReaderMock.Setup(x => x.AnalyzeProject(testProjectPath, null))
            .Returns(new ProjectAnalyzerResult(null, null)
            {
                ProjectReferences = new List <string>()
                {
                    projectUnderTestPath
                },
                TargetFrameworkVersionString = "netcoreapp2.1",
                ProjectFilePath = testProjectPath,
            });
            projectFileReaderMock.Setup(x => x.AnalyzeProject(projectUnderTestPath, null))
            .Returns(new ProjectAnalyzerResult(null, null)
            {
                ProjectReferences            = new List <string>(),
                TargetFrameworkVersionString = "netcoreapp2.1",
                ProjectFilePath = projectUnderTestPath,
                Properties      = new Dictionary <string, string>(),
            });
            var target = new InputFileResolver(fileSystem, projectFileReaderMock.Object);

            var exception = Assert.Throws <StrykerInputException>(() => target.ResolveInput(new StrykerOptions(fileSystem: fileSystem, basePath: _basePath)));

            exception.Message.ShouldBe($"Missing MSBuild property (SharedDir) in project reference (..{FilePathUtils.NormalizePathSeparators("/$(SharedDir)/Example.projitems")}). Please check your project file ({projectUnderTestPath}) and try again.");
        }
Exemple #9
0
        private string ValidateTestProjectFilter(string basePath, string userSuppliedFilter)
        {
            string filter;

            if (userSuppliedFilter.Contains(".."))
            {
                filter = FilePathUtils.ConvertPathSeparators(Path.GetFullPath(Path.Combine(basePath, userSuppliedFilter)));
            }
            else
            {
                filter = FilePathUtils.ConvertPathSeparators(userSuppliedFilter);
            }

            if (userSuppliedFilter.Contains("..") && !filter.StartsWith(basePath))
            {
                throw new StrykerInputException(ErrorMessage,
                                                $"The test project filter {userSuppliedFilter} is invalid. Test project file according to filter should exist at {filter} but this is not a child of {FilePathUtils.ConvertPathSeparators(basePath)} so this is not allowed.");
            }
            return(filter);
        }
Exemple #10
0
        // initialize the test context and mock objects
        public VsTestRunnersShould()
        {
            var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var filesystemRoot   = Path.GetPathRoot(currentDirectory);

            var          sourceFile                     = File.ReadAllText(currentDirectory + "/TestResources/ExampleSourceFile.cs");
            var          testProjectPath                = FilePathUtils.NormalizePathSeparators(Path.Combine(filesystemRoot, "TestProject", "TestProject.csproj"));
            var          projectUnderTestPath           = FilePathUtils.NormalizePathSeparators(Path.Combine(filesystemRoot, "ExampleProject", "ExampleProject.csproj"));
            const string defaultTestProjectFileContents = @"<Project Sdk=""Microsoft.NET.Sdk"">
    <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <IsPackable>false</IsPackable>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include=""Microsoft.NET.Test.Sdk"" Version = ""15.5.0"" />
        <PackageReference Include=""xunit"" Version=""2.3.1"" />
        <PackageReference Include=""xunit.runner.visualstudio"" Version=""2.3.1"" />
        <DotNetCliToolReference Include=""dotnet-xunit"" Version=""2.3.1"" />
    </ItemGroup>
    <ItemGroup>
        <ProjectReference Include=""..\ExampleProject\ExampleProject.csproj"" />
    </ItemGroup>
</Project>";

            _testAssemblyPath = Path.Combine(filesystemRoot, "_firstTest", "bin", "Debug", "TestApp.dll");
            _executorUri      = new Uri("exec://nunit");
            var firstTest  = new TestCase("T0", _executorUri, _testAssemblyPath);
            var secondTest = new TestCase("T1", _executorUri, _testAssemblyPath);

            var content = new CsharpFolderComposite();

            _coverageProperty = TestProperty.Register("Stryker.Coverage", "Coverage", typeof(string), typeof(TestResult));
            _mutant           = new Mutant {
                Id = 0
            };
            _otherMutant = new Mutant {
                Id = 1
            };
            _projectContents = content;
            _projectContents.Add(new CsharpFileLeaf {
                Mutants = new[] { _otherMutant, _mutant }
            });
            _targetProject = new ProjectInfo()
            {
                TestProjectAnalyzerResults = new List <IAnalyzerResult> {
                    TestHelper.SetupProjectAnalyzerResult(
                        properties: new Dictionary <string, string>()
                    {
                        { "TargetDir", Path.GetDirectoryName(_testAssemblyPath) },
                        { "TargetFileName", Path.GetFileName(_testAssemblyPath) }
                    },
                        targetFramework: "toto").Object
                },
                ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(
                    properties: new Dictionary <string, string>()
                {
                    { "TargetDir", Path.Combine(filesystemRoot, "app", "bin", "Debug") },
                    { "TargetFileName", "AppToTest.dll" },
                    { "Language", "C#" }
                }).Object,
                ProjectContents = _projectContents
            };
            //CodeInjection.HelperNamespace = "Stryker.Core.UnitTest.TestRunners";
            _fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { projectUnderTestPath, new MockFileData(defaultTestProjectFileContents) },
                { Path.Combine(filesystemRoot, "ExampleProject", "Recursive.cs"), new MockFileData(sourceFile) },
                { Path.Combine(filesystemRoot, "ExampleProject", "OneFolderDeeper", "Recursive.cs"), new MockFileData(sourceFile) },
                { testProjectPath, new MockFileData(defaultTestProjectFileContents) },
                { _testAssemblyPath, new MockFileData("Bytecode") },
                { Path.Combine(filesystemRoot, "app", "bin", "Debug", "AppToTest.dll"), new MockFileData("Bytecode") },
            });

            _testCases = new List <TestCase> {
                firstTest, secondTest
            };
        }