public void Dispose() { var hitsFilePath = ModuleTrackerTemplate.HitsFilePath; _mmap.Dispose(); InstrumentationHelper.DeleteHitsFile(hitsFilePath); }
public void TestDeleteHitsFile() { var tempFile = Path.GetTempFileName(); Assert.True(File.Exists(tempFile)); InstrumentationHelper.DeleteHitsFile(tempFile); Assert.False(File.Exists(tempFile)); }
private void CalculateCoverage() { foreach (var result in _results) { if (!File.Exists(result.HitsFilePath)) { continue; } var lines = InstrumentationHelper.ReadHitsFile(result.HitsFilePath); foreach (var line in lines) { var info = line.Split(','); // Ignore malformed lines if (info.Length != 4) { continue; } var document = result.Documents.FirstOrDefault(d => d.Path == info[0]); if (document == null) { continue; } int start = int.Parse(info[1]); int end = int.Parse(info[2]); bool target = info[3] == "B"; for (int j = start; j <= end; j++) { var subLine = document.Lines.First(l => l.Number == j); subLine.Hits = subLine.Hits + 1; if (j == start) { subLine.IsBranchTarget = target; } } } InstrumentationHelper.DeleteHitsFile(result.HitsFilePath); } }
private void CalculateCoverage() { foreach (var result in _results) { if (!File.Exists(result.HitsFilePath)) { // Hits file could be missed mainly for two reason // 1) Issue during module Unload() // 2) Instrumented module is never loaded or used so we don't have any hit to register and // module tracker is never used _logger.LogVerbose($"Hits file:'{result.HitsFilePath}' not found for module: '{result.Module}'"); continue; } List <Document> documents = result.Documents.Values.ToList(); if (_useSourceLink && result.SourceLink != null) { var jObject = JObject.Parse(result.SourceLink)["documents"]; var sourceLinkDocuments = JsonConvert.DeserializeObject <Dictionary <string, string> >(jObject.ToString()); foreach (var document in documents) { document.Path = GetSourceLinkUrl(sourceLinkDocuments, document.Path); } } using (var fs = new FileStream(result.HitsFilePath, FileMode.Open)) using (var br = new BinaryReader(fs)) { int hitCandidatesCount = br.ReadInt32(); // TODO: hitCandidatesCount should be verified against result.HitCandidates.Count var documentsList = result.Documents.Values.ToList(); for (int i = 0; i < hitCandidatesCount; ++i) { var hitLocation = result.HitCandidates[i]; var document = documentsList[hitLocation.docIndex]; int hits = br.ReadInt32(); if (hitLocation.isBranch) { var branch = document.Branches[new BranchKey(hitLocation.start, hitLocation.end)]; branch.Hits += hits; } else { for (int j = hitLocation.start; j <= hitLocation.end; j++) { var line = document.Lines[j]; line.Hits += hits; } } } } // for MoveNext() compiler autogenerated method we need to patch false positive (IAsyncStateMachine for instance) // we'll remove all MoveNext() not covered branch foreach (var document in result.Documents) { List <KeyValuePair <BranchKey, Branch> > branchesToRemove = new List <KeyValuePair <BranchKey, Branch> >(); foreach (var branch in document.Value.Branches) { //if one branch is covered we search the other one only if it's not covered if (IsAsyncStateMachineMethod(branch.Value.Method) && branch.Value.Hits > 0) { foreach (var moveNextBranch in document.Value.Branches) { if (moveNextBranch.Value.Method == branch.Value.Method && moveNextBranch.Value != branch.Value && moveNextBranch.Value.Hits == 0) { branchesToRemove.Add(moveNextBranch); } } } } foreach (var branchToRemove in branchesToRemove) { document.Value.Branches.Remove(branchToRemove.Key); } } InstrumentationHelper.DeleteHitsFile(result.HitsFilePath); } }
private void CalculateCoverage() { foreach (var result in _results) { var i = 0; while (true) { var file = $"{result.HitsFilePath}_compressed_{i}"; if (!File.Exists(file)) { break; } using (var fs = new FileStream(file, FileMode.Open)) using (var gz = new GZipStream(fs, CompressionMode.Decompress)) using (var sr = new StreamReader(gz)) { string row; while ((row = sr.ReadLine()) != null) { var info = row.Split(','); // Ignore malformed lines if (info.Length != 4) { continue; } bool isBranch = info[0] == "B"; var document = result.Documents.FirstOrDefault(d => d.Path == info[1]); if (document == null) { continue; } int start = int.Parse(info[2]); if (isBranch) { uint ordinal = uint.Parse(info[3]); var branch = document.Branches.First(b => b.Number == start && b.Ordinal == ordinal); if (branch.Hits != int.MaxValue) { branch.Hits += branch.Hits + 1; } } else { int end = int.Parse(info[3]); for (int j = start; j <= end; j++) { var line = document.Lines.First(l => l.Number == j); if (line.Hits != int.MaxValue) { line.Hits = line.Hits + 1; } } } } } InstrumentationHelper.DeleteHitsFile(file); i++; } } }