public CoverageData DeserializeCoverageObject(string json, TestContext testContext)
        {
            var data = jsonSerializer.Deserialize <BlanketCoverageObject>(json);
            IDictionary <string, string> generatedToOriginalFilePath =
                testContext.ReferencedFiles.Where(rf => rf.GeneratedFilePath != null).ToDictionary(rf => rf.GeneratedFilePath, rf => rf.Path);

            var coverageData = new CoverageData(testContext.TestFileSettings.CodeCoverageSuccessPercentage);

            // Rewrite all keys in the coverage object dictionary in order to change URIs
            // to paths and generated paths to original paths, then only keep the ones
            // that match the include/exclude patterns.
            foreach (var entry in data)
            {
                Uri uri = new Uri(entry.Key, UriKind.RelativeOrAbsolute);
                if (!uri.IsAbsoluteUri)
                {
                    // Resolve against the test file path.
                    string basePath = Path.GetDirectoryName(testContext.TestHarnessPath);
                    uri = new Uri(Path.Combine(basePath, entry.Key));
                }

                string filePath = uri.LocalPath;

                // Fix local paths of the form: file:///c:/zzz should become c:/zzz not /c:/zzz
                // but keep network paths of the form: file://network/files/zzz as //network/files/zzz
                filePath = RegexPatterns.InvalidPrefixedLocalFilePath.Replace(filePath, "$1");
                var fileUri = new Uri(filePath, UriKind.RelativeOrAbsolute);
                filePath = fileUri.LocalPath;

                string newKey;
                if (!generatedToOriginalFilePath.TryGetValue(filePath, out newKey))
                {
                    newKey = filePath;
                }

                if (IsFileEligibleForInstrumentation(newKey))
                {
                    string[] sourceLines = fileSystem.GetLines(filePath);
                    coverageData.Add(newKey, new CoverageFileData
                    {
                        LineExecutionCounts = entry.Value,
                        FilePath            = newKey,
                        SourceLines         = sourceLines
                    });
                }
            }
            return(coverageData);
        }
Esempio n. 2
0
        public CoverageData DeserializeCoverageObject(string json, TestContext testContext)
        {
            var data = jsonSerializer.Deserialize <BlanketCoverageObject>(json);
            var generatedToReferencedFile =
                testContext.ReferencedFiles.Where(rf => rf.GeneratedFilePath != null).ToLookup(rf => rf.GeneratedFilePath, rf => rf);

            var coverageData = new CoverageData(testContext.TestFileSettings.CodeCoverageSuccessPercentage.Value);

            // Rewrite all keys in the coverage object dictionary in order to change URIs
            // to paths and generated paths to original paths, then only keep the ones
            // that match the include/exclude patterns.
            foreach (var entry in data)
            {
                Uri uri = new Uri(entry.Key, UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri)
                {
                    // Resolve against the test file path.
                    string basePath = Path.GetDirectoryName(testContext.TestHarnessPath);
                    uri = new Uri(Path.Combine(basePath, entry.Key));
                }

                if (uri.Scheme != "file")
                {
                    continue;
                }


                string executedFilePath = uri.LocalPath;

                // Fix local paths of the form: file:///c:/zzz should become c:/zzz not /c:/zzz
                // but keep network paths of the form: file://network/files/zzz as //network/files/zzz
                executedFilePath = RegexPatterns.InvalidPrefixedLocalFilePath.Replace(executedFilePath, "$1");

                //REMOVE URI Query part from filepath like ?ver=1233123
                executedFilePath = RegexPatterns.IgnoreQueryPartFromUri.Replace(executedFilePath, "$1");

                var fileUri = new Uri(executedFilePath, UriKind.RelativeOrAbsolute);
                executedFilePath = fileUri.LocalPath;
                var referencedFiles = new List <ReferencedFile>();

                if (!generatedToReferencedFile.Contains(executedFilePath))
                {
                    // This does not appear to be a compiled file so just created a referencedFile with the path
                    referencedFiles.Add(new ReferencedFile {
                        Path = executedFilePath
                    });
                }
                else
                {
                    referencedFiles = generatedToReferencedFile[executedFilePath].ToList();
                }

                referencedFiles = referencedFiles.Where(file => IsFileEligibleForInstrumentation(file.Path) &&
                                                        !IsIgnored(file.Path)).ToList();

                foreach (var referencedFile in referencedFiles)
                {
                    // The coveredPath is the file which we have coverage lines for. We assume generated if it exsits otherwise the file path
                    var coveredPath = referencedFile.GeneratedFilePath ?? referencedFile.Path;

                    // If the user is using source maps then always take sourcePath and not generates.
                    if (testContext.TestFileSettings.Compile != null && testContext.TestFileSettings.Compile.UseSourceMaps.GetValueOrDefault() && referencedFile.SourceMapFilePath != null)
                    {
                        coveredPath = referencedFile.Path;
                    }

                    if (fileSystem.FileExists(coveredPath))
                    {
                        string[] sourceLines         = fileSystem.GetLines(coveredPath);
                        int?[]   lineExecutionCounts = entry.Value;

                        if (testContext.TestFileSettings.Compile != null && testContext.TestFileSettings.Compile.UseSourceMaps.GetValueOrDefault() && referencedFile.SourceMapFilePath != null)
                        {
                            lineExecutionCounts = this.lineCoverageMapper.GetOriginalFileLineExecutionCounts(entry.Value, sourceLines.Length, referencedFile);
                        }


                        var coverageFileData = new CoverageFileData
                        {
                            LineExecutionCounts = lineExecutionCounts,
                            FilePath            = referencedFile.Path,
                            SourceLines         = sourceLines
                        };

                        // If some AMD modules has different "non canonical" references (like "../../module" and "./../../module"). Coverage trying to add files many times
                        if (coverageData.ContainsKey(referencedFile.Path))
                        {
                            coverageData[referencedFile.Path].Merge(coverageFileData);
                        }
                        else
                        {
                            coverageData.Add(referencedFile.Path, coverageFileData);
                        }
                    }
                }
            }
            return(coverageData);
        }
Esempio n. 3
0
        public CoverageData DeserializeCoverageObject(string json, TestContext testContext)
        {
            var data = jsonSerializer.Deserialize <BlanketCoverageObject>(json);
            IDictionary <string, ReferencedFile> generatedToReferencedFile =
                testContext.ReferencedFiles.Where(rf => rf.GeneratedFilePath != null).ToDictionary(rf => rf.GeneratedFilePath, rf => rf);

            var coverageData = new CoverageData(testContext.TestFileSettings.CodeCoverageSuccessPercentage.Value);

            // Rewrite all keys in the coverage object dictionary in order to change URIs
            // to paths and generated paths to original paths, then only keep the ones
            // that match the include/exclude patterns.
            foreach (var entry in data)
            {
                Uri uri = new Uri(entry.Key, UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri)
                {
                    // Resolve against the test file path.
                    string basePath = Path.GetDirectoryName(testContext.TestHarnessPath);
                    uri = new Uri(Path.Combine(basePath, entry.Key));
                }

                if (uri.Scheme != "file")
                {
                    continue;
                }


                string filePath = uri.LocalPath;

                // Fix local paths of the form: file:///c:/zzz should become c:/zzz not /c:/zzz
                // but keep network paths of the form: file://network/files/zzz as //network/files/zzz
                filePath = RegexPatterns.InvalidPrefixedLocalFilePath.Replace(filePath, "$1");

                //REMOVE URI Query part from filepath like ?ver=1233123
                filePath = RegexPatterns.IgnoreQueryPartFromUri.Replace(filePath, "$1");

                var fileUri = new Uri(filePath, UriKind.RelativeOrAbsolute);
                filePath = fileUri.LocalPath;

                ReferencedFile referencedFile;
                string         newKey;
                if (!generatedToReferencedFile.TryGetValue(filePath, out referencedFile))
                {
                    newKey = filePath;
                }
                else
                {
                    newKey = referencedFile.Path;
                    if (referencedFile.SourceMapFilePath != null && testContext.TestFileSettings.Compile != null && testContext.TestFileSettings.Compile.UseSourceMaps)
                    {
                        filePath = referencedFile.Path;
                    }
                }

                if (IsFileEligibleForInstrumentation(newKey) && fileSystem.FileExists(filePath))
                {
                    string[] sourceLines         = fileSystem.GetLines(filePath);
                    int?[]   lineExecutionCounts = entry.Value;

                    if (testContext.TestFileSettings.Compile != null && testContext.TestFileSettings.Compile.UseSourceMaps && referencedFile != null && referencedFile.SourceMapFilePath != null)
                    {
                        lineExecutionCounts = this.lineCoverageMapper.GetOriginalFileLineExecutionCounts(entry.Value, sourceLines.Length, referencedFile);
                    }

                    coverageData.Add(newKey, new CoverageFileData
                    {
                        LineExecutionCounts = lineExecutionCounts,
                        FilePath            = newKey,
                        SourceLines         = sourceLines
                    });
                }
            }
            return(coverageData);
        }