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

me-viper / OpaDotNet.Compilation / 5925482438

21 Aug 2023 11:11AM UTC coverage: 88.682% (+1.0%) from 87.667%
5925482438

push

github

me-viper
test: Fix unit test

137 of 160 branches covered (85.63%)

Branch coverage included in aggregate %.

4 of 4 new or added lines in 1 file covered. (100.0%)

482 of 538 relevant lines covered (89.59%)

137.4 hits per line

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

97.56
/src/OpaDotNet.Compilation.Interop/RegoInteropCompiler.cs
1
using System.Runtime.InteropServices;
2
using System.Text;
3

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

8
using OpaDotNet.Compilation.Abstractions;
9

10
namespace OpaDotNet.Compilation.Interop;
11

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

19
    private readonly ILogger _logger;
20

21
    private readonly IOptions<RegoCompilerOptions> _options;
22

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

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

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

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

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

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

56
        return Task.FromResult(result);
1✔
57
    }
1✔
58

59
    /// <inheritdoc />
60
    public Task<Stream> CompileBundle(
61
        string bundlePath,
62
        IEnumerable<string>? entrypoints = null,
63
        string? capabilitiesFilePath = null,
64
        CancellationToken cancellationToken = default)
65
    {
14✔
66
        ArgumentException.ThrowIfNullOrEmpty(bundlePath);
14✔
67

68
        var path = bundlePath;
14✔
69

70
        if (path.StartsWith("./") || path.StartsWith(".\\"))
14✔
71
            path = path[2..];
3✔
72

73
        var result = Interop.Compile(
14✔
74
            NormalizePath(path),
14✔
75
            true,
14✔
76
            _options.Value,
14✔
77
            entrypoints,
14✔
78
            capabilitiesFilePath,
14✔
79
            _logger
14✔
80
            );
14✔
81

82
        return Task.FromResult(result);
13✔
83
    }
13✔
84

85
    /// <inheritdoc />
86
    public Task<Stream> CompileFile(
87
        string sourceFilePath,
88
        IEnumerable<string>? entrypoints = null,
89
        CancellationToken cancellationToken = default)
90
    {
9✔
91
        ArgumentException.ThrowIfNullOrEmpty(sourceFilePath);
9✔
92

93
        var path = sourceFilePath;
9✔
94

95
        if (path.StartsWith("./") || path.StartsWith(".\\"))
9✔
96
            path = path[2..];
2✔
97

98
        var result = Interop.Compile(
9✔
99
            NormalizePath(path),
9✔
100
            false,
9✔
101
            _options.Value,
9✔
102
            entrypoints,
9✔
103
            logger: _logger
9✔
104
            );
9✔
105

106
        return Task.FromResult(result);
8✔
107
    }
8✔
108

109
    /// <inheritdoc />
110
    public async Task<Stream> CompileSource(
111
        string source,
112
        IEnumerable<string>? entrypoints = null,
113
        CancellationToken cancellationToken = default)
114
    {
2✔
115
        ArgumentException.ThrowIfNullOrEmpty(source);
2✔
116

117
        var path = _options.Value.OutputPath ?? AppContext.BaseDirectory;
2✔
118
        var sourceFile = new FileInfo(Path.Combine(path, $"{Guid.NewGuid()}.rego"));
2✔
119
        await File.WriteAllTextAsync(sourceFile.FullName, source, Encoding.UTF8, cancellationToken).ConfigureAwait(false);
2✔
120

121
        try
122
        {
2✔
123
            return await CompileFile(sourceFile.FullName, entrypoints, cancellationToken).ConfigureAwait(false);
2✔
124
        }
125
        finally
126
        {
2✔
127
            if (!_options.Value.PreserveBuildArtifacts)
2✔
128
                File.Delete(sourceFile.FullName);
2✔
129
        }
2✔
130
    }
1✔
131
}
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

© 2026 Coveralls, Inc