Ejemplo n.º 1
0
        public void TranslatePathRoot_Success()
        {
            var logger     = new Mock <ILogger>();
            var fileSystem = new Mock <IFileSystem>();

            fileSystem.Setup(f => f.Exists(It.IsAny <string>())).Returns((string p) =>
            {
                if (p == "testLib.dll" || p == @"C:\git\coverlet\src\coverlet.core\obj\Debug\netstandard2.0\coverlet.core.pdb" || p == "CoverletSourceRootsMapping")
                {
                    return(true);
                }
                return(false);
            });
            fileSystem.Setup(f => f.ReadAllLines(It.IsAny <string>())).Returns(File.ReadAllLines(@"TestAssets/CoverletSourceRootsMappingTest"));
            var translator = new SourceRootTranslator("testLib.dll", logger.Object, fileSystem.Object);

            Assert.Equal(@"C:\git\coverlet\", translator.ResolvePathRoot("/_/")[0].OriginalPath);
        }
Ejemplo n.º 2
0
        public void Translate_EmptyFile()
        {
            string             fileToTranslate = "/_/src/coverlet.core/obj/Debug/netstandard2.0/coverlet.core.pdb";
            Mock <ILogger>     logger          = new Mock <ILogger>();
            Mock <IFileSystem> fileSystem      = new Mock <IFileSystem>();

            fileSystem.Setup(f => f.Exists(It.IsAny <string>())).Returns((string p) =>
            {
                if (p == "testLib.dll" || p == "CoverletSourceRootsMapping")
                {
                    return(true);
                }
                return(false);
            });
            fileSystem.Setup(f => f.ReadAllLines(It.IsAny <string>())).Returns(new string[0]);
            SourceRootTranslator translator = new SourceRootTranslator("testLib.dll", logger.Object, fileSystem.Object);

            Assert.Equal(fileToTranslate, translator.ResolveFilePath(fileToTranslate));
        }
        public void Prepare(Project testProject, ConfigurationSelector configuration, DataCollectorSettings coverageSettings)
        {
            var unitTestDll           = testProject.GetOutputFileName(configuration).ToString();
            var sourceRootTranslator  = new SourceRootTranslator(logger, fileSystem);
            var cecilSymbolHelper     = new CecilSymbolHelper();
            var instrumentationHelper = new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), fileSystem, logger, sourceRootTranslator);
            var coverageParameters    = GetCoverageParameters(coverageSettings);

            var coverage = new CoverletCoverage(unitTestDll,
                                                coverageParameters,
                                                logger,
                                                instrumentationHelper,
                                                fileSystem,
                                                sourceRootTranslator,
                                                cecilSymbolHelper);

            coverage.PrepareModules();
            projectCoverageMap[new Tuple <Project, ConfigurationSelector>(testProject, configuration)] = coverage;
        }
Ejemplo n.º 4
0
        public void Translate_MalformedFile()
        {
            string fileToTranslate = "/_/src/coverlet.core/obj/Debug/netstandard2.0/coverlet.core.pdb";
            var    logger          = new Mock <ILogger>();
            var    fileSystem      = new Mock <IFileSystem>();

            fileSystem.Setup(f => f.Exists(It.IsAny <string>())).Returns((string p) =>
            {
                if (p == "testLib.dll" || p == "CoverletSourceRootsMapping")
                {
                    return(true);
                }
                return(false);
            });
            fileSystem.Setup(f => f.ReadAllLines(It.IsAny <string>())).Returns(new string[1] {
                "malformedRow"
            });
            var translator = new SourceRootTranslator("testLib.dll", logger.Object, fileSystem.Object);

            Assert.Equal(fileToTranslate, translator.ResolveFilePath(fileToTranslate));
            logger.Verify(l => l.LogWarning(It.IsAny <string>()), Times.Once);
        }
Ejemplo n.º 5
0
        public void TestCoreLibInstrumentation()
        {
            DirectoryInfo directory = Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), nameof(TestCoreLibInstrumentation)));

            string[] files = new[]
            {
                "System.Private.CoreLib.dll",
                "System.Private.CoreLib.pdb"
            };

            foreach (var file in files)
            {
                File.Copy(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", file), Path.Combine(directory.FullName, file), overwrite: true);
            }

            Mock <FileSystem> partialMockFileSystem = new Mock <FileSystem>();

            partialMockFileSystem.CallBase = true;
            partialMockFileSystem.Setup(fs => fs.OpenRead(It.IsAny <string>())).Returns((string path) =>
            {
                if (Path.GetFileName(path) == files[1])
                {
                    return(File.OpenRead(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets"), files[1])));
                }
                else
                {
                    return(File.OpenRead(path));
                }
            });
            partialMockFileSystem.Setup(fs => fs.Exists(It.IsAny <string>())).Returns((string path) =>
            {
                if (Path.GetFileName(path) == files[1])
                {
                    return(File.Exists(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets"), files[1])));
                }
                else
                {
                    if (path.Contains(@":\git\runtime"))
                    {
                        return(true);
                    }
                    else
                    {
                        return(File.Exists(path));
                    }
                }
            });
            var sourceRootTranslator                    = new SourceRootTranslator(_mockLogger.Object, new FileSystem());
            CoverageParameters    parameters            = new CoverageParameters();
            InstrumentationHelper instrumentationHelper =
                new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), partialMockFileSystem.Object, _mockLogger.Object, sourceRootTranslator);
            Instrumenter instrumenter = new Instrumenter(Path.Combine(directory.FullName, files[0]), "_coverlet_instrumented", parameters, _mockLogger.Object, instrumentationHelper, partialMockFileSystem.Object, sourceRootTranslator, new CecilSymbolHelper());

            Assert.True(instrumenter.CanInstrument());
            InstrumenterResult result = instrumenter.Instrument();

            Assert.NotNull(result);
            Assert.Equal(1052, result.Documents.Count);
            foreach ((string docName, Document _) in result.Documents)
            {
                Assert.False(docName.EndsWith(@"System.Private.CoreLib\src\System\Threading\Interlocked.cs"));
            }
            directory.Delete(true);
        }