Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FileRegionsCache testability #2131

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/Sarif/FileRegionsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public class FileRegionsCache
private readonly IFileSystem _fileSystem;
private readonly Cache<string, Tuple<string, NewLineIndex>> _cache;

/// <summary>
/// Creates a new <see cref="FileRegionsCache"/> object.
/// </summary>
/// <param name="run">
/// The <see cref="Run"/> object whose data is to be cached.
/// </param>
/// <param name="capacity">
/// The initial capacity of the cache.
/// </param>
/// <param name="fileSystem">
/// An object that provides access to file system services.
/// </param>
public FileRegionsCache(Run run, int capacity = DefaultCacheCapacity, IFileSystem fileSystem = null)
{
// Each file regions cache is associated with a single SARIF run.
Expand All @@ -28,20 +40,32 @@ public FileRegionsCache(Run run, int capacity = DefaultCacheCapacity, IFileSyste
_fileSystem = fileSystem ?? FileSystem.Instance;

// Build a cache for this data, with the load method it should use to add new entries
_cache = new Cache<string, Tuple<string, NewLineIndex>>(BuildIndexForFile);
_cache = new Cache<string, Tuple<string, NewLineIndex>>(BuildIndexForFile, capacity);
}

/// <summary>
/// Accepts a physical location and returns a Region object, based on the input
/// physicalLocation.region property, that has all its properties populated. If an
/// input text region, for example, only specifies the startLine property, the returned
/// Region instance will have computed and populated other properties, such as charOffset,
/// charLength, etc.
/// Creates a <see cref="Region"/> object, based on an existing Region, in which all
/// text-related properties have been populated.
/// </summary>
/// <param name="physicalLocation">The physical location containing the region which should be populated.</param>
/// <param name="populateSnippet">Specifies whether the physicalLocation.region.snippet property should be populated.</param>
/// <returns></returns>
public Region PopulateTextRegionProperties(Region inputRegion, Uri uri, bool populateSnippet)
/// <remarks>
/// For example, if the input Region specifies only the StartLine property, the returned
/// Region instance will have computed and populated other text-related properties, such
/// as properties, such as CharOffset, CharLength, etc.
/// </remarks>
/// <param name="inputRegion">
/// Region object that forms the basis of the returned Region object.
/// </param>
/// <param name="uri">
/// URI of the artifact in which <paramref name="inputRegion"/> lies, used to retrieve
/// from the cache the location of each newline in the artifact.
/// </param>
/// <param name="populateSnippet">
/// Boolean that indicates if the region's Snippet property will be populated.
/// </param>
/// <returns>
/// A Region object whose text-related properties have been fully populated.
/// </returns>
public virtual Region PopulateTextRegionProperties(Region inputRegion, Uri uri, bool populateSnippet)
{
if (inputRegion == null || inputRegion.IsBinaryRegion)
{
Expand Down