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

update ruby bindings version to 0.1.5, bump deps #251

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion bindings/ffi/RegorusFFI.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace RegorusFFI
{
internal static unsafe partial class API
{
const string __DllName = "regorusc";
const string __DllName = "regorus_ffi";
anakrish marked this conversation as resolved.
Show resolved Hide resolved



Expand Down
2 changes: 1 addition & 1 deletion bindings/ruby/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 3.3.0
ruby 3.3.1
4 changes: 2 additions & 2 deletions bindings/ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ gemspec

# These gems are required for local development and testing,
# but won't be included in the published gem
gem "minitest", "~> 5.22"
gem "minitest", "~> 5.23"
gem "rake", "~> 13.2"
gem "rake-compiler"
gem "rake-compiler-dock"
gem "rubocop", "~> 1.63", require: false
gem "rubocop", "~> 1.64", require: false
gem "rubocop-minitest", require: false
gem "rubocop-rake", require: false
14 changes: 7 additions & 7 deletions bindings/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
regorusrb (0.1.0)
regorusrb (0.1.5)
anakrish marked this conversation as resolved.
Show resolved Hide resolved
rb_sys (~> 0.9.97)

GEM
Expand All @@ -10,12 +10,12 @@ GEM
ast (2.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
minitest (5.23.0)
minitest (5.23.1)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
racc (1.7.3)
racc (1.8.0)
rainbow (3.1.1)
rake (13.2.1)
rake-compiler (1.2.7)
Expand All @@ -25,7 +25,7 @@ GEM
regexp_parser (2.9.2)
rexml (3.2.8)
strscan (>= 3.0.9)
rubocop (1.63.5)
rubocop (1.64.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand All @@ -52,14 +52,14 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
minitest (~> 5.22)
minitest (~> 5.23)
rake (~> 13.2)
rake-compiler
rake-compiler-dock
regorusrb!
rubocop (~> 1.63)
rubocop (~> 1.64)
rubocop-minitest
rubocop-rake

BUNDLED WITH
2.5.7
2.5.10
4 changes: 2 additions & 2 deletions bindings/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ puts results_json

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To update the gem version or dependencies within `bindings/ruby/Gemfile.lock`, run `bundle update --gemfile=bindings/ruby/Gemfile` from the project directory, or `cd bindings/ruby && bundle update`

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
After checking out the repo, `cd bindings/ruby` and run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

4 changes: 2 additions & 2 deletions bindings/ruby/ext/regorusrb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib"]
path = "src/lib.rs"

[dependencies]
magnus = { version = "0.6.3" }
magnus = { version = "0.6.4" }
regorus = { git = "https://github.com/microsoft/regorus" }
serde_json = "1.0.115"
serde_json = "1.0.117"
serde_magnus = "0.8.1"
85 changes: 85 additions & 0 deletions bindings/ruby/ext/regorusrb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,68 @@ impl Engine {
fn eval_deny_query(&self, query: String) -> Result<bool, Error> {
Ok(self.engine.borrow_mut().eval_deny_query(query, false))
}

fn set_enable_coverage(&self, enable: bool) -> Result<(), Error> {
Ok(self.engine.borrow_mut().set_enable_coverage(enable))
}

fn get_coverage_report_as_json(&self) -> Result<String, Error> {
let report = self
.engine
.borrow_mut()
.get_coverage_report()
.map_err(|e| {
Error::new(
runtime_error(),
format!("Failed to get coverage report as json: {}", e),
)
})?;

serde_json::to_string(&report).map_err(|e| {
Error::new(
runtime_error(),
format!("Failed to serialize coverage report: {}", e),
)
})
}

fn get_coverage_report_pretty(&self) -> Result<String, Error> {
let report = self
.engine
.borrow_mut()
.get_coverage_report()
.map_err(|e| {
Error::new(
runtime_error(),
format!("Failed to get coverage report: {}", e),
)
})?;

report.to_colored_string().map_err(|e| {
Error::new(
runtime_error(),
format!("Failed to convert report to colored string: {}", e),
)
})
}

fn clear_coverage_data(&self) -> Result<(), Error> {
Ok(self.engine.borrow_mut().clear_coverage_data())
}

// Print statements can be gathered async instead of printing to stderr
fn set_gather_prints(&self, enable: bool) -> Result<(), Error> {
Ok(self.engine.borrow_mut().set_gather_prints(enable))
}

fn take_prints(&self) -> Result<Vec<String>, Error> {
self.engine.borrow_mut().take_prints().map_err(|e| {
Error::new(
runtime_error(),
format!("Failed to gather print statement: {}", e),
)
})
}
}

#[magnus::init]
Expand Down Expand Up @@ -237,5 +299,28 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
engine_class.define_method("eval_bool_query", method!(Engine::eval_bool_query, 1))?;
engine_class.define_method("eval_allow_query", method!(Engine::eval_allow_query, 1))?;
engine_class.define_method("eval_deny_query", method!(Engine::eval_deny_query, 1))?;

// coverage operations
engine_class.define_method(
"set_enable_coverage",
method!(Engine::set_enable_coverage, 1),
)?;
engine_class.define_method(
"get_coverage_report_as_json",
method!(Engine::get_coverage_report_as_json, 0),
)?;
engine_class.define_method(
"get_coverage_report_pretty",
method!(Engine::get_coverage_report_pretty, 0),
)?;
engine_class.define_method(
"clear_coverage_data",
method!(Engine::clear_coverage_data, 0),
)?;

// print statements
engine_class.define_method("set_gather_prints", method!(Engine::set_gather_prints, 1))?;
engine_class.define_method("take_prints", method!(Engine::take_prints, 0))?;

Ok(())
}
2 changes: 1 addition & 1 deletion bindings/ruby/lib/regorus/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Regorus
VERSION = "0.1.0"
VERSION = "0.1.5"
end
27 changes: 27 additions & 0 deletions bindings/ruby/test/test_regorus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ def test_engine_cloning
refute_same @engine, cloned_engine
end

def test_coverage_printing_json
@engine.set_input(input_for(ALICE))
@engine.set_enable_coverage(true)
@engine.eval_rule("data.regorus_test.is_employee")

assert_match(/covered":\[7\],"not_covered":\[3,11,12,15,19\]/, @engine.get_coverage_report_as_json)
end

def test_coverage_printing_pretty
@engine.set_input(input_for(ALICE))
@engine.set_enable_coverage(true)
@engine.eval_rule("data.regorus_test.is_employee")

# to see the colors in the ruby terminal, use `puts @engine.get_coverage_report``
pretty_coverage_report = @engine.get_coverage_report_pretty

assert_match(/\e\[31m 3 input.name == data.managers\[_\]/, pretty_coverage_report)
assert_match(/\e\[32m 7 input.name == data.employees\[_\]/, pretty_coverage_report)
end

def test_gather_print_statements
@engine.set_gather_prints(true)
@engine.eval_query('print("Hello")')

assert_equal ["<query.rego>:1: Hello"], @engine.take_prints
end

def alice_results
{
result: [
Expand Down
Loading