• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

me-viper / OpaDotNet.Compilation / 6942841257

21 Nov 2023 09:53AM UTC coverage: 88.266%. Remained the same
6942841257

push

github

me-viper
chore: Update CHANGELOG

180 of 220 branches covered (0.0%)

Branch coverage included in aggregate %.

670 of 743 relevant lines covered (90.17%)

533.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.27
/src/OpaDotNet.Compilation.Interop/RegoInteropCompiler.cs
1
using System.Runtime.InteropServices;
2

3
using Microsoft.Extensions.Logging;
4
using Microsoft.Extensions.Logging.Abstractions;
5
using Microsoft.Extensions.Options;
6

7
using OpaDotNet.Compilation.Abstractions;
8

9
namespace OpaDotNet.Compilation.Interop;
10

11
/// <summary>
12
/// Compiles OPA bundle with OPA SDK interop wrapper.
13
/// </summary>
14
public class RegoInteropCompiler : IRegoCompiler
15
{
16
    private static IOptions<RegoCompilerOptions> Default { get; } = new OptionsWrapper<RegoCompilerOptions>(new());
4✔
17

18
    private readonly ILogger _logger;
19

20
    private readonly IOptions<RegoCompilerOptions> _options;
21

22
    /// <summary>
23
    /// Creates new instance of <see cref="RegoInteropCompiler"/> class.
24
    /// </summary>
25
    /// <param name="options">Compilation options</param>
26
    /// <param name="logger">Logger instance</param>
27
    public RegoInteropCompiler(
74✔
28
        IOptions<RegoCompilerOptions>? options = null,
74✔
29
        ILogger<RegoInteropCompiler>? logger = null)
74✔
30
    {
74✔
31
        _options = options ?? Default;
74✔
32
        _logger = logger ?? NullLogger<RegoInteropCompiler>.Instance;
74✔
33
    }
74✔
34

35
    private static string NormalizePath(string path) => path.Replace("\\", "/");
50✔
36

37
    /// <inheritdoc />
38
    public Task<RegoCompilerVersion> Version(CancellationToken cancellationToken = default)
39
    {
2✔
40
        var vp = Interop.OpaGetVersion();
2✔
41

42
        if (vp == nint.Zero)
2!
43
            throw new RegoCompilationException("Failed to get version");
×
44

45
        var v = Marshal.PtrToStructure<Interop.OpaVersion>(vp);
2✔
46

47
        var result = new RegoCompilerVersion
2✔
48
        {
2✔
49
            Version = v.LibVersion,
2✔
50
            Commit = v.Commit,
2✔
51
            Platform = v.Platform,
2✔
52
            GoVersion = v.GoVersion,
2✔
53
        };
2✔
54

55
        return Task.FromResult(result);
2✔
56
    }
2✔
57

58
    /// <inheritdoc />
59
    public async Task<Stream> Compile(
60
        string path,
61
        CompilationParameters parameters,
62
        CancellationToken cancellationToken)
63
    {
50✔
64
        ArgumentException.ThrowIfNullOrEmpty(path);
50✔
65
        ArgumentNullException.ThrowIfNull(parameters);
50✔
66

67
        if (path.StartsWith("./") || path.StartsWith(".\\"))
50✔
68
            path = path[2..];
10✔
69

70
        var caps = parameters.CapabilitiesStream;
50✔
71

72
        try
73
        {
50✔
74
            if (!string.IsNullOrWhiteSpace(parameters.CapabilitiesFilePath))
50✔
75
                caps = new FileStream(parameters.CapabilitiesFilePath, FileMode.Open);
8✔
76

77
            var result = Interop.Compile(
50✔
78
                NormalizePath(path),
50✔
79
                parameters.IsBundle,
50✔
80
                _options.Value,
50✔
81
                parameters.Entrypoints,
50✔
82
                caps,
50✔
83
                _logger
50✔
84
                );
50✔
85

86
            return result;
46✔
87
        }
88
        finally
89
        {
50✔
90
            if (caps != null)
50✔
91
                await caps.DisposeAsync().ConfigureAwait(false);
8✔
92
        }
50✔
93
    }
46✔
94

95
    /// <inheritdoc />
96
    public async Task<Stream> Compile(
97
        Stream stream,
98
        CompilationParameters parameters,
99
        CancellationToken cancellationToken)
100
    {
22✔
101
        ArgumentNullException.ThrowIfNull(stream);
22✔
102
        ArgumentNullException.ThrowIfNull(parameters);
22✔
103

104
        var caps = parameters.CapabilitiesStream;
22✔
105
        Stream? bundle = null;
22✔
106

107
        try
108
        {
22✔
109
            if (!string.IsNullOrWhiteSpace(parameters.CapabilitiesFilePath))
22!
110
                caps = new FileStream(parameters.CapabilitiesFilePath, FileMode.Open);
×
111

112
            if (!parameters.IsBundle)
22!
113
            {
×
114
                bundle = new MemoryStream();
×
115
                var bw = new BundleWriter(bundle);
×
116

117
                await using (bw.ConfigureAwait(false))
×
118
                    bw.WriteEntry(stream, "policy.rego");
×
119

120
                bundle.Seek(0, SeekOrigin.Begin);
×
121
            }
×
122

123
            var result = Interop.Compile(
22✔
124
                bundle ?? stream,
22✔
125
                true,
22✔
126
                _options.Value,
22✔
127
                parameters.Entrypoints,
22✔
128
                caps,
22✔
129
                _logger
22✔
130
                );
22✔
131

132
            return result;
18✔
133
        }
134
        finally
135
        {
22✔
136
            if (bundle != null)
22!
137
                await bundle.DisposeAsync().ConfigureAwait(false);
×
138

139
            if (caps != null)
22✔
140
                await caps.DisposeAsync().ConfigureAwait(false);
4✔
141
        }
22✔
142
    }
18✔
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc